End of lesson 18 The command processor, Chapter 5

This commit is contained in:
2025-04-05 19:27:03 -03:00
parent d6ef6289ea
commit 909c385dee
22 changed files with 174 additions and 47 deletions

View 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())

View File

@@ -0,0 +1 @@
uid://dcqqr8b42tn0x

View File

@@ -0,0 +1,7 @@
extends Node
var defaultMapPath: String = "res://scenes/game/maps/world_map.tscn"
var currentMapPath: String = ""
func startNewGame():
currentMapPath = defaultMapPath

View File

@@ -0,0 +1 @@
uid://bos3psfp6dgdn

View File

@@ -0,0 +1,13 @@
class_name Command
signal COMMAND_PROCESSED(label)
var commandLabel
func execute():
COMMAND_PROCESSED.emit(commandLabel)
func getCommandText():
return commandLabel

View File

@@ -0,0 +1 @@
uid://b4ee6hsxy7qwc

View File

@@ -0,0 +1,5 @@
extends Node
signal PROCESS_COMMAND(command)
signal WAIT_FOR_COMMAND
signal PAUSE_PROCESSOR

View File

@@ -0,0 +1 @@
uid://buuwlxv16l2lu

View File

@@ -0,0 +1,10 @@
extends Command
class_name PassCommand
func _init() -> void:
commandLabel = "Pass"
func execute():
print("Player passed.")
COMMAND_PROCESSED.emit(commandLabel)

View File

@@ -0,0 +1 @@
uid://1a8ulyob7ld5

View File

@@ -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())

View File

@@ -1 +1 @@
uid://dq5qq5xj76nwe
uid://c0uvlwmkm3r8o