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
2023-04-10 23:50:19 +02:00

86 lines
2.3 KiB
GDScript

extends CharacterBody2D
class_name Bunny
@export var animation_player : AnimationPlayer
@export var min_distance_to_player := 80.0
@export var max_distance_to_player := 800.0
@export var agent : NavigationAgent2D
@export var speed := 100.0
var health : int
var team : int
var player : Node2D
var on_death_callbacks : Array
var dir : Vector2
var spread_time : float
var current_time : float
var collisions : int
func _ready():
seed(int(str(self)))
spread_time = randf() * 2 + 0.1
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():
var next_location = agent.get_next_path_position() + Vector2(randf() - 0.5 * 2, randf() - 0.5 * 2)
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()
if delta > 0.5:
print(delta)
move_and_slide()
else:
animation_player.stop()
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
func update_animation():
match self.velocity:
Vector2.ZERO: animation_player.play("Idle")
_:
var left_dot = Vector2.LEFT.dot(self.velocity)
var right_dot = Vector2.RIGHT.dot(self.velocity)
var up_dot = Vector2.UP.dot(self.velocity)
var down_dot = Vector2.DOWN.dot(self.velocity)
var max_dot = maxf(left_dot, maxf(right_dot, maxf(up_dot, down_dot)))
match max_dot:
left_dot: animation_player.play("MoveLeft")
right_dot: animation_player.play("MoveRight")
up_dot: animation_player.play("MoveUp")
down_dot: animation_player.play("MoveDown")
pass