59 lines
1.6 KiB
GDScript
59 lines
1.6 KiB
GDScript
class_name Planter
|
|
|
|
extends Node2D
|
|
|
|
signal seed_list_updated
|
|
|
|
const QUEUE_LENGTH := 6 # ENORME
|
|
|
|
@onready var plant_scene = preload("res://objects/Plant.tscn")
|
|
@onready var timer: Timer = $Timer
|
|
|
|
@export var plants: Array[PlantType]
|
|
|
|
@export var camera: Camera2D
|
|
|
|
# index of the PlantType in plants
|
|
var seed_queue: Array[int]
|
|
var can_plant := true
|
|
|
|
func get_plant_from_queue(index: int = -1) -> PlantType:
|
|
if index == -1:
|
|
return plants[seed_queue.back()]
|
|
return plants[seed_queue[index]]
|
|
|
|
func _ready() -> void:
|
|
for i in range(QUEUE_LENGTH):
|
|
seed_queue.push_front(randi_range(0, plants.size() - 1))
|
|
seed_list_updated.emit()
|
|
|
|
func _process(_delta: float) -> void:
|
|
var space := get_world_2d().direct_space_state
|
|
var parameters = PhysicsPointQueryParameters2D.new()
|
|
parameters.position = camera.get_global_mouse_position()
|
|
parameters.collide_with_areas = true
|
|
parameters.collide_with_bodies = false
|
|
parameters.collision_mask = 1
|
|
var result := space.intersect_point(parameters, 1)
|
|
can_plant = result.size() == 0
|
|
|
|
func take_next_seed() -> PlantType:
|
|
var plant_ind: int = seed_queue.pop_back()
|
|
seed_queue.push_front(randi_range(0, plants.size() - 1))
|
|
seed_list_updated.emit()
|
|
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()
|