fx: prey-predator interactions

This commit is contained in:
2026-03-01 14:55:25 +01:00
parent f83b290f5c
commit 2b46fd18f7
8 changed files with 15 additions and 16 deletions

View File

@@ -83,12 +83,9 @@ func _on_attack_hit(body: Node2D) -> void:
if body.has_method("handle_damage"): if body.has_method("handle_damage"):
body.handle_damage(damage, self) body.handle_damage(damage, self)
hit_hittable = true hit_hittable = true
elif body.is_in_group("resources"):
pass
if hit_hittable: if hit_hittable:
await get_tree().create_timer(0.2).timeout await get_tree().create_timer(0.2).timeout
sprite.play("default") sprite.play("default")
# TODO: resource handling logic
func _on_invulnerable_cooldown_timeout() -> void: func _on_invulnerable_cooldown_timeout() -> void:
hasiframes = false hasiframes = false

View File

@@ -31,7 +31,6 @@ animations = [{
}] }]
[sub_resource type="CircleShape2D" id="CircleShape2D_5hxmy"] [sub_resource type="CircleShape2D" id="CircleShape2D_5hxmy"]
radius = 10.999997
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_4flbx"] [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_4flbx"]
radius = 5.999982 radius = 5.999982
@@ -51,7 +50,7 @@ rotation = -1.5732701
collision_mask = 6 collision_mask = 6
[node name="CollisionShape2D" type="CollisionShape2D" parent="AttackArea" unique_id=1968501358] [node name="CollisionShape2D" type="CollisionShape2D" parent="AttackArea" unique_id=1968501358]
position = Vector2(55.984985, 6.138512) position = Vector2(55.977566, 9.138502)
shape = SubResource("CircleShape2D_5hxmy") shape = SubResource("CircleShape2D_5hxmy")
debug_color = Color(0.80813414, 0.3957308, 0.3356335, 0.41960785) debug_color = Color(0.80813414, 0.3957308, 0.3356335, 0.41960785)

View File

@@ -39,7 +39,7 @@ func attack(target: Node) -> void:
var hit_hittable = false var hit_hittable = false
if target.is_in_group("prey") or target.is_in_group("player"): if target.is_in_group("prey") or target.is_in_group("player"):
if target.has_method("handle_damage"): if target.has_method("handle_damage"):
target.handle_damage(damage, self) target.handle_damage(damage, self.collision)
hit_hittable = true hit_hittable = true
elif target.is_in_group("resources"): elif target.is_in_group("resources"):
# TODO: resource handling logic # TODO: resource handling logic

View File

@@ -92,12 +92,12 @@ animations = [{
"speed": 5.0 "speed": 5.0
}] }]
[node name="HammerheadPredator" type="Node" unique_id=1312337720 groups=["predator"]] [node name="HammerheadPredator" type="Node" unique_id=1312337720]
script = ExtResource("1_xp037") script = ExtResource("1_xp037")
maxHealth = 50 maxHealth = 50
metadata/_custom_type_script = "uid://dgfimmq53whll" metadata/_custom_type_script = "uid://dgfimmq53whll"
[node name="Collision" type="CharacterBody2D" parent="." unique_id=76553184] [node name="Collision" type="CharacterBody2D" parent="." unique_id=76553184 groups=["predator"]]
scale = Vector2(0.8, 0.8) scale = Vector2(0.8, 0.8)
collision_layer = 4 collision_layer = 4
collision_mask = 3 collision_mask = 3

View File

@@ -11,16 +11,16 @@ func enter(previous_state_path: String, data := {}) -> void:
finished.emit(owner.fsm.States.IDLE, {}) finished.emit(owner.fsm.States.IDLE, {})
func physics_update(_delta: float) -> void: func physics_update(_delta: float) -> void:
if target == null or GameManager.calc_distance(owner.position, target.position) > owner.sight_range: if target == null or GameManager.calc_distance(owner.collision.position, target.position) > owner.sight_range:
finished.emit(owner.fsm.States.IDLE, {}) finished.emit(owner.fsm.States.IDLE, {})
return return
# alternatively, we could use a collision shape and inbuilt signals, but im not sure if that works better (mixing signals and custom fsm stuff i mean # alternatively, we could use a collision shape and inbuilt signals, but im not sure if that works better (mixing signals and custom fsm stuff i mean
if GameManager.calc_distance(owner.position, target.position) > owner.attack_range: if GameManager.calc_distance(owner.collision.position, target.position) > owner.attack_range:
owner.move(move_towards(target.position)) owner.move(move_towards(target.position))
else: else:
owner.attack(target) owner.attack(target)
func move_towards(pos: Vector2) -> Vector3: func move_towards(pos: Vector2) -> Vector3:
var diff = target.position - owner.position var diff = target.position - owner.collision.position
return Vector3(diff.x, diff.y ,0) return Vector3(diff.x, diff.y ,0)

View File

@@ -18,6 +18,10 @@ func move(motion: Vector3, mod: float) -> void:
position = GameManager.get_boundaried_position(position) position = GameManager.get_boundaried_position(position)
func handle_damage(dmg: int, src: Node) -> void:
if owner:
owner.handle_damage(dmg, src)
func duplicate_init() -> void: func duplicate_init() -> void:
var sight = $Sight var sight = $Sight
remove_child(sight) remove_child(sight)

View File

@@ -55,7 +55,7 @@ animations = [{
script = ExtResource("2_0227s") script = ExtResource("2_0227s")
maxHealth = 20 maxHealth = 20
[node name="Collision" type="CharacterBody2D" parent="." index="0" unique_id=331517910] [node name="Collision" type="CharacterBody2D" parent="." index="0" unique_id=331517910 groups=["prey"]]
scale = Vector2(0.2, 0.2) scale = Vector2(0.2, 0.2)
collision_layer = 2 collision_layer = 2
collision_mask = 5 collision_mask = 5

View File

@@ -1,6 +1,6 @@
extends State extends State
var threat: Node2D var threat: Node
var threshold: float = 100 var threshold: float = 100
func enter(previous_state_path: String, data := {}) -> void: func enter(previous_state_path: String, data := {}) -> void:
@@ -11,13 +11,12 @@ func enter(previous_state_path: String, data := {}) -> void:
finished.emit(owner.fsm.States.IDLE, {}) finished.emit(owner.fsm.States.IDLE, {})
func physics_update(_delta: float) -> void: func physics_update(_delta: float) -> void:
if not is_instance_valid(threat) or GameManager.calc_distance(owner.position, threat.position) > threshold: if not is_instance_valid(threat) or GameManager.calc_distance(owner.collision.position, threat.position) > threshold:
finished.emit(owner.fsm.States.IDLE, {}) finished.emit(owner.fsm.States.IDLE, {})
return return
owner.move(flee_from(threat.position)) owner.move(flee_from(threat.position))
func flee_from(pos: Vector2) -> Vector3: func flee_from(pos: Vector2) -> Vector3:
# FIXME: What is the argument "pos" and why is it unused? var diff = pos - owner.collision.position
var diff = threat.position - owner.position
diff = diff.normalized() * -1 diff = diff.normalized() * -1
return Vector3(diff.x, diff.y ,0) return Vector3(diff.x, diff.y ,0)