Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ================================================================================================
- class InputBuffer:
- # used for return if there is no input
- const NO_INPUT = {"NO_INPUT": Vector2(0, 0)}
- # the buffer for inputs an array of dictionaries
- var inputs_array = []
- # avaliable buttons for reading input for walking
- # left is the name of the input, the right is the corresponding direction in Vector2()
- # change these according to the input mapping you assinged
- const walk_inputs = {
- "move_down": Vector2(0, 1),
- "move_up": Vector2(0, -1),
- "move_left": Vector2(-1, 0),
- "move_right": Vector2(1, 0),
- }
- # constructor
- func _init():
- assert(walk_inputs.size() > 0, "walk inputs empty")
- # PUBLIC METHODS-----
- # for external use
- # NOTE: convert input into a single direction, this is the one used exterinally for movement
- func input_to_direction() -> Vector2:
- var input = return_latest_input()
- # return direction of input, or (0, 0) direction if no input
- var key = input.keys()[0]
- return input.get(key)
- # PRIVATE METHODS-----
- # don't need to use these outside the class
- # return the most recent input or if no input return a no input constant
- func return_latest_input() -> Dictionary:
- update_array()
- check_input_array_size()
- # return last element (most recent input press) if there is input
- if inputs_array:
- var output: Dictionary = inputs_array [inputs_array.size()-1]
- assert(output.size() == 1, "dictionary should have only size of one")
- return output
- else:
- return NO_INPUT
- # store and remove inputs from the buffer
- func update_array():
- store_pressed_input()
- remove_released_input()
- # if one of the walk buttons is just pressed, store it into the buffer
- func store_pressed_input():
- for key in walk_inputs.keys():
- if (Input.is_action_just_pressed(key)):
- inputs_array.push_back({key: walk_inputs.get(key)})
- check_input_array_size()
- # if one of the walk buttons is no longer pressed, erase it from the buffer
- func remove_released_input():
- for key in walk_inputs.keys():
- if (not Input.is_action_pressed(key)):
- inputs_array.erase({key: walk_inputs.get(key)})
- check_input_array_size()
- # error check -----------------------
- # make sure input buffer is not too large
- # if it is too large, there should be a programming error
- func check_input_array_size():
- assert(inputs_array.size() <= walk_inputs.size(), "input array too large")
- # ================================================================================================
- # create an input buffer object to use it's methods
- var input_buffer = InputBuffer.new()
- # direction ----------------------------------
- # get normalized direction
- func get_direction() -> Vector2:
- var new_direction: Vector2 = Vector2.ZERO
- # NOTE: here is where the input buffer input to direction is used!
- new_direction = input_buffer.input_to_direction()
- return new_direction.normalized()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement