Add choice-based events with a resolution modal
Standard/kaos events can now present 2 options instead of a fixed effect, pausing new event triggers until the player answers. Adds 4 choice events as a first batch; resources keep accruing while a choice is pending. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
0efbe9d55b
commit
b9561b05a5
@@ -2,7 +2,7 @@ import type { State } from './engine.ts';
|
||||
import { UPGRADES } from './upgrades.ts';
|
||||
import { VERSION, CHANGELOG } from './data/version.ts';
|
||||
|
||||
export function initUI(app: HTMLElement, onUpgrade: (id: string) => void, onRestart: () => void): void {
|
||||
export function initUI(app: HTMLElement, onUpgrade: (id: string) => void, onRestart: () => void, onEventChoice: (index: number) => void): void {
|
||||
app.innerHTML = `
|
||||
<div id="header">
|
||||
<div class="brand">DJUPET</div>
|
||||
@@ -120,10 +120,24 @@ export function initUI(app: HTMLElement, onUpgrade: (id: string) => void, onRest
|
||||
<div class="overlay-body" id="overlay-body"></div>
|
||||
<button class="overlay-btn" id="overlay-btn">Börja om</button>
|
||||
</div>
|
||||
|
||||
<div id="event-modal" class="help-modal">
|
||||
<div class="help-modal-inner" id="event-modal-inner">
|
||||
<div class="help-modal-body">
|
||||
<div class="event-modal-text" id="event-modal-text"></div>
|
||||
<div class="event-modal-choices" id="event-modal-choices"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.getElementById('overlay-btn')!.addEventListener('click', onRestart);
|
||||
|
||||
document.getElementById('event-modal-choices')!.addEventListener('click', e => {
|
||||
const btn = (e.target as Element).closest('[data-choice]') as HTMLElement | null;
|
||||
if (btn?.dataset['choice'] !== undefined) onEventChoice(Number(btn.dataset['choice']));
|
||||
});
|
||||
|
||||
const helpModal = document.getElementById('help-modal')!;
|
||||
const helpInner = document.getElementById('help-modal-inner')!;
|
||||
document.getElementById('help-btn')!.addEventListener('click', () => helpModal.classList.toggle('visible'));
|
||||
@@ -198,6 +212,31 @@ export function renderState(state: State): void {
|
||||
|
||||
// Overlay
|
||||
if (state.phase !== 'playing') showOverlay(state);
|
||||
|
||||
// Event choice modal
|
||||
renderEventModal(state);
|
||||
}
|
||||
|
||||
let lastPendingEventId: string | null = null;
|
||||
|
||||
function renderEventModal(state: State): void {
|
||||
const modal = document.getElementById('event-modal')!;
|
||||
const pending = state.pendingEvent;
|
||||
const id = pending?.event.id ?? null;
|
||||
if (id === lastPendingEventId) return;
|
||||
lastPendingEventId = id;
|
||||
|
||||
if (!pending) {
|
||||
modal.classList.remove('visible');
|
||||
return;
|
||||
}
|
||||
|
||||
document.getElementById('event-modal-text')!.textContent = pending.event.text;
|
||||
const choices = document.getElementById('event-modal-choices')!;
|
||||
choices.innerHTML = (pending.event.choices ?? []).map((c, i) =>
|
||||
`<button class="upgrade-btn" data-choice="${i}"><span class="up-name">${c.text}</span></button>`
|
||||
).join('');
|
||||
modal.classList.add('visible');
|
||||
}
|
||||
|
||||
let lastUpgradeKey = '';
|
||||
|
||||
Reference in New Issue
Block a user