Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- var velocity = Vector2()
- var jumping = false
- var running = false
- var dashing = false
- var crouching = false
- var wallsliding = false
- var direction = 1
- var lastaction = "right"
- const DRAG = float(.5)
- const FRICTION = float(.2)
- const ICEFRICTION = float(.04)
- const MAXSPEED = float(80)
- const MAXFALL = float(40)
- const GRAVITY = float(8)
- const JUMPPOWER = float (-150)
- const FLOOR = Vector2(0, -1)
- #input
- func _input(event):
- if Input.is_action_just_pressed("game_left"):
- lastaction = "game_left"
- if Input.is_action_just_pressed("game_right"):
- lastaction = "game_right"
- if Input.is_action_just_pressed("game_jump"):
- lastaction = "game_jump"
- #physics
- func _physics_process(delta):
- #check
- if is_on_floor(): # If I am on the floor:
- jumping = false
- wallsliding = false
- if !is_on_wall(): # If I am not on the wall:
- wallsliding = false
- if is_on_wall() && !is_on_floor(): # If I am on the wall, but not the floor:
- wallsliding = true
- running = false
- if is_on_wall() && wallsliding: # If I am on the wall and wallsliding:
- jumping = false
- running = false
- #running
- if Input.is_action_pressed("game_left") && lastaction == "game_left":
- velocity.x -= ((MAXSPEED + velocity.x) * FRICTION)
- direction = -1 #left
- running = true
- elif Input.is_action_pressed("game_right") && lastaction == "game_right":
- velocity.x += ((MAXSPEED - velocity.x) * FRICTION)
- direction = 1 #right
- running = true
- elif Input.is_action_pressed("game_left"):
- velocity.x -= ((MAXSPEED + velocity.x) * FRICTION)
- direction = -1 #left
- running = true
- elif Input.is_action_pressed("game_right"):
- velocity.x += ((MAXSPEED - velocity.x) * FRICTION)
- direction = 1 #right
- running = true
- else:
- velocity.x += ((0 - velocity.x) * FRICTION)
- #jumping
- if Input.is_action_just_pressed("game_jump") && !jumping:
- if wallsliding: #wallsliding
- velocity.x = direction * (JUMPPOWER)
- velocity.y = JUMPPOWER
- else:
- velocity.y = JUMPPOWER
- jumping = true
- wallsliding = false
- #physics update
- velocity.y += GRAVITY
- velocity = move_and_slide(velocity, FLOOR)
- #get_node("RichTextLabel").set_text(str(velocity.y))
- get_node("RichTextLabel").set_text(str(direction))
- #animation
- func _process(delta):
- if direction:
- get_node("AnimatedSprite").set_flip_h(false)
- if !direction:
- get_node("AnimatedSprite").set_flip_h(true)
- if running:
- get_node("AnimatedSprite").play("RunRight")
- if jumping:
- get_node("AnimatedSprite").play("Jump")
- else:
- get_node("AnimatedSprite").play("Idle")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement