SHARE
TWEET

Untitled

a guest Feb 16th, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. var velocity = Vector2()
  4. var jumping = false
  5. var running = false
  6. var dashing = false
  7. var crouching = false
  8. var wallsliding = false
  9. var direction = 1
  10. var lastaction = "right"
  11.  
  12. const DRAG = float(.5)
  13. const FRICTION = float(.2)
  14. const ICEFRICTION = float(.04)
  15. const MAXSPEED = float(80)
  16. const MAXFALL = float(40)
  17. const GRAVITY = float(8)
  18. const JUMPPOWER = float (-150)
  19. const FLOOR = Vector2(0, -1)
  20.  
  21. #input
  22. func _input(event):
  23.     if Input.is_action_just_pressed("game_left"):
  24.         lastaction = "game_left"
  25.     if Input.is_action_just_pressed("game_right"):
  26.         lastaction = "game_right"
  27.     if Input.is_action_just_pressed("game_jump"):
  28.         lastaction = "game_jump"
  29.  
  30. #physics
  31. func _physics_process(delta):
  32.  
  33. #check
  34.     if is_on_floor(): # If I am on the floor:
  35.         jumping = false
  36.         wallsliding = false
  37.        
  38.     if !is_on_wall(): # If I am not on the wall:
  39.         wallsliding = false
  40.        
  41.     if is_on_wall() && !is_on_floor(): # If I am on the wall, but not the floor:
  42.         wallsliding = true
  43.         running = false
  44.        
  45.     if is_on_wall() && wallsliding: # If I am on the wall and wallsliding:
  46.         jumping = false
  47.         running = false
  48.  
  49.  
  50. #running
  51.     if Input.is_action_pressed("game_left") && lastaction == "game_left":
  52.         velocity.x -= ((MAXSPEED + velocity.x) * FRICTION)
  53.         direction = -1 #left
  54.         running = true
  55.     elif Input.is_action_pressed("game_right") && lastaction == "game_right":
  56.         velocity.x += ((MAXSPEED - velocity.x) * FRICTION)
  57.         direction = 1 #right
  58.         running = true
  59.     elif Input.is_action_pressed("game_left"):
  60.         velocity.x -= ((MAXSPEED + velocity.x) * FRICTION)
  61.         direction = -1 #left
  62.         running = true
  63.     elif Input.is_action_pressed("game_right"):
  64.         velocity.x += ((MAXSPEED - velocity.x) * FRICTION)
  65.         direction = 1 #right
  66.         running = true
  67.     else:
  68.         velocity.x += ((0 - velocity.x) * FRICTION)
  69.  
  70. #jumping
  71.     if Input.is_action_just_pressed("game_jump") && !jumping:
  72.  
  73.         if wallsliding: #wallsliding
  74.             velocity.x = direction * (JUMPPOWER)
  75.             velocity.y = JUMPPOWER
  76.         else:
  77.             velocity.y = JUMPPOWER
  78.         jumping = true
  79.         wallsliding = false
  80.  
  81.  
  82. #physics update
  83.  
  84.     velocity.y += GRAVITY
  85.  
  86.     velocity = move_and_slide(velocity, FLOOR)
  87.    
  88.     #get_node("RichTextLabel").set_text(str(velocity.y))
  89.     get_node("RichTextLabel").set_text(str(direction))
  90. #animation
  91. func _process(delta):
  92.     if direction:
  93.         get_node("AnimatedSprite").set_flip_h(false)
  94.     if !direction:
  95.         get_node("AnimatedSprite").set_flip_h(true)
  96.     if running:
  97.         get_node("AnimatedSprite").play("RunRight")
  98.     if jumping:
  99.         get_node("AnimatedSprite").play("Jump")
  100.     else:
  101.         get_node("AnimatedSprite").play("Idle")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top