2023-04-08 15:00:54 +00:00
|
|
|
extends CharacterBody2D
|
|
|
|
class_name Bunny
|
|
|
|
|
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-08 15:00:54 +00:00
|
|
|
|
2023-04-09 14:39:45 +00:00
|
|
|
var health : int
|
2023-04-08 15:00:54 +00:00
|
|
|
var team : int
|
2023-04-09 14:39:45 +00:00
|
|
|
var player : Node2D
|
2023-04-08 15:00:54 +00:00
|
|
|
|
|
|
|
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
|
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
|
|
|
|
|
2023-04-09 15:57:37 +00:00
|
|
|
func damage(damage_amount : int):
|
|
|
|
health -= damage_amount
|
2023-04-08 15:00:54 +00:00
|
|
|
if(health <= 0): on_death()
|
|
|
|
pass
|
|
|
|
|
2023-04-09 15:57:37 +00:00
|
|
|
func heal(health_amount : int):
|
|
|
|
self.health += health_amount
|
2023-04-08 15:00:54 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|