Advertisement
nezvers

Godot Input sequence

Dec 27th, 2019
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. extends Node2D
  2.  
  3.  
  4. enum {PUNCH, KICK}
  5. var timer:Timer
  6. var Sequence:Array = []
  7. var Moves:Dictionary = {
  8.     "Punch Rain" : [PUNCH, PUNCH, PUNCH, PUNCH],
  9.     "Kick Rain" : [KICK, KICK, KICK, KICK],
  10.     "Alternate" : [PUNCH, KICK, PUNCH, KICK],
  11.     "Heavy Blow" : [PUNCH, PUNCH, KICK, KICK]
  12.     }
  13. var Names:Array = Moves.keys()
  14.  
  15. func _ready()->void:
  16.     timer = Timer.new()
  17.     add_child(timer)
  18.     timer.wait_time = 1
  19.     timer.one_shot = true
  20.     timer.connect("timeout", self, "on_timeout")
  21.     $Label.text = str(Sequence)
  22.  
  23. func on_timeout()->void:
  24.     print("timeout")
  25.     Sequence = [] #clear input sequence
  26.     $Label.text = str(Sequence)
  27.  
  28. func _input(event)->void:
  29.     if not event is InputEventKey: #for particular example limit to keyboard
  30.         return
  31.     if event.is_action_pressed("ui_right"):
  32.         add_input_to_sequence(PUNCH)
  33.     elif event.is_action_pressed("ui_left"):
  34.         add_input_to_sequence(KICK)
  35.     $Label.text = str(Sequence)
  36.     timer.start() #reset timeout timer
  37.     check_sequence()
  38.  
  39. func add_input_to_sequence(button:int)->void:
  40.     Sequence.push_back(button)
  41.  
  42. func check_sequence()->void:
  43.     for Name in Names:
  44.         var combo:Array = Moves[Name] #Give sequence of current Combo
  45.         var trim: = Sequence.duplicate() #next steps would alter original sequence
  46.         trim.invert() #Because need to leave last entries
  47.         trim.resize(combo.size()) #Trim to last needed count of entries
  48.         trim.invert() #return to right order
  49.         if trim == combo: #Combo match
  50.             print("COMBO: ", Name)
  51.             Sequence = [] #clear input sequence
  52.             return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement