pollen-not-included/entities/scripts/snail.gd
Dan 7d47bdbf5a Enhanced snail interaction and sleep behavior
The update includes improvements to the snail's interactive behavior. Now, when the mouse hovers over a snail that is eating, it changes to a hand cursor. This change enhances user feedback during interaction.

Additionally, the sleeping state of the snail has been expanded. The snail now wakes up after a random interval between 15 and 25 seconds. A timer tracks this duration and triggers a state transition to "Eating" once elapsed.

A new hand cursor resource was also added to the CursorManager for use in these interactions.
2024-05-15 14:47:28 +01:00

48 lines
No EOL
1.4 KiB
GDScript

extends CharacterBody2D
class_name Snail
@onready var fsm : FiniteStateMachine = $StateMachine as FiniteStateMachine
@onready var flowers : Flowers = get_parent()
var enabled : bool = false
var eating : bool = false
var speed : float = 20.0
var mouse_over : bool = false
func _ready() -> void:
connect("mouse_entered", Callable(self, "on_mouse_entered"))
connect("mouse_exited", Callable(self, "on_mouse_exited"))
# Detect mouse left click and trigger function
func _input(event : InputEvent) -> void:
if mouse_over and eating:
if event is InputEventMouseButton:
if (event is InputEventMouseButton && event.button_index == MOUSE_BUTTON_LEFT && event.pressed):
maybe_sleep()
func eat() -> void:
# Play the munch animation and noise
# Reduce the GameState flower_nectar_level
GameState.flower_nectar_level -= 1
func maybe_sleep() -> void:
# If the snail is still eating, then we want a 30% chance of switching it it to the sleeping state
if eating:
if randf() < 0.3:
if fsm.get_current_state_name() == "eating":
fsm.change_state(fsm.current_state, "sleeping")
func get_random_target() -> Vector2:
return flowers.get_random_circumference_points()
func on_mouse_entered() -> void:
if eating:
mouse_over = true
CursorMgr.hand()
Log.pr("Mouse entered the snail!")
func on_mouse_exited() -> void:
# Reset the cursor to the default
mouse_over = false
CursorMgr.reset_cursor()