robot wanderer and liberation only once
This commit is contained in:
@@ -3,13 +3,16 @@ extends Node2D
|
||||
signal liberated
|
||||
|
||||
@export var plant_need: String
|
||||
var libarated := false
|
||||
|
||||
func _on_area_2d_area_entered(area: Area2D) -> void:
|
||||
var plant = area.get_parent()
|
||||
if plant is Plant:
|
||||
if plant is Plant and not liberated:
|
||||
if plant_need == plant.parameter.type:
|
||||
plant.grown.connect(tracked_plant_grew)
|
||||
|
||||
func tracked_plant_grew():
|
||||
if liberated:
|
||||
return
|
||||
print("Liberated !!")
|
||||
liberated.emit()
|
||||
|
||||
@@ -8,6 +8,8 @@ const QUEUE_LENGTH := 6 # ENORME
|
||||
|
||||
@onready var plant_scene = preload("res://objects/Plant.tscn")
|
||||
@onready var timer: Timer = $Timer
|
||||
@onready var robot: Robot = $Robot
|
||||
@onready var robot_seed: Sprite2D = $Robot/RobotSeed
|
||||
|
||||
@export var plants: Array[PlantType]
|
||||
|
||||
@@ -44,15 +46,21 @@ func take_next_seed() -> PlantType:
|
||||
return plants[plant_ind]
|
||||
|
||||
func _unhandled_input(_event: InputEvent) -> void:
|
||||
|
||||
if Input.is_action_just_pressed("plant") :
|
||||
var mouse_pos = camera.get_global_mouse_position()
|
||||
var click_on_map = GameTerrain.is_on_map(mouse_pos)
|
||||
|
||||
if can_plant and click_on_map and timer.is_stopped():
|
||||
var chosen_type: PlantType = take_next_seed()
|
||||
var plant = plant_scene.instantiate()
|
||||
add_child(plant)
|
||||
plant.init(chosen_type)
|
||||
plant.plant(mouse_pos)
|
||||
timer.start()
|
||||
if can_plant and click_on_map and robot.state == robot.MoveState.IDLE:
|
||||
var next_plant := plants[seed_queue.back()]
|
||||
robot_seed.texture = next_plant.seed_sprite
|
||||
robot.go_to(mouse_pos)
|
||||
|
||||
|
||||
func _on_robot_planted() -> void:
|
||||
var chosen_type: PlantType = take_next_seed()
|
||||
var plant = plant_scene.instantiate()
|
||||
add_child(plant)
|
||||
plant.init(chosen_type)
|
||||
plant.plant(robot.position)
|
||||
timer.start()
|
||||
robot_seed.texture = null
|
||||
|
||||
47
scripts/robot.gd
Normal file
47
scripts/robot.gd
Normal file
@@ -0,0 +1,47 @@
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
|
||||
func go_to(new_target_pos: Vector2):
|
||||
state = MoveState.MOVING
|
||||
target_pos = new_target_pos
|
||||
|
||||
|
||||
func _on_planting_timeout() -> void:
|
||||
Planted.emit()
|
||||
state = MoveState.IDLE
|
||||
22
scripts/wanderer.gd
Normal file
22
scripts/wanderer.gd
Normal file
@@ -0,0 +1,22 @@
|
||||
class_name Wanderer
|
||||
|
||||
extends Node
|
||||
|
||||
const DIST_FROM_MOUSE := 1000.0
|
||||
|
||||
@onready var new_target: Timer = $NewTarget
|
||||
|
||||
@export var node_to_move: Node2D
|
||||
@export var speed := 75
|
||||
@export var move := false
|
||||
@export var wait_time_min := 1.0
|
||||
@export var wait_time_max := 2.0
|
||||
var target_pos := Vector2()
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if move:
|
||||
node_to_move.position += node_to_move.position.direction_to(target_pos) * speed * delta
|
||||
|
||||
func _on_new_target_timeout() -> void:
|
||||
target_pos = get_viewport().get_camera_2d().get_global_mouse_position() + (Vector2.RIGHT * DIST_FROM_MOUSE).rotated(randf_range(0, PI))
|
||||
new_target.start(randf_range(wait_time_min, wait_time_max))
|
||||
Reference in New Issue
Block a user