57 lines
1.4 KiB
GDScript
57 lines
1.4 KiB
GDScript
|
extends Sprite2D
|
||
|
|
||
|
signal health_changed(old_value, new_value)
|
||
|
signal health_depleted
|
||
|
|
||
|
var speed: int = 40
|
||
|
var max_speed: int = 500
|
||
|
var angular_speed: float = PI * 0.15
|
||
|
var motion: Vector2 = Vector2()
|
||
|
var angular_motion: float = 0
|
||
|
|
||
|
var react_to_input: bool = true
|
||
|
|
||
|
var health: int = 100
|
||
|
|
||
|
func _ready() -> void:
|
||
|
var timer: Timer = get_node("Timer")
|
||
|
timer.timeout.connect(_on_timer_timeout)
|
||
|
|
||
|
func _on_timer_timeout() -> void:
|
||
|
modulate.a = 1 if modulate.a == 0.5 else 0.5
|
||
|
|
||
|
func _process(delta: float) -> void:
|
||
|
angular_motion = signf(angular_motion) * abs(angular_motion) * 0.9
|
||
|
motion = motion * 0.9
|
||
|
|
||
|
if react_to_input:
|
||
|
var direction: float = 0
|
||
|
if Input.is_action_pressed("ui_left"):
|
||
|
direction -= 1
|
||
|
if Input.is_action_pressed("ui_right"):
|
||
|
direction += 1
|
||
|
angular_motion += angular_speed * direction * delta
|
||
|
|
||
|
if Input.is_action_pressed("ui_up"):
|
||
|
var velocity: Vector2 = Vector2.UP.rotated(rotation) * speed
|
||
|
motion += velocity * delta
|
||
|
if Input.is_action_pressed("ui_down"):
|
||
|
var velocity: Vector2 = Vector2.DOWN.rotated(rotation) * speed
|
||
|
motion += velocity * delta
|
||
|
|
||
|
if motion.length() > max_speed:
|
||
|
motion = motion.normalized() * max_speed
|
||
|
|
||
|
rotation += angular_motion
|
||
|
position += motion
|
||
|
|
||
|
|
||
|
func _on_button_pressed() -> void:
|
||
|
react_to_input = not react_to_input
|
||
|
var new_health: int = health - 10
|
||
|
health_changed.emit(health, new_health)
|
||
|
health = new_health
|
||
|
if health <= 0:
|
||
|
health_depleted.emit()
|
||
|
queue_free()
|