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())
|
||||
1
scripts/game/CommandProcessor.gd.uid
Normal file
1
scripts/game/CommandProcessor.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dcqqr8b42tn0x
|
||||
7
scripts/game/GameManager.gd
Normal file
7
scripts/game/GameManager.gd
Normal file
@@ -0,0 +1,7 @@
|
||||
extends Node
|
||||
|
||||
var defaultMapPath: String = "res://scenes/game/maps/world_map.tscn"
|
||||
var currentMapPath: String = ""
|
||||
|
||||
func startNewGame():
|
||||
currentMapPath = defaultMapPath
|
||||
1
scripts/game/GameManager.gd.uid
Normal file
1
scripts/game/GameManager.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bos3psfp6dgdn
|
||||
13
scripts/game/commands/Command.gd
Normal file
13
scripts/game/commands/Command.gd
Normal file
@@ -0,0 +1,13 @@
|
||||
class_name Command
|
||||
|
||||
signal COMMAND_PROCESSED(label)
|
||||
|
||||
var commandLabel
|
||||
|
||||
|
||||
func execute():
|
||||
COMMAND_PROCESSED.emit(commandLabel)
|
||||
|
||||
|
||||
func getCommandText():
|
||||
return commandLabel
|
||||
1
scripts/game/commands/Command.gd.uid
Normal file
1
scripts/game/commands/Command.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b4ee6hsxy7qwc
|
||||
5
scripts/game/commands/CommandDispatcher.gd
Normal file
5
scripts/game/commands/CommandDispatcher.gd
Normal file
@@ -0,0 +1,5 @@
|
||||
extends Node
|
||||
|
||||
signal PROCESS_COMMAND(command)
|
||||
signal WAIT_FOR_COMMAND
|
||||
signal PAUSE_PROCESSOR
|
||||
1
scripts/game/commands/CommandDispatcher.gd.uid
Normal file
1
scripts/game/commands/CommandDispatcher.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://buuwlxv16l2lu
|
||||
10
scripts/game/commands/PassCommand.gd
Normal file
10
scripts/game/commands/PassCommand.gd
Normal file
@@ -0,0 +1,10 @@
|
||||
extends Command
|
||||
|
||||
class_name PassCommand
|
||||
|
||||
func _init() -> void:
|
||||
commandLabel = "Pass"
|
||||
|
||||
func execute():
|
||||
print("Player passed.")
|
||||
COMMAND_PROCESSED.emit(commandLabel)
|
||||
1
scripts/game/commands/PassCommand.gd.uid
Normal file
1
scripts/game/commands/PassCommand.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://1a8ulyob7ld5
|
||||
@@ -8,7 +8,5 @@ class_name GameScreen
|
||||
func _ready() -> void:
|
||||
map.add_child(load(GameManager.currentMapPath).instantiate())
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
func _on_pass_button_pressed() -> void:
|
||||
CommandDispatcher.PROCESS_COMMAND.emit(PassCommand.new())
|
||||
|
||||
@@ -1 +1 @@
|
||||
uid://dq5qq5xj76nwe
|
||||
uid://c0uvlwmkm3r8o
|
||||
|
||||
Reference in New Issue
Block a user