Skip to content

Commit 1b24727

Browse files
refactor
1 parent 8ecdf28 commit 1b24727

File tree

9 files changed

+512
-354
lines changed

9 files changed

+512
-354
lines changed

js/CanvasManager.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class CanvasManager {
2+
constructor(canvasId, virtualWidth, virtualHeight) {
3+
this.canvas = document.getElementById(canvasId);
4+
this.ctx = this.canvas.getContext('2d');
5+
this.virtualWidth = virtualWidth;
6+
this.virtualHeight = virtualHeight;
7+
this.scale = 1;
8+
this.offsetX = 0;
9+
this.offsetY = 0;
10+
11+
this.setupCanvas();
12+
this.bindEvents();
13+
}
14+
15+
setupCanvas() {
16+
const displayWidth = window.innerWidth;
17+
const displayHeight = window.innerHeight;
18+
this.scale = Math.min(displayWidth / this.virtualWidth, displayHeight / this.virtualHeight);
19+
this.canvas.width = displayWidth;
20+
this.canvas.height = displayHeight;
21+
this.offsetX = (displayWidth - (this.virtualWidth * this.scale)) / 2;
22+
this.offsetY = (displayHeight - (this.virtualHeight * this.scale)) / 2;
23+
this.applyTransform();
24+
}
25+
26+
applyTransform() {
27+
this.ctx.setTransform(this.scale, 0, 0, this.scale, this.offsetX, this.offsetY);
28+
}
29+
30+
clearScreen() {
31+
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
32+
this.ctx.fillStyle = '#000000';
33+
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
34+
this.applyTransform();
35+
}
36+
37+
bindEvents() {
38+
let resizeTimeout;
39+
window.addEventListener('resize', () => {
40+
clearTimeout(resizeTimeout);
41+
resizeTimeout = setTimeout(() => {
42+
this.setupCanvas();
43+
}, 100);
44+
});
45+
}
46+
47+
getContext() {
48+
return this.ctx;
49+
}
50+
}
51+
52+
export default CanvasManager;

js/InputManager.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class InputManager {
2+
constructor() {
3+
this.handlers = new Map();
4+
this.currentScreen = null;
5+
this.debugHandler = null;
6+
7+
window.addEventListener('keydown', (e) => this.handleKeyDown(e));
8+
}
9+
10+
setCurrentScreen(screenName) {
11+
this.currentScreen = screenName;
12+
}
13+
14+
registerScreen(screenName, handler) {
15+
this.handlers.set(screenName, handler);
16+
}
17+
18+
setDebugHandler(callback) {
19+
this.debugHandler = callback;
20+
}
21+
22+
handleKeyDown(e) {
23+
// Handle debug toggle
24+
if (e.key === 'd' || e.key === 'D') {
25+
if (this.debugHandler) {
26+
this.debugHandler();
27+
}
28+
return;
29+
}
30+
31+
// Handle screen-specific input
32+
if (this.currentScreen && this.handlers.has(this.currentScreen)) {
33+
const handler = this.handlers.get(this.currentScreen);
34+
const nextScreen = handler(e.key);
35+
if (nextScreen) {
36+
return nextScreen;
37+
}
38+
}
39+
}
40+
}
41+
42+
export default InputManager;

js/PatternFormation.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class PatternFormation {
113113
this.diveCurveIntensity = 0.8; // How much they curve toward player
114114
this.currentDiveChance = this.baseDiveChance;
115115
this.currentDiveSpeed = this.diveSpeed;
116+
117+
this.onPointsScored = options.onPointsScored || (() => {});
116118
}
117119

118120
applyDifficultyModifiers() {
@@ -453,11 +455,8 @@ class PatternFormation {
453455
const pointsMultiplier = this.difficulty * (1 + (this.initialAlienCount - this.aliens.length) * 0.1);
454456
const points = Math.floor(this.pointsBase * pointsMultiplier);
455457

456-
// Add points to game score using window.game
457-
if (window.game) {
458-
console.log('Alien destroyed, adding points:', points); // Debug log
459-
window.game.addPoints(points);
460-
}
458+
// Replace direct call with callback
459+
this.onPointsScored(points);
461460
return true;
462461
}
463462
}

js/backgroundScroller.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)