83 lines
2.1 KiB
GDScript
83 lines
2.1 KiB
GDScript
extends Window
|
|
class_name Dragon
|
|
|
|
@export var dragon_speed: float = 20.0
|
|
@export var dragon: DragonSprite
|
|
|
|
@onready var _actual_position: Vector2 = position
|
|
var main_window_rect: Rect2i
|
|
@export var draggable: Draggable
|
|
|
|
var _walking: bool = false
|
|
var _thinking_path: bool = false
|
|
var _target_pos: Vector2
|
|
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
|
|
|
signal place_back(dragon: Dragon)
|
|
|
|
var id: int
|
|
|
|
|
|
func on_place_back() -> void:
|
|
place_back.emit(self)
|
|
|
|
|
|
func start_dragon_drag()-> void:
|
|
draggable.queue_initial_drag()
|
|
|
|
func _process(delta: float) -> void:
|
|
if draggable.dragging:
|
|
_actual_position = position
|
|
_walking = false
|
|
return
|
|
|
|
if not _walking:
|
|
if not _thinking_path:
|
|
_thinking_path = true
|
|
await get_tree().create_timer(rng.randf_range(2, 7)).timeout
|
|
_pick_random_screen_position()
|
|
return
|
|
|
|
_move_to_target(delta)
|
|
|
|
|
|
func dress(hat: Texture2D, shirt: Texture2D, shoes: Texture2D):
|
|
dragon.dress(hat, shirt, shoes)
|
|
|
|
|
|
func set_dragon_name(dragon_name: String):
|
|
dragon.set_dragon_name(dragon_name)
|
|
|
|
|
|
func _move_to_target(delta: float):
|
|
if _actual_position.distance_to(_target_pos) > 10.0:
|
|
var direction: Vector2 = (_target_pos - _actual_position).normalized()
|
|
_actual_position += dragon_speed * direction * delta
|
|
position = _actual_position
|
|
return
|
|
|
|
_walking = false
|
|
|
|
|
|
func _pick_random_screen_position() -> void:
|
|
_walking = true
|
|
_thinking_path = false
|
|
|
|
var display_index: int = DisplayServer.window_get_current_screen()
|
|
var work_area_position: Vector2i = DisplayServer.screen_get_usable_rect(display_index).position
|
|
var work_area_size: Vector2i = DisplayServer.screen_get_usable_rect(display_index).size
|
|
|
|
var random_pos: Vector2i = Vector2i(
|
|
work_area_position.x + rng.randi_range(10, work_area_size.x - 10 - size.x),
|
|
work_area_position.y + rng.randi_range(10, work_area_size.y - 10 - size.y)
|
|
)
|
|
|
|
_target_pos = random_pos
|
|
|
|
|
|
func set_scale(scale: float) -> void:
|
|
var original_size: Vector2i = size
|
|
size = Vector2i(size.x * scale, size.y * scale)
|
|
dragon.scale = Vector2(scale, scale)
|
|
|
|
position += Vector2i((original_size.x - size.x) / 2.0, (original_size.y - size.y) / 2.0)
|