ft: prey player detection

This commit is contained in:
Djairo Hougee 2026-01-27 19:54:16 +01:00
parent c9880d9b35
commit 7207626315
3 changed files with 22 additions and 11 deletions

View File

@ -1,7 +1,7 @@
extends CharacterBody2D
@export var attack_duration = 0.2 # TODO: finetune
@export var attack_cooldown_duration = 0.4
@export var attack_duration = 0.4
@export var attack_cooldown_duration = 0.6
@onready var attack_area: Area2D = $AttackArea
@onready var attack_timer: Timer = $AttackTimer

View File

@ -3,7 +3,9 @@ extends AbstractPrey2D
@onready var sprite = get_node("AnimatedSprite2D")
@onready var fsm = $StateMachine
# Mirroed sprites for periodic boundary
var desired_rotation: float = self.rotation
# Mirrored sprites for periodic boundary
var mirrorSprite1: Node2D
var mirrorSprite2: Node2D
var mirrorSprite3: Node2D
@ -29,11 +31,14 @@ func _process(delta: float) -> void:
_handle_wrapping()
func _physics_process(delta: float) -> void:
pass
# rotate smoothly
if self.rotation != self.desired_rotation: # FIXME: causes the mirror sprites to flip out
self.rotation = lerp_angle(self.rotation, self.desired_rotation, 4 * delta)
func move(motion: Vector3) -> void:
move_and_collide(Vector2(motion.x, motion.y)) # Moves along the given vector
self.desired_rotation = atan2(motion.y, motion.x)
# Apply boundary to new position
position = GameManager.get_boundaried_position(position)
@ -55,6 +60,11 @@ func become_injured() -> void:
mirrorSprite2.play("Injured")
mirrorSprite3.play("Injured")
func _on_sight_body_entered(body: Node2D) -> void:
if body.is_in_group("predators") or body.is_in_group("player"):
fsm.transition_to_next_state(fsm.States.FLEEING, {"threat": body})
# Mirroring table:
# |---|---|---|---|
# | 4 | 3 | 4 | 3 |
@ -142,9 +152,3 @@ func _handle_wrapping():
# Top
#mirrorSprite3.position = Vector2(sprite.position.x, sprite.position.y - GameManager.screen_size.y)
mirrorSprite3.position = Vector2(0, - GameManager.screen_size.y)
func _on_timer_timeout() -> void:
pass # Replace with function body.

View File

@ -81,5 +81,12 @@ metadata/_custom_type_script = "uid://co2xp7gauamql"
[node name="Timer" type="Timer" parent="StateMachine/Idle" index="0"]
one_shot = true
[node name="Sight" type="Area2D" parent="." index="3"]
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Sight" index="0"]
polygon = PackedVector2Array(3.8686981, -6.2705374, 7.0000973, -0.08602524, 3.5555592, 5.6287766, -3.0986624, 5.589636, -6.1517763, 0.031402588, -2.942093, -6.0748243, -29.993027, -74.37026, -11.10141, -83.6233, 9.332382, -84.00884, 35.163773, -77.06906)
[connection signal="timeout" from="StateMachine/RandomMovement/Timer" to="StateMachine/RandomMovement" method="_on_timer_timeout"]
[connection signal="timeout" from="StateMachine/Idle/Timer" to="StateMachine/Idle" method="_on_timer_timeout"]
[connection signal="body_entered" from="Sight" to="." method="_on_sight_body_entered"]
[connection signal="body_exited" from="Sight" to="." method="_on_sight_body_exited"]