98 lines
3.2 KiB
GDScript
98 lines
3.2 KiB
GDScript
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"
|
|
|
|
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
|
|
|
|
# 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:
|
|
Instance = self
|
|
health.damaged.connect(self.damaged)
|
|
|
|
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
|
|
if PlayerState == PlayerStates.SPRINTING && Input.is_action_just_released('player_sprint'):
|
|
self.PlayerState = PlayerStates.STANDING
|
|
|
|
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")
|