about summary refs log tree commit diff stats
path: root/html/story-teller/js/renderer.js
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-12-01 15:00:37 -0500
committerelioat <elioat@tilde.institute>2024-12-01 15:00:37 -0500
commitecff6d35e51ee35dfb34df03acdc9ceb244f7204 (patch)
treefb4411e2ba51b30b728fa720964e673607ece91b /html/story-teller/js/renderer.js
parent1768e1fd0ddc4a059869caa8ff4b08b5734e3982 (diff)
downloadtour-ecff6d35e51ee35dfb34df03acdc9ceb244f7204.tar.gz
*
Diffstat (limited to 'html/story-teller/js/renderer.js')
-rw-r--r--html/story-teller/js/renderer.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/html/story-teller/js/renderer.js b/html/story-teller/js/renderer.js
new file mode 100644
index 0000000..82ab004
--- /dev/null
+++ b/html/story-teller/js/renderer.js
@@ -0,0 +1,44 @@
+import { scenes } from './scenes.js';
+
+export const renderScene = (state, updateState) => {
+  const scene = scenes[state.currentScene];
+  if (!scene) throw new Error(`Scene "${state.currentScene}" not found`);
+
+  const app = document.getElementById('app');
+  app.innerHTML = '';
+
+  const description = document.createElement('p');
+  description.textContent = scene.description;
+  app.appendChild(description);
+
+  const inventory = document.createElement('div');
+  inventory.textContent = `Inventory: ${Array.from(state.inventory).join(', ') || 'None'}`;
+  app.appendChild(inventory);
+
+  const log = document.createElement('div');
+  log.innerHTML = `<strong>Action Log:</strong><br>${state.actionLog.join('<br>')}`;
+  app.appendChild(log);
+
+  scene.choices.forEach((choice) => {
+    if (choice.condition && !choice.condition(state)) {
+      return; // Skip choices that don't meet their conditions
+    }
+
+    const button = document.createElement('button');
+    button.textContent = choice.text;
+    button.onclick = () => {
+      state.actionLog.push(`You chose: "${choice.text}"`);
+      if (choice.action) choice.action(state); // Perform any action tied to the choice
+
+      // Consume items if specified
+      if (choice.consume) {
+        choice.consume.forEach((item) => state.inventory.delete(item));
+        state.actionLog.push(`You used: ${choice.consume.join(', ')}`);
+      }
+
+      if (choice.nextScene) state.currentScene = choice.nextScene;
+      updateState(state); // Re-render after state update
+    };
+    app.appendChild(button);
+  });
+};