Advertisement
NoneApplicableGames

Player Movement 3

Mar 15th, 2024
51
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. var initJumpForce : float
  11. @export var downForce : float
  12. @export var maxSpeed : float
  13. @export var acceleration : float
  14. @export var friction : float
  15. @export var weight : float
  16. @export var terminalFall : float
  17. @export var velocityWhileCrouching : float
  18.  
  19. @export_category("Hit Box")
  20. @export var normalHitBoxSize : int
  21. @export var crouchingHitBoxSize : int
  22.  
  23. @export_category("Sprite Properties")
  24. @export var offSetWhileCrouching : Vector2
  25.  
  26.  
  27. #instance of playerStates so we can emit the current state to the state machine
  28. var playerStates = preload("res://Scripts/Player/Player_State_Machine.gd").playerStates
  29. var currentPlayerState = playerStates.standing
  30. signal change_player_state(currentPlayerState : PlayerStateMachine.playerStates)
  31.  
  32.  
  33.  
  34. ##functions
  35.  
  36. #_physics_process(delta) runs at a fixed rate every cycle
  37. func _physics_process(delta):
  38.    
  39.    
  40.     #Calculate horrizontal vector. Stops the player holding left and right at the same time.
  41.     var direction : Vector2
  42.     direction.x = Input.get_axis("move_left" , "move_right")
  43.    
  44.     #normalise direction for joysticks
  45.     direction= direction.normalized()
  46.    
  47.     #set flip h based on axis.
  48.     %SpriteHandle.flip_h = direction.x < 0 if direction.x != 0 else %SpriteHandle.flip_h
  49.    
  50.     #are we moving?
  51.     if direction.x != 0:
  52.         velocity.x = clamp(velocity.x + direction.x * acceleration * delta, -maxSpeed, maxSpeed)
  53.        
  54.         if self.is_on_floor() == true:
  55.             currentPlayerState= playerStates.running
  56.    
  57.     #have we stopped?
  58.     elif direction.x == 0:    
  59.        
  60.         #gradual deceleration
  61.         velocity.x = direction.x * max(abs(velocity.x) - friction, 0)
  62.        
  63.         if self.is_on_floor() == true:
  64.             currentPlayerState = playerStates.standing
  65.    
  66.     ##Subroutine for basic jump.
  67.    
  68.     if self.is_on_floor() == true:
  69.         #disable gravity
  70.         velocity.y = 0
  71.         initJumpForce = constantJumpForce
  72.    
  73.    
  74.     if Input.is_action_just_pressed("jump"):
  75.         currentPlayerState = playerStates.jumping
  76.        
  77.         initJumpForce -= downForce
  78.         var jump : Vector2= initJumpForce * up_direction
  79.         velocity += jump
  80.         if velocity.y > 0:
  81.             velocity.y = -2
  82.        
  83.         #start timer to set state back to falling
  84.         %JumpTimer.start()
  85.    
  86.    
  87.     if !self.is_on_floor() && currentPlayerState != playerStates.jumping:
  88.         start_falling()
  89.    
  90.     #if player releases jump early they should be able to fall sooner also set
  91.     #state to falling if player isn't current toutching the ground
  92.     elif Input.is_action_just_released("jump") && !self.is_on_floor():
  93.         start_falling()
  94.    
  95.     if Input.is_action_pressed("crouch") && self.is_on_floor():
  96.         crouch(direction)
  97.    
  98.     #player remains in crouched state if sandwiched
  99.     #between ceiling and floor.
  100.    
  101.     elif %CeilingRay.is_colliding() && !Input.is_action_pressed("crouch"):
  102.         crouch(direction)
  103.         currentPlayerState = playerStates.crouching
  104.    
  105.     #update player state needs to be done every tick for physics purposes.
  106.     change_player_state.emit(currentPlayerState)
  107.     print(velocity.y)
  108.     move_and_slide()
  109.  
  110. ##Sets player to falling state when timer runs out.
  111. ##Stops them from being in the air too long.
  112. func start_falling():
  113.     currentPlayerState = playerStates.falling
  114.     #make falling velocity increase linearly over time
  115.     velocity += weight * Vector2.DOWN
  116.    
  117.     #cap fall speed to allow player to control their fall
  118.     if velocity.y >= terminalFall:
  119.         velocity.y = terminalFall
  120.  
  121. func crouch(direction : Vector2):
  122.     #are we moving?
  123.     if direction.x != 0:
  124.         currentPlayerState = playerStates.crouchwalking
  125.         #player should be punished for crouchwalking instead of sliding under
  126.         #to maintain momentum
  127.         velocity.x *= velocityWhileCrouching
  128.    
  129.     else:
  130.         currentPlayerState = playerStates.crouching
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement