97 lines
2.2 KiB
GDScript
97 lines
2.2 KiB
GDScript
extends CharacterBody2D
|
|
|
|
const ROT_SPEED = 4.0
|
|
const MOVE_ACC = 120
|
|
const FRICTION = 0.995
|
|
const FIRE_RATE = 0.2
|
|
const INV_FLASH_INT = 0.1
|
|
|
|
@export var Laser: PackedScene
|
|
@export var pew: Resource
|
|
|
|
var speed = Vector2(0, 0)
|
|
var last_shot = 0.0
|
|
var last_flash = 0.0
|
|
var inv_time = 0.0
|
|
|
|
var explosion = preload('res://Scenes/explosion.tscn')
|
|
var pop = preload("res://Sounds/Explosion5.wav")
|
|
|
|
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)
|
|
|
|
if is_invincible():
|
|
inv_time -= delta
|
|
last_flash += delta
|
|
if last_flash >= INV_FLASH_INT:
|
|
$AnimatedSprite2D.visible = !$AnimatedSprite2D.visible
|
|
last_flash = 0.0
|
|
else:
|
|
$AnimatedSprite2D.visible = true
|
|
|
|
# --- 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 die():
|
|
position = get_viewport_rect().size
|
|
speed = Vector2(0, 0)
|
|
inv_time = 3.0
|
|
|
|
func is_invincible():
|
|
return inv_time > 0
|
|
|
|
func hit() -> bool:
|
|
if !is_invincible():
|
|
Game.set_lives(Game.lives - 1)
|
|
|
|
Utils.play_sound(pop, get_parent())
|
|
|
|
var boom = explosion.instantiate()
|
|
boom.position = global_position
|
|
boom.scale = Vector2(6, 6)
|
|
get_parent().add_child(boom)
|
|
die()
|
|
return true
|
|
|
|
return false
|