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

69 lines
1.6 KiB
GDScript

extends CharacterBody2D
class_name Bunny
@export var min_distance_to_player := 80.0
@export var max_distance_to_player := 800.0
@export var agent : NavigationAgent2D
@export var speed := 200.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() * 3
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 * 10, randf() - 0.5 * 10)
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
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 : int):
health -= damage
if(health <= 0): on_death()
pass
func heal(health : int):
self.health += health
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