Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- const UP = Vector2(0, -1)
- const JUMP_FORCE = -550
- const GRAVITY = 20
- const MAX_FALL_SPEED = 1000
- var motion = Vector2()
- var max_speed = 200
- var acceleration = 50
- var facing = ""
- var in_wall_counter = 0
- onready var anim_player = $AnimationPlayer
- onready var sprite = $Sprite
- func _physics_process(delta):
- if Input.is_action_pressed("sprint"):
- max_speed = 300
- acceleration = 75
- else:
- max_speed = 200
- acceleration = 50
- #motion.y += GRAVITY
- var friction = false
- var move_dir = 0
- if Input.is_action_pressed("move_right"):
- motion.x = min(motion.x + acceleration, max_speed)
- facing = "right"
- if Input.is_action_pressed("move_left"):
- motion.x = max(motion.x - acceleration, -max_speed)
- facing = "left"
- if !Input.is_action_pressed("move_left") and !Input.is_action_pressed("move_right"):
- friction = true
- facing = ""
- motion = move_and_slide(motion, UP)
- var grounded = is_on_floor()
- var wall = is_on_wall()
- if is_on_wall():
- in_wall_counter += 1
- else:
- in_wall_counter = 0
- if in_wall_counter >= 50:
- motion.y = 1000
- if wall and Input.is_action_just_pressed("jump"):
- motion.y = JUMP_FORCE
- if facing == "right":
- motion.x = -300
- elif facing == "left":
- motion.x = 300
- if grounded:
- if friction:
- motion.x = lerp(motion.x, 0, 0.2)
- if Input.is_action_pressed("jump"):
- motion.y += JUMP_FORCE
- if move_dir == 0:
- play_anim("idle")
- else:
- play_anim("walk")
- else:
- if !friction:
- motion.x = lerp(motion.x, 0, 0.05)
- play_anim("jump")
- if motion.y > MAX_FALL_SPEED:
- motion.y = MAX_FALL_SPEED
- motion.x = 0
- motion.y = 0
- if Input.is_action_pressed("ui_up"):
- motion.y = -50
- if Input.is_action_pressed("ui_down"):
- motion.y = 50
- if Input.is_action_pressed("ui_right"):
- motion.x = 50
- if Input.is_action_pressed("ui_left"):
- motion.x = -50
- motion = move_and_slide(motion, UP)
- func flip():
- sprite.flip_h = !sprite.flip_h
- func play_anim(anim_name):
- return
- if anim_player.is_playing() and anim_player.current_animation == anim_name:
- return
- anim_player.play(anim_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement