File size: 13,485 Bytes
7e144db | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 | // 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 = `
<div class="font-bold" style="color: ${nameColor}">${character}</div>
<div class="mt-2">${text}</div>
<button class="mt-4 ml-auto block px-3 py-1 bg-pink-600 text-white rounded hover:bg-pink-700 transition" id="continue-btn">
Continue
</button>
`;
dialogueBox.querySelector('#continue-btn').addEventListener('click', () => {
resolve();
}, { once: true });
});
}
displayMenu(options) {
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);
}
dialogueBox.innerHTML = `
<div class="font-bold text-pink-800">Make a choice:</div>
<div class="menu-options" id="menu-options"></div>
`;
const optionsContainer = dialogueBox.querySelector('#menu-options');
options.forEach((option, index) => {
const optionElement = document.createElement('div');
optionElement.className = 'menu-option';
optionElement.textContent = option.text;
optionElement.addEventListener('click', () => {
resolve(option);
});
optionsContainer.appendChild(optionElement);
});
});
}
displayEnd() {
return new Promise((resolve) => {
let dialogueBox = this.previewContainer.querySelector('.dialogue-box') ||
document.createElement('div');
dialogueBox.className = 'dialogue-box';
dialogueBox.innerHTML = `
<div class="text-center py-8">
<h3 class="text-xl font-bold text-pink-800 mb-2">The End</h3>
<p class="mb-4">Thank you for playing!</p>
<button class="px-4 py-2 bg-pink-600 text-white rounded hover:bg-pink-700 transition" id="restart-btn">
Play Again
</button>
</div>
`;
this.previewContainer.appendChild(dialogueBox);
dialogueBox.querySelector('#restart-btn').addEventListener('click', () => {
this.execute(this.scenes);
resolve();
}, { once: true });
});
}
}
// Initialize the engine when page loads
document.addEventListener('DOMContentLoaded', () => {
const previewContainer = document.querySelector('preview-window').shadowRoot.getElementById('preview-container');
const engine = new LovelyScriptEngine(previewContainer);
const editor = document.getElementById('script-editor');
// Run button functionality
document.getElementById('run-button').addEventListener('click', () => {
const script = editor.value;
const scenes = engine.parseScript(script);
engine.execute(scenes);
});
// Initialize Feather Icons
feather.replace();
}); |