Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- # Movement
- const UP = Vector2(0, -1)
- var motion = Vector2()
- export var max_run_speed = 300
- export var max_fall_speed = 600
- export var run_acceleration = 10
- export var gravity = 10
- export var jump_force = -300
- export var friction = 10
- onready var anim = get_node("AnimatedSprite")
- # What we should do when a certain animation finishes
- func _on_AnimatedSprite_animation_finished():
- anim.set_speed_scale(2)
- if not ["idle", "jump", "fall"].has(anim.animation):
- motion.x = 0
- if is_on_floor():
- anim.play("idle")
- elif anim.animation == "jump":
- if motion.y < 0:
- anim.play("jump")
- anim.frame = 2
- else:
- anim.play("fall")
- elif ["fall"].has(anim.animation):
- anim.play("fall")
- anim.frame = 2
- else:
- anim.play("fall")
- func get_inputs():
- # Perform movement calculations based on which button is pressed
- if Input.is_action_pressed('ui_right') && motion.x < max_run_speed:
- anim.flip_h = false
- if motion.x > max_run_speed / 8&& anim.animation == "idle":
- anim.play("move")
- motion.x += run_acceleration
- elif Input.is_action_pressed('ui_left') && motion.x > -max_run_speed:
- anim.flip_h = true
- if motion.x < max_run_speed / -8 && anim.animation == "idle":
- anim.play("move")
- motion.x -= run_acceleration
- # If no movement keys were pressed, apply friction
- else:
- if motion.x > friction:
- motion.x -= friction
- elif motion.x < 0 && motion.x < -friction:
- motion.x += friction
- else:
- motion.x = 0
- func _physics_process(delta):
- # Stop movement for two frames on every movement cycle.
- if anim.animation == "move" && anim.frame > 10 or anim.animation == "landing" && anim.frame < 2:
- motion.x = 0
- if Input.is_action_pressed('ui_right') || Input.is_action_pressed('ui_left'):
- anim.play("move")
- anim.frame = 1
- if motion.y < max_fall_speed - gravity:
- motion.y += gravity
- else:
- motion.y == max_fall_speed
- get_inputs()
- floor_check()
- move_and_slide(motion, UP)
- # Chech whether the character is on the floor or in air and play animations accordingly
- func floor_check():
- if is_on_floor():
- if not anim.is_playing():
- anim.play("idle")
- # If we are in the middle of an animation when on the floor, play the appropriate animation
- if anim.is_playing():
- if ["fall"].has(anim.animation):
- anim.play("landing")
- if Input.is_action_just_pressed("ui_up"):
- motion.y = jump_force
- anim.play("jump")
- else:
- motion.y = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement