Files
wolf_dart/lib/features/renderer/raycast_painter.dart
2026-03-13 17:24:35 +01:00

234 lines
6.6 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:wolf_dart/classes/linear_coordinates.dart';
import 'package:wolf_dart/classes/matrix.dart';
import 'package:wolf_dart/features/renderer/color_palette.dart';
class RaycasterPainter extends CustomPainter {
final Matrix<int> map;
final List<Matrix<int>> textures;
final LinearCoordinates player;
final double playerAngle;
final double fov;
final Map<String, double> doorOffsets;
RaycasterPainter({
required this.map,
required this.textures,
required this.player,
required this.playerAngle,
required this.fov,
required this.doorOffsets,
});
@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]!,
);
int screenWidth = size.width.toInt();
double dirX = math.cos(playerAngle);
double dirY = math.sin(playerAngle);
double planeX = -dirY * math.tan(fov / 2);
double planeY = dirX * math.tan(fov / 2);
for (int x = 0; x < screenWidth; x++) {
double cameraX = 2 * x / screenWidth - 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; // Track the slide amount to pass to the texture drawer
Set<String> ignoredDoors =
{}; // Prevent the ray from checking the same door gap twice
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;
}
// 3. The True DDA Loop
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) {
// NEW: DOOR PASS-THROUGH LOGIC
String doorKey = '$mapX,$mapY';
if (map[mapY][mapX] >= 90 && !ignoredDoors.contains(doorKey)) {
double currentOffset = doorOffsets[doorKey] ?? 0.0;
if (currentOffset > 0.0) {
// Calculate exactly where the ray hit the block
double perpWallDistTemp = (side == 0)
? (sideDistX - deltaDistX)
: (sideDistY - deltaDistY);
double wallXTemp = (side == 0)
? player.y + perpWallDistTemp * rayDirY
: player.x + perpWallDistTemp * rayDirX;
wallXTemp -= wallXTemp.floor();
// If the hit coordinate is less than the open amount, the ray passes through!
if (wallXTemp < currentOffset) {
ignoredDoors.add(doorKey);
continue; // Skip the rest of this loop iteration and keep raycasting!
}
}
doorOffset =
currentOffset; // Save the offset so we can slide the texture
}
hit = true;
hitWallId = map[mapY][mapX];
}
}
// If the ray escaped the map, don't draw garbage, just draw the background!
if (hitOutOfBounds) continue;
if (side == 0) {
perpWallDist = (sideDistX - deltaDistX);
} else {
perpWallDist = (sideDistY - deltaDistY);
}
double wallX;
if (side == 0) {
wallX = player.y + perpWallDist * rayDirY;
} else {
wallX = player.x + perpWallDist * rayDirX;
}
wallX -= wallX.floor();
_drawTexturedColumn(
canvas,
x,
perpWallDist,
wallX,
side,
size,
hitWallId,
textures,
doorOffset,
);
}
}
void _drawTexturedColumn(
Canvas canvas,
int x,
double distance,
double wallX,
int side,
Size size,
int hitWallId,
List<Matrix<int>> textures,
double doorOffset,
) {
if (distance <= 0.01) distance = 0.01;
double wallHeight = size.height / distance;
int drawStart = ((size.height / 2) - (wallHeight / 2)).toInt();
// TEXTURE MAPPING
// Wolf3D stores textures in pairs. Even = N/S (Light), Odd = E/W (Dark).
int texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2);
int texX = (wallX * 64).toInt().clamp(0, 63);
// INTERCEPT DOORS
if (hitWallId >= 90) {
// Safely clamp the door texture index so it never crashes the paint loop!
texNum = 98.clamp(0, textures.length - 1);
texX = ((wallX - doorOffset) * 64).toInt().clamp(0, 63);
} else {
// Standard wall texture pairing
texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2);
if (side == 1) texNum += 1;
}
if (side == 0 && math.cos(playerAngle) > 0) texX = 63 - texX;
if (side == 1 && math.sin(playerAngle) < 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];
// 2. NO MORE SHADOW MATH
// Because we selected the correct dark texture above, we just draw the raw color!
Color pixelColor = ColorPalette.vga[colorByte];
double endY = startY + stepY;
if (endY > 0 && startY < size.height) {
canvas.drawLine(
Offset(x.toDouble(), startY),
Offset(x.toDouble(), endY),
Paint()
..color = pixelColor
..strokeWidth = 1.1,
);
}
startY += stepY;
}
}
@override
bool shouldRepaint(covariant RaycasterPainter oldDelegate) {
return oldDelegate.player != player ||
oldDelegate.playerAngle != playerAngle;
}
}