49 lines
1.5 KiB
GDScript
49 lines
1.5 KiB
GDScript
extends CharacterBody3D
|
|
class_name Enemy
|
|
|
|
const MOVEMENT_SPEED = 1
|
|
|
|
@onready var animationtree: AnimationTree = $enemies/AnimationPlayer/AnimationTree
|
|
@onready var agent:NavigationAgent3D = $NavigationAgent
|
|
@onready var enemyAi: EnemyAI = $BTPlayer
|
|
@onready var health: Health = $Health
|
|
|
|
const SPEED = 5.0
|
|
const JUMP_VELOCITY = 4.5
|
|
|
|
var movementBlend = 0;
|
|
|
|
func _ready() -> void:
|
|
health.death.connect(died)
|
|
|
|
func died() -> void:
|
|
queue_free()
|
|
|
|
func updateMovementBlend(delta: float, to: float) -> void:
|
|
movementBlend = move_toward(movementBlend, to, delta * 2);
|
|
|
|
func rotateTo(delta: float, targetDirection: Vector3) -> void:
|
|
self.global_basis.z = lerp(self.global_basis.z, targetDirection, delta * 10);
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if agent.is_navigation_finished():
|
|
if enemyAi.aggressive:
|
|
rotateTo(delta, (Player.Instance.global_position - self.global_position).normalized())
|
|
|
|
updateMovementBlend(delta, 0);
|
|
self.animationtree['parameters/Movement/blend_position'] = movementBlend
|
|
return
|
|
|
|
var current_agent_position: Vector3 = global_position
|
|
var next_path_position: Vector3 = agent.get_next_path_position()
|
|
|
|
velocity = current_agent_position.direction_to(next_path_position) * MOVEMENT_SPEED
|
|
move_and_slide()
|
|
|
|
if enemyAi.aggressive:
|
|
rotateTo(delta, (Player.Instance.global_position - self.global_position).normalized())
|
|
else:
|
|
rotateTo(delta, -velocity.normalized())
|
|
|
|
updateMovementBlend(delta, velocity.length())
|
|
self.animationtree['parameters/Movement/blend_position'] = movementBlend
|