51 lines
1.5 KiB
GDScript
51 lines
1.5 KiB
GDScript
extends Area2D
|
|
class_name DragonEntity
|
|
|
|
|
|
@export var dragon: DragonSprite
|
|
@export_range(3, 20) var animation_duration: float
|
|
|
|
@export var min_exit_time: float
|
|
@export var max_exit_time: float
|
|
|
|
signal on_pick(dragon_id: int, position: Vector2, hat: Texture2D, shirt: Texture2D, shoes: Texture2D)
|
|
signal on_quit(dragon_id: int)
|
|
|
|
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
|
var id: int
|
|
|
|
|
|
func _ready() -> void:
|
|
set_process_input(true)
|
|
_play_initial_animation()
|
|
|
|
|
|
func _queue_exit():
|
|
await get_tree().create_timer(rng.randf_range(min_exit_time, max_exit_time)).timeout
|
|
var tween = get_tree().create_tween()
|
|
tween.tween_property($CollisionShape2D, "position", Vector2(get_window().size.x + 200, 0), animation_duration)
|
|
tween.tween_callback(_proceed_exit)
|
|
|
|
|
|
func _proceed_exit():
|
|
on_quit.emit(id)
|
|
queue_free()
|
|
|
|
|
|
func _play_initial_animation() -> void:
|
|
$CollisionShape2D.position = Vector2(get_window().size.x + 200, 0)
|
|
var tween = get_tree().create_tween()
|
|
tween.tween_property($CollisionShape2D, "position", Vector2(0, 0), animation_duration)
|
|
tween.tween_callback(_queue_exit)
|
|
|
|
|
|
func _input_event(viewport, event, shape_idx) -> void:
|
|
if event is InputEventMouseButton \
|
|
and event.button_index == MOUSE_BUTTON_LEFT \
|
|
and event.is_pressed():
|
|
on_pick.emit(id, position + $CollisionShape2D.position, dragon.hat.texture, dragon.shirt.texture, dragon.shoes.texture)
|
|
queue_free()
|
|
|
|
|
|
func dress(hat: Texture2D, shirt: Texture2D, shoes: Texture2D):
|
|
dragon.dress(hat, shirt, shoes)
|