ft: predator mostly done
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
extends AbstractPredator2D
|
||||
|
||||
# TODO: attacking logic + behaviour
|
||||
# TODO: movement is buged (seems to not move/teleport somewhat
|
||||
# FIXME: (general) tracking across wrapping boundary
|
||||
# TODO: mirroring (thought, extracct that to general function/resource?
|
||||
|
||||
@onready var sprite = $AnimatedSprite2D
|
||||
@onready var fsm = $StateMachine
|
||||
|
||||
@export var speed = 0.8
|
||||
var can_attack: bool = true
|
||||
var desired_rotation: float = self.rotation
|
||||
@onready var sprite = $AnimatedSprite2D
|
||||
@onready var fsm: StateMachine = $StateMachine
|
||||
@onready var attack_cooldown_timer: Timer = $AttackCooldownTimer
|
||||
|
||||
@export var damage: int = 15
|
||||
@export var attack_range = 20
|
||||
@export var sight_range = 200
|
||||
@export var speed = 0.8
|
||||
|
||||
func _ready() -> void:
|
||||
health = maxHealth
|
||||
@@ -24,7 +28,7 @@ func _process(delta: float) -> void:
|
||||
func _physics_process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
# FIXME: (also goes for prey) this is framerate dependent
|
||||
# FIXME: (also goes for prey) this is framerate dependent UNLESS called from a _physics function.
|
||||
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)
|
||||
@@ -32,7 +36,45 @@ func move(motion: Vector3, mod: float = 1.0) -> void:
|
||||
# Apply boundary to new position
|
||||
position = GameManager.get_boundaried_position(position)
|
||||
|
||||
func try_attack(target: Node) -> void:
|
||||
if not can_attack:
|
||||
return
|
||||
attack(target)
|
||||
|
||||
func attack(target: Node) -> void:
|
||||
can_attack = false
|
||||
var hit_hittable = false
|
||||
if target.is_in_group("prey") or target.is_in_group("player"):
|
||||
if target.has_method("handle_damage"):
|
||||
target.handle_damage(damage, self)
|
||||
hit_hittable = true
|
||||
elif target.is_in_group("resources"):
|
||||
# TODO: resource handling logic
|
||||
pass
|
||||
if hit_hittable:
|
||||
attack_cooldown_timer.start()
|
||||
|
||||
|
||||
func handle_damage(dmg: int, src: Node) -> void:
|
||||
health = max(0, health-dmg)
|
||||
if health == 0:
|
||||
die()
|
||||
if health < maxHealth/2:
|
||||
become_injured()
|
||||
fsm.transition_to_next_state(fsm.States.FLEEING, {"threat": src})
|
||||
elif health < maxHealth:
|
||||
become_injured()
|
||||
fsm.transition_to_next_state(fsm.States.HUNTING, {"target": src})
|
||||
|
||||
func die() -> void:
|
||||
super.die()
|
||||
|
||||
func become_injured() -> void:
|
||||
sprite.play("Hurt")
|
||||
|
||||
func _on_sight_body_entered(body: Node2D) -> void:
|
||||
if body.is_in_group("prey") or (health < maxHealth and body.is_in_group("player")):
|
||||
if fsm.map(fsm.state) != fsm.States.HUNTING and body.is_in_group("prey") or (health < maxHealth and body.is_in_group("player")):
|
||||
fsm.transition_to_next_state(fsm.States.HUNTING, {"target": body})
|
||||
|
||||
func _on_attack_cooldown_timer_timeout() -> void:
|
||||
can_attack = true
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
|
||||
[ext_resource type="Script" uid="uid://d07cjelbqbiug" path="res://molecular/predator/hammerhead_predator.gd" id="1_xp037"]
|
||||
[ext_resource type="Texture2D" uid="uid://ch5rddsumyyhm" path="res://molecular/assets/predator/predator-healthy.png" id="2_34kwa"]
|
||||
[ext_resource type="Texture2D" uid="uid://30uwkdbnuu3h" path="res://molecular/assets/predator/hammerheadRibozyme-hunting.png" id="3_0ts4d"]
|
||||
[ext_resource type="Script" uid="uid://cygrmt03sx0k1" path="res://molecular/predator/state_machine.gd" id="3_xp037"]
|
||||
[ext_resource type="Script" uid="uid://xbiqj7ubmj7d" path="res://molecular/prey/state_idle.gd" id="4_8a23j"]
|
||||
[ext_resource type="Texture2D" uid="uid://jyuf4lgjo64" path="res://molecular/assets/predator/hammerheadRibozyme-hurt.png" id="4_shhro"]
|
||||
[ext_resource type="Script" uid="uid://ubcu8fdfxxj1" path="res://molecular/prey/state_random_movement.gd" id="5_6rsu5"]
|
||||
[ext_resource type="Script" uid="uid://bc7apl71t0q04" path="res://molecular/predator/state_hunting.gd" id="8_7qt2q"]
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_8a23j"]
|
||||
atlas = ExtResource("2_34kwa")
|
||||
@@ -18,6 +21,30 @@ region = Rect2(64, 0, 64, 64)
|
||||
atlas = ExtResource("2_34kwa")
|
||||
region = Rect2(128, 0, 64, 64)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_nu6jw"]
|
||||
atlas = ExtResource("3_0ts4d")
|
||||
region = Rect2(0, 0, 64, 64)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_8inuv"]
|
||||
atlas = ExtResource("3_0ts4d")
|
||||
region = Rect2(64, 0, 64, 64)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_orf3n"]
|
||||
atlas = ExtResource("3_0ts4d")
|
||||
region = Rect2(128, 0, 64, 64)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_vkkje"]
|
||||
atlas = ExtResource("4_shhro")
|
||||
region = Rect2(0, 0, 64, 64)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_jfmyn"]
|
||||
atlas = ExtResource("4_shhro")
|
||||
region = Rect2(64, 0, 64, 64)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_f26lq"]
|
||||
atlas = ExtResource("4_shhro")
|
||||
region = Rect2(128, 0, 64, 64)
|
||||
|
||||
[sub_resource type="SpriteFrames" id="SpriteFrames_shhro"]
|
||||
animations = [{
|
||||
"frames": [{
|
||||
@@ -33,6 +60,34 @@ animations = [{
|
||||
"loop": true,
|
||||
"name": &"Healthy",
|
||||
"speed": 5.0
|
||||
}, {
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_nu6jw")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_8inuv")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_orf3n")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"Hunting",
|
||||
"speed": 5.0
|
||||
}, {
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_vkkje")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_jfmyn")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_f26lq")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"Hurt",
|
||||
"speed": 5.0
|
||||
}]
|
||||
|
||||
[node name="HammerheadPredator" type="CharacterBody2D" unique_id=678504815 groups=["predator"]]
|
||||
@@ -45,18 +100,20 @@ maxHealth = 50
|
||||
metadata/_custom_type_script = "uid://dgfimmq53whll"
|
||||
|
||||
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="." unique_id=410999609]
|
||||
rotation = 4.712389
|
||||
sprite_frames = SubResource("SpriteFrames_shhro")
|
||||
animation = &"Healthy"
|
||||
animation = &"Hunting"
|
||||
|
||||
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="." unique_id=1596156928]
|
||||
light_mask = 4
|
||||
visibility_layer = 4
|
||||
position = Vector2(0.11167908, 1.1167793)
|
||||
rotation = -1.5707964
|
||||
polygon = PackedVector2Array(-22.184862, -27.994831, 23.481365, -27.21198, 13.82622, 25.891317, -6.005971, 25.891317)
|
||||
|
||||
[node name="StateMachine" type="Node" parent="." unique_id=1857729810 node_paths=PackedStringArray("initial_state")]
|
||||
script = ExtResource("3_xp037")
|
||||
initial_state = NodePath("Idle")
|
||||
initial_state = NodePath("RandomMovement")
|
||||
metadata/_custom_type_script = "uid://ck7k8ht54snsy"
|
||||
|
||||
[node name="Idle" type="Node" parent="StateMachine" unique_id=265876039]
|
||||
@@ -73,6 +130,10 @@ metadata/_custom_type_script = "uid://co2xp7gauamql"
|
||||
[node name="Timer" type="Timer" parent="StateMachine/RandomMovement" unique_id=447822526]
|
||||
one_shot = true
|
||||
|
||||
[node name="Hunting" type="Node" parent="StateMachine" unique_id=1569866955]
|
||||
script = ExtResource("8_7qt2q")
|
||||
metadata/_custom_type_script = "uid://co2xp7gauamql"
|
||||
|
||||
[node name="Sight" type="Area2D" parent="." unique_id=1608385873]
|
||||
collision_layer = 0
|
||||
collision_mask = 7
|
||||
@@ -80,6 +141,15 @@ collision_mask = 7
|
||||
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Sight" unique_id=1707240701]
|
||||
light_mask = 4
|
||||
visibility_layer = 4
|
||||
position = Vector2(-1.0900421, 1.6350927)
|
||||
rotation = -1.5707964
|
||||
polygon = PackedVector2Array(-27.769547, -29.426758, 31.88504, -29.184647, 12.700996, 28.7294, 56.058624, 148.93633, 22.979004, 163.77974, -19.854843, 161.65926, -53.782654, 143.84715, -8.333115, 30.157196)
|
||||
|
||||
[node name="AttackCooldownTimer" type="Timer" parent="." unique_id=435253442]
|
||||
wait_time = 0.5
|
||||
one_shot = true
|
||||
|
||||
[connection signal="timeout" from="StateMachine/Idle/Timer" to="StateMachine/Idle" method="_on_timer_timeout"]
|
||||
[connection signal="timeout" from="StateMachine/RandomMovement/Timer" to="StateMachine/RandomMovement" method="_on_timer_timeout"]
|
||||
[connection signal="body_entered" from="Sight" to="." method="_on_sight_body_entered"]
|
||||
[connection signal="timeout" from="AttackCooldownTimer" to="." method="_on_attack_cooldown_timer_timeout"]
|
||||
|
||||
26
evolve-die-repeat/molecular/predator/state_hunting.gd
Normal file
26
evolve-die-repeat/molecular/predator/state_hunting.gd
Normal file
@@ -0,0 +1,26 @@
|
||||
extends State
|
||||
|
||||
var target: Node2D
|
||||
|
||||
func enter(previous_state_path: String, data := {}) -> void:
|
||||
if data.has("target"):
|
||||
target = data["target"]
|
||||
owner.sprite.play("Hunting")
|
||||
else:
|
||||
# default behaviour; do nothing
|
||||
finished.emit(owner.fsm.States.IDLE, {})
|
||||
|
||||
func physics_update(_delta: float) -> void:
|
||||
if target == owner or target == null or owner.position.distance_to(target.position) > owner.sight_range:
|
||||
finished.emit(owner.fsm.States.IDLE, {})
|
||||
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
|
||||
if owner.position.distance_to(target.position) > owner.attack_range:
|
||||
owner.move(move_towards(target.position))
|
||||
else:
|
||||
owner.attack(target)
|
||||
|
||||
func move_towards(pos: Vector2) -> Vector3:
|
||||
var diff = target.position - owner.position
|
||||
return Vector3(diff.x, diff.y ,0)
|
||||
@@ -0,0 +1 @@
|
||||
uid://bc7apl71t0q04
|
||||
@@ -16,3 +16,13 @@ func transition_to_next_state(target: int, data: Dictionary = {}) -> void:
|
||||
States.FLEEING: _transition_to_next_state("Fleeing", data)
|
||||
States.HUNTING: _transition_to_next_state("Hunting", data)
|
||||
_: push_error("Trying to transition to unknown state {target}")
|
||||
|
||||
func map(state: Node) -> States:
|
||||
match state.name:
|
||||
"Idle": return States.IDLE
|
||||
"RandomMovement": return States.RANDOMMOVEMENT
|
||||
"Feeding": return States.FEEDING
|
||||
"Fleeing": return States.FLEEING
|
||||
"Hunting": return States.HUNTING
|
||||
_: push_error("Unknown state {state.name}")
|
||||
return map(self.initial_state)
|
||||
|
||||
Reference in New Issue
Block a user