Advertisement
NoneApplicableGames

Player Movement

Mar 12th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class_name PlayerMovement extends CharacterBody2D
  2.  
  3. ###Class for everything pretaining to player physics and input.
  4.  
  5. ##export varibles
  6. #Anything realted to physics that we cannot know an extact value for and needs
  7. #to be tweaked through playtesting.
  8. @export_category("Physics Properties")
  9. @export var constantJumpForce : float
  10. @export var downForce : float
  11. @export var maxSpeed : float
  12. @export var acceleration : float
  13. @export var friction : float
  14. @export var weight : float
  15. @export var terminalFall : float
  16. var initJumpForce : float = constantJumpForce
  17.  
  18. @export_category("Hit Box")
  19. @export var normalHitBoxSize : int
  20. @export var crouchingHitBoxSize : int
  21.  
  22. @export_category("Sprite Properties")
  23. @export var offSetWhileCrouching : Vector2
  24.  
  25.  
  26. #instance of playerStates so we can emit the current state to the state machine
  27. var playerStates = preload("res://Scripts/Player/Player_State_Machine.gd").playerStates
  28. var currentPlayerState = playerStates.standing
  29. signal change_player_state(currentPlayerState : PlayerStateMachine.playerStates)
  30.  
  31.  
  32. ##functions
  33.  
  34. #_physics_process(delta) runs at a fixed rate every cycle
  35. func _physics_process(delta):
  36.    
  37.    
  38.     #Calculate horrizontal vector. Stops the player holding left and right at the same time.
  39.     var direction : Vector2
  40.     direction.x = Input.get_axis("move_left" , "move_right")
  41.    
  42.     #normalise direction for joysticks
  43.     direction= direction.normalized()
  44.    
  45.     #set flip h based on axis.
  46.     %SpriteHandle.flip_h = direction.x < 0 if direction.x != 0 else %SpriteHandle.flip_h
  47.    
  48.     #are we moving?
  49.     if direction.x != 0:
  50.         velocity.x = clamp(velocity.x + direction.x * acceleration * delta, -maxSpeed, maxSpeed)
  51.        
  52.         #are we also crouching?
  53.         if Input.is_action_pressed("crouch") && self.is_on_floor():
  54.             currentPlayerState = playerStates.crouchwalking
  55.        
  56.         else:
  57.             currentPlayerState = playerStates.running
  58.    
  59.     #have we stopped?
  60.     elif direction.x == 0:    
  61.         #gradual deceleration
  62.         velocity.x = direction.x * max(abs(velocity.x) - friction, 0)
  63.        
  64.         #are we also crouching?
  65.         if Input.is_action_pressed("crouch") && self.is_on_floor():
  66.             currentPlayerState = playerStates.crouching
  67.        
  68.         else:
  69.             currentPlayerState = playerStates.standing
  70.    
  71.     ##Subroutine for basic jump.
  72.    
  73.     if self.is_on_floor() == true:
  74.         #disable gravity
  75.         velocity.y = 0
  76.         currentPlayerState= playerStates.standing
  77.        
  78.         if Input.is_action_just_pressed("jump"):
  79.             currentPlayerState = playerStates.jumping
  80.            
  81.             #reduce jump force by down force every
  82.             #physics process we are in the air for
  83.             initJumpForce -= downForce
  84.             var jump : Vector2= initJumpForce * up_direction
  85.             velocity += jump
  86.            
  87.             #i forgot what this is there for but Im scared to toutch it
  88.             if velocity.y > 0:
  89.                 velocity.y = -2
  90.            
  91.             #start timer to set state back to falling
  92.             %JumpTimer.start()
  93.    
  94.    
  95.     if !self.is_on_floor() && currentPlayerState != playerStates.jumping:
  96.         start_falling()
  97.    
  98.     #if player releases jump early they should be able to fall sooner also set
  99.     #state to falling if player isn't current toutching the ground
  100.     if Input.is_action_just_released("jump") && !self.is_on_floor():
  101.         start_falling()
  102.    
  103.     #player remains in crouched state if sandwiched
  104.     #between ceiling and floor. Ray casting has to be used
  105.     #over is_on_ceiling and floor because its impossible for the
  106.     #hitbox to be the right size to both fit through these gaps and
  107.     #toutch the edges of them.
  108.     elif %CeilingRay.is_colliding() && !Input.is_action_pressed("crouch"):
  109.         currentPlayerState = playerStates.crouching
  110.    
  111.     #update player state needs to be done every tick for physics purposes.
  112.     change_player_state.emit(currentPlayerState)
  113.     move_and_slide()
  114.  
  115. ##Sets player to falling state when timer runs out.
  116. ##Stops them from being in the air too long.
  117. func start_falling():
  118.     currentPlayerState = playerStates.falling
  119.     #make falling velocity increase linearly over time
  120.     velocity += weight * Vector2.DOWN
  121.    
  122.     #cap fall speed to allow player to control their fall
  123.     if velocity.y >= terminalFall:
  124.         velocity.y = terminalFall
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement