Bee State Machine

This commit is contained in:
Dan 2024-05-03 15:43:05 +01:00
parent 20bcab01b1
commit 752131c955
16 changed files with 467 additions and 13 deletions

View file

@ -0,0 +1,29 @@
extends State
class_name BeeGathering
@export var animator : AnimationPlayer
@onready var bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works
var time_at_patch : float = 0.0
func enter(_msg := {}):
Log.pr("I am going to attempt to gather some stuff!")
#animator.play("Idle")
#if !bee.in_range_of_flowers:
# ## Go home bee, you're drunk
# state_transition.emit(self, "Idle")
func update(_delta : float):
if bee.in_range_of_flowers:
#animator.play("Gathering")
time_at_patch += _delta
if time_at_patch > 5.0:
bee.nectar += 1
state_transition.emit(self, "Idle")
else:
state_transition.emit(self, "Idle")

View file

@ -0,0 +1,37 @@
extends State
class_name BeeIdle
@export var animator : AnimationPlayer
@onready var bee = get_parent().get_parent() # I think this is bad but I dont care it works
var idle_time : float = 0.0
func _ready():
Log.pr(bee, bee.nectar)
func enter(_msg := {}):
Log.pr("I sit by idl-bee")
#animator.play("Idle")
pass
func update(delta : float):
idle_time += delta
if idle_time > 2.0:
Log.pr("Been idle for 2 seconds, check for something to do...")
find_something_to_do()
idle_time = 0.0
pass
func find_something_to_do():
if bee.nectar > 0:
## Bee has pollen - head home
Log.pr(bee, "I have nectar, I should return to the hive")
state_transition.emit(self, "Traveling")
else:
## Bee has no pollen - they should move to a flower
Log.pr(bee, "I have no nectar, I should find a flower")
state_transition.emit(self, "Traveling")
pass

View file

@ -0,0 +1,37 @@
extends State
class_name BeeTraveling
@export var animator : AnimationPlayer
@export var target : Drone = null
@onready var bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works
var t = 0
func enter(_msg := {}):
Log.pr("I am on the move!")
## Get the next target location from the bee
target = bee.get_next_target()
#animator.play("Idle")
pass
func update(delta : float):
if target:
if bee.position.distance_to(target.position) > 3:
bee.velocity = (target.get_global_position() - bee.position).normalized() * bee.speed * delta
bee.move_and_collide(bee.velocity)
bee.look_at(target.position)
else:
# Bee has arrived at location, if its the hive or a collector drone do the things
if(target.name == "CollectorDrone"):
state_transition.emit(self, "Gathering")
else:
state_transition.emit(self, "Idle")
pass