53 lines
1.6 KiB
GDScript
53 lines
1.6 KiB
GDScript
extends AbstractPrey2D
|
|
|
|
@onready var wrapper: WrappingManager = $WrappingManager
|
|
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
|
@onready var fsm: StateMachine = $StateMachine
|
|
|
|
@export var speed = 0.5
|
|
var desired_rotation: float = self.rotation
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
health = maxHealth
|
|
sprite.play("Healthy")
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
# smoothly rotate
|
|
if self.rotation != self.desired_rotation:
|
|
self.rotation = lerp_angle(self.rotation, self.desired_rotation, clampf(4 * delta, 0, 1))
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
pass
|
|
|
|
func move(motion: Vector3, mod: float = 1.0) -> void:
|
|
move_and_collide(Vector2(motion.x, motion.y).normalized() * self.speed * mod) # Moves along the given vector
|
|
self.desired_rotation = atan2(motion.y, motion.x)
|
|
|
|
# Apply boundary to new position
|
|
position = GameManager.get_boundaried_position(position)
|
|
|
|
func handle_damage(dmg: int, src: Node) -> void:
|
|
health = max(0, health-dmg)
|
|
if health == 0:
|
|
die()
|
|
if health < maxHealth:
|
|
become_injured()
|
|
fsm.transition_to_next_state(fsm.States.FLEEING, {"threat": src})
|
|
|
|
func die() -> void:
|
|
sprite.play("Dying")
|
|
wrapper.play_sprite("Dying")
|
|
GameManager.foodManager._spawn_food(position)
|
|
super.die()
|
|
|
|
func become_injured() -> void:
|
|
sprite.play("Injured")
|
|
wrapper.play_sprite("Injured")
|
|
|
|
func _on_sight_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("predator") or (health < maxHealth and body.is_in_group("player")):
|
|
fsm.transition_to_next_state(fsm.States.FLEEING, {"threat": body})
|