Advertisement
norbyscook

godot input buffer to disable diagonal movement

Sep 4th, 2023 (edited)
1,819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.94 KB | Gaming | 0 0
  1. # ================================================================================================
  2. class InputBuffer:
  3.  
  4.     # used for return if there is no input
  5.     const NO_INPUT = {"NO_INPUT": Vector2(0, 0)}
  6.    
  7.     # the buffer for inputs an array of dictionaries
  8.     var inputs_array = []
  9.    
  10.     # avaliable buttons for reading input for walking
  11.     # left is the name of the input, the right is the corresponding direction in Vector2()
  12.     # change these according to the input mapping you assinged
  13.     const walk_inputs = {
  14.         "move_down": Vector2(0, 1),
  15.         "move_up": Vector2(0, -1),
  16.         "move_left": Vector2(-1, 0),
  17.         "move_right": Vector2(1, 0),
  18.     }
  19.  
  20.     # constructor
  21.     func _init():
  22.         assert(walk_inputs.size() > 0, "walk inputs empty")
  23.  
  24.     # PUBLIC METHODS-----
  25.     # for external use
  26.     # NOTE: convert input into a single direction, this is the one used exterinally for movement
  27.     func input_to_direction() -> Vector2:
  28.         var input = return_latest_input()
  29.         # return direction of input, or (0, 0) direction if no input
  30.         var key = input.keys()[0]
  31.         return input.get(key)
  32.    
  33.     # PRIVATE METHODS-----
  34.     # don't need to use these outside the class
  35.     # return the most recent input or if no input return a no input constant
  36.     func return_latest_input() -> Dictionary:
  37.         update_array()
  38.         check_input_array_size()
  39.         # return last element (most recent input press)  if there is input
  40.         if inputs_array:
  41.             var output: Dictionary = inputs_array [inputs_array.size()-1]
  42.             assert(output.size() == 1, "dictionary should have only size of one")
  43.             return output
  44.         else:
  45.             return NO_INPUT
  46.  
  47.     # store and remove inputs from the buffer
  48.     func update_array():
  49.         store_pressed_input()
  50.         remove_released_input()
  51.  
  52.     # if one of the walk buttons is just pressed, store it into the buffer
  53.     func store_pressed_input():
  54.         for key in walk_inputs.keys():
  55.             if (Input.is_action_just_pressed(key)):
  56.                 inputs_array.push_back({key: walk_inputs.get(key)})
  57.         check_input_array_size()
  58.  
  59.     # if one of the walk buttons is no longer pressed, erase it from the buffer
  60.     func remove_released_input():
  61.         for key in walk_inputs.keys():
  62.             if (not Input.is_action_pressed(key)):
  63.                 inputs_array.erase({key: walk_inputs.get(key)})
  64.         check_input_array_size()
  65.  
  66.     # error check -----------------------
  67.  
  68.     # make sure input buffer is not too large
  69.     # if it is too large, there should be a programming error
  70.     func check_input_array_size():
  71.         assert(inputs_array.size() <= walk_inputs.size(), "input array too large")
  72. # ================================================================================================
  73.  
  74. # create an input buffer object to use it's methods
  75. var input_buffer = InputBuffer.new()
  76.  
  77. # direction ----------------------------------
  78. # get normalized direction
  79. func get_direction() -> Vector2:
  80.         var new_direction: Vector2 = Vector2.ZERO
  81.         # NOTE: here is where the input buffer input to direction is used!
  82.         new_direction = input_buffer.input_to_direction()
  83.         return new_direction.normalized()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement