Advertisement
gdquest

Platform #4 - Last tutorial starting code

Feb 14th, 2017
1,587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. var input_direction = 0
  4. var direction = 1
  5.  
  6. var speed = Vector2()
  7. var velocity = Vector2()
  8.  
  9. const MAX_SPEED = 600
  10. const ACCELERATION = 1200
  11. const DECELERATION = 2000
  12.  
  13. const JUMP_FORCE = 700
  14. const GRAVITY = 2000
  15.  
  16.  
  17. func _ready():
  18.     set_process(true)
  19.     set_process_input(true)
  20.  
  21.  
  22. func _input(event):
  23.     if jump_count < MAX_JUMP_COUNT and event.is_action_pressed("jump"):
  24.         speed.y = -JUMP_FORCE
  25.         jump_count += 1
  26.  
  27.  
  28. func _process(delta):
  29.     if input_direction:
  30.         direction = input_direction
  31.    
  32.     if Input.is_action_pressed("move_left"):
  33.         input_direction = -1
  34.     elif Input.is_action_pressed("move_right"):
  35.         input_direction = 1
  36.     else:
  37.         input_direction = 0
  38.    
  39.     if input_direction == - direction:
  40.         speed.x /= 3
  41.     if input_direction:
  42.         speed.x += ACCELERATION * delta
  43.     else:
  44.         speed.x -= DECELERATION * delta
  45.     speed.x = clamp(speed.x, 0, MAX_SPEED)
  46.    
  47.     speed.y += GRAVITY * delta
  48.    
  49.     velocity = Vector2(speed.x * delta * direction, speed.y * delta)
  50.     var movement_remainder = move(velocity)
  51.    
  52.     if is_colliding():
  53.         var normal = get_collision_normal()
  54.         var final_movement = normal.slide(movement_remainder)
  55.         speed = normal.slide(speed)
  56.         move(final_movement)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement