This repository has been archived on 2024-07-02. You can view files and clone it, but cannot push or open issues or pull requests.
HoppyEaster/Scripts/EntitySystem/Bunny.gd

85 lines
2.4 KiB
GDScript3
Raw Normal View History

extends CharacterBody2D
class_name Bunny
@export var animation_player : AnimationPlayer
2023-04-09 14:39:45 +00:00
@export var min_distance_to_player := 80.0
@export var max_distance_to_player := 800.0
@export var agent : NavigationAgent2D
2023-04-09 21:22:44 +00:00
@export var speed := 100.0
2023-04-09 14:39:45 +00:00
var health : int
var team : int
2023-04-09 14:39:45 +00:00
var player : Node2D
var on_death_callbacks : Array
2023-04-09 14:39:45 +00:00
var dir : Vector2
var spread_time : float
var current_time : float
var collisions : int
func _ready():
seed(int(str(self)))
2023-04-09 21:22:44 +00:00
spread_time = randf() * 2 + 0.1
2023-04-09 14:39:45 +00:00
current_time = spread_time
func _physics_process(delta):
current_time -= delta
if player != null and self.global_position.distance_to(player.global_position) < max_distance_to_player and collisions <= 3:
if current_time <= 0:
current_time = spread_time
agent.target_position = player.global_position
if min_distance_to_player and agent.is_target_reachable():
2023-04-09 21:22:44 +00:00
var next_location = agent.get_next_path_position() + Vector2(randf() - 0.5 * 2, randf() - 0.5 * 2)
2023-04-09 14:39:45 +00:00
dir = self.global_position.direction_to(next_location).normalized()
if self.global_position.distance_to(player.global_position) > min_distance_to_player:
self.velocity = dir * delta * 60 * speed
update_animation()
2023-04-09 21:22:44 +00:00
if delta > 0.5:
print(delta)
2023-04-09 14:39:45 +00:00
move_and_slide()
pass
func _on_collision(body):
if body != self: collisions += 1
pass
func _of_collision(body):
if body != self: collisions-= 1
pass
func damage(damage_amount : int):
health -= damage_amount
if(health <= 0): on_death()
pass
func heal(health_amount : int):
self.health += health_amount
pass
func on_death():
for callback in on_death_callbacks:
callback.call(self)
pass
func sub_on_death(callback : Callable):
on_death_callbacks.push_front(callback)
pass
2023-04-09 14:39:45 +00:00
func update_animation():
var move_vector = Vector2(1 if velocity.x > 0 else (-1 if velocity.x < 0 else 0), 1 if velocity.y > 0 else (-1 if velocity.y < 0 else 0))
match move_vector:
Vector2.ZERO: animation_player.play("Idle")
Vector2.LEFT: animation_player.play("MoveLeft")
Vector2.RIGHT: animation_player.play("MoveRight")
Vector2.UP: animation_player.play("MoveUp")
Vector2.DOWN: animation_player.play("MoveDown")
_: handle_diagonal_animations(move_vector)
pass
2023-04-09 14:39:45 +00:00
func handle_diagonal_animations(dir : Vector2):
if (dir.y > 0 and dir.x < 0) or (dir.y < 0 and dir.x < 0): animation_player.play("MoveLeft")
elif (dir.y > 0 and dir.x > 0) or (dir.y < 0 and dir.x > 0): animation_player.play("MoveRight")
else: animation_player.play("Idle")
pass