GabHtml's picture
Deve ser funcional, e em arquivos separados o css js e html
7e144db verified
Raw History Blame Contribute Delete
13.5 kB
// 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();
});