feat: dragon animations

This commit is contained in:
Gerard Gascón 2025-04-15 22:21:23 +02:00
parent 9f2b78c1bd
commit 787344e5e4
4 changed files with 46 additions and 19 deletions

View file

@ -9,9 +9,8 @@ size = Vector2(108, 108)
[node name="Dragon" type="Area2D" node_paths=PackedStringArray("dragon")]
script = ExtResource("1_jccds")
dragon = NodePath("CollisionShape2D/Dragon")
animation_duration = 10.0
min_exit_time = 60.0
max_exit_time = 600.0
animation_duration = 7.0
max_exit_time = 10.0
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(-2, -4)

View file

@ -14,16 +14,24 @@ signal on_quit(dragon_id: int)
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
var properties: DragonProperties
var _start_position: Vector2
var _is_flying: bool = false
func _ready() -> void:
set_process_input(true)
_play_initial_animation()
func _queue_exit():
if not _is_flying:
dragon.play_idle()
await get_tree().create_timer(rng.randf_range(min_exit_time, max_exit_time)).timeout
if not _is_flying:
dragon.play_walk()
dragon.walk_right()
var tween: Tween = get_tree().create_tween()
tween.tween_property($CollisionShape2D, "position", Vector2(get_window().size.x + 200, 0), animation_duration)
tween.tween_property($CollisionShape2D, "position", _start_position, animation_duration)
tween.tween_callback(_proceed_exit)
@ -32,8 +40,13 @@ func _proceed_exit():
queue_free()
func _play_initial_animation() -> void:
$CollisionShape2D.position = Vector2(get_window().size.x + 200, 0)
func play_initial_animation(start_spot: Node2D) -> void:
if get_parent().position.y < 150:
_is_flying = true
dragon.play_fly()
$CollisionShape2D.position = start_spot.position
_start_position = start_spot.position
var tween = get_tree().create_tween()
tween.tween_property($CollisionShape2D, "position", Vector2(0, 0), animation_duration)
tween.tween_callback(_queue_exit)
@ -43,7 +56,7 @@ func _input_event(_viewport, event, _shape_idx) -> void:
if event is InputEventMouseButton \
and event.button_index == MOUSE_BUTTON_LEFT \
and event.is_pressed():
properties.position = position + $CollisionShape2D.position
properties.position = global_position + $CollisionShape2D.position
on_pick.emit(properties)
queue_free()