import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:wolf_dart/classes/matrix.dart'; import 'package:wolf_dart/features/entities/entity.dart'; import 'package:wolf_dart/features/player/player.dart'; import 'package:wolf_dart/features/renderer/color_palette.dart'; class RaycasterPainter extends CustomPainter { final Matrix map; final List> textures; final Player player; final double fov; final Map doorOffsets; final List> sprites; final List entities; RaycasterPainter({ required this.map, required this.textures, required this.player, required this.fov, required this.doorOffsets, required this.sprites, required this.entities, }); @override void paint(Canvas canvas, Size size) { // 1. Draw Ceiling & Floor canvas.drawRect( Rect.fromLTWH(0, 0, size.width, size.height / 2), Paint()..color = Colors.blueGrey[900]!, ); canvas.drawRect( Rect.fromLTWH(0, size.height / 2, size.width, size.height / 2), Paint()..color = Colors.brown[900]!, ); // --- OPTIMIZATION: Lock to Retro Resolution --- const int renderWidth = 320; // Calculate how wide each column should be on the actual screen double columnWidth = size.width / renderWidth; // Create a single Paint object to reuse (massive performance boost) // Add 0.5 to strokeWidth to prevent anti-aliasing seams between columns final Paint columnPaint = Paint()..strokeWidth = columnWidth + 0.5; // The 1D Z-Buffer locked to our render width List zBuffer = List.filled(renderWidth, 0.0); double dirX = math.cos(player.angle); double dirY = math.sin(player.angle); double planeX = -dirY * math.tan(fov / 2); double planeY = dirX * math.tan(fov / 2); // --- 1. CAST WALLS --- for (int x = 0; x < renderWidth; x++) { double cameraX = 2 * x / renderWidth - 1.0; double rayDirX = dirX + planeX * cameraX; double rayDirY = dirY + planeY * cameraX; int mapX = player.x.toInt(); int mapY = player.y.toInt(); double sideDistX; double sideDistY; double deltaDistX = (rayDirX == 0) ? 1e30 : (1.0 / rayDirX).abs(); double deltaDistY = (rayDirY == 0) ? 1e30 : (1.0 / rayDirY).abs(); double perpWallDist; int stepX; int stepY; bool hit = false; bool hitOutOfBounds = false; int side = 0; int hitWallId = 0; double doorOffset = 0.0; Set ignoredDoors = {}; if (rayDirX < 0) { stepX = -1; sideDistX = (player.x - mapX) * deltaDistX; } else { stepX = 1; sideDistX = (mapX + 1.0 - player.x) * deltaDistX; } if (rayDirY < 0) { stepY = -1; sideDistY = (player.y - mapY) * deltaDistY; } else { stepY = 1; sideDistY = (mapY + 1.0 - player.y) * deltaDistY; } while (!hit) { if (sideDistX < sideDistY) { sideDistX += deltaDistX; mapX += stepX; side = 0; } else { sideDistY += deltaDistY; mapY += stepY; side = 1; } if (mapY < 0 || mapY >= map.length || mapX < 0 || mapX >= map[0].length) { hit = true; hitOutOfBounds = true; } else if (map[mapY][mapX] > 0) { String doorKey = '$mapX,$mapY'; if (map[mapY][mapX] >= 90 && !ignoredDoors.contains(doorKey)) { double currentOffset = doorOffsets[doorKey] ?? 0.0; if (currentOffset > 0.0) { double perpWallDistTemp = (side == 0) ? (sideDistX - deltaDistX) : (sideDistY - deltaDistY); double wallXTemp = (side == 0) ? player.y + perpWallDistTemp * rayDirY : player.x + perpWallDistTemp * rayDirX; wallXTemp -= wallXTemp.floor(); if (wallXTemp < currentOffset) { ignoredDoors.add(doorKey); continue; } } doorOffset = currentOffset; } hit = true; hitWallId = map[mapY][mapX]; } } if (hitOutOfBounds) continue; if (side == 0) { perpWallDist = (sideDistX - deltaDistX); } else { perpWallDist = (sideDistY - deltaDistY); } zBuffer[x] = perpWallDist; double wallX; if (side == 0) { wallX = player.y + perpWallDist * rayDirY; } else { wallX = player.x + perpWallDist * rayDirX; } wallX -= wallX.floor(); // Pass the scaled drawX instead of the raw loop index double drawX = x * columnWidth; _drawTexturedColumn( canvas, drawX, // <-- Updated perpWallDist, wallX, side, size, hitWallId, textures, doorOffset, columnPaint, // <-- Pass the reusable Paint object ); } // --- 2. DRAW SPRITES --- List activeSprites = List.from(entities); activeSprites.sort((a, b) { double distA = math.pow(player.x - a.x, 2) + math.pow(player.y - a.y, 2).toDouble(); double distB = math.pow(player.x - b.x, 2) + math.pow(player.y - b.y, 2).toDouble(); return distB.compareTo(distA); }); for (Entity entity in activeSprites) { double spriteX = entity.x - player.x; double spriteY = entity.y - player.y; double invDet = 1.0 / (planeX * dirY - dirX * planeY); double transformX = invDet * (dirY * spriteX - dirX * spriteY); double transformY = invDet * (-planeY * spriteX + planeX * spriteY); if (transformY > 0) { // Map sprite X to our 320 renderWidth int spriteScreenX = ((renderWidth / 2) * (1 + transformX / transformY)) .toInt(); // Calculate height in REAL screen pixels int spriteHeight = (size.height / transformY).abs().toInt(); // Calculate width in COLUMNS (320 space) to maintain the square aspect ratio int spriteColumnWidth = (spriteHeight / columnWidth).toInt(); // Use the new column width for start/end points int drawStartX = -spriteColumnWidth ~/ 2 + spriteScreenX; int drawEndX = spriteColumnWidth ~/ 2 + spriteScreenX; // Clip to screen boundaries int clipStartX = math.max(0, drawStartX); int clipEndX = math.min(renderWidth - 1, drawEndX); for (int stripe = clipStartX; stripe < clipEndX; stripe++) { // THE Z-BUFFER CHECK! if (transformY < zBuffer[stripe]) { // Map the texture X using the new column width! double texXDouble = (stripe - drawStartX) * 64 / spriteColumnWidth; int texX = texXDouble.toInt().clamp(0, 63); double startY = (size.height / 2) - (spriteHeight / 2); double stepY = spriteHeight / 64.0; double drawX = stripe * columnWidth; int safeIndex = entity.spriteIndex.clamp(0, sprites.length - 1); Matrix spritePixels = sprites[safeIndex]; for (int ty = 0; ty < 64; ty++) { int colorByte = spritePixels[texX][ty]; if (colorByte != 255) { double endY = startY + stepY; if (endY > 0 && startY < size.height) { columnPaint.color = ColorPalette.vga[colorByte]; canvas.drawLine( Offset(drawX, startY), Offset(drawX, endY), columnPaint, ); } } startY += stepY; } } } } } } void _drawTexturedColumn( Canvas canvas, double drawX, // <-- Receive scaled draw position double distance, double wallX, int side, Size size, int hitWallId, List> textures, double doorOffset, Paint paint, // <-- Receive reused Paint object ) { if (distance <= 0.01) distance = 0.01; double wallHeight = size.height / distance; int drawStart = ((size.height / 2) - (wallHeight / 2)).toInt(); int texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2); int texX = (wallX * 64).toInt().clamp(0, 63); if (hitWallId >= 90) { texNum = 98.clamp(0, textures.length - 1); texX = ((wallX - doorOffset) * 64).toInt().clamp(0, 63); } else { texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2); if (side == 1) texNum += 1; } if (side == 0 && math.cos(player.angle) > 0) texX = 63 - texX; if (side == 1 && math.sin(player.angle) < 0) texX = 63 - texX; double startY = drawStart.toDouble(); double stepY = wallHeight / 64.0; for (int ty = 0; ty < 64; ty++) { int colorByte = textures[texNum][texX][ty]; // Update the color of our shared paint object paint.color = ColorPalette.vga[colorByte]; double endY = startY + stepY; if (endY > 0 && startY < size.height) { canvas.drawLine( Offset(drawX, startY), Offset(drawX, endY), paint, ); } startY += stepY; } } @override bool shouldRepaint(RaycasterPainter oldDelegate) { return oldDelegate.player != player || oldDelegate.player.angle != player.angle; } }