WIP: Mapping sprite IDs (broken)

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-13 18:06:40 +01:00
parent af17a0aaf7
commit bf8d9d7eb1
7 changed files with 220 additions and 31 deletions

View File

@@ -1,9 +1,10 @@
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:wolf_dart/classes/color_palette.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';
import 'package:wolf_dart/classes/sprite.dart';
class RaycasterPainter extends CustomPainter {
final Matrix<int> map;
@@ -12,6 +13,8 @@ class RaycasterPainter extends CustomPainter {
final double playerAngle;
final double fov;
final Map<String, double> doorOffsets;
final List<Matrix<int>> sprites;
final List<Sprite> entities;
RaycasterPainter({
required this.map,
@@ -20,6 +23,8 @@ class RaycasterPainter extends CustomPainter {
required this.playerAngle,
required this.fov,
required this.doorOffsets,
required this.sprites,
required this.entities,
});
@override
@@ -36,11 +41,15 @@ class RaycasterPainter extends CustomPainter {
int screenWidth = size.width.toInt();
// The 1D Z-Buffer
List<double> zBuffer = List.filled(screenWidth, 0.0);
double dirX = math.cos(playerAngle);
double dirY = math.sin(playerAngle);
double planeX = -dirY * math.tan(fov / 2);
double planeY = dirX * math.tan(fov / 2);
// --- 1. CAST WALLS ---
for (int x = 0; x < screenWidth; x++) {
double cameraX = 2 * x / screenWidth - 1.0;
double rayDirX = dirX + planeX * cameraX;
@@ -51,23 +60,18 @@ class RaycasterPainter extends CustomPainter {
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
double doorOffset = 0.0;
Set<String> ignoredDoors = {};
if (rayDirX < 0) {
stepX = -1;
@@ -84,7 +88,6 @@ class RaycasterPainter extends CustomPainter {
sideDistY = (mapY + 1.0 - player.y) * deltaDistY;
}
// 3. The True DDA Loop
while (!hit) {
if (sideDistX < sideDistY) {
sideDistX += deltaDistX;
@@ -103,13 +106,10 @@ class RaycasterPainter extends CustomPainter {
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);
@@ -117,23 +117,18 @@ class RaycasterPainter extends CustomPainter {
? 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!
continue;
}
}
doorOffset =
currentOffset; // Save the offset so we can slide the texture
doorOffset = currentOffset;
}
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) {
@@ -142,6 +137,9 @@ class RaycasterPainter extends CustomPainter {
perpWallDist = (sideDistY - deltaDistY);
}
// STORE THE DISTANCE IN THE Z-BUFFER!
zBuffer[x] = perpWallDist;
double wallX;
if (side == 0) {
wallX = player.y + perpWallDist * rayDirY;
@@ -162,6 +160,89 @@ class RaycasterPainter extends CustomPainter {
doorOffset,
);
}
// --- 2. DRAW SPRITES ---
// Sort sprites from furthest to closest (Painter's Algorithm)
List<Sprite> 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 (Sprite sprite in activeSprites) {
// Translate sprite position to relative to camera
double spriteX = sprite.x - player.x;
double spriteY = sprite.y - player.y;
// Inverse camera matrix (Transform to screen space)
double invDet = 1.0 / (planeX * dirY - dirX * planeY);
double transformX = invDet * (dirY * spriteX - dirX * spriteY);
double transformY = invDet * (-planeY * spriteX + planeX * spriteY);
// print("Sprite at ${sprite.x}, ${sprite.y} transformY: $transformY");
// Is the sprite in front of the camera?
if (transformY > 0) {
// print(
// "Attempting to draw Sprite Index: ${sprite.spriteIndex} (Total Sprites Loaded: ${sprites.length})",
// );
int spriteScreenX = ((screenWidth / 2) * (1 + transformX / transformY))
.toInt();
// Calculate height and width (Sprites are 64x64 squares)
int spriteHeight = (size.height / transformY).abs().toInt();
int spriteWidth = spriteHeight;
int drawStartX = -spriteWidth ~/ 2 + spriteScreenX;
int drawEndX = spriteWidth ~/ 2 + spriteScreenX;
// Clip to screen boundaries
int clipStartX = math.max(0, drawStartX);
int clipEndX = math.min(screenWidth - 1, drawEndX);
for (int stripe = clipStartX; stripe < clipEndX; stripe++) {
// THE Z-BUFFER CHECK!
// Only draw if the sprite is closer to the camera than the wall at this pixel column
if (transformY < zBuffer[stripe]) {
int texX = ((stripe - drawStartX) * 64 / spriteWidth).toInt().clamp(
0,
63,
);
double startY = (size.height / 2) - (spriteHeight / 2);
double stepY = spriteHeight / 64.0;
// Safeguard against bad sprite indices
int safeIndex = sprite.spriteIndex.clamp(0, sprites.length - 1);
Matrix<int> spritePixels = sprites[safeIndex];
for (int ty = 0; ty < 64; ty++) {
int colorByte = spritePixels[texX][ty];
// Only draw if the pixel is NOT 255 (Magenta Transparency)
if (colorByte != 255) {
double endY = startY + stepY;
if (endY > 0 && startY < size.height) {
canvas.drawLine(
Offset(stripe.toDouble(), startY),
Offset(stripe.toDouble(), endY),
Paint()
..color = ColorPalette.vga[colorByte]
..strokeWidth = 1.1,
);
}
}
startY += stepY;
}
}
}
}
}
}
void _drawTexturedColumn(