63 lines
1.5 KiB
GDScript
63 lines
1.5 KiB
GDScript
extends CharacterBody2D
|
|
|
|
const ROT_SPEED = 3.0
|
|
const MOVE_ACC = 100
|
|
const FRICTION = 0.995
|
|
const FIRE_RATE = 0.2
|
|
|
|
@export var Laser: PackedScene
|
|
@export var pew: Resource
|
|
|
|
var speed = Vector2(0, 0)
|
|
var last_shot = 0.0
|
|
|
|
func _ready() -> void:
|
|
Game.player = self
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
var forward = Input.is_action_pressed("ui_up")
|
|
var left = Input.is_action_pressed("ui_left")
|
|
var right = Input.is_action_pressed("ui_right")
|
|
|
|
if left:
|
|
rotation -= ROT_SPEED * delta
|
|
|
|
if $AnimatedSprite2D.animation != "left":
|
|
$AnimatedSprite2D.play("left")
|
|
if right:
|
|
rotation += ROT_SPEED * delta
|
|
|
|
if $AnimatedSprite2D.animation != "right":
|
|
$AnimatedSprite2D.play("right")
|
|
if forward:
|
|
speed += Vector2.UP.rotated(rotation) * MOVE_ACC * delta
|
|
|
|
if $AnimatedSprite2D.animation != "fly":
|
|
$AnimatedSprite2D.play("fly")
|
|
if Input.is_action_pressed("ui_down"):
|
|
speed -= Vector2.UP.rotated(rotation) * MOVE_ACC * delta
|
|
|
|
if !(forward || left || right) and $AnimatedSprite2D.animation != "idle":
|
|
$AnimatedSprite2D.play("idle")
|
|
|
|
speed *= FRICTION
|
|
velocity = speed
|
|
|
|
move_and_slide()
|
|
|
|
Utils.wrap_position(self)
|
|
|
|
# --- Fire projectile on space ---
|
|
last_shot += delta
|
|
if Input.is_action_pressed("ui_accept") and last_shot >= FIRE_RATE: # default = Space/Enter
|
|
var bullet = Laser.instantiate()
|
|
|
|
Utils.play_sound(pew, self)
|
|
|
|
get_parent().add_child(bullet)
|
|
bullet.transform = $Muzzle.global_transform
|
|
last_shot = 0.0
|
|
|
|
func reset():
|
|
position = get_viewport_rect().size / 2
|
|
speed = Vector2(0, 0)
|