From ecff6d35e51ee35dfb34df03acdc9ceb244f7204 Mon Sep 17 00:00:00 2001 From: elioat Date: Sun, 1 Dec 2024 15:00:37 -0500 Subject: * --- html/story-teller/js/game.js | 47 ++++++++++++++++++++++++++++++++++++++++ html/story-teller/js/renderer.js | 44 +++++++++++++++++++++++++++++++++++++ html/story-teller/js/scenes.js | 37 +++++++++++++++++++++++++++++++ html/story-teller/js/state.js | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 html/story-teller/js/game.js create mode 100644 html/story-teller/js/renderer.js create mode 100644 html/story-teller/js/scenes.js create mode 100644 html/story-teller/js/state.js (limited to 'html/story-teller/js') diff --git a/html/story-teller/js/game.js b/html/story-teller/js/game.js new file mode 100644 index 0000000..d3e8144 --- /dev/null +++ b/html/story-teller/js/game.js @@ -0,0 +1,47 @@ +import { createGameState, saveGameState, loadGameState, resetGameState } from './state.js'; +import { renderScene } from './renderer.js'; + +let gameState = loadGameState(); // Load state on initialization + +const updateAndRender = (newState) => { + Object.assign(gameState, newState); + saveGameState(gameState); // Save automatically after each update + renderScene(gameState, updateAndRender); +}; + +document.addEventListener('DOMContentLoaded', () => { + const app = document.getElementById('app'); + + app.innerHTML = ''; + + const controls = document.createElement('div'); + controls.id = 'controls'; + + const saveButton = document.createElement('button'); + saveButton.textContent = 'Save Progress'; + saveButton.onclick = () => saveGameState(gameState); + + const loadButton = document.createElement('button'); + loadButton.textContent = 'Load Progress'; + loadButton.onclick = () => { + const loadedState = loadGameState(); + updateAndRender(loadedState); + }; + + const resetButton = document.createElement('button'); + resetButton.textContent = 'Reset Game'; + resetButton.onclick = () => { + if (confirm('Are you sure you want to reset the game? This will erase all progress.')) { + gameState = resetGameState(); // Clear state and reset game + updateAndRender(gameState); + } + }; + + controls.appendChild(saveButton); + controls.appendChild(loadButton); + controls.appendChild(resetButton); + + app.appendChild(controls); + + updateAndRender(gameState); +}); 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 = `Action Log:
${state.actionLog.join('
')}`; + 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); + }); +}; diff --git a/html/story-teller/js/scenes.js b/html/story-teller/js/scenes.js new file mode 100644 index 0000000..7b8db35 --- /dev/null +++ b/html/story-teller/js/scenes.js @@ -0,0 +1,37 @@ +export const scenes = { + start: { + description: "You are standing in a dark cave. There's a faint glimmer in the distance, and a torch on the ground.", + choices: [ + { text: "Explore further", nextScene: "deepCave" }, + { text: "Pick up the torch", action: (state) => state.inventory.add("torch") }, + { + text: "Use a torch to illuminate the cave", + nextScene: "litCave", + condition: (state) => state.inventory.has("torch"), + consume: ["torch"] + }, + ], + }, + deepCave: { + description: "The darkness becomes overwhelming. You can't proceed further.", + choices: [ + { text: "Go back", nextScene: "start" }, + ], + }, + litCave: { + description: "With the torchlight, you see glittering gemstones embedded in the walls.", + choices: [ + { text: "Pick up the key", action: (state) => state.inventory.add("key") }, + { text: "Collect a gemstone", action: (state) => state.inventory.add("gemstone") }, + { text: "Go back", nextScene: "start" }, + { text: "Go deeper into the cave", nextScene: "treasureRoom", condition: (state) => state.inventory.has("key") }, + ], + }, + treasureRoom: { + description: "You find a room filled with treasure. You are rich beyond your wildest dreams.", + choices: [ + { text: "Swim in the treasure", nextScene: "treasureRoom" }, + ], + }, + }; + \ No newline at end of file diff --git a/html/story-teller/js/state.js b/html/story-teller/js/state.js new file mode 100644 index 0000000..e6c4fe7 --- /dev/null +++ b/html/story-teller/js/state.js @@ -0,0 +1,43 @@ +export const createGameState = () => ({ + currentScene: 'start', + inventory: new Set(), + actionLog: [], + collectedItems: new Set(), + }); + + + +export const updateGameState = (state, updates) => ({ + ...state, + ...updates, +}); + +const STORAGE_KEY = 'gameState'; + +export const saveGameState = (state) => { + const stateToSave = { + currentScene: state.currentScene, + inventory: Array.from(state.inventory), + actionLog: state.actionLog, + }; + localStorage.setItem(STORAGE_KEY, JSON.stringify(stateToSave)); +}; + +export const loadGameState = () => { + const savedState = localStorage.getItem(STORAGE_KEY); + if (savedState) { + const parsedState = JSON.parse(savedState); + return { + currentScene: parsedState.currentScene, + inventory: new Set(parsedState.inventory), + actionLog: parsedState.actionLog, + }; + } + return createGameState(); +}; + +export const resetGameState = () => { + localStorage.removeItem(STORAGE_KEY); + return createGameState(); + }; + \ No newline at end of file -- cgit 1.4.1-2-gfad0 ='n126' href='#n126'>126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376