~ CACTUS CARROT LAMA ~
I use this simple state class for all of the goobers.
The Cactus
They hover around you and look for opportunities to push their pricks into you.
They have two states:
"Running"
and
"Planted".
Running State
They randomly choose a spot on a circle around the player and book it. The "StuckTimer" goes off every 3 seconds and compares the position of the cactus to 3 seconds ago. If it's below a certain threshold, we assume the cactus is stuck and we choose a new random spot on the circle.
func _physics_loop(_delta):
if player_pos.distance_squared_to(body.global_position)\
> body.plant_radius * body.plant_radius:
get_parent().switch_state(%Running)
Planted State
turns on the collisions so that if the player steps on it, it gets detected.
func _start():
body.velocity = Vector2.ZERO
%AnimatedSprite2D.play("plant")
%PlantTimer.start()
The Carrot
Don't let the amount of states fool you. It's barely more complicated than the cactus. The carrot chases the player around and lunges at him when it gets close enough.
The Lama
Here the "Tracking" state is basically a wind up to let the player know what's coming. The rest are self-explanatory.
Good night!