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
|
||||
|
||||
Reference in New Issue
Block a user