// LovelyScript Engine Core class LovelyScriptEngine { constructor(previewContainer) { this.previewContainer = previewContainer; this.characters = {}; this.currentScene = null; this.background = null; this.state = { variables: {}, flags: {} }; } parseScript(script) { const lines = script.split('\n'); const scenes = {}; let currentScene = null; for (const line of lines) { const trimmed = line.trim(); if (!trimmed || trimmed.startsWith('#')) continue; // Character definition if (trimmed.startsWith('character')) { const match = trimmed.match(/character\s+"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"/); if (match) { this.characters[match[1]] = { displayName: match[2], color: match[3], expressions: {} }; } continue; } // Background definition if (trimmed.startsWith('background')) { const match = trimmed.match(/background\s+"([^"]+)"/); if (match) { this.background = match[1]; } continue; } // Scene definition if (trimmed.startsWith('scene')) { const sceneName = trimmed.split(/\s+/)[1]; scenes[sceneName] = []; currentScene = sceneName; continue; } // Dialogue if (trimmed.match(/^".*"\s+".*"$/)) { const match = trimmed.match(/^"([^"]+)"\s+"([^"]+)"$/); if (match && currentScene) { scenes[currentScene].push({ type: 'dialogue', character: match[1], text: match[2] }); } continue; } // Show character if (trimmed.startsWith('show')) { const match = trimmed.match(/show\s+([^\s]+)\s+([^\s]+)/); if (match && currentScene) { scenes[currentScene].push({ type: 'show', character: match[1], expression: match[2] }); } continue; } // Menu if (trimmed.startsWith('menu')) { const menu = { type: 'menu', options: [] }; let i = lines.indexOf(line) + 1; while (i < lines.length) { const menuLine = lines[i].trim(); if (menuLine.startsWith('"') && menuLine.includes(':')) { const optionMatch = menuLine.match(/^"([^"]+)"\s*:/); if (optionMatch) { const optionText = optionMatch[1]; let actions = []; let actionLines = []; // Collect all action lines for this option let j = i + 1; while (j < lines.length && lines[j].trim() !== '') { actionLines.push(lines[j].trim()); j++; } // Parse action lines for (const actionLine of actionLines) { if (actionLine.match(/^".*"\s+".*"$/)) { const dialogueMatch = actionLine.match(/^"([^"]+)"\s+"([^"]+)"$/); if (dialogueMatch) { actions.push({ type: 'dialogue', character: dialogueMatch[1], text: dialogueMatch[2] }); } } else if (actionLine.startsWith('show')) { const showMatch = actionLine.match(/show\s+([^\s]+)\s+([^\s]+)/); if (showMatch) { actions.push({ type: 'show', character: showMatch[1], expression: showMatch[2] }); } } else if (actionLine.startsWith('jump')) { const jumpMatch = actionLine.match(/jump\s+([^\s]+)/); if (jumpMatch) { actions.push({ type: 'jump', scene: jumpMatch[1] }); } } } menu.options.push({ text: optionText, actions: actions }); i = j; } } else if (menuLine === '') { break; } i++; } if (currentScene) { scenes[currentScene].push(menu); } continue; } // Jump if (trimmed.startsWith('jump')) { const sceneName = trimmed.split(/\s+/)[1]; if (currentScene) { scenes[currentScene].push({ type: 'jump', scene: sceneName }); } continue; } // End if (trimmed === 'end') { if (currentScene) { scenes[currentScene].push({ type: 'end' }); } continue; } } return scenes; } execute(scenes, startScene = 'start') { this.scenes = scenes; this.currentScene = startScene; this.executeScene(startScene); } async executeScene(sceneName) { const scene = this.scenes[sceneName]; if (!scene) return; for (const command of scene) { switch (command.type) { case 'show': await this.showCharacter(command.character, command.expression); break; case 'dialogue': await this.displayDialogue(command.character, command.text); break; case 'menu': const selectedOption = await this.displayMenu(command.options); if (selectedOption !== null) { for (const action of selectedOption.actions) { switch (action.type) { case 'show': await this.showCharacter(action.character, action.expression); break; case 'dialogue': await this.displayDialogue(action.character, action.text); break; case 'jump': await this.executeScene(action.scene); return; } } } break; case 'jump': await this.executeScene(command.scene); return; case 'end': await this.displayEnd(); return; } } } clearDisplay() { this.previewContainer.innerHTML = ''; } showCharacter(character, expression) { return new Promise((resolve) => { const characterData = this.characters[character]; if (!characterData) return resolve(); let sprite = this.previewContainer.querySelector(`.character-sprite[data-character="${character}"]`); if (!sprite) { sprite = document.createElement('div'); sprite.className = `character-sprite character-enter`; sprite.dataset.character = character; sprite.style.left = '50%'; sprite.style.transform = 'translateX(-50%)'; this.previewContainer.appendChild(sprite); } // In a real implementation, we'd set the actual sprite image sprite.textContent = `${character} (${expression})`; sprite.style.color = characterData.color; setTimeout(resolve, 300); }); } displayDialogue(character, text) { return new Promise((resolve) => { let dialogueBox = this.previewContainer.querySelector('.dialogue-box'); if (!dialogueBox) { dialogueBox = document.createElement('div'); dialogueBox.className = 'dialogue-box'; this.previewContainer.appendChild(dialogueBox); } const characterData = this.characters[character]; const nameColor = characterData ? characterData.color : '#db2777'; dialogueBox.innerHTML = `
Thank you for playing!