about summary refs log tree commit diff stats
path: root/js/puzzle-dungeon
diff options
context:
space:
mode:
Diffstat (limited to 'js/puzzle-dungeon')
-rw-r--r--js/puzzle-dungeon/commandHandler.js28
-rw-r--r--js/puzzle-dungeon/game.js1
-rw-r--r--js/puzzle-dungeon/index.html7
-rw-r--r--js/puzzle-dungeon/parser.js10
4 files changed, 43 insertions, 3 deletions
diff --git a/js/puzzle-dungeon/commandHandler.js b/js/puzzle-dungeon/commandHandler.js
new file mode 100644
index 0000000..12140c2
--- /dev/null
+++ b/js/puzzle-dungeon/commandHandler.js
@@ -0,0 +1,28 @@
+const createCommandHandler = () => {
+    const commands = {};
+
+    const registerCommand = (name, callback) => {
+        commands[name] = callback;
+    };
+
+    const executeCommand = (parsedCommand) => {
+        const { command, args } = parsedCommand;
+        if (commands[command]) {
+            commands[command](...args);
+        } else {
+            console.error(`Unknown command: ${command}`);
+        }
+    };
+
+    const executeCommands = (parsedCommands) => {
+        parsedCommands.forEach(cmd => executeCommand(cmd));
+    };
+
+    return {
+        registerCommand,
+        executeCommand,
+        executeCommands
+    };
+};
+
+export default createCommandHandler;
diff --git a/js/puzzle-dungeon/game.js b/js/puzzle-dungeon/game.js
index e5a0fc4..f742844 100644
--- a/js/puzzle-dungeon/game.js
+++ b/js/puzzle-dungeon/game.js
@@ -1,4 +1,3 @@
-// game.js
 const canvas = document.getElementById('gameCanvas');
 const ctx = canvas.getContext('2d');
 const cellSize = canvas.width / 10;
diff --git a/js/puzzle-dungeon/index.html b/js/puzzle-dungeon/index.html
index a238af3..ed13d3d 100644
--- a/js/puzzle-dungeon/index.html
+++ b/js/puzzle-dungeon/index.html
@@ -15,7 +15,10 @@
         <textarea id="commands" rows="10" cols="30"></textarea><br>
         <button type="submit">Submit</button>
     </form>
-
-    <script src="game.js"></script>
+    <script type="module">
+    import './parser.js';
+    import './commandHandler.js';
+    import './game.js';
+    </script>
 </body>
 </html>
diff --git a/js/puzzle-dungeon/parser.js b/js/puzzle-dungeon/parser.js
new file mode 100644
index 0000000..ec7f33f
--- /dev/null
+++ b/js/puzzle-dungeon/parser.js
@@ -0,0 +1,10 @@
+export function parseCommands(input) {
+    const commands = input.split('\n').map(cmd => cmd.trim()).filter(cmd => cmd.length > 0);
+    return commands.map(command => {
+        const parts = command.split(' ');
+        return {
+            command: parts[0],
+            args: parts.slice(1)
+        };
+    });
+}