class SakuraScriptEngine { constructor() { this.state = { variables: {}, currentScene: null, sceneStack: [], characters: {}, backgrounds: {}, audio: {}, saveSlots: Array(5).fill(null) }; this.initElements(); this.bindEvents(); this.loadSampleScript(); } initElements() { this.elements = { background: document.getElementById('background-image'), charLeft: document.getElementById('char-left'), charRight: document.getElementById('char-right'), textDisplay: document.getElementById('text-display'), nameplate: document.getElementById('nameplate'), nextIndicator: document.getElementById('next-indicator'), choiceContainer: document.getElementById('choice-container'), choices: document.getElementById('choices'), menuButton: document.getElementById('menu-button'), menuPanel: document.getElementById('menu-panel') }; } bindEvents() { // Text box click to advance this.elements.textboxContainer.addEventListener('click', () => this.nextAction()); // Menu events this.elements.menuButton.addEventListener('click', () => this.toggleMenu()); this.elements.closeMenu.addEventListener('click', () => this.toggleMenu()); this.elements.saveBtn.addEventListener('click', () => this.showSaveMenu()); this.elements.loadBtn.addEventListener('click', () => this.showLoadMenu()); // Keyboard controls document.addEventListener('keydown', (e) => { if (e.key === 'Enter' || e.key === ' ') { this.nextAction(); } else if (e.key === 'Escape') { this.toggleMenu(); } }); } loadSampleScript() { // This would normally load from an external file const sampleScript = ` # Sample SakuraScript @characters { "protagonist": { "name": "Haru", "sprites": { "happy": "http://static.photos/people/640x360/1", "sad": "http://static.photos/people/640x360/2" } }, "heroine": { "name": "Sakura", "sprites": { "happy": "http://static.photos/people/640x360/3", "angry": "http://static.photos/people/640x360/4" } } } @backgrounds { "park": "http://static.photos/nature/1200x630/10", "school": "http://static.photos/cityscape/1200x630/5" } @start scene1 @scene scene1 { background: park play music: "bgm1.mp3" show protagonist happy left show heroine happy right protagonist "Hello, Sakura! Nice weather today, isn't it?" heroine "Yes, Haru! It's perfect for a walk in the park." menu { "Should we go to the café?" -> scene2 "Let's stay here a bit longer" -> scene3 } } @scene scene2 { background: school hide protagonist heroine "The café sounds lovely! I heard they have new desserts." protagonist happy left "Great! Let's go then." jump scene4 } @scene scene3 { background: park hide heroine protagonist "I'd like to enjoy this view a little more." heroine angry right "Fine... but I'm getting hungry!" jump scene4 } @scene scene4 { background: school show protagonist happy left show heroine happy right protagonist "Thanks for today, Sakura!" heroine "Let's do this again soon!" end } `; this.parseScript(sampleScript); this.startGame(); } parseScript(script) { // This would parse the SakuraScript DSL // For now, we'll just set up some dummy data this.state.characters = { protagonist: { name: "Haru", sprites: { happy: "http://static.photos/people/640x360/1", sad: "http://static.photos/people/640x360/2" } }, heroine: { name: "Sakura", sprites: { happy: "http://static.photos/people/640x360/3", angry: "http://static.photos/people/640x360/4" } } }; this.state.backgrounds = { park: "http://static.photos/nature/1200x630/10", school: "http://static.photos/cityscape/1200x630/5" }; this.state.scenes = { scene1: { background: "park", commands: [ { type: "show", character: "protagonist", sprite: "happy", position: "left" }, { type: "show", character: "heroine", sprite: "happy", position: "right" }, { type: "dialogue", character: "protagonist", text: "Hello, Sakura! Nice weather today, isn't it?" }, { type: "dialogue", character: "heroine", text: "Yes, Haru! It's perfect for a walk in the park." }, { type: "menu", options: [ { text: "Should we go to the café?", target: "scene2" }, { text: "Let's stay here a bit longer", target: "scene3" } ]} ] }, scene2: { background: "school", commands: [ { type: "hide", character: "protagonist" }, { type: "dialogue", character: "heroine", text: "The café sounds lovely! I heard they have new desserts." }, { type: "show", character: "protagonist", sprite: "happy", position: "left" }, { type: "dialogue", character: "protagonist", text: "Great! Let's go then." }, { type: "jump", target: "scene4" } ] }, scene3: { background: "park", commands: [ { type: "hide", character: "heroine" }, { type: "dialogue", character: "protagonist", text: "I'd like to enjoy this view a little more." }, { type: "show", character: "heroine", sprite: "angry", position: "right" }, { type: "dialogue", character: "heroine", text: "Fine... but I'm getting hungry!" }, { type: "jump", target: "scene4" } ] }, scene4: { background: "school", commands: [ { type: "show", character: "protagonist", sprite: "happy", position: "left" }, { type: "show", character: "heroine", sprite: "happy", position: "right" }, { type: "dialogue", character: "protagonist", text: "Thanks for today, Sakura!" }, { type: "dialogue", character: "heroine", text: "Let's do this again soon!" }, { type: "end" } ] } }; } startGame() { this.state.currentScene = "scene1"; this.state.sceneStack = []; this.state.commandIndex = 0; this.executeScene(); } executeScene() { const scene = this.state.scenes[this.state.currentScene]; if (!scene) { console.error(`Scene ${this.state.currentScene} not found!`); return; } // Set background this.setBackground(scene.background); // Execute commands this.state.commandIndex = 0; this.executeNextCommand(); } executeNextCommand() { const scene = this.state.scenes[this.state.currentScene]; if (this.state.commandIndex >= scene.commands.length) { // Scene ended return; } const command = scene.commands[this.state.commandIndex]; this.state.commandIndex++; switch (command.type) { case "show": this.showCharacter(command.character, command.sprite, command.position); this.executeNextCommand(); break; case "hide": this.hideCharacter(command.character); this.executeNextCommand(); break; case "dialogue": this.showDialogue(command.character, command.text); break; case "menu": this.showMenu(command.options); break; case "jump": this.jumpToScene(command.target); break; case "end": this.endGame(); break; default: console.error(`Unknown command type: ${command.type}`); this.executeNextCommand(); } } setBackground(bgKey) { const bgUrl = this.state.backgrounds[bgKey]; if (bgUrl) { this.elements.background.src = bgUrl; this.elements.background.style.opacity = 1; } } showCharacter(charKey, spriteKey, position) { const character = this.state.characters[charKey]; if (!character) return; const spriteUrl = character.sprites[spriteKey]; if (!spriteUrl) return; const element = position === 'left' ? this.elements.charLeft : this.elements.charRight; element.src = spriteUrl; element.style.opacity = 1; element.classList.add('char-enter'); } hideCharacter(charKey) { const character = this.state.characters[charKey]; if (!character) return; // In a real implementation, we'd track which character is on which side // For now, we'll just hide both and let the next show command handle it this.elements.charLeft.style.opacity = 0; this.elements.charRight.style.opacity = 0; } showDialogue(charKey, text) { const character = this.state.characters[charKey]; this.elements.nameplate.textContent = character ? character.name : ""; this.elements.nameplate.style.display = character ? "block" : "none"; this.typeText(text, () => { this.elements.nextIndicator.classList.remove('hidden'); }); } typeText(text, onComplete) { this.elements.textDisplay.textContent = ''; this.elements.textDisplay.classList.add('typing'); let i = 0; const typing = () => { if (i < text.length) { this.elements.textDisplay.textContent += text.charAt(i); i++; setTimeout(typing, 20); } else { this.elements.textDisplay.classList.remove('typing'); onComplete(); } }; typing(); } showMenu(options) { this.elements.choices.innerHTML = ''; options.forEach((option, index) => { const button = document.createElement('button'); button.className = 'choice-btn w-full bg-pink-100 hover:bg-pink-200 text-pink-700 py-3 px-4 rounded-lg text-left'; button.textContent = option.text; button.addEventListener('click', () => { this.selectChoice(option.target); }); this.elements.choices.appendChild(button); }); this.elements.textboxContainer.classList.add('hidden'); this.elements.choiceContainer.classList.remove('hidden'); } selectChoice(target) { this.elements.choiceContainer.classList.add('hidden'); this.elements.textboxContainer.classList.remove('hidden'); this.jumpToScene(target); } jumpToScene(scene) { this.state.sceneStack.push({ scene: this.state.currentScene, commandIndex: this.state.commandIndex }); this.state.currentScene = scene; this.state.commandIndex = 0; this.executeScene(); } nextAction() { if (this.elements.textDisplay.classList.contains('typing')) { // Skip typing animation const text = this.elements.textDisplay.textContent; this.elements.textDisplay.textContent = text; this.elements.textDisplay.classList.remove('typing'); clearTimeout(this.typeTimeout); this.elements.nextIndicator.classList.remove('hidden'); return; } this.elements.nextIndicator.classList.add('hidden'); this.executeNextCommand(); } toggleMenu() { this.elements.menuPanel.classList.toggle('hidden'); } showSaveMenu() { console.log("Save menu would open here"); this.toggleMenu(); } showLoadMenu() { console.log("Load menu would open here"); this.toggleMenu(); } endGame() { this.elements.textDisplay.textContent = "The End"; this.elements.nameplate.style.display = "none"; this.elements.nextIndicator.classList.add('hidden'); }