ld-56/src/creature.gd

79 lines
1.9 KiB
GDScript3
Raw Normal View History

2024-10-07 05:05:27 +00:00
extends Node2D
var key
@onready var data = Data.data.creatures[key]
@onready var image = Data.data.images[key]
@onready var sprite_2d: Sprite2D = $Sprite2D
2024-10-07 21:37:16 +00:00
@onready var audio_stream_player: AudioStreamPlayer = $AudioStreamPlayer
@onready var audio_stream_player_2: AudioStreamPlayer = $AudioStreamPlayer2
2024-10-07 05:05:27 +00:00
2024-10-07 18:53:09 +00:00
const COIN = preload("res://src/Coin.tscn")
var held = false
2024-10-07 21:37:16 +00:00
var target
var start_pos
var lifetime_timer = randi_range(10, 15)
var end_pos
var tween
var tiled = false
var tiles = []
2024-10-07 05:05:27 +00:00
func _ready() -> void:
sprite_2d.texture = image
2024-10-07 18:53:09 +00:00
Triggerer.listen("hour", _on_hour)
2024-10-07 21:37:16 +00:00
func _process(delta: float) -> void:
if target:
position = position.lerp(target, delta * 2)
if get_window().name == "CreatureWindow":
if Persister.get_value("paused"):
return
lifetime_timer -= delta
if lifetime_timer <= 0 and not end_pos:
end_pos = true
match randi_range(0, 3):
0:
target = Vector2(-25, randi_range(-25, 100))
1:
target = Vector2(100, randi_range(-25, 100))
2:
target = Vector2(randi_range(-25, 100), -25)
3:
target = Vector2(randi_range(-25, 100), 100)
if lifetime_timer <= -2:
queue_free()
2024-10-07 18:53:09 +00:00
func _on_hour(_data):
if not held and randf() < (Persister.get_value("creature_resource_spawn_rate") / 100.0):
var new_coin = COIN.instantiate()
new_coin.global_position = global_position
get_window().add_child(new_coin)
2024-10-07 21:37:16 +00:00
#audio_stream_player.play()
Triggerer.trigger("coin_sound")
2024-10-07 05:05:27 +00:00
func _on_mouse_handler_clicked() -> void:
2024-10-07 21:37:16 +00:00
Triggerer.trigger("grab_creature", {"creature": key, "old_window": get_window(), "old_position": position, "tiled": tiled, "tiles": tiles})
2024-10-07 17:07:46 +00:00
Persister.persist_data("grabbing", true)
queue_free()
2024-10-07 21:37:16 +00:00
func _on_mouse_handler_hovered() -> void:
if held:
return
if tween:
tween.kill()
tween = create_tween()
tween.tween_property(sprite_2d, "position:y", -1, 0.075)
tween.tween_property(sprite_2d, "position:y", 0, 0.075)
#audio_stream_player_2.play()
Triggerer.trigger("creature_sound")