Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- const TARGET_FPS = 60
- const ACCELERATION = 200
- const MAX_SPEED = 150
- const FRICTION = 100
- const AIR_RESISTANCE = 10
- const JUMP_FORCE = 350
- var GRAVITY = 15
- var motion = Vector2.ZERO # current player x/y speed
- var hanging = false # are we hanging?
- var was_in_air = false # boolean that is used for the landing sound
- var last_dir # -1 when facing left, 1 when facing right
- func _physics_process(delta):
- if (!hanging):
- var direction = round(Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"))
- if direction != 0:
- last_dir = direction
- motion.x += direction * ACCELERATION * delta * TARGET_FPS
- motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
- $Sprite.flip_h = direction < 0
- $Upper_Hang_Check.cast_to = Vector2(25 * direction, 0)
- $Lower_Hang_Check.cast_to = Vector2(25 * direction, 0)
- $Camera2D.position = Vector2(direction * 60.0, 0)
- anim_state_running()
- else:
- anim_state_idle()
- if (motion.y > 0):
- GRAVITY = 20
- else:
- GRAVITY = 15
- motion.y += GRAVITY * delta * TARGET_FPS
- if is_on_floor():
- anim_state_on_floor()
- if (was_in_air):
- $Land.play()
- was_in_air = false
- if direction == 0:
- motion.x = 0
- if Input.is_action_just_pressed("ui_up"):
- motion.y = -JUMP_FORCE
- anim_state_jump()
- $Walk.play()
- else:
- anim_state_in_air()
- was_in_air = true
- if Input.is_action_just_released("ui_up") and motion.y < -JUMP_FORCE/2:
- motion.y = -JUMP_FORCE/2
- if motion.y >= 100:
- anim_state_fall()
- if direction == 0:
- motion.x = lerp(motion.x, 0, AIR_RESISTANCE * delta)
- if (motion.y > -100 && $Lower_Hang_Check.is_colliding() && !$Upper_Hang_Check.is_colliding() && !$Hang_Check_Rear.is_colliding()):
- if ($Lower_Hang_Check.get_collider().is_in_group("world")):
- motion.y = 0
- hanging = true
- var tileMap = $Lower_Hang_Check.get_collider()
- var tilePos = tileMap.world_to_map($Lower_Hang_Check.get_collision_point())
- self.position = Vector2(float(tilePos.x) * 16 - (last_dir * 13), float(tilePos.y) * 16 + 16)
- motion = move_and_slide(motion, Vector2.UP)
- else:
- state_hanging(delta)
- func coin_picked_up():
- $Gem.play()
- func state_hanging(delta):
- anim_state_hanging()
- if Input.is_action_just_pressed("ui_up"):
- motion.y = -JUMP_FORCE * 0.8
- anim_state_jump()
- enable_hang_check(0.3)
- if Input.is_action_just_pressed("ui_down"):
- motion.y = JUMP_FORCE * 0.25
- anim_state_fall()
- enable_hang_check(0.3)
- #
- # Re-enable the Raycast2Ds for grabbing ledges after <seconds> seconds
- #
- func enable_hang_check(seconds):
- hanging = false
- yield(get_tree().create_timer(seconds), "timeout")
- $Lower_Hang_Check.enabled = true
- $Upper_Hang_Check.enabled = true
- $Hang_Check_Rear.enabled = true
- #
- # Animation states
- # Set the animation tree parameters for the correct animation
- #
- func anim_state_hanging():
- $Lower_Hang_Check.enabled = false
- $Upper_Hang_Check.enabled = false
- $Hang_Check_Rear.enabled = false
- $AnimationTree.set("parameters/in_air_state/current", 2)
- func anim_state_on_floor():
- $AnimationTree.set("parameters/in_air_state/current", 0)
- func anim_state_in_air():
- $AnimationTree.set("parameters/in_air_state/current", 1)
- func anim_state_running():
- $AnimationTree.set("parameters/movement/current", 1)
- #$AnimationTree.set("parameters/movement_time/scale", 1)
- func anim_state_idle():
- $AnimationTree.set("parameters/movement/current", 0)
- func anim_state_jump():
- $AnimationTree.set("parameters/in_air_state/current", 1)
- $AnimationTree.set("parameters/in_air/current", 1)
- func anim_state_fall():
- $AnimationTree.set("parameters/in_air_state/current", 1)
- $AnimationTree.set("parameters/in_air/current", 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement