extends CharacterBody3D class_name Player enum PlayerStates { STANDING, CROUCHING, SPRINTING } static var Instance: Player; @onready var animationtree: AnimationTree = $CollisionShape3D/Camera3D/Weapon/AnimationPlayer/AnimationTree @onready var collisionShape: CollisionShape3D = $CollisionShape3D @onready var health: Health = $Health @onready var enemyTarget: Node3D = $"Enemy Target" @onready var deathHandler: DeathHandler = $DeathHandler @onready var cameraController: CameraController = $CollisionShape3D/Camera3D @onready var ui: UI = $Ui @onready var weapon: Node3D = $CollisionShape3D/Camera3D/Weapon @onready var walkingSound = $Walking @onready var sprintingSound = $Sprinting @onready var startupSound = $Startup @onready var hitSound = $HitSound const CROUCHED_SPEED: float = 2.5 const SPEED: float = 5.0 const SPRINT_SPEED: float = 10.0 const FALLOFF_SPEED: float = 0.5 const JUMP_VELOCITY: float = 4.5 var lastDirection = Vector2(0,0) @export var PlayerState: PlayerStates = PlayerStates.STANDING var controls = true # Get the gravity from the project settings to be synced with RigidBody nodes. var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity") func _ready() -> void: weapon.process_mode = Node.PROCESS_MODE_ALWAYS get_tree().paused = true ui.setFade(0) get_tree().create_timer(1).timeout.connect(readying) weapon.process_mode = Node.PROCESS_MODE_PAUSABLE Instance = self health.damaged.connect(self.damaged) health.death.connect(self.onDeath) func readying(): get_tree().paused = false; func _physics_process(delta: float) -> void: # Add the gravity. if not is_on_floor(): velocity.y -= gravity * delta # Handle Jump. if Input.is_action_just_pressed("player_jump") and is_on_floor(): velocity.y = JUMP_VELOCITY # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. var input_dir := Input.get_vector("player_right", "player_left", "player_down", "player_up") var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() var falloffSpeed = FALLOFF_SPEED if not is_on_floor(): falloffSpeed = 0.02; var speed = SPEED; self.handleCrouching() if PlayerState == PlayerStates.STANDING && Input.is_action_pressed('player_sprint'): self.PlayerState = PlayerStates.SPRINTING walkingSound.process_mode = Node.PROCESS_MODE_DISABLED sprintingSound.process_mode = Node.PROCESS_MODE_INHERIT if PlayerState == PlayerStates.SPRINTING && Input.is_action_just_released('player_sprint'): self.PlayerState = PlayerStates.STANDING walkingSound.process_mode = Node.PROCESS_MODE_INHERIT sprintingSound.process_mode = Node.PROCESS_MODE_DISABLED if PlayerState == PlayerStates.SPRINTING: speed = SPRINT_SPEED if PlayerState == PlayerStates.CROUCHING: speed = CROUCHED_SPEED if direction: velocity.x = direction.x * speed velocity.z = direction.z * speed else: velocity.x = move_toward(velocity.x, 0, falloffSpeed) velocity.z = move_toward(velocity.z, 0, falloffSpeed) move_and_slide() if self.PlayerState == PlayerStates.SPRINTING: input_dir.y *= 2 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); var yLeft = clamp(lastDirection.y - 1, 0, 1); animationtree.set('parameters/Movement/blend_position', yLeft) var yRemainder = Vector2(lastDirection.x, clamp(lastDirection.y, -1, 1)); animationtree.set(blendProperty, yRemainder); func handleCrouching(): if Input.is_action_just_pressed('player_crouching'): self.PlayerState = PlayerStates.CROUCHING (collisionShape.shape as CylinderShape3D).height = 1 self.animationtree['parameters/playback'].travel('Crouched Movement') if Input.is_action_just_released('player_crouching'): self.PlayerState = PlayerStates.STANDING (collisionShape.shape as CylinderShape3D).height = 2 self.animationtree['parameters/playback'].travel('Movement') func damaged(): print("Damage") func onDeath(from): hitSound.play() deathHandler.died(from) cameraController.disableInput() ui.displayYouDied() get_tree().paused = true await get_tree().create_timer(2.5).timeout ui.setFade(1) await get_tree().create_timer(1.0).timeout get_tree().reload_current_scene()