@@ -27,27 +27,29 @@ class RaycasterPainter extends CustomPainter {
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
// Disable anti-aliasing for the background to prevent edge bleeding
|
||||
final Paint bgPaint = Paint()..isAntiAlias = false;
|
||||
|
||||
// 1. Draw Ceiling & Floor
|
||||
canvas.drawRect(
|
||||
Rect.fromLTWH(0, 0, size.width, size.height / 2),
|
||||
Paint()..color = Colors.blueGrey[900]!,
|
||||
bgPaint..color = Colors.blueGrey[900]!,
|
||||
);
|
||||
canvas.drawRect(
|
||||
Rect.fromLTWH(0, size.height / 2, size.width, size.height / 2),
|
||||
Paint()..color = Colors.brown[900]!,
|
||||
bgPaint..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;
|
||||
// CRITICAL FIX: Disable anti-aliasing so edges remain perfectly sharp
|
||||
final Paint columnPaint = Paint()
|
||||
..isAntiAlias = false
|
||||
..strokeWidth = columnWidth + 0.5;
|
||||
|
||||
// The 1D Z-Buffer locked to our render width
|
||||
List<double> zBuffer = List.filled(renderWidth, 0.0);
|
||||
|
||||
double dirX = math.cos(player.angle);
|
||||
@@ -153,12 +155,11 @@ class RaycasterPainter extends CustomPainter {
|
||||
}
|
||||
wallX -= wallX.floor();
|
||||
|
||||
// Pass the scaled drawX instead of the raw loop index
|
||||
double drawX = x * columnWidth;
|
||||
|
||||
_drawTexturedColumn(
|
||||
canvas,
|
||||
drawX, // <-- Updated
|
||||
drawX,
|
||||
perpWallDist,
|
||||
wallX,
|
||||
side,
|
||||
@@ -166,7 +167,7 @@ class RaycasterPainter extends CustomPainter {
|
||||
hitWallId,
|
||||
textures,
|
||||
doorOffset,
|
||||
columnPaint, // <-- Pass the reusable Paint object
|
||||
columnPaint,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -189,34 +190,24 @@ class RaycasterPainter extends CustomPainter {
|
||||
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);
|
||||
@@ -226,7 +217,9 @@ class RaycasterPainter extends CustomPainter {
|
||||
int colorByte = spritePixels[texX][ty];
|
||||
|
||||
if (colorByte != 255) {
|
||||
double endY = startY + stepY;
|
||||
// ADDED 0.5 OVERLAP TO PREVENT VERTICAL SEAMS
|
||||
double endY = startY + stepY + 0.5;
|
||||
|
||||
if (endY > 0 && startY < size.height) {
|
||||
columnPaint.color = ColorPalette.vga[colorByte];
|
||||
canvas.drawLine(
|
||||
@@ -246,7 +239,7 @@ class RaycasterPainter extends CustomPainter {
|
||||
|
||||
void _drawTexturedColumn(
|
||||
Canvas canvas,
|
||||
double drawX, // <-- Receive scaled draw position
|
||||
double drawX,
|
||||
double distance,
|
||||
double wallX,
|
||||
int side,
|
||||
@@ -254,7 +247,7 @@ class RaycasterPainter extends CustomPainter {
|
||||
int hitWallId,
|
||||
List<Matrix<int>> textures,
|
||||
double doorOffset,
|
||||
Paint paint, // <-- Receive reused Paint object
|
||||
Paint paint,
|
||||
) {
|
||||
if (distance <= 0.01) distance = 0.01;
|
||||
|
||||
@@ -281,10 +274,10 @@ class RaycasterPainter extends CustomPainter {
|
||||
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;
|
||||
// ADDED 0.5 OVERLAP TO PREVENT VERTICAL SEAMS
|
||||
double endY = startY + stepY + 0.5;
|
||||
|
||||
if (endY > 0 && startY < size.height) {
|
||||
canvas.drawLine(
|
||||
@@ -300,7 +293,8 @@ class RaycasterPainter extends CustomPainter {
|
||||
|
||||
@override
|
||||
bool shouldRepaint(RaycasterPainter oldDelegate) {
|
||||
return oldDelegate.player != player ||
|
||||
oldDelegate.player.angle != player.angle;
|
||||
// Because the Player object instance remains the same, a pure equality check fails.
|
||||
// Given that your Ticker loop calls setState every frame, returning true is safest.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user