Files
Azzteroid/script/ship.gd
2025-09-08 14:49:21 +02:00

76 lines
1.8 KiB
GDScript

extends CharacterBody2D
class_name Ship
@export var MAX_SPEED: float = 200
@export var SHOOT_NODE: PackedScene
@export var TIME_INVUL := 1.0
@export var animation : AnimationPlayer
@export var target : Node2D
const SPEED = 25.0
const JUMP_VELOCITY = -400.0
var mouse_position: Vector2 = Vector2.ZERO
var direction :=Vector2(1,0)
var horizontal : float
var vertical : float
var rota_temp: float
var delay := 0.0
var is_in = true
var delay_inv := 0.0
var is_currently_hit := false
func _input(event):
pass
func _ready() -> void:
delay_inv = TIME_INVUL +1
GameController.connect("player_hit", start_inv)
func _physics_process(delta: float) -> void:
delay += delta
if is_inv():
delay_inv += delta
animation.play("blink")
else:
animation.play("RESET")
horizontal = Input.get_axis("ui_up", "ui_down")
vertical = Input.get_axis("ui_left", "ui_right")
if vertical != 0:
velocity.x = move_toward(velocity.x, MAX_SPEED * vertical, SPEED)
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if horizontal != 0:
velocity.y = move_toward(velocity.y, MAX_SPEED * horizontal, SPEED)
else:
velocity.y = move_toward(velocity.y, 0, SPEED)
if horizontal != 0 or vertical != 0:
direction = Vector2(vertical, horizontal)
rotation = rotate_toward(rotation ,atan2(horizontal, vertical), 0.5)
if Input.is_action_pressed("ui_accept") and delay >= 0.5:
shoot()
delay = 0
if !is_inv() and is_currently_hit:
GameController.emit_signal("player_hit")
move_and_slide()
func shoot()->void:
var new = SHOOT_NODE.instantiate()
new.direction = (target.global_position - global_position).normalized()
new.position = target.global_position
get_parent().add_child(new)
func is_inv() -> bool:
return delay_inv <= TIME_INVUL
func start_inv()->void:
delay_inv = 0.0