Files
CybelovZOVod/addons/PlayerCharacter/Scripts/StateMachine/PlayerCharacterScript.gd
T
2026-07-20 22:48:58 +07:00

137 lines
4.3 KiB
GDScript

extends CharacterBody3D
class_name PlayerCharacter
@export_group("Movement variables")
var moveSpeed : float
var moveAccel : float
var moveDeccel : float
var desiredMoveSpeed : float
@export var desiredMoveSpeedCurve : Curve
@export var maxSpeed : float
@export var inAirMoveSpeedCurve : Curve
var inputDirection : Vector2
var moveDirection : Vector3
@export var hitGroundCooldown : float #amount of time the character keep his accumulated speed before losing it (while being on ground)
var hitGroundCooldownRef : float
@export var bunnyHopDmsIncre : float #bunny hopping desired move speed incrementer
@export var autoBunnyHop : bool = false
var lastFramePosition : Vector3
var lastFrameVelocity : Vector3
var wasOnFloor : bool
var walkOrRun : String = "WalkState" #keep in memory if play char was walking or running before being in the air
#for crouch visible changes
@export var baseHitboxHeight : float
@export var baseModelHeight : float
@export var heightChangeSpeed : float
@export_group("Crouch variables")
@export var crouchSpeed : float
@export var crouchAccel : float
@export var crouchDeccel : float
@export var continiousCrouch : bool = false #if true, doesn't need to keep crouch button on to crouch
@export var crouchHitboxHeight : float
@export var crouchModelHeight : float
@export_group("Walk variables")
@export var walkSpeed : float
@export var walkAccel : float
@export var walkDeccel : float
@export_group("Run variables")
@export var runSpeed : float
@export var runAccel : float
@export var runDeccel : float
@export var continiousRun : bool = false #if true, doesn't need to keep run button on to run
@export_group("Jump variables")
@export var jumpHeight : float
@export var jumpTimeToPeak : float
@export var jumpTimeToFall : float
@onready var jumpVelocity : float = (2.0 * jumpHeight) / jumpTimeToPeak
@export var jumpCooldown : float
var jumpCooldownRef : float
@export var nbJumpsInAirAllowed : int
var nbJumpsInAirAllowedRef : int
var jumpBuffOn : bool = false
var bufferedJump : bool = false
@export var coyoteJumpCooldown : float
var coyoteJumpCooldownRef : float
var coyoteJumpOn : bool = false
@export_range(0.1, 1.0, 0.05) var inAirInputMultiplier: float = 1.0
@export_group("Gravity variables")
@onready var jumpGravity : float = (-2.0 * jumpHeight) / (jumpTimeToPeak * jumpTimeToPeak)
@onready var fallGravity : float = (-2.0 * jumpHeight) / (jumpTimeToFall * jumpTimeToFall)
@export_group("Keybind variables")
@export var moveForwardAction : String = ""
@export var moveBackwardAction : String = ""
@export var moveLeftAction : String = ""
@export var moveRightAction : String = ""
@export var runAction : String = ""
@export var crouchAction : String = ""
@export var jumpAction : String = ""
#references variables
@onready var camHolder : Node3D = $CameraHolder
@onready var model : MeshInstance3D = $Model
@onready var hitbox : CollisionShape3D = $Hitbox
@onready var stateMachine : Node = %StateMachine
@onready var hud : CanvasLayer = $HUD
@onready var ceilingCheck : RayCast3D = $Raycasts/CeilingCheck
@onready var floorCheck : RayCast3D = $Raycasts/FloorCheck
func _ready():
#set move variables, and value references
moveSpeed = walkSpeed
moveAccel = walkAccel
moveDeccel = walkDeccel
hitGroundCooldownRef = hitGroundCooldown
jumpCooldownRef = jumpCooldown
nbJumpsInAirAllowedRef = nbJumpsInAirAllowed
coyoteJumpCooldownRef = coyoteJumpCooldown
func _process(_delta: float):
displayProperties()
func _physics_process(_delta : float):
modifyPhysicsProperties()
move_and_slide()
func displayProperties():
#display properties on the hud
if hud != null:
hud.displayCurrentState(stateMachine.currStateName)
hud.displayCurrentDirection(moveDirection)
hud.displayDesiredMoveSpeed(desiredMoveSpeed)
hud.displayVelocity(velocity.length())
hud.displayNbJumpsInAirAllowed(nbJumpsInAirAllowed)
func modifyPhysicsProperties():
lastFramePosition = position #get play char position every frame
lastFrameVelocity = velocity #get play char velocity every frame
wasOnFloor = !is_on_floor() #check if play char was on floor every frame
func gravityApply(delta : float):
#if play char goes up, apply jump gravity
#otherwise, apply fall gravity
if !is_on_floor():
if velocity.y >= 0.0: velocity.y += jumpGravity * delta
elif velocity.y < 0.0: velocity.y += fallGravity * delta