Browse Source

clean up refactoring comments. swish.

main
Stephanie Gredell 4 months ago
parent
commit
e561bd18d5
  1. 1
      src/engine/GameStateMachine.js
  2. 10
      src/engine/events.js
  3. 3
      src/engine/states/BattleState.js
  4. 1
      src/engine/states/DefeatState.js
  5. 3
      src/engine/states/EventState.js
  6. 5
      src/engine/states/MapState.js
  7. 1
      src/engine/states/RelicSelectionState.js
  8. 3
      src/engine/states/RestState.js
  9. 3
      src/engine/states/ShopState.js
  10. 1
      src/engine/states/VictoryState.js
  11. 7
      src/input/InputManager.js
  12. 18
      src/main.js
  13. 4
      src/ui/render-clean.js
  14. 5
      src/ui/render.js

1
src/engine/GameStateMachine.js

@ -1,6 +1,5 @@ @@ -1,6 +1,5 @@
/**
* GameStateMachine - Centralized state management
* Manages all game states and transitions without adding new functionality
*/
export class GameStateMachine {
constructor(gameRoot) {

10
src/engine/events.js

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
export class EventHandler {
constructor(root) {
this.root = root;
this.listeners = new Map(); // Track active listeners for cleanup
this.listeners = new Map();
this.keyHandlers = new Map(); // Track keyboard shortcuts
this.globalHandlers = new Set(); // Track global event handlers
this.currentScreen = null;
@ -41,18 +41,12 @@ export class EventHandler { @@ -41,18 +41,12 @@ export class EventHandler {
this.globalHandlers.add('escape');
}
/**
* Switch to a new screen and cleanup old events
*/
switchScreen(screenName) {
this.cleanup();
this.currentScreen = screenName;
this.keyHandlers.clear();
}
/**
* Add event listener with automatic tracking for cleanup
*/
on(element, event, handler, options = {}) {
if (!element) return;
@ -66,7 +60,6 @@ export class EventHandler { @@ -66,7 +60,6 @@ export class EventHandler {
element.addEventListener(event, wrappedHandler, options);
// Track for cleanup
if (!this.listeners.has(element)) {
this.listeners.set(element, []);
}
@ -188,7 +181,6 @@ export class EventHandler { @@ -188,7 +181,6 @@ export class EventHandler {
this.on(messagesBtn, "click", () => this.showMessagesModal());
}
// Reset button
const resetBtn = this.root.app.querySelector("[data-reset]");
if (resetBtn) {
this.on(resetBtn, "click", async () => {

3
src/engine/states/BattleState.js

@ -12,11 +12,8 @@ export class BattleState extends GameState { @@ -12,11 +12,8 @@ export class BattleState extends GameState {
}
async enter(gameRoot, previousState = null) {
// Set battle flag (preserves existing behavior)
gameRoot._battleInProgress = true;
// If we don't have an enemy yet, we need to create the battle
// This happens when transitioning from map to battle
if (!gameRoot.enemy) {
const node = gameRoot.map.nodes.find(n => n.id === gameRoot.nodeId);
if (node && node.enemy) {

1
src/engine/states/DefeatState.js

@ -11,7 +11,6 @@ export class DefeatState extends GameState { @@ -11,7 +11,6 @@ export class DefeatState extends GameState {
}
async enter(gameRoot, previousState = null) {
// Trigger initial render when entering the state
await gameRoot.render();
}

3
src/engine/states/EventState.js

@ -11,10 +11,7 @@ export class EventState extends GameState { @@ -11,10 +11,7 @@ export class EventState extends GameState {
}
async enter(gameRoot, previousState = null) {
// Save when entering event (preserves existing behavior)
gameRoot.save();
// Trigger initial render when entering the state
await gameRoot.render();
}

5
src/engine/states/MapState.js

@ -11,14 +11,9 @@ export class MapState extends GameState { @@ -11,14 +11,9 @@ export class MapState extends GameState {
}
async enter(gameRoot, previousState = null) {
// Clear battle-specific state when entering map
gameRoot.enemy = null;
gameRoot._battleInProgress = false;
// Save when entering map (preserves existing behavior)
gameRoot.save();
// Trigger initial render when entering the state
await gameRoot.render();
}

1
src/engine/states/RelicSelectionState.js

@ -11,7 +11,6 @@ export class RelicSelectionState extends GameState { @@ -11,7 +11,6 @@ export class RelicSelectionState extends GameState {
}
async enter(gameRoot, previousState = null) {
// Trigger initial render when entering the state
await gameRoot.render();
}

3
src/engine/states/RestState.js

@ -11,10 +11,7 @@ export class RestState extends GameState { @@ -11,10 +11,7 @@ export class RestState extends GameState {
}
async enter(gameRoot, previousState = null) {
// Save when entering rest (preserves existing behavior)
gameRoot.save();
// Trigger initial render when entering the state
await gameRoot.render();
}

3
src/engine/states/ShopState.js

@ -11,10 +11,7 @@ export class ShopState extends GameState { @@ -11,10 +11,7 @@ export class ShopState extends GameState {
}
async enter(gameRoot, previousState = null) {
// Save when entering shop (preserves existing behavior)
gameRoot.save();
// Trigger initial render when entering the state
await gameRoot.render();
}

1
src/engine/states/VictoryState.js

@ -11,7 +11,6 @@ export class VictoryState extends GameState { @@ -11,7 +11,6 @@ export class VictoryState extends GameState {
}
async enter(gameRoot, previousState = null) {
// Trigger initial render when entering the state
await gameRoot.render();
}

7
src/input/InputManager.js

@ -14,7 +14,7 @@ import { RestActionCommand } from '../commands/RestActionCommand.js'; @@ -14,7 +14,7 @@ import { RestActionCommand } from '../commands/RestActionCommand.js';
export class InputManager {
constructor(gameRoot) {
this.root = gameRoot;
this.activeHandlers = new Map(); // Track active event listeners for cleanup
this.activeHandlers = new Map();
this.globalHandlers = new Set(); // Track global document listeners
// Bind methods to preserve 'this' context
@ -138,7 +138,6 @@ export class InputManager { @@ -138,7 +138,6 @@ export class InputManager {
return;
}
// Check for direct data attributes on target (fallback)
if (target.dataset.node !== undefined) {
this.handleMapNodeClick(target, event);
}
@ -393,7 +392,6 @@ export class InputManager { @@ -393,7 +392,6 @@ export class InputManager {
// Execute the callback with selected index
this.root._codeReviewCallback(selectedIndex);
// Clean up state
this.root._codeReviewCards = null;
this.root._codeReviewCallback = null;
} catch (error) {
@ -620,9 +618,6 @@ export class InputManager { @@ -620,9 +618,6 @@ export class InputManager {
}
}
/**
* Clean up all event listeners
*/
cleanup() {
// Remove global listeners
if (this.globalHandlers.has('keydown')) {

18
src/main.js

@ -41,7 +41,6 @@ const root = { @@ -41,7 +41,6 @@ const root = {
if (this.stateMachine) {
await this.stateMachine.render();
} else {
// Fallback for initialization
await renderBattle(this);
}
},
@ -57,11 +56,10 @@ const root = { @@ -57,11 +56,10 @@ const root = {
async go(nextId) {
this.nodeId = nextId; // Always set nodeId (needed for battle logic)
this.nodeId = nextId;
const node = this.map.nodes.find(n => n.id === nextId);
if (!node) return;
// Use state machine for transitions
if (node.kind === "battle" || node.kind === "elite" || node.kind === "boss") {
await this.stateMachine.setState('BATTLE');
} else if (node.kind === "rest") {
@ -125,18 +123,15 @@ const root = { @@ -125,18 +123,15 @@ const root = {
const node = this.map.nodes.find(n => n.id === this.nodeId);
if (node.kind === "boss") {
// Check if there's a next act
const nextAct = this.currentAct === "act1" ? "act2" : null;
if (nextAct && MAPS[nextAct]) {
// Advance to next act
this.currentAct = nextAct;
this.map = MAPS[nextAct];
this.nodeId = this.map.nodes.find(n => n.kind === "start").id;
this.completedNodes = [];
this.log(`Act ${this.currentAct === "act2" ? "II" : "I"} Complete! Advancing to the next challenge...`);
// Save Act 2 checkpoint when first reaching it
if (nextAct === "act2") {
this.saveAct2Checkpoint();
}
@ -144,9 +139,8 @@ const root = { @@ -144,9 +139,8 @@ const root = {
this.save();
await this.stateMachine.setState('MAP');
} else {
// Final victory
this.save(); // Save progress before clearing on victory
this.clearSave(); // Clear save on victory
this.save();
this.clearSave();
await this.stateMachine.setState('VICTORY');
}
}
@ -158,7 +152,7 @@ const root = { @@ -158,7 +152,7 @@ const root = {
async onLose() {
this._battleInProgress = false;
this.clearSave(); // Clear save on defeat
this.clearSave();
await this.stateMachine.setState('DEFEAT');
},
@ -356,14 +350,12 @@ const root = { @@ -356,14 +350,12 @@ const root = {
localStorage.removeItem('birthday-spire-act2-checkpoint'); // Also clear Act 2 checkpoint
},
// Clear any old saves with outdated card IDs
clearOldSaves() {
localStorage.removeItem('birthday-spire-save');
localStorage.removeItem('birthday-spire-act2-checkpoint');
}
};
// Initialize State Machine
try {
root.stateMachine = new GameStateMachine(root);
root.stateMachine.registerState('MAP', new MapState());
@ -412,8 +404,6 @@ async function initializeGame() { @@ -412,8 +404,6 @@ async function initializeGame() {
const screenParam = urlParams.get('screen');
const dev = urlParams.get('dev');
// Check if it's ThePrimeagen's birthday yet (September 9, 2025)
// Skip countdown if ?dev=true is in URL
const now = new Date();
const birthday = new Date('2025-09-09T00:00:00');

4
src/ui/render-clean.js

@ -240,11 +240,9 @@ export async function renderBattleClean(root) { @@ -240,11 +240,9 @@ export async function renderBattleClean(root) {
logContent.scrollTop = logContent.scrollHeight;
}
// Note: Event handling is now managed by EventHandler class
// No addEventListener calls needed here!
}
// Utility functions (kept for compatibility)
// Utility functions
function getRelicArt(relicId, RELICS = null) {
if (RELICS && RELICS[relicId]?.art) {
const imagePath = RELICS[relicId].art;

5
src/ui/render.js

@ -946,11 +946,6 @@ export function renderShop(root) { @@ -946,11 +946,6 @@ export function renderShop(root) {
if (!root.player.gold) root.player.gold = 100;
// Note: Card purchase events are now handled by InputManager
// Note: Shop purchase events are now handled by InputManager
// Note: Leave shop event is handled by InputManager
});
});
}

Loading…
Cancel
Save