Adds enemies
This commit is contained in:
parent
cf8ba8bacb
commit
76e5b1927f
324 changed files with 28447 additions and 106 deletions
49
scripts/enemies/enemy.gd
Normal file
49
scripts/enemies/enemy.gd
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
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
|
||||
49
scripts/enemies/enemy_ai.gd
Normal file
49
scripts/enemies/enemy_ai.gd
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
extends BTPlayer
|
||||
|
||||
class_name EnemyAI
|
||||
|
||||
const ROAM_DISTANCE: float = 10.0
|
||||
const DISTANCE_TO_PLAYER: float = 10.0
|
||||
|
||||
@export var character: CharacterBody3D
|
||||
|
||||
@export_category("Sprite")
|
||||
@export var sprite:Sprite3D;
|
||||
@export var idleTexture: Texture2D;
|
||||
@export var aggressiveTexture: Texture2D;
|
||||
|
||||
@export_category("Agents")
|
||||
@export var agent: NavigationAgent3D;
|
||||
|
||||
@export var animationTree: AnimationTree;
|
||||
|
||||
@export_category("Firing")
|
||||
@export var MuzzleFlash: PackedScene
|
||||
@export var Origin: Node3D
|
||||
|
||||
var aggressive = false
|
||||
|
||||
func _physics_process(_delta: float) -> void:
|
||||
self.blackboard.set_var('PlayerDistance', character.global_position.distance_to(Player.Instance.global_position))
|
||||
self.blackboard.set_var('LookingAngleToPlayer', (-character.global_basis.z).dot((character.global_position - Player.Instance.global_position).normalized()))
|
||||
|
||||
func setAggressive(value: bool) -> void:
|
||||
if not aggressive && value:
|
||||
agent.target_position = character.global_position;
|
||||
|
||||
sprite.texture = aggressiveTexture if value else idleTexture
|
||||
aggressive = value
|
||||
|
||||
|
||||
func roam() -> void:
|
||||
var targetPosition = character.global_position + (Vector3(randf_range(-1, 1), randf_range(-1, 1), randf_range(-1,1)).normalized() * randf_range(0, ROAM_DISTANCE))
|
||||
agent.target_position = targetPosition
|
||||
|
||||
func moveToPlayer() -> void:
|
||||
var toPlayerDirection = Player.Instance.global_position - character.global_position
|
||||
agent.target_position = Player.Instance.global_position + toPlayerDirection.normalized() * -DISTANCE_TO_PLAYER
|
||||
|
||||
func Fire() -> void:
|
||||
self.animationTree['parameters/playback'].travel('Fire')
|
||||
var instance = MuzzleFlash.instantiate(PackedScene.GEN_EDIT_STATE_INSTANCE)
|
||||
Origin.add_child(instance)
|
||||
Loading…
Add table
Add a link
Reference in a new issue