daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2. #initialize varaibles
  3. var vel = Vector2()
  4. var vspd = 0
  5. var hspd = 0
  6. var dir = 0
  7. var input_dir = 0
  8. #flags
  9. var wall_jump = false
  10. var can_play = true
  11. var fullscreen_mode = false
  12. #access sprite
  13. onready var sprite = get_node("Player_Sprite")
  14. onready var wallLeft_signal = get_node("wallLeft_signal")
  15. onready var wallRight_signal = get_node("wallRight_signal")
  16. #initialize constant variable (fixed)
  17. const JUMPSPEED = 850
  18. const ACCELERATION = 50
  19. const FRICTION = 40
  20. const AIR_RESISTANCE = 20
  21. const MAX_SPEED = 500
  22. const NORMAL_FORCE = Vector2(0,-1)
  23. const GRAVITY = 50
  24. const WALL_SLIDE = 25
  25. const TERMINAL_VELOCITY = 2500
  26. const LOW_JUMPSPEED = 400
  27. const WALL_JUMP_SPEED = 280
  28.  
  29. #initialize all objects
  30. func _ready():
  31.     set_physics_process(true)
  32.     set_process_input(true)
  33.     pass
  34.  
  35. #input events
  36. func _input(event):
  37.    
  38.     pass
  39.    
  40. func _physics_process(delta):
  41.     #layer of abstractions
  42.     var walk_right = Input.is_action_pressed("ui_right")
  43.     var walk_left = Input.is_action_pressed("ui_left")
  44.     var jump_up = Input.is_action_just_pressed("ui_up")
  45.     var jump_released = Input.is_action_just_released("ui_up")
  46.     var escape = Input.is_action_just_pressed("ui_escape")
  47.     var restart = Input.is_action_just_pressed("ui_restart")
  48.     var fullscreen = Input.is_action_just_pressed("ui_fullscreen")
  49.     #raycast's variables
  50.     var on_wallLeft = wallLeft_signal.is_colliding()
  51.     var on_wallRight = wallRight_signal.is_colliding()
  52.    
  53.     #conditions, store previous dir for inertia in opposite direction as well as making so that it wouldn't stop immediately
  54.     #see vel.y for more context. As Oppose to just input.dir the
  55.     if input_dir != 0:
  56.         dir = input_dir
  57.  
  58.     #if player is not in air or is wall jumping
  59.     if can_play:
  60.         #Input direction
  61.         if walk_right and not walk_left:
  62.             input_dir = 1
  63.         elif walk_left and not walk_right:
  64.             input_dir = -1
  65.         elif walk_right and walk_left:
  66.             input_dir = 0
  67.         else:
  68.             input_dir = 0
  69.        
  70.     #Animate the player
  71.     #walking right
  72.     if input_dir == 1 and vspd >= 0:
  73.         sprite.flip_h = false
  74.     #walking left
  75.     elif input_dir == -1 and vspd >= 0:
  76.         sprite.flip_h = true
  77.  
  78.     #jumping down
  79.     if not is_on_floor() and vel.y > 0:
  80.         sprite.animation = "Jump-down"
  81.         #This adds some air drag. Fixes the issue with dropping too fast
  82.         vspd -= AIR_RESISTANCE
  83.     #jumping up
  84.     elif vel.y < 0:
  85.         sprite.animation = "Jump-up"
  86.     #running
  87.     elif vel.y == 0 and vel.x != 0:
  88.         sprite.animation = "Running"
  89.     #idle
  90.     else:
  91.         sprite.animation = "Idle"
  92.    
  93.     #activate hspd if there's input
  94.     if input_dir != 0:
  95.         hspd += ACCELERATION
  96.     else:
  97.         hspd -= FRICTION
  98.    
  99.     #max speed, as well as constant acceleration on player
  100.     hspd = clamp(hspd,0,MAX_SPEED)
  101.    
  102.     #don't want the player to constantly accelerate even on floor
  103.     if not is_on_floor():
  104.         vspd += GRAVITY
  105.     else:
  106.         wall_jump = false
  107.         can_play = true
  108.        
  109.     #Terminal Velocity reached
  110.     if vspd >= TERMINAL_VELOCITY:
  111.         vspd = TERMINAL_VELOCITY
  112.  
  113.     #jump is the player is on floor, if there's a ceiling, apply newton's third law
  114.     if is_on_floor() and jump_up:
  115.         vspd = -JUMPSPEED
  116.     elif is_on_ceiling():
  117.         vspd = GRAVITY
  118.     #limits the player jump to a minimum
  119.     if jump_released:
  120.         if vspd < -LOW_JUMPSPEED:
  121.             vspd = -LOW_JUMPSPEED
  122.            
  123.     #Inertia for sudden turning
  124.     if input_dir == -dir:
  125.         hspd = 0
  126.    
  127.     #checks if player is colliding with a wall
  128.     if (on_wallLeft || on_wallRight) and jump_up and not is_on_floor():
  129.         #if it is flagged
  130.         if wall_jump:
  131.             #propels the player up
  132.             vspd = -JUMPSPEED
  133.         #trigger flag
  134.         wall_jump = true
  135.         #remove control from player
  136.         can_play = false
  137.         #change direciont
  138.         dir = -dir
  139.  
  140.     #Horizontal motion for the x component and vertical for y
  141.     if wall_jump:
  142.         hspd = 0
  143.         input_dir = dir
  144.         vel.x = WALL_JUMP_SPEED * -dir
  145.         #this is the equivalent switch statement in gdscript
  146.         match -dir:
  147.             -1: sprite.flip_h = true
  148.             1: sprite.flip_h = false
  149.     else:
  150.         #by default
  151.         vel.x = hspd * dir
  152.     #if player presses ESC
  153.     if escape:
  154.         get_tree().quit()
  155.     #if player presses R
  156.     if restart:
  157.         get_tree().reload_current_scene()
  158.     #if player played Ctrl-F
  159.     if fullscreen and (fullscreen_mode==false):
  160.         fullscreen_mode = true
  161.         OS.set_window_fullscreen(true)
  162.     elif fullscreen and (fullscreen_mode==true):
  163.         fullscreen_mode = false
  164.         OS.set_window_fullscreen(false)
  165.        
  166.     #proceed with wall jump
  167.     vel.y = vspd
  168.     #move using linear velocity only
  169.     vel = move_and_slide(vel,NORMAL_FORCE)
  170.     pass
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