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 @@
/** /**
* GameStateMachine - Centralized state management * GameStateMachine - Centralized state management
* Manages all game states and transitions without adding new functionality
*/ */
export class GameStateMachine { export class GameStateMachine {
constructor(gameRoot) { constructor(gameRoot) {

10
src/engine/events.js

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

3
src/engine/states/BattleState.js

@ -12,11 +12,8 @@ export class BattleState extends GameState {
} }
async enter(gameRoot, previousState = null) { async enter(gameRoot, previousState = null) {
// Set battle flag (preserves existing behavior)
gameRoot._battleInProgress = true; 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) { if (!gameRoot.enemy) {
const node = gameRoot.map.nodes.find(n => n.id === gameRoot.nodeId); const node = gameRoot.map.nodes.find(n => n.id === gameRoot.nodeId);
if (node && node.enemy) { if (node && node.enemy) {

1
src/engine/states/DefeatState.js

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

3
src/engine/states/EventState.js

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

5
src/engine/states/MapState.js

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

1
src/engine/states/RelicSelectionState.js

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

3
src/engine/states/RestState.js

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

3
src/engine/states/ShopState.js

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

1
src/engine/states/VictoryState.js

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

7
src/input/InputManager.js

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

18
src/main.js

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

4
src/ui/render-clean.js

@ -240,11 +240,9 @@ export async function renderBattleClean(root) {
logContent.scrollTop = logContent.scrollHeight; 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) { function getRelicArt(relicId, RELICS = null) {
if (RELICS && RELICS[relicId]?.art) { if (RELICS && RELICS[relicId]?.art) {
const imagePath = RELICS[relicId].art; const imagePath = RELICS[relicId].art;

5
src/ui/render.js

@ -946,11 +946,6 @@ export function renderShop(root) {
if (!root.player.gold) root.player.gold = 100; 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