Advertisement
NoneApplicableGames

Untitled

Jun 23rd, 2024
140
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("Jumping Properties")
  9. @export var constantJumpForce : float= 350
  10. var initJumpForce : float
  11.  
  12. @export_category("Running Properties")
  13. @export var maxSpeed : float = 360
  14. @export var acceleration : float = 190
  15. @export var friction : float = 4
  16. @export var runningThreshold : float
  17.  
  18. @export_category("Airdash Properties")
  19. @export var dashForce : float
  20.  
  21. @export_category("Falling Properties")
  22. @export var coyoteFrames : float = 6.0
  23. @export var downForce : float = 0.375
  24. @export var weight : float = 60
  25. @export var terminalFall : float = 700
  26. @export var slowFallMultiplier : float = 0.45
  27. @export var velocityWhileCrouching : float = 0.9
  28.  
  29. #Coyote Time
  30. var coyote := false
  31. var lastFloor := false
  32.  
  33.  
  34. #instance of playerStates so we can emit the current state to the state machine
  35. var playerStates = preload("res://Scripts/Player/Player_State_Machine.gd").playerStates
  36. var currentPlayerState = playerStates.standing
  37. signal change_player_state(currentPlayerState : PlayerStateMachine.playerStates)
  38.  
  39. func  _ready():
  40.     %CoyoteTimer.wait_time = coyoteFrames / 60.0
  41.  
  42. #_physics_process(delta) runs at a fixed rate every cycle
  43. func _physics_process(delta):
  44.  
  45.     #reset everything if on floor
  46.     if self.is_on_floor_only() == true:
  47.         currentPlayerState = playerStates.landing
  48.         #disable gravity
  49.         velocity.y = 0
  50.         #reset inital jump force
  51.         initJumpForce = constantJumpForce
  52.        
  53.    
  54.     lastFloor = is_on_floor()
  55.    
  56.     #Calculate horrizontal vector. Stops the player holding left and right at the same time.
  57.     var direction : Vector2
  58.     direction.x = Input.get_axis("move_left" , "move_right")
  59.    
  60.     #direction.y is only used for calcuating player's air dash
  61.     direction.y = Input.get_axis("move_up", "move_down")
  62.    
  63.     #normalise direction for joysticks
  64.     direction= direction.normalized()
  65.    
  66.     #set flip h based on axis.
  67.     %SpriteHandle.flip_h = direction.x < 0 if direction.x != 0 else %SpriteHandle.flip_h
  68.    
  69.     #are we moving?
  70.     if direction.x != 0:
  71.         velocity.x = clamp(velocity.x + direction.x * acceleration * delta * 5, -maxSpeed, maxSpeed)
  72.        
  73.         if self.is_on_floor():
  74.             currentPlayerState = playerStates.running
  75.    
  76.     #have we stopped?
  77.     elif direction.x == 0:    
  78.        
  79.         #gradual deceleration
  80.         velocity.x = direction.x * max(abs(velocity.x) - friction, 0)
  81.        
  82.         if self.is_on_floor() == true:
  83.             currentPlayerState = playerStates.standing
  84.    
  85.     ##Subroutine for basic jump.
  86.    
  87.     if Input.is_action_just_pressed("jump") && (is_on_floor() or coyote):
  88.        
  89.         #start counting the accumulated delta
  90.        
  91.         currentPlayerState = playerStates.jumping
  92.        
  93.         initJumpForce -= downForce
  94.         var jump : Vector2= initJumpForce * up_direction
  95.         velocity += jump
  96.        
  97.        
  98.         if velocity.y > 0:
  99.             velocity.y = -2
  100.        
  101.         #start timer to set state back to falling
  102.         $JumpTimer.start()
  103.    
  104.      
  105.     if !self.is_on_floor() && currentPlayerState != playerStates.jumping && lastFloor:
  106.         coyote= true
  107.         $CoyoteTimer.start()
  108.    
  109.     #if player releases jump early they should be able to fall sooner also set
  110.     #state to falling if player isn't current toutching the ground
  111.     elif Input.is_action_just_released("jump") && !self.is_on_floor():
  112.         start_falling()
  113.    
  114.     elif !self.is_on_floor() && currentPlayerState != playerStates.jumping:
  115.         start_falling()
  116.    
  117.     if Input.is_action_pressed("crouch") && self.is_on_floor():
  118.         crouch(direction)
  119.    
  120.     #player remains in crouched state if sandwiched
  121.     #between ceiling and floor.
  122.    
  123.     elif %CeilingRay.is_colliding() && !Input.is_action_pressed("crouch"):
  124.         crouch(direction)
  125.         currentPlayerState = playerStates.crouching
  126.    
  127.     if Input.is_action_just_pressed("dash"):
  128.         dash(direction)
  129.    
  130.     #update player state needs to be done every tick for physics purposes.
  131.     change_player_state.emit(currentPlayerState)
  132.     move_and_slide()
  133.  
  134. ##Sets player to falling state when timer runs out.
  135. ##Stops them from being in the air too long.
  136. func start_falling():
  137.    
  138.     currentPlayerState = playerStates.falling
  139.    
  140.     #make player fall more slowly when holding space
  141.     if Input.is_action_pressed("jump") && !is_on_floor():
  142.         var fallSpeed := Vector2.DOWN * weight * slowFallMultiplier
  143.         velocity += fallSpeed
  144.    
  145.     elif !Input.is_action_pressed("jump") && !is_on_floor():
  146.         velocity += weight * Vector2.DOWN
  147.    
  148.     #cap fall speed to allow player to control their fall
  149.     if velocity.y >= terminalFall:
  150.         velocity.y = terminalFall
  151.  
  152. func crouch(direction : Vector2):
  153.     #are we moving?
  154.     if direction.x != 0:
  155.         currentPlayerState = playerStates.crouchwalking
  156.         #player should be punished for crouchwalking instead of sliding under
  157.         #to maintain momentum
  158.         velocity.x *= velocityWhileCrouching
  159.    
  160.     else:
  161.         currentPlayerState = playerStates.crouching
  162.  
  163. func dash(direction : Vector2):
  164.     currentPlayerState = playerStates.dashing
  165.     var dashVector : Vector2 = direction * dashForce
  166.     velocity += dashVector
  167.  
  168.  
  169. func _on_coyote_timer_timeout():
  170.     coyote = false
  171.     start_falling()
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement