61 lines
1.3 KiB
GDScript
61 lines
1.3 KiB
GDScript
class_name Robot
|
|
|
|
extends Node2D
|
|
|
|
enum MoveState { IDLE, MOVING, PLANTING }
|
|
|
|
const IDLE_SPEED := 75
|
|
const MOVE_SPEED := 500
|
|
const PLANTING_TIME := 1
|
|
const DIST_TO_PLANT_SQR := 100
|
|
|
|
signal Planted
|
|
|
|
@onready var wanderer: Wanderer = $Wanderer
|
|
@onready var planting: Timer = $Planting
|
|
@onready var move_sounds = [
|
|
$MoveSound,
|
|
$MoveSound2,
|
|
$MoveSound3,
|
|
$MoveSound4,
|
|
$MoveSound5,
|
|
]
|
|
|
|
var target_pos := Vector2()
|
|
|
|
var state := MoveState.IDLE
|
|
|
|
func _ready() -> void:
|
|
wanderer.speed = IDLE_SPEED
|
|
wanderer.move = false
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
match state:
|
|
MoveState.IDLE:
|
|
wanderer.move = true
|
|
MoveState.MOVING:
|
|
wanderer.move = false
|
|
position += position.direction_to(target_pos) * MOVE_SPEED * delta
|
|
if position.distance_squared_to(target_pos) < DIST_TO_PLANT_SQR:
|
|
state = MoveState.PLANTING
|
|
planting.start(PLANTING_TIME)
|
|
MoveState.PLANTING:
|
|
wanderer.move = false
|
|
|
|
if target_pos.x > position.x:
|
|
$AnimatedSprite2D.flip_h = false
|
|
else :
|
|
$AnimatedSprite2D.flip_h = true
|
|
|
|
func go_to(new_target_pos: Vector2):
|
|
move_sounds[randi()%len(move_sounds)].play()
|
|
state = MoveState.MOVING
|
|
target_pos = new_target_pos
|
|
|
|
|
|
func _on_planting_timeout() -> void:
|
|
$PlantSound.play()
|
|
Planted.emit()
|
|
state = MoveState.IDLE
|