Spaces:
Paused
Paused
File size: 14,954 Bytes
2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 4539eae 2d54981 | 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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | // Game Renderer - handles all canvas drawing operations
import { GameState, Block, Enemy, PlayerState, Position } from './types';
import { GAME_CONFIG } from '../constants';
import { LevelTheme } from './LevelManager';
const { CANVAS_WIDTH, CANVAS_HEIGHT, TILE_SIZE, COLORS } = GAME_CONFIG;
const THEME_COLORS = {
overworld: { sky: COLORS.SKY, ground: COLORS.GROUND },
underground: { sky: '#1a1a2e', ground: '#4a4a4a' },
castle: { sky: '#2d1b1b', ground: '#5a3030' },
};
export class Renderer {
private ctx: CanvasRenderingContext2D;
private animationFrame: number = 0;
constructor(ctx: CanvasRenderingContext2D) {
this.ctx = ctx;
}
private currentTheme: LevelTheme = 'overworld';
clear(): void {
// Draw sky background based on theme
this.ctx.fillStyle = THEME_COLORS[this.currentTheme].sky;
this.ctx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
}
drawClouds(cameraX: number): void {
this.ctx.fillStyle = '#ffffff';
const clouds = [
{ x: 100, y: 60, width: 80, height: 30 },
{ x: 400, y: 80, width: 100, height: 35 },
{ x: 700, y: 50, width: 70, height: 25 },
{ x: 1100, y: 70, width: 90, height: 30 },
{ x: 1500, y: 55, width: 75, height: 28 },
{ x: 1900, y: 85, width: 85, height: 32 },
{ x: 2300, y: 60, width: 95, height: 35 },
{ x: 2700, y: 75, width: 80, height: 30 },
];
clouds.forEach(cloud => {
const screenX = cloud.x - cameraX * 0.3; // Parallax effect
if (screenX > -cloud.width && screenX < CANVAS_WIDTH + cloud.width) {
// Draw cloud as overlapping circles
this.ctx.beginPath();
this.ctx.arc(screenX, cloud.y, cloud.height * 0.6, 0, Math.PI * 2);
this.ctx.arc(screenX + cloud.width * 0.3, cloud.y - 5, cloud.height * 0.7, 0, Math.PI * 2);
this.ctx.arc(screenX + cloud.width * 0.6, cloud.y, cloud.height * 0.5, 0, Math.PI * 2);
this.ctx.fill();
}
});
}
drawBlock(block: Block, cameraX: number): void {
const screenX = block.x - cameraX;
// Skip if off screen
if (screenX < -block.width || screenX > CANVAS_WIDTH) return;
switch (block.type) {
case 'ground':
this.drawGroundBlock(screenX, block.y, block.width, block.height);
break;
case 'brick':
this.drawBrickBlock(screenX, block.y, block.width, block.height);
break;
case 'question':
this.drawQuestionBlock(screenX, block.y, block.width, block.height, block.isHit);
break;
case 'pipe':
this.drawPipe(screenX, block.y, block.width, block.height);
break;
case 'coin':
this.drawCoin(screenX, block.y, block.width, block.height);
break;
}
}
private drawGroundBlock(x: number, y: number, width: number, height: number): void {
// Main color
this.ctx.fillStyle = COLORS.GROUND;
this.ctx.fillRect(x, y, width, height);
// Texture pattern
this.ctx.fillStyle = '#8B4513';
this.ctx.fillRect(x + 2, y + 2, 4, 4);
this.ctx.fillRect(x + width - 8, y + height - 8, 4, 4);
this.ctx.fillRect(x + width / 2, y + height / 2 - 2, 3, 3);
// Border
this.ctx.strokeStyle = '#5a3000';
this.ctx.lineWidth = 1;
this.ctx.strokeRect(x, y, width, height);
}
private drawBrickBlock(x: number, y: number, width: number, height: number): void {
this.ctx.fillStyle = COLORS.BRICK;
this.ctx.fillRect(x, y, width, height);
// Brick pattern
this.ctx.strokeStyle = '#5a3000';
this.ctx.lineWidth = 2;
this.ctx.strokeRect(x, y, width, height);
// Horizontal line
this.ctx.beginPath();
this.ctx.moveTo(x, y + height / 2);
this.ctx.lineTo(x + width, y + height / 2);
this.ctx.stroke();
// Vertical lines
this.ctx.beginPath();
this.ctx.moveTo(x + width / 2, y);
this.ctx.lineTo(x + width / 2, y + height / 2);
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.moveTo(x + width / 4, y + height / 2);
this.ctx.lineTo(x + width / 4, y + height);
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.moveTo(x + width * 3 / 4, y + height / 2);
this.ctx.lineTo(x + width * 3 / 4, y + height);
this.ctx.stroke();
}
private drawQuestionBlock(x: number, y: number, width: number, height: number, isHit?: boolean): void {
// Animate if not hit
const bounce = isHit ? 0 : Math.sin(this.animationFrame * 0.1) * 2;
this.ctx.fillStyle = isHit ? '#8B7355' : COLORS.QUESTION;
this.ctx.fillRect(x, y + bounce, width, height);
// Border
this.ctx.strokeStyle = '#8B4513';
this.ctx.lineWidth = 2;
this.ctx.strokeRect(x, y + bounce, width, height);
// Question mark or empty
this.ctx.fillStyle = isHit ? '#666' : '#fff';
this.ctx.font = 'bold 20px Arial';
this.ctx.textAlign = 'center';
this.ctx.textBaseline = 'middle';
this.ctx.fillText(isHit ? '' : '?', x + width / 2, y + height / 2 + bounce);
}
private drawPipe(x: number, y: number, width: number, height: number): void {
// Pipe body
this.ctx.fillStyle = COLORS.PIPE;
this.ctx.fillRect(x, y, width, height);
// Pipe rim (top part is wider)
if (y <= GAME_CONFIG.LEVEL_HEIGHT - TILE_SIZE * 2 - height + TILE_SIZE) {
this.ctx.fillStyle = '#00c800';
this.ctx.fillRect(x - 4, y, width + 8, TILE_SIZE / 2);
}
// Highlights
this.ctx.fillStyle = '#00e000';
this.ctx.fillRect(x + 4, y, 8, height);
// Shadow
this.ctx.fillStyle = '#006800';
this.ctx.fillRect(x + width - 8, y, 6, height);
// Border
this.ctx.strokeStyle = '#004000';
this.ctx.lineWidth = 2;
this.ctx.strokeRect(x, y, width, height);
}
private drawCoin(x: number, y: number, width: number, height: number): void {
// Animate coin
const scale = 0.7 + Math.abs(Math.sin(this.animationFrame * 0.15)) * 0.3;
const centerX = x + width / 2;
const centerY = y + height / 2;
this.ctx.save();
this.ctx.translate(centerX, centerY);
this.ctx.scale(scale, 1);
// Coin circle
this.ctx.beginPath();
this.ctx.arc(0, 0, width / 2 - 2, 0, Math.PI * 2);
this.ctx.fillStyle = COLORS.COIN;
this.ctx.fill();
this.ctx.strokeStyle = '#cc9900';
this.ctx.lineWidth = 2;
this.ctx.stroke();
// Inner detail
this.ctx.beginPath();
this.ctx.arc(0, 0, width / 4, 0, Math.PI * 2);
this.ctx.strokeStyle = '#ffeb3b';
this.ctx.stroke();
this.ctx.restore();
}
drawPlayer(player: PlayerState, cameraX: number): void {
const screenX = player.x - cameraX;
if (!player.isAlive) {
// Death animation - player falling
this.ctx.save();
this.ctx.globalAlpha = 0.7;
}
// Body (blue overalls)
this.ctx.fillStyle = COLORS.MARIO_BLUE;
this.ctx.fillRect(screenX + 4, player.y + 20, 24, 28);
// Head
this.ctx.fillStyle = COLORS.MARIO_SKIN;
this.ctx.fillRect(screenX + 6, player.y + 4, 20, 16);
// Hat
this.ctx.fillStyle = COLORS.MARIO_RED;
this.ctx.fillRect(screenX + 4, player.y, 24, 8);
if (player.facingRight) {
this.ctx.fillRect(screenX + 24, player.y + 4, 6, 6);
} else {
this.ctx.fillRect(screenX + 2, player.y + 4, 6, 6);
}
// Eyes
this.ctx.fillStyle = '#000';
if (player.facingRight) {
this.ctx.fillRect(screenX + 18, player.y + 8, 4, 4);
} else {
this.ctx.fillRect(screenX + 10, player.y + 8, 4, 4);
}
// Mustache
this.ctx.fillStyle = '#4a2800';
this.ctx.fillRect(screenX + 8, player.y + 14, 16, 4);
// Buttons on overalls
this.ctx.fillStyle = COLORS.COIN;
this.ctx.fillRect(screenX + 10, player.y + 24, 4, 4);
this.ctx.fillRect(screenX + 18, player.y + 24, 4, 4);
if (!player.isAlive) {
this.ctx.restore();
}
}
drawEnemy(enemy: Enemy, cameraX: number): void {
const screenX = enemy.x - cameraX;
// Skip if off screen
if (screenX < -enemy.width || screenX > CANVAS_WIDTH) return;
if (!enemy.isAlive) return;
if (enemy.type === 'koopa') {
this.drawKoopa(screenX, enemy);
return;
}
// Goomba body
this.ctx.fillStyle = COLORS.GOOMBA;
// Head (mushroom shape)
this.ctx.beginPath();
this.ctx.arc(screenX + enemy.width / 2, enemy.y + 10, 14, Math.PI, 0);
this.ctx.fill();
// Body
this.ctx.fillRect(screenX + 4, enemy.y + 10, enemy.width - 8, 14);
// Feet (animated)
const footOffset = Math.sin(this.animationFrame * 0.3) * 2;
this.ctx.fillStyle = '#000';
this.ctx.fillRect(screenX + 2, enemy.y + 24, 10, 8 + footOffset);
this.ctx.fillRect(screenX + enemy.width - 12, enemy.y + 24, 10, 8 - footOffset);
// Eyes
this.ctx.fillStyle = '#fff';
this.ctx.fillRect(screenX + 8, enemy.y + 8, 6, 8);
this.ctx.fillRect(screenX + enemy.width - 14, enemy.y + 8, 6, 8);
// Pupils
this.ctx.fillStyle = '#000';
const pupilOffset = enemy.direction > 0 ? 2 : 0;
this.ctx.fillRect(screenX + 10 + pupilOffset, enemy.y + 12, 3, 4);
this.ctx.fillRect(screenX + enemy.width - 13 + pupilOffset, enemy.y + 12, 3, 4);
// Eyebrows (angry)
this.ctx.strokeStyle = '#000';
this.ctx.lineWidth = 2;
this.ctx.beginPath();
this.ctx.moveTo(screenX + 6, enemy.y + 6);
this.ctx.lineTo(screenX + 14, enemy.y + 8);
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.moveTo(screenX + enemy.width - 6, enemy.y + 6);
this.ctx.lineTo(screenX + enemy.width - 14, enemy.y + 8);
this.ctx.stroke();
}
private drawKoopa(screenX: number, enemy: Enemy): void {
// Shell (green)
this.ctx.fillStyle = '#00aa00';
this.ctx.beginPath();
this.ctx.ellipse(screenX + enemy.width / 2, enemy.y + enemy.height - 12, 14, 10, 0, 0, Math.PI * 2);
this.ctx.fill();
// Shell pattern
this.ctx.strokeStyle = '#006600';
this.ctx.lineWidth = 2;
this.ctx.beginPath();
this.ctx.arc(screenX + enemy.width / 2, enemy.y + enemy.height - 12, 8, 0, Math.PI * 2);
this.ctx.stroke();
// Head
this.ctx.fillStyle = '#ffcc00';
this.ctx.beginPath();
this.ctx.arc(screenX + enemy.width / 2, enemy.y + 10, 10, 0, Math.PI * 2);
this.ctx.fill();
// Eyes
this.ctx.fillStyle = '#fff';
this.ctx.fillRect(screenX + enemy.width / 2 - 6, enemy.y + 6, 5, 6);
this.ctx.fillRect(screenX + enemy.width / 2 + 1, enemy.y + 6, 5, 6);
// Pupils
this.ctx.fillStyle = '#000';
const pupilOffset = enemy.direction > 0 ? 2 : 0;
this.ctx.fillRect(screenX + enemy.width / 2 - 4 + pupilOffset, enemy.y + 8, 2, 3);
this.ctx.fillRect(screenX + enemy.width / 2 + 2 + pupilOffset, enemy.y + 8, 2, 3);
// Feet
const footOffset = Math.sin(this.animationFrame * 0.3) * 2;
this.ctx.fillStyle = '#ffcc00';
this.ctx.fillRect(screenX + 4, enemy.y + enemy.height - 6, 8, 6 + footOffset);
this.ctx.fillRect(screenX + enemy.width - 12, enemy.y + enemy.height - 6, 8, 6 - footOffset);
}
drawFlag(position: Position, cameraX: number): void {
const screenX = position.x - cameraX;
// Skip if off screen
if (screenX < -100 || screenX > CANVAS_WIDTH + 100) return;
// Flag pole
this.ctx.fillStyle = '#00aa00';
this.ctx.fillRect(screenX, position.y, 8, GAME_CONFIG.LEVEL_HEIGHT - position.y - TILE_SIZE * 2);
// Pole ball on top
this.ctx.beginPath();
this.ctx.arc(screenX + 4, position.y - 5, 8, 0, Math.PI * 2);
this.ctx.fillStyle = COLORS.COIN;
this.ctx.fill();
// Flag
const flagWave = Math.sin(this.animationFrame * 0.1) * 3;
this.ctx.fillStyle = COLORS.FLAG;
this.ctx.beginPath();
this.ctx.moveTo(screenX + 8, position.y);
this.ctx.lineTo(screenX + 50 + flagWave, position.y + 20);
this.ctx.lineTo(screenX + 8, position.y + 40);
this.ctx.closePath();
this.ctx.fill();
// Star on flag
this.ctx.fillStyle = '#fff';
this.ctx.font = '16px Arial';
this.ctx.fillText('★', screenX + 22 + flagWave / 2, position.y + 25);
}
drawUI(player: PlayerState, levelNumber: number = 1): void {
// UI Background
this.ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
this.ctx.fillRect(10, 10, 280, 60);
// Score
this.ctx.fillStyle = '#fff';
this.ctx.font = 'bold 16px "Press Start 2P", monospace';
this.ctx.textAlign = 'left';
this.ctx.fillText(`SCORE`, 20, 32);
this.ctx.fillText(`${player.score.toString().padStart(6, '0')}`, 20, 52);
// Coins
this.ctx.fillStyle = COLORS.COIN;
this.ctx.beginPath();
this.ctx.arc(150, 35, 10, 0, Math.PI * 2);
this.ctx.fill();
this.ctx.fillStyle = '#fff';
this.ctx.fillText(`×${player.coins}`, 165, 40);
// Level indicator
this.ctx.fillStyle = '#fff';
this.ctx.fillText(`WORLD`, 220, 32);
this.ctx.fillText(`1-${levelNumber}`, 220, 52);
}
update(): void {
this.animationFrame++;
}
render(state: GameState, theme: LevelTheme = 'overworld', levelNumber: number = 1): void {
this.currentTheme = theme;
this.clear();
// Only draw clouds in overworld
if (theme === 'overworld') {
this.drawClouds(state.camera.x);
}
// Draw all blocks
state.blocks.forEach(block => this.drawBlock(block, state.camera.x));
// Draw flag
this.drawFlag(state.level.flagPosition, state.camera.x);
// Draw enemies
state.enemies.forEach(enemy => this.drawEnemy(enemy, state.camera.x));
// Draw player
this.drawPlayer(state.player, state.camera.x);
// Draw UI
this.drawUI(state.player, levelNumber);
this.update();
}
}
|