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

72 lines
1.6 KiB
GDScript3
Raw Normal View History

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-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
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