Files
wolf_dart/lib/features/renderer/raycast_painter.dart
2026-03-13 16:04:14 +01:00

233 lines
6.5 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';
class RaycasterPainter extends CustomPainter {
final Matrix<int> map;
final LinearCoordinates player;
final double playerAngle;
final double fov;
RaycasterPainter({
required this.map,
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,
);
}
}
void _drawTexturedColumn(
Canvas canvas,
int x,
double distance,
double wallX,
int side,
Size size,
int hitWallId,
) {
if (distance <= 0.01) distance = 0.01;
double wallHeight = size.height / distance;
double drawStart = (size.height / 2) - (wallHeight / 2);
double drawEnd = (size.height / 2) + (wallHeight / 2);
// --- PROCEDURAL TEXTURE LOGIC ---
Color baseColor;
// Draw a dark edge on the sides of the block to create "tiles"
if (wallX < 0.05 || wallX > 0.95) {
baseColor = Colors.black87;
} else {
switch (hitWallId) {
case 1:
case 2:
case 3:
baseColor = Colors.grey[600]!; // Standard Grey Stone
break;
case 7:
case 8:
case 19:
baseColor = Colors.brown[600]!; // Wood Paneling
break;
case 9:
case 10:
baseColor = Colors.indigo[800]!; // Blue Stone
break;
case 17:
baseColor = Colors.red[900]!; // Red Brick
break;
case 41:
case 42:
baseColor = Colors.blueGrey; // Elevator walls
break;
default:
baseColor = Colors.teal; // Fallback for unknown IDs
}
}
// Faux-Lighting: Darken East/West walls to give a 3D pop to corners
if (side == 1) {
baseColor = Color.fromARGB(
255,
((baseColor.r * 255).round().clamp(0, 255) * 0.7).toInt(),
((baseColor.g * 255).round().clamp(0, 255) * 0.7).toInt(),
((baseColor.b * 255).round().clamp(0, 255) * 0.7).toInt(),
);
}
// Depth cueing: Dim colors as they get further away
double dimFactor = (1.0 - (distance / 15)).clamp(0.0, 1.0);
Color finalColor = Color.fromARGB(
255,
((baseColor.r * 255).round().clamp(0, 255) * dimFactor).toInt(),
((baseColor.g * 255).round().clamp(0, 255) * dimFactor).toInt(),
((baseColor.b * 255).round().clamp(0, 255) * dimFactor).toInt(),
);
final paint = Paint()
..color = finalColor
..strokeWidth =
1.1 // Prevent transparent gaps between line strokes
..style = PaintingStyle.stroke;
canvas.drawLine(
Offset(x.toDouble(), drawStart),
Offset(x.toDouble(), drawEnd),
paint,
);
}
@override
bool shouldRepaint(covariant RaycasterPainter oldDelegate) {
return oldDelegate.player != player ||
oldDelegate.playerAngle != playerAngle;
}
}