daily pastebin goal
80%
SHARE
TWEET

Old character controller

CornerLord Mar 24th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. # Initialize the nodes
  4. onready var tween_node = $Tween
  5. onready var animation_player_node = $AnimationPlayer
  6. onready var sprite_node = $Sprite
  7. onready var collision_capsule_node = $CollisionCapsule
  8.  
  9. # Finite state machine
  10. enum states {
  11.         Idle,
  12.         Move,
  13.     }
  14. var state = states.Move
  15.  
  16. # TODO tween gravity?, build a parkour and test the player in it, adjust slope angle and slope stop
  17. # FIXME walking against a wall causes stuttering on certain resolutions
  18. # States.Idle is never used!!!
  19.  
  20. var velocity = Vector2.ZERO;
  21. var max_slope = deg2rad(45)
  22. const FLOOR = Vector2(0, -1)
  23.  
  24. export var jump_speed = 400
  25. export var gravity = 12
  26. export var walking_speed = 10
  27. export var max_running_speed = 200
  28.  
  29. var ground_damping = 0.5
  30. var ground_turning_damping = 0.3
  31. var air_damping = 0.55
  32.  
  33. var spriting_turning_multiplier = 0.9
  34. var sprinting_multiplier = 1.2
  35.  
  36. var animation_name = "Idle"
  37. var unskippable_animations = ["RunTurn", "FastLanding", "SlowLanding"];
  38. var _scale = 1
  39.  
  40. var min_speed_for_falling_animation = 100
  41. var min_walking_speed = 10
  42. var max_walking_speed = 115
  43.  
  44. var jump_pressed_timer = 0
  45. var jump_pressed_timer_max_time = 0.2
  46. var was_on_floor_timer = 0
  47. var was_on_floor_timer_max_time = 0.1
  48.  
  49. var variable_jump_decrease = 0.8
  50. var slowdown_timer = 0
  51. var slowdown_timer_max_time = 1.0
  52.  
  53. var air_speed = 0
  54. var speed_for_slow_landing = 380
  55.  
  56. var has_player_jump_start = false
  57.  
  58. # Input
  59. var left;
  60. var right;
  61. var turning;
  62.  
  63. func _physics_process(delta):
  64.     # Always running functions
  65.     _animate();
  66.  
  67.     match state:
  68.         states.Idle:
  69.             _get_input()
  70.             _idle_state()
  71.            
  72.         states.Move:
  73.             _get_input()
  74.             _movement(delta)
  75.             _jumping(delta)
  76.  
  77. func _idle_state() -> void:
  78.     if left || right:
  79.         state = states.Move
  80.    
  81.     if Input.is_action_just_pressed("jump"):
  82.         state = states.Jump
  83.  
  84. func _get_input():
  85.     left = Input.is_action_pressed("move_left")
  86.     right = Input.is_action_pressed("move_right")
  87.     turning = (velocity.x > 0 && left) || (velocity.x < 0 && right);
  88.  
  89. func _movement(delta) -> void:
  90.     if animation_name == "FastLanding" || animation_name == "SlowLanding":
  91.         return
  92.    
  93.     if left && (animation_name != "RunTurn" || sprite_node.scale.x == 1):
  94.         velocity.x -= walking_speed
  95.     if right && (animation_name != "RunTurn" || sprite_node.scale.x == -1):
  96.         velocity.x += walking_speed
  97.    
  98.     if is_on_floor():
  99.         # Activates when changing direction
  100.         if turning:
  101.             velocity.x *= pow(1.0 - ground_turning_damping, delta * 10.0)
  102.         # Slows down the velocity
  103.         else:
  104.             velocity.x *= pow(1.0 - ground_damping, delta * 10.0)
  105.     # Slows horizontal movement when in the air
  106.     else:
  107.         velocity.x *= pow(1.0 - air_damping, delta * 10.0)
  108.    
  109.     # Sprinting
  110.     if Input.is_action_pressed("sprint") && abs(velocity.x) < 200 && (left || right):
  111.         # Deaccelerate faster when changing direction while sprinting
  112.         if turning:
  113.             velocity.x *= spriting_turning_multiplier
  114.         # Boost the speed
  115.         else:
  116.             velocity.x *= sprinting_multiplier
  117.            
  118.             # Adjust the running speed
  119.             velocity.x = clamp(velocity.x, 0, max_running_speed) if right else clamp(velocity.x, -max_running_speed, 0)
  120.  
  121.     # Move the character
  122.     velocity = move_and_slide(velocity, FLOOR, false, 4, max_slope)
  123.    
  124.     if velocity.y > air_speed:
  125.         air_speed = velocity.y
  126.    
  127.     # Jumping
  128.     _jumping(delta)
  129.  
  130. func _jumping(delta) -> void:
  131.     velocity.y += gravity;
  132.    
  133.     if jump_pressed_timer > 0: jump_pressed_timer -= delta
  134.     if was_on_floor_timer > 0: was_on_floor_timer -= delta
  135.    
  136.     # Jump a bit after exited the collision to make platforming smoother
  137.     if is_on_floor():
  138.         was_on_floor_timer = was_on_floor_timer_max_time
  139.    
  140.     # Jump even if the jump button was pressed a bit early
  141.     if Input.is_action_just_pressed("jump") && animation_name != "RunTurn":
  142.         jump_pressed_timer = jump_pressed_timer_max_time
  143.    
  144.     # Decrease y velocity when not holding jump button and the peak height is not reached
  145.     if Input.is_action_just_released("jump") && velocity.y < 0:
  146.         velocity.y *= variable_jump_decrease
  147.    
  148.     if jump_pressed_timer > 0 && was_on_floor_timer > 0:
  149.         jump_pressed_timer = 0
  150.         was_on_floor_timer = 0
  151.         velocity.y = -jump_speed
  152.  
  153. func _animate() -> void:
  154.     if (animation_name == "JumpFall" || animation_name == "RunJumpFall") && is_on_floor():
  155.         if(air_speed > speed_for_slow_landing):
  156.             air_speed = 0
  157.             animation_name = "SlowLanding"
  158.         else:
  159.             animation_name = "FastLanding"
  160.            
  161.         animation_player_node.play(animation_name);
  162.         return
  163.    
  164.     # Animations that have to be played completely
  165.     if(unskippable_animations.has(animation_name)):
  166.         return
  167.    
  168.     # Scaling
  169.     if round(velocity.x) > 0:
  170.         _scale = 1
  171.     elif round(velocity.x) < 0:
  172.         _scale = -1
  173.    
  174.     if(sprite_node.scale.x != _scale || collision_capsule_node.position.x != _scale):
  175.         sprite_node.scale.x = _scale;
  176.         collision_capsule_node.position.x *= _scale
  177.    
  178.     # On floor
  179.     var running = abs(velocity.x) > max_walking_speed
  180.    
  181.     if is_on_floor():
  182.         if abs(velocity.x) < min_walking_speed:
  183.             animation_name = "Idle"
  184.         else:
  185.             if running:
  186.                 animation_name = "Run"
  187.             else:
  188.                 animation_name = "Walk"
  189.        
  190.         if turning:
  191.             if(abs(velocity.x) <= max_walking_speed):
  192.                 animation_name = "IdleTurn"
  193.             else:
  194.                 animation_name = "RunTurn"
  195.        
  196.     # In the air
  197.     elif get_slide_count() == 0 && abs(velocity.y) > min_speed_for_falling_animation:
  198.         # Going upwards
  199.         if velocity.y < 0:
  200.             if has_player_jump_start:
  201.                 if(running):
  202.                     animation_name = "RunJump"
  203.                 else:
  204.                     animation_name = "Jump"
  205.             else:
  206.                 if(running):
  207.                     animation_name = "RunJumpStart"
  208.                 else:
  209.                     animation_name = "JumpStart"
  210.        
  211.         # Falling down
  212.         else:
  213.             if(running):
  214.                 animation_name = "RunJumpFall"
  215.             else:
  216.                 animation_name = "JumpFall"
  217.    
  218.     if animation_player_node.current_animation != animation_name:
  219.         animation_player_node.play(animation_name)
  220.  
  221. func _on_AnimationPlayer_animation_finished(anim_name):
  222.     animation_name = ""
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