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

215 lines
6.1 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;
RaycasterPainter({
required this.map,
required this.textures,
required this.player,
required this.playerAngle,
required this.fov,
});
@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();
// 2. Camera Plane Setup
// Direction vector of the player
double dirX = math.cos(playerAngle);
double dirY = math.sin(playerAngle);
// The camera plane is perpendicular to the direction vector.
// Multiplying by tan(fov/2) scales the plane to match our field of view.
double planeX = -dirY * math.tan(fov / 2);
double planeY = dirX * math.tan(fov / 2);
for (int x = 0; x < screenWidth; x++) {
// Calculate where on the camera plane this ray passes (-1 is left edge, 1 is right edge)
double cameraX = 2 * x / screenWidth - 1.0;
double rayDirX = dirX + planeX * cameraX;
double rayDirY = dirY + planeY * cameraX;
// Current map box we are in
int mapX = player.x.toInt();
int mapY = player.y.toInt();
// Length of ray from current position to next x or y-side
double sideDistX;
double sideDistY;
// Length of ray from one x or y-side to next x or y-side
double deltaDistX = (rayDirX == 0)
? double.infinity
: (1.0 / rayDirX).abs();
double deltaDistY = (rayDirY == 0)
? double.infinity
: (1.0 / rayDirY).abs();
double perpWallDist;
// Direction to step in x or y direction (+1 or -1)
int stepX;
int stepY;
bool hit = false;
// 0 for North/South (vertical) walls, 1 for East/West (horizontal) walls
int side = 0;
int hitWallId = 0;
// Calculate step and initial sideDist
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) {
// Jump to next map square, either in x-direction, or in y-direction
if (sideDistX < sideDistY) {
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
} else {
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
// Check bounds and wall collisions
if (mapY < 0 ||
mapY >= map.length ||
mapX < 0 ||
mapX >= map[0].length) {
hit = true;
perpWallDist = 20.0; // Out of bounds fallback
} else if (map[mapY][mapX] > 0) {
hit = true;
hitWallId = map[mapY][mapX];
}
}
// Calculate distance projected on camera direction (No fisheye effect!)
if (side == 0) {
perpWallDist = (sideDistX - deltaDistX);
} else {
perpWallDist = (sideDistY - deltaDistY);
}
// 4. Calculate exact wall hit coordinate for textures
double wallX;
if (side == 0) {
wallX = player.y + perpWallDist * rayDirY;
} else {
wallX = player.x + perpWallDist * rayDirX;
}
wallX -= wallX.floor(); // Get just the fractional part (0.0 to 0.99)
_drawTexturedColumn(
canvas,
x,
perpWallDist,
wallX,
side,
size,
hitWallId,
textures,
);
}
}
void _drawTexturedColumn(
Canvas canvas,
int x,
double distance,
double wallX,
int side,
Size size,
int hitWallId,
List<Matrix<int>> textures,
) {
if (distance <= 0.01) distance = 0.01;
double wallHeight = size.height / distance;
int drawStart = ((size.height / 2) - (wallHeight / 2)).toInt();
// 1. PERFECT 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);
if (hitWallId >= 90) {
texNum = 98; // 98 is the canonical index for the Wood Door in VSWAP
// Optional: Doors don't usually have a dark E/W variant, so we don't add +1 for side
} else {
// Standard wall texture pairing
texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2);
if (side == 1) texNum += 1;
}
int texX = (wallX * 64).toInt().clamp(0, 63);
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;
}
}