Adds enemies

This commit is contained in:
Michel 2025-02-01 22:51:01 +01:00
parent cf8ba8bacb
commit 76e5b1927f
324 changed files with 28447 additions and 106 deletions

49
scripts/enemies/enemy.gd Normal file
View 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

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

12
scripts/muzzleflash.gd Normal file
View file

@ -0,0 +1,12 @@
extends Node3D
@onready var timer: Timer = $Timer
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
rotate_z(randf())
timer.timeout.connect(self.completed)
func completed() -> void:
queue_free()

View file

@ -11,6 +11,9 @@ func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED;
func _input(event: InputEvent) -> void:
if event is InputEventKey && event.key_label == KEY_ESCAPE:
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
if event is not InputEventMouseMotion:
return

View file

@ -12,7 +12,9 @@ static var Instance: Player;
@onready var animationtree: AnimationTree = $CollisionShape3D/Camera3D/Weapon/AnimationPlayer/AnimationTree
@onready var collisionShape: CollisionShape3D = $CollisionShape3D
@onready var health: Health = $Health
const CROUCHED_SPEED: float = 2.5
const SPEED: float = 5.0
const SPRINT_SPEED: float = 10.0
const FALLOFF_SPEED: float = 0.5
@ -54,6 +56,8 @@ func _physics_process(delta: float) -> void:
if PlayerState == PlayerStates.SPRINTING:
speed = SPRINT_SPEED
if PlayerState == PlayerStates.CROUCHING:
speed = CROUCHED_SPEED
if direction:
velocity.x = direction.x * speed
@ -67,13 +71,16 @@ func _physics_process(delta: float) -> void:
if self.PlayerState == PlayerStates.SPRINTING:
input_dir.y *= 2
var blendProperty = 'parameters/Movement/blend_position'
var blendProperty = 'parameters/Movement/0/blend_position'
if PlayerState == PlayerStates.CROUCHING:
blendProperty = 'parameters/Crouched Movement/blend_position'
lastDirection.x = move_toward(lastDirection.x, input_dir.x, 0.05);
lastDirection.y = move_toward(lastDirection.y, input_dir.y, 0.05);
animationtree.set(blendProperty, lastDirection);
var yLeft = clamp(lastDirection.y - 1, 0, 1);
animationtree.set('parameters/Movement/blend_position', yLeft)
var yRemainder = Vector2(lastDirection.x, clamp(lastDirection.y, 0, 1));
animationtree.set(blendProperty, yRemainder);
func handleCrouching():
if Input.is_action_just_pressed('player_crouching'):

View file

@ -2,13 +2,18 @@ extends Node3D
@onready var animationtree: AnimationTree = $AnimationPlayer/AnimationTree
@onready var timer: Timer = $CooldownTimer
@export_category("Firing")
@export var MuzzleFlash: PackedScene
@export var Origin: Node3D
@export var BulletOrigin: Node3D
var statemachine: AnimationNodeStateMachinePlayback
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
self.statemachine = animationtree['parameters/playback']
func _input(event: InputEvent) -> void:
if event is not InputEventMouseButton:
return
@ -19,6 +24,9 @@ func _input(event: InputEvent) -> void:
if self.timer.time_left > 0:
return
if Player.Instance.PlayerState == Player.PlayerStates.SPRINTING:
return
var travelTarget = 'Fire'
if Player.Instance.PlayerState == Player.PlayerStates.CROUCHING:
travelTarget = 'Crouched Fire'
@ -26,4 +34,24 @@ func _input(event: InputEvent) -> void:
self.animationtree.set('parameters/' + travelTarget + '/blend_position', randf_range(0, 2))
self.statemachine.travel(travelTarget)
fireBullet()
self.timer.start()
func fireBullet() -> void:
var instance = self.MuzzleFlash.instantiate(PackedScene.GEN_EDIT_STATE_INSTANCE)
Origin.add_child(instance)
var query = PhysicsRayQueryParameters3D.create(BulletOrigin.global_position, BulletOrigin.global_position + BulletOrigin.global_basis.z * -200)
var result = get_world_3d().direct_space_state.intersect_ray(query)
if not result:
print('No result')
return
var health = result.collider.get_node('Health')
if health:
print("Dealing Damage")
result.collider.health.take_damage(1, Vector2(0,0))
return
print('No Health Element')