End of lesson 18 The command processor, Chapter 5
This commit is contained in:
46
scripts/game/CommandProcessor.gd
Normal file
46
scripts/game/CommandProcessor.gd
Normal file
@@ -0,0 +1,46 @@
|
||||
extends Timer
|
||||
|
||||
class_name CommandProcessor
|
||||
signal BROADCAST_COMMAND(label)
|
||||
|
||||
var resumeWaiting:bool = true
|
||||
|
||||
func _ready():
|
||||
CommandDispatcher.PROCESS_COMMAND.connect(processCommand)
|
||||
CommandDispatcher.WAIT_FOR_COMMAND.connect(waitForCommand)
|
||||
CommandDispatcher.PAUSE_PROCESSOR.connect(pauseProcessor)
|
||||
|
||||
|
||||
func processCommand(command:Command):
|
||||
if (is_stopped()):
|
||||
return
|
||||
|
||||
stop()
|
||||
|
||||
command.COMMAND_PROCESSED.connect(onCommandProcessed)
|
||||
|
||||
BROADCAST_COMMAND.emit(command.getCommandText())
|
||||
|
||||
command.execute()
|
||||
|
||||
|
||||
func waitForCommand():
|
||||
resumeWaiting = true
|
||||
start()
|
||||
|
||||
|
||||
func pauseProcessor():
|
||||
stop()
|
||||
resumeWaiting = false
|
||||
|
||||
|
||||
func onCommandProcessed(result):
|
||||
if (result != null):
|
||||
print(result)
|
||||
|
||||
if (resumeWaiting):
|
||||
waitForCommand()
|
||||
|
||||
|
||||
func _on_timeout():
|
||||
processCommand(PassCommand.new())
|
||||
Reference in New Issue
Block a user