Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class_name PlayerMovement extends CharacterBody2D
- ###Class for everything pretaining to player physics and input.
- ##export varibles
- #Anything realted to physics that we cannot know an extact value for and needs
- #to be tweaked through playtesting.
- @export_category("Jumping Properties")
- @export var constantJumpForce : float= 350
- var initJumpForce : float
- @export_category("Running Properties")
- @export var maxSpeed : float = 360
- @export var acceleration : float = 190
- @export var friction : float = 4
- @export var runningThreshold : float
- @export_category("Airdash Properties")
- @export var dashForce : float
- @export_category("Falling Properties")
- @export var coyoteFrames : float = 6.0
- @export var downForce : float = 0.375
- @export var weight : float = 60
- @export var terminalFall : float = 700
- @export var slowFallMultiplier : float = 0.45
- @export var velocityWhileCrouching : float = 0.9
- #Coyote Time
- var coyote := false
- var lastFloor := false
- #instance of playerStates so we can emit the current state to the state machine
- var playerStates = preload("res://Scripts/Player/Player_State_Machine.gd").playerStates
- var currentPlayerState = playerStates.standing
- signal change_player_state(currentPlayerState : PlayerStateMachine.playerStates)
- func _ready():
- %CoyoteTimer.wait_time = coyoteFrames / 60.0
- #_physics_process(delta) runs at a fixed rate every cycle
- func _physics_process(delta):
- #reset everything if on floor
- if self.is_on_floor_only() == true:
- currentPlayerState = playerStates.landing
- #disable gravity
- velocity.y = 0
- #reset inital jump force
- initJumpForce = constantJumpForce
- lastFloor = is_on_floor()
- #Calculate horrizontal vector. Stops the player holding left and right at the same time.
- var direction : Vector2
- direction.x = Input.get_axis("move_left" , "move_right")
- #direction.y is only used for calcuating player's air dash
- direction.y = Input.get_axis("move_up", "move_down")
- #normalise direction for joysticks
- direction= direction.normalized()
- #set flip h based on axis.
- %SpriteHandle.flip_h = direction.x < 0 if direction.x != 0 else %SpriteHandle.flip_h
- #are we moving?
- if direction.x != 0:
- velocity.x = clamp(velocity.x + direction.x * acceleration * delta * 5, -maxSpeed, maxSpeed)
- if self.is_on_floor():
- currentPlayerState = playerStates.running
- #have we stopped?
- elif direction.x == 0:
- #gradual deceleration
- velocity.x = direction.x * max(abs(velocity.x) - friction, 0)
- if self.is_on_floor() == true:
- currentPlayerState = playerStates.standing
- ##Subroutine for basic jump.
- if Input.is_action_just_pressed("jump") && (is_on_floor() or coyote):
- #start counting the accumulated delta
- currentPlayerState = playerStates.jumping
- initJumpForce -= downForce
- var jump : Vector2= initJumpForce * up_direction
- velocity += jump
- if velocity.y > 0:
- velocity.y = -2
- #start timer to set state back to falling
- $JumpTimer.start()
- if !self.is_on_floor() && currentPlayerState != playerStates.jumping && lastFloor:
- coyote= true
- $CoyoteTimer.start()
- #if player releases jump early they should be able to fall sooner also set
- #state to falling if player isn't current toutching the ground
- elif Input.is_action_just_released("jump") && !self.is_on_floor():
- start_falling()
- elif !self.is_on_floor() && currentPlayerState != playerStates.jumping:
- start_falling()
- if Input.is_action_pressed("crouch") && self.is_on_floor():
- crouch(direction)
- #player remains in crouched state if sandwiched
- #between ceiling and floor.
- elif %CeilingRay.is_colliding() && !Input.is_action_pressed("crouch"):
- crouch(direction)
- currentPlayerState = playerStates.crouching
- if Input.is_action_just_pressed("dash"):
- dash(direction)
- #update player state needs to be done every tick for physics purposes.
- change_player_state.emit(currentPlayerState)
- move_and_slide()
- ##Sets player to falling state when timer runs out.
- ##Stops them from being in the air too long.
- func start_falling():
- currentPlayerState = playerStates.falling
- #make player fall more slowly when holding space
- if Input.is_action_pressed("jump") && !is_on_floor():
- var fallSpeed := Vector2.DOWN * weight * slowFallMultiplier
- velocity += fallSpeed
- elif !Input.is_action_pressed("jump") && !is_on_floor():
- velocity += weight * Vector2.DOWN
- #cap fall speed to allow player to control their fall
- if velocity.y >= terminalFall:
- velocity.y = terminalFall
- func crouch(direction : Vector2):
- #are we moving?
- if direction.x != 0:
- currentPlayerState = playerStates.crouchwalking
- #player should be punished for crouchwalking instead of sliding under
- #to maintain momentum
- velocity.x *= velocityWhileCrouching
- else:
- currentPlayerState = playerStates.crouching
- func dash(direction : Vector2):
- currentPlayerState = playerStates.dashing
- var dashVector : Vector2 = direction * dashForce
- velocity += dashVector
- func _on_coyote_timer_timeout():
- coyote = false
- start_falling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement