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

123 lines
3.8 KiB
GDScript3
Raw Permalink Normal View History

2023-04-16 12:36:10 +00:00
extends CharacterBody2D
class_name Bunny
@export var animation_player : AnimationPlayer
@export var min_distance_to_player := 10.0
2023-04-12 19:16:14 +00:00
@export var max_distance_to_player := 500.0
2023-04-09 14:39:45 +00:00
@export var agent : NavigationAgent2D
2023-04-12 19:16:14 +00:00
@export var speed := 3.0
2023-04-09 14:39:45 +00:00
var health : int
2023-04-23 12:50:34 +00:00
var damage : int
var attack_speed : float
var time_since_last_attack : float
var team : int
2023-04-23 13:21:22 +00:00
var target : Node2D
var on_death_callbacks : Array
2023-04-12 19:16:14 +00:00
var move_velocity : Vector2
2023-04-12 19:16:14 +00:00
var next_location : Vector2
var randomized_next_location : Vector2
2023-04-09 14:39:45 +00:00
var dir : Vector2
2023-04-12 19:16:14 +00:00
var time_since_got_player := 100.0
var time_since_got_move_location := 100.0
var last_player_velocity := 0.0
var last_target_position : Vector2
var max_pos_check_time := 0.5
var max_pos_check_time_inactive_scalar := 10.0
var max_get_move_location_time := 0.125
var random_goal_spread_factor := 2.5
var random_move_location_spread_factor := 8.0
var random_angle_change := deg_to_rad(4.0)
2023-04-09 14:39:45 +00:00
func _ready():
2023-04-12 19:16:14 +00:00
pass
2023-04-09 14:39:45 +00:00
func _physics_process(delta : float):
2023-04-23 13:21:22 +00:00
if target == null: return
2023-04-12 19:16:14 +00:00
randomize()
2023-04-23 13:21:22 +00:00
if abs(target.velocity.length() - last_player_velocity) > 100.0:
2023-04-12 19:16:14 +00:00
time_since_got_player = max_pos_check_time
2023-04-23 13:21:22 +00:00
if time_since_got_player >= max_pos_check_time and target.velocity.length() > 0.1:
2023-04-12 19:16:14 +00:00
time_since_got_player = randf() * max_pos_check_time * 0.5
update_target_pos()
2023-04-23 13:21:22 +00:00
if time_since_got_player >= max_pos_check_time * max_pos_check_time_inactive_scalar and target.velocity.length() < 0.1:
2023-04-12 19:16:14 +00:00
time_since_got_player = -(randf() * max_pos_check_time * 0.5)
update_target_pos()
2023-04-23 13:21:22 +00:00
if self.global_position.distance_to(target.global_position) < max_distance_to_player and time_since_got_move_location > max_get_move_location_time:
2023-04-12 19:16:14 +00:00
time_since_got_move_location = 0
if agent.is_target_reachable():
next_location = agent.get_next_path_position()
randomized_next_location = next_location + Vector2(randf() - 0.5, randf() - 0.5 ) * 2 * random_move_location_spread_factor
dir = self.global_position.direction_to(randomized_next_location).normalized().rotated((randf() - 0.5) * 2 * random_angle_change)
2023-04-23 13:21:22 +00:00
if self.global_position.distance_to(target.global_position) >= min_distance_to_player:
2023-04-12 19:16:14 +00:00
move_velocity = dir * delta * 60 * speed
update_animation()
if self.global_position.distance_to(next_location) < move_velocity.length():
self.global_position = next_location
time_since_got_player = 10.0
time_since_got_move_location = 10.0
else:
2023-04-16 12:36:10 +00:00
move_and_collide(move_velocity)
2023-04-12 19:16:14 +00:00
else:
animation_player.stop()
update_attack(delta)
2023-04-12 19:16:14 +00:00
time_since_got_player += delta
time_since_got_move_location += delta
time_since_last_attack += delta
2023-04-23 13:21:22 +00:00
last_player_velocity = target.velocity.length()
2023-04-12 19:16:14 +00:00
last_target_position = agent.target_position
2023-04-09 14:39:45 +00:00
pass
func update_attack(delta : float):
if time_since_last_attack >= attack_speed:
time_since_last_attack = 0
2023-04-23 13:21:22 +00:00
target.damage(damage)
pass
2023-04-12 19:16:14 +00:00
func update_target_pos():
2023-04-23 13:21:22 +00:00
agent.target_position = Vector2(target.global_position) + Vector2(randf() - 0.5 , randf() - 0.5) * 2 * random_goal_spread_factor
2023-04-09 14:39:45 +00:00
pass
2023-04-23 12:50:34 +00:00
func take_damage(damage_amount : int):
health -= damage_amount
if(health <= 0): on_death()
pass
2023-04-23 12:50:34 +00:00
func get_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():
2023-04-12 19:16:14 +00:00
match self.move_velocity:
Vector2.ZERO: animation_player.play("Idle")
2023-04-10 21:50:19 +00:00
_:
2023-04-12 19:16:14 +00:00
var left_dot = Vector2.LEFT.dot(self.move_velocity)
var right_dot = Vector2.RIGHT.dot(self.move_velocity)
var up_dot = Vector2.UP.dot(self.move_velocity)
var down_dot = Vector2.DOWN.dot(self.move_velocity)
2023-04-10 21:50:19 +00:00
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