add ant gatherer
This commit is contained in:
77
scripts/units/ant_gatherer.gd
Normal file
77
scripts/units/ant_gatherer.gd
Normal file
@@ -0,0 +1,77 @@
|
||||
extends ControlledUnit
|
||||
class_name AntGatherer
|
||||
|
||||
enum AntGathererState {
|
||||
WANDERING,
|
||||
MOVING,
|
||||
GATHERING,
|
||||
}
|
||||
|
||||
var state: AntGathererState = AntGathererState.WANDERING
|
||||
|
||||
@onready var gathering: Gathering = $Gathering
|
||||
|
||||
|
||||
static func get_cost() -> int:
|
||||
return 15
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
assert(gathering != null, "gathering missing!")
|
||||
super._ready()
|
||||
moving_started.connect(_on_moving_started)
|
||||
moving_ended.connect(_on_moving_ended)
|
||||
nav_agent.navigation_finished.connect(gathering.on_nav_agent_navigation_finished)
|
||||
gathering.initialize(anthill, 8, 0.4, 1)
|
||||
gathering.target_set.connect(_on_gathering_target_set)
|
||||
gathering.stop_gathering.connect(_on_gathering_stop)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
super._process(delta)
|
||||
if moving_to_target:
|
||||
state = AntGathererState.MOVING
|
||||
|
||||
_handle_wandering(delta)
|
||||
_handle_gathering()
|
||||
|
||||
|
||||
func _interact(with: Interactable) -> void:
|
||||
if with is Honeydew:
|
||||
state = AntGathererState.GATHERING
|
||||
gathering.go_gather(with as Honeydew)
|
||||
|
||||
|
||||
func _handle_wandering(delta: float) -> void:
|
||||
if state != AntGathererState.WANDERING:
|
||||
return
|
||||
|
||||
_wander(delta)
|
||||
|
||||
|
||||
func _handle_gathering() -> void:
|
||||
gathering.handle_gathering(state != AntGathererState.GATHERING)
|
||||
|
||||
|
||||
func _on_moving_ended() -> void:
|
||||
state = AntGathererState.WANDERING
|
||||
|
||||
|
||||
func _on_moving_started() -> void:
|
||||
if state == AntGathererState.GATHERING:
|
||||
gathering.stop_all_gathering()
|
||||
state = AntGathererState.MOVING
|
||||
|
||||
|
||||
func _on_gathering_target_set(pos: Vector3) -> void:
|
||||
if state != AntGathererState.GATHERING:
|
||||
return
|
||||
|
||||
if pos != Vector3.ZERO:
|
||||
nav_agent.set_target_position(pos)
|
||||
else:
|
||||
nav_agent.set_target_position(anthill.global_position)
|
||||
|
||||
|
||||
func _on_gathering_stop() -> void:
|
||||
state = AntGathererState.WANDERING
|
||||
@@ -22,7 +22,7 @@ func _ready() -> void:
|
||||
moving_started.connect(_on_moving_started)
|
||||
moving_ended.connect(_on_moving_ended)
|
||||
nav_agent.navigation_finished.connect(gathering.on_nav_agent_navigation_finished)
|
||||
gathering.anthill = anthill
|
||||
gathering.initialize(anthill)
|
||||
gathering.target_set.connect(_on_gathering_target_set)
|
||||
gathering.stop_gathering.connect(_on_gathering_stop)
|
||||
|
||||
|
||||
@@ -4,6 +4,10 @@ class_name Gathering
|
||||
signal target_set(pos: Vector3)
|
||||
signal stop_gathering
|
||||
|
||||
const DEFAULT_MAX_CARRYING = 3
|
||||
const DEFAULT_DROP_INTERVAL = 0.25
|
||||
const DEFAULT_PICKUP_INTERVAL = 0.5
|
||||
|
||||
enum GatherState {
|
||||
PICKING_UP,
|
||||
DEPOSITING,
|
||||
@@ -12,11 +16,13 @@ enum GatherState {
|
||||
|
||||
var nearby_items: Dictionary = {}
|
||||
var carrying_items: Array[Honeydew] = []
|
||||
var max_carrying: int = 3
|
||||
var max_carrying: int = DEFAULT_MAX_CARRYING
|
||||
var deposit_leftover: int = 0
|
||||
var state: GatherState = GatherState.STOP
|
||||
var target: Honeydew
|
||||
var anthill: Anthill
|
||||
var drop_interval: float = DEFAULT_DROP_INTERVAL
|
||||
var pickup_interval: float = DEFAULT_PICKUP_INTERVAL
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -37,6 +43,18 @@ func _process(_delta: float) -> void:
|
||||
DebugDraw.circle(target.global_position)
|
||||
|
||||
|
||||
func initialize(
|
||||
from: Anthill,
|
||||
max_carry: int = DEFAULT_MAX_CARRYING,
|
||||
drop_interv: float = DEFAULT_DROP_INTERVAL,
|
||||
pickup_interv: float = DEFAULT_PICKUP_INTERVAL,
|
||||
) -> void:
|
||||
anthill = from
|
||||
max_carrying = max_carry
|
||||
drop_interval = drop_interv
|
||||
pickup_interval = pickup_interv
|
||||
|
||||
|
||||
func go_gather(item: Honeydew) -> void:
|
||||
if anthill.space_left() <= 0:
|
||||
return
|
||||
@@ -80,7 +98,7 @@ func _pick_up() -> void:
|
||||
carrying_items.append(target)
|
||||
target.set_carried(true)
|
||||
|
||||
await get_tree().create_timer(0.5).timeout
|
||||
await get_tree().create_timer(pickup_interval).timeout
|
||||
if carrying_items.size() >= max_carrying:
|
||||
go_deposit()
|
||||
return
|
||||
@@ -111,7 +129,7 @@ func _deposit() -> void:
|
||||
_erase_honeydew(item)
|
||||
item.queue_free()
|
||||
anthill.deposit_honeydew(1)
|
||||
await get_tree().create_timer(0.25).timeout
|
||||
await get_tree().create_timer(drop_interval).timeout
|
||||
|
||||
state = GatherState.PICKING_UP
|
||||
var nearest := _find_nearest(nearby_items.values())
|
||||
@@ -132,7 +150,7 @@ func _drop_everything() -> void:
|
||||
while carrying_items.size() > 0:
|
||||
var item := carrying_items.pop_back() as Honeydew
|
||||
item.set_carried(false)
|
||||
await get_tree().create_timer(0.25).timeout
|
||||
await get_tree().create_timer(drop_interval).timeout
|
||||
|
||||
|
||||
func _find_nearest(items: Array) -> Honeydew:
|
||||
|
||||
Reference in New Issue
Block a user