Advertisement
Guest User

Untitled

a guest
Apr 12th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. const TARGET_FPS = 60
  4. const ACCELERATION = 200
  5. const MAX_SPEED = 150
  6. const FRICTION = 100
  7. const AIR_RESISTANCE = 10
  8. const JUMP_FORCE = 350
  9.  
  10. var GRAVITY = 15
  11.  
  12. var motion = Vector2.ZERO # current player x/y speed
  13. var hanging = false # are we hanging?
  14. var was_in_air = false # boolean that is used for the landing sound
  15. var last_dir # -1 when facing left, 1 when facing right
  16.  
  17. func _physics_process(delta):
  18.     if (!hanging):
  19.         var direction = round(Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"))
  20.        
  21.         if direction != 0:
  22.             last_dir = direction
  23.             motion.x += direction * ACCELERATION * delta * TARGET_FPS
  24.             motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
  25.             $Sprite.flip_h = direction < 0
  26.             $Upper_Hang_Check.cast_to = Vector2(25 * direction, 0)
  27.             $Lower_Hang_Check.cast_to = Vector2(25 * direction, 0)
  28.             $Camera2D.position = Vector2(direction * 60.0, 0)
  29.             anim_state_running()   
  30.         else:
  31.             anim_state_idle()
  32.  
  33.         if (motion.y > 0):
  34.             GRAVITY = 20
  35.         else:
  36.             GRAVITY = 15
  37.        
  38.         motion.y += GRAVITY * delta * TARGET_FPS   
  39.        
  40.         if is_on_floor():
  41.             anim_state_on_floor()
  42.  
  43.             if (was_in_air):
  44.                 $Land.play()
  45.                 was_in_air = false
  46.  
  47.             if direction == 0:
  48.                 motion.x = 0
  49.  
  50.             if Input.is_action_just_pressed("ui_up"):
  51.                 motion.y = -JUMP_FORCE
  52.                 anim_state_jump()
  53.                 $Walk.play()
  54.         else:
  55.             anim_state_in_air()
  56.             was_in_air = true
  57.            
  58.             if Input.is_action_just_released("ui_up") and motion.y < -JUMP_FORCE/2:
  59.                 motion.y = -JUMP_FORCE/2
  60.            
  61.             if motion.y >= 100:
  62.                 anim_state_fall()
  63.            
  64.             if direction == 0:
  65.                 motion.x = lerp(motion.x, 0, AIR_RESISTANCE * delta)
  66.                
  67.             if (motion.y > -100 && $Lower_Hang_Check.is_colliding() && !$Upper_Hang_Check.is_colliding() && !$Hang_Check_Rear.is_colliding()):
  68.                 if ($Lower_Hang_Check.get_collider().is_in_group("world")):
  69.                     motion.y = 0
  70.                     hanging = true
  71.                     var tileMap = $Lower_Hang_Check.get_collider()
  72.                     var tilePos = tileMap.world_to_map($Lower_Hang_Check.get_collision_point())
  73.                     self.position = Vector2(float(tilePos.x) * 16 - (last_dir * 13), float(tilePos.y) * 16 + 16)
  74.                
  75.         motion = move_and_slide(motion, Vector2.UP)
  76.     else:
  77.         state_hanging(delta)
  78.        
  79. func coin_picked_up():
  80.     $Gem.play()
  81.        
  82. func state_hanging(delta):
  83.     anim_state_hanging()
  84.  
  85.     if Input.is_action_just_pressed("ui_up"):
  86.         motion.y = -JUMP_FORCE * 0.8
  87.         anim_state_jump()
  88.         enable_hang_check(0.3)
  89.  
  90.     if Input.is_action_just_pressed("ui_down"):
  91.         motion.y = JUMP_FORCE * 0.25
  92.         anim_state_fall()
  93.         enable_hang_check(0.3)
  94.  
  95. #
  96. # Re-enable the Raycast2Ds for grabbing ledges after <seconds> seconds
  97. #
  98. func enable_hang_check(seconds):
  99.     hanging = false
  100.     yield(get_tree().create_timer(seconds), "timeout")
  101.     $Lower_Hang_Check.enabled = true
  102.     $Upper_Hang_Check.enabled = true
  103.     $Hang_Check_Rear.enabled = true
  104.  
  105. #
  106. # Animation states
  107. # Set the animation tree parameters for the correct animation
  108. #
  109. func anim_state_hanging():
  110.     $Lower_Hang_Check.enabled = false
  111.     $Upper_Hang_Check.enabled = false
  112.     $Hang_Check_Rear.enabled = false
  113.     $AnimationTree.set("parameters/in_air_state/current", 2)
  114.    
  115. func anim_state_on_floor():
  116.     $AnimationTree.set("parameters/in_air_state/current", 0)
  117.  
  118. func anim_state_in_air():
  119.     $AnimationTree.set("parameters/in_air_state/current", 1)
  120.  
  121. func anim_state_running():
  122.     $AnimationTree.set("parameters/movement/current", 1)
  123.     #$AnimationTree.set("parameters/movement_time/scale", 1)
  124.    
  125. func anim_state_idle():
  126.     $AnimationTree.set("parameters/movement/current", 0)
  127.    
  128. func anim_state_jump():
  129.     $AnimationTree.set("parameters/in_air_state/current", 1)
  130.     $AnimationTree.set("parameters/in_air/current", 1) 
  131.    
  132. func anim_state_fall():
  133.     $AnimationTree.set("parameters/in_air_state/current", 1)
  134.     $AnimationTree.set("parameters/in_air/current", 0) 
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement