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
379
380
381
|
/**
* Rendering Module
*
* This module handles all game rendering operations using HTML5 Canvas.
* Demonstrates key game development patterns:
* 1. Layer-based rendering
* 2. Particle systems
* 3. Visual state feedback
* 4. Canvas state management
*
* @module renderer
*/
/**
* Renders the game grid with path and hover previews
* Implements visual feedback for player actions
*
* @param {CanvasRenderingContext2D} ctx - Canvas rendering context
* @param {Array<Array<string>>} grid - Game grid state
*/
function renderGrid(ctx, grid) {
const cellSize = canvas.width / 20;
// Draw grid lines for visual reference
ctx.strokeStyle = '#ccc';
ctx.lineWidth = 1;
for (let i = 0; i <= 20; i++) {
// Vertical lines
ctx.beginPath();
ctx.moveTo(i * cellSize, 0);
ctx.lineTo(i * cellSize, canvas.height);
ctx.stroke();
// Horizontal lines
ctx.beginPath();
ctx.moveTo(0, i * cellSize);
ctx.lineTo(canvas.width, i * cellSize);
ctx.stroke();
}
// Render grid cells with path highlighting
grid.forEach((row, y) => {
row.forEach((cell, x) => {
if (cell === 'path') {
ctx.fillStyle = '#f4a460';
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
});
});
// Render tower placement preview
if (gameState.phase === GamePhase.PLACEMENT && draggedTowerType && hoverCell) {
const tower = TowerTypes[draggedTowerType];
const canPlace = grid[hoverCell.y][hoverCell.x] === 'empty' &&
gameState.playerCurrency >= tower.cost;
// Visual feedback for placement validity
ctx.fillStyle = canPlace ? tower.color + '80' : 'rgba(255, 0, 0, 0.3)';
ctx.fillRect(
hoverCell.x * cellSize,
hoverCell.y * cellSize,
cellSize,
cellSize
);
// Range indicator preview
ctx.beginPath();
ctx.arc(
(hoverCell.x + 0.5) * cellSize,
(hoverCell.y + 0.5) * cellSize,
tower.range * cellSize,
0,
Math.PI * 2
);
ctx.strokeStyle = canPlace ? tower.color + '40' : 'rgba(255, 0, 0, 0.2)';
ctx.stroke();
}
}
/**
* Renders all enemies with health indicators and effects
* Implements visual state representation
*
* @param {CanvasRenderingContext2D} ctx - Canvas rendering context
* @param {Array<Object>} enemies - Array of enemy objects
*/
function renderEnemies(ctx, enemies) {
const cellSize = canvas.width / 20;
enemies.forEach(enemy => {
// Health-based opacity for visual feedback
const healthPercent = enemy.currentHealth / enemy.maxHealth;
const opacity = 0.3 + (healthPercent * 0.7);
// Dynamic color based on enemy state
const color = EnemyTypes[enemy.type].color;
const hexOpacity = Math.floor(opacity * 255).toString(16).padStart(2, '0');
// Draw enemy body with solid black border
ctx.beginPath();
ctx.arc(
(enemy.position.x + 0.5) * cellSize,
(enemy.position.y + 0.5) * cellSize,
cellSize / 3,
0,
Math.PI * 2
);
// Fill with dynamic opacity
ctx.fillStyle = `${color}${hexOpacity}`;
ctx.fill();
// Add solid black border
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
ctx.stroke();
// Range indicator for special enemy types
if (EnemyTypes[enemy.type].isRanged) {
ctx.beginPath();
ctx.arc(
(enemy.position.x + 0.5) * cellSize,
(enemy.position.y + 0.5) * cellSize,
EnemyTypes[enemy.type].attackRange * cellSize,
0,
Math.PI * 2
);
ctx.strokeStyle = `${EnemyTypes[enemy.type].color}40`;
ctx.stroke();
}
});
}
/**
* Renders game UI elements with clean state management
* Implements heads-up display (HUD) pattern
*
* @param {CanvasRenderingContext2D} ctx - Canvas rendering context
* @param {Object} gameState - Current game state
*/
function renderUI(ctx, gameState) {
const padding = 20;
const lineHeight = 30;
const startY = padding;
const width = 200;
const height = lineHeight * 5;
// Save the current canvas state
ctx.save();
// Reset any transformations
ctx.setTransform(1, 0, 0, 1, 0, 0);
// Semi-transparent background for readability
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
ctx.fillRect(0, 0, width, height + padding);
// Text rendering setup
ctx.fillStyle = 'black';
ctx.font = '20px Arial';
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
// Game state information
ctx.fillText(`Level: ${gameState.level}`, padding, startY);
ctx.fillText(`Currency: $${gameState.currency}`, padding, startY + lineHeight);
ctx.fillText(`Phase: ${gameState.phase}`, padding, startY + lineHeight * 2);
ctx.fillText(`Destroyed: ${gameState.enemiesDestroyed}`, padding, startY + lineHeight * 3);
ctx.fillText(`Escaped: ${gameState.enemiesEscaped}`, padding, startY + lineHeight * 4);
// Restore the canvas state
ctx.restore();
}
function renderTowers(ctx, towers) {
const cellSize = canvas.width / 20;
towers.forEach(tower => {
const healthPercent = tower.currentHealth / tower.maxHealth;
// Draw tower body
ctx.fillStyle = tower.color + Math.floor(healthPercent * 255).toString(16).padStart(2, '0');
ctx.fillRect(
tower.position.x * cellSize + cellSize * 0.1,
tower.position.y * cellSize + cellSize * 0.1,
cellSize * 0.8,
cellSize * 0.8
);
// Draw ammo count
ctx.fillStyle = 'white';
ctx.font = '12px Arial';
ctx.textAlign = 'center';
ctx.fillText(
tower.ammo,
(tower.position.x + 0.5) * cellSize,
(tower.position.y + 0.7) * cellSize
);
// Draw range indicator
if (gameState.phase === GamePhase.PLACEMENT) {
ctx.beginPath();
ctx.arc(
(tower.position.x + 0.5) * cellSize,
(tower.position.y + 0.5) * cellSize,
tower.range * cellSize,
0,
Math.PI * 2
);
ctx.strokeStyle = tower.color + '40';
ctx.stroke();
}
});
}
// Add new render function for particles
function renderParticles(ctx, particles) {
particles.forEach(particle => {
const age = performance.now() - particle.createdAt;
const lifePercent = age / particle.lifetime;
if (lifePercent <= 1) {
if (particle.type === 'SLIME_TRAIL') {
// Calculate opacity based on lifetime and fade start
let opacity = 1;
if (lifePercent > particle.fadeStart) {
opacity = 1 - ((lifePercent - particle.fadeStart) / (1 - particle.fadeStart));
}
opacity *= 0.3; // Make it translucent
ctx.globalAlpha = opacity;
ctx.fillStyle = particle.color;
// Draw a circular slime splat
ctx.beginPath();
ctx.arc(
particle.position.x,
particle.position.y,
particle.size * (1 - lifePercent * 0.3), // Slightly shrink over time
0,
Math.PI * 2
);
ctx.fill();
// Add some variation to the splat
for (let i = 0; i < 3; i++) {
const angle = (Math.PI * 2 * i) / 3;
const distance = particle.size * 0.4;
ctx.beginPath();
ctx.arc(
particle.position.x + Math.cos(angle) * distance,
particle.position.y + Math.sin(angle) * distance,
particle.size * 0.4 * (1 - lifePercent * 0.3),
0,
Math.PI * 2
);
ctx.fill();
}
} else if (particle.type === 'AOE_EXPLOSION') {
// Draw expanding circle
const radius = particle.initialRadius +
(particle.finalRadius - particle.initialRadius) * lifePercent;
// Draw multiple rings for better effect
const numRings = 3;
for (let i = 0; i < numRings; i++) {
const ringRadius = radius * (1 - (i * 0.2));
const ringAlpha = (1 - lifePercent) * (1 - (i * 0.3));
ctx.beginPath();
ctx.arc(
particle.position.x,
particle.position.y,
ringRadius,
0,
Math.PI * 2
);
ctx.strokeStyle = particle.color;
ctx.lineWidth = particle.ringWidth * (1 - (i * 0.2));
ctx.globalAlpha = ringAlpha;
ctx.stroke();
}
// Draw affected area
ctx.beginPath();
ctx.arc(
particle.position.x,
particle.position.y,
radius,
0,
Math.PI * 2
);
ctx.fillStyle = particle.color + '20'; // Very transparent fill
ctx.fill();
} else {
// Original particle rendering
ctx.fillStyle = particle.color;
ctx.beginPath();
ctx.arc(
particle.position.x,
particle.position.y,
particle.size * (1 - lifePercent),
0,
Math.PI * 2
);
ctx.fill();
}
}
});
ctx.globalAlpha = 1;
}
// Add new render function for projectiles
function renderProjectiles(ctx, projectiles) {
const cellSize = canvas.width / 20;
projectiles.forEach(projectile => {
const age = performance.now() - projectile.createdAt;
const progress = age / projectile.lifetime;
if (progress <= 1) {
// Draw projectile trail
ctx.beginPath();
ctx.moveTo(
projectile.startPos.x * cellSize + cellSize / 2,
projectile.startPos.y * cellSize + cellSize / 2
);
const currentX = projectile.startPos.x + (projectile.targetPos.x - projectile.startPos.x) * progress;
const currentY = projectile.startPos.y + (projectile.targetPos.y - projectile.startPos.y) * progress;
ctx.lineTo(
currentX * cellSize + cellSize / 2,
currentY * cellSize + cellSize / 2
);
ctx.strokeStyle = '#fff';
ctx.lineWidth = 2;
ctx.stroke();
// Draw projectile head
ctx.beginPath();
ctx.arc(
currentX * cellSize + cellSize / 2,
currentY * cellSize + cellSize / 2,
4,
0,
Math.PI * 2
);
ctx.fillStyle = '#fff';
ctx.fill();
}
});
}
// Update level complete message in game.js
function handleLevelComplete() {
gameState.phase = GamePhase.TRANSITION;
// Calculate ammo bonus
let ammoBonus = 0;
gameState.towers.forEach(tower => {
ammoBonus += tower.ammo * 2;
});
const message = `
Level ${gameState.level} Complete!
Current Money: $${gameState.currency}
Ammo Bonus: +$${ammoBonus}
Level Bonus: +$10
Ready for Level ${gameState.level + 1}?
`;
setTimeout(() => {
if (confirm(message)) {
startNextLevel();
}
}, 100);
}
|