File size: 13,727 Bytes
f8ae288 53a7a09 | 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 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 377 378 | 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');
}
|