Files
wolf_dart/lib/features/renderer/raycast_painter.dart
2026-03-13 23:27:40 +01:00

301 lines
8.8 KiB
Dart

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<int> map;
final List<Matrix<int>> textures;
final Player player;
final double fov;
final Map<String, double> doorOffsets;
final List<Matrix<int>> sprites;
final List<Entity> 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) {
// 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),
bgPaint..color = Colors.blueGrey[900]!,
);
canvas.drawRect(
Rect.fromLTWH(0, size.height / 2, size.width, size.height / 2),
bgPaint..color = Colors.brown[900]!,
);
// --- OPTIMIZATION: Lock to Retro Resolution ---
const int renderWidth = 320;
double columnWidth = size.width / renderWidth;
// CRITICAL FIX: Disable anti-aliasing so edges remain perfectly sharp
final Paint columnPaint = Paint()
..isAntiAlias = false
..strokeWidth = columnWidth + 0.5;
List<double> 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<String> 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();
double drawX = x * columnWidth;
_drawTexturedColumn(
canvas,
drawX,
perpWallDist,
wallX,
side,
size,
hitWallId,
textures,
doorOffset,
columnPaint,
);
}
// --- 2. DRAW SPRITES ---
List<Entity> 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) {
int spriteScreenX = ((renderWidth / 2) * (1 + transformX / transformY))
.toInt();
int spriteHeight = (size.height / transformY).abs().toInt();
int spriteColumnWidth = (spriteHeight / columnWidth).toInt();
int drawStartX = -spriteColumnWidth ~/ 2 + spriteScreenX;
int drawEndX = spriteColumnWidth ~/ 2 + spriteScreenX;
int clipStartX = math.max(0, drawStartX);
int clipEndX = math.min(renderWidth - 1, drawEndX);
for (int stripe = clipStartX; stripe < clipEndX; stripe++) {
if (transformY < zBuffer[stripe]) {
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<int> spritePixels = sprites[safeIndex];
for (int ty = 0; ty < 64; ty++) {
int colorByte = spritePixels[texX][ty];
if (colorByte != 255) {
// 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(
Offset(drawX, startY),
Offset(drawX, endY),
columnPaint,
);
}
}
startY += stepY;
}
}
}
}
}
}
void _drawTexturedColumn(
Canvas canvas,
double drawX,
double distance,
double wallX,
int side,
Size size,
int hitWallId,
List<Matrix<int>> textures,
double doorOffset,
Paint paint,
) {
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];
paint.color = ColorPalette.vga[colorByte];
// ADDED 0.5 OVERLAP TO PREVENT VERTICAL SEAMS
double endY = startY + stepY + 0.5;
if (endY > 0 && startY < size.height) {
canvas.drawLine(
Offset(drawX, startY),
Offset(drawX, endY),
paint,
);
}
startY += stepY;
}
}
@override
bool shouldRepaint(RaycasterPainter oldDelegate) {
// 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;
}
}