Fixed color palette and player starting location

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-13 17:05:15 +01:00
parent 464a22e1f3
commit 0c3ca1c090
5 changed files with 207 additions and 119 deletions

View File

@@ -22,12 +22,14 @@ class _WolfRendererState extends State<WolfRenderer>
late WolfMap gameMap;
late Matrix<int> currentLevel;
bool _isLoading = true;
LinearCoordinates player = (x: 2.5, y: 2.5);
double playerAngle = 0.0;
final double fov = math.pi / 3;
late LinearCoordinates player;
late double playerAngle;
bool _isLoading = true;
bool _spaceWasPressed = false;
@override
void initState() {
super.initState();
@@ -41,16 +43,41 @@ class _WolfRendererState extends State<WolfRenderer>
// 2. Extract Level 1 (E1M1)
currentLevel = gameMap.levels[0].wallGrid;
// 3. (Optional) Remap the Wolf3D floor IDs so they work with your raycaster.
// In Wolf3D, 90 through 106 are usually empty floor. Your raycaster currently
// expects 0 to be empty space. Let's force them to 0 for now.
final Matrix<int> objectLevel = gameMap.levels[0].objectGrid;
// 1. SCAN FOR PLAYER SPAWN
for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
// In Wolf3D, wall values are 1 through ~63.
// Values 90+ represent empty floor spaces and doors.
// Let's zero out anything 90 or above, and LEAVE the walls alone.
if (currentLevel[y][x] >= 90) {
currentLevel[y][x] = 0; // Empty space
int objId = objectLevel[y][x];
// In Wolf3D, IDs 19-22 represent the player's spawn point and facing direction.
if (objId >= 19 && objId <= 22) {
// Place the player perfectly in the center of the block
player = (x: x + 0.5, y: y + 0.5);
// Map the ID to standard radians
switch (objId) {
case 19:
playerAngle = 3 * math.pi / 2; // North (Facing up the Y-axis)
case 20:
playerAngle = 0.0; // East (Facing right)
case 21:
playerAngle = math.pi / 2; // South (Facing down)
case 22:
playerAngle = math.pi; // West (Facing left)
}
}
}
}
// 2. CLEAN UP WALLS / PRESERVE DOORS
for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
int id = currentLevel[y][x];
if ((id >= 1 && id <= 63) || (id >= 90 && id <= 101)) {
// Leave walls and doors solid
} else {
currentLevel[y][x] = 0;
}
}
}
@@ -105,38 +132,79 @@ class _WolfRendererState extends State<WolfRenderer>
}
void _tick(Duration elapsed) {
const double moveSpeed = 0.05;
const double turnSpeed = 0.04;
const double moveSpeed = 0.12;
const double turnSpeed = 0.08;
double newX = player.x;
double newY = player.y;
double moveStepX = 0;
double moveStepY = 0;
final pressedKeys = HardwareKeyboard.instance.logicalKeysPressed;
// 1. Calculate intended movement amounts
if (pressedKeys.contains(LogicalKeyboardKey.keyW)) {
newX += math.cos(playerAngle) * moveSpeed;
newY += math.sin(playerAngle) * moveSpeed;
moveStepX += math.cos(playerAngle) * moveSpeed;
moveStepY += math.sin(playerAngle) * moveSpeed;
}
if (pressedKeys.contains(LogicalKeyboardKey.keyS)) {
newX -= math.cos(playerAngle) * moveSpeed;
newY -= math.sin(playerAngle) * moveSpeed;
moveStepX -= math.cos(playerAngle) * moveSpeed;
moveStepY -= math.sin(playerAngle) * moveSpeed;
}
// 2. Handle Turning
if (pressedKeys.contains(LogicalKeyboardKey.keyA)) {
playerAngle -= turnSpeed;
}
if (pressedKeys.contains(LogicalKeyboardKey.keyD)) {
playerAngle += turnSpeed;
}
// Keep the angle mapped cleanly between 0 and 2*PI (optional, but good practice)
if (playerAngle < 0) playerAngle += 2 * math.pi;
if (playerAngle > 2 * math.pi) playerAngle -= 2 * math.pi;
if (currentLevel[newY.toInt()][newX.toInt()] == 0) {
player = (x: newX, y: newY);
// 3. Wall Sliding Collision (with Hitbox Margin!)
// A 0.3 margin keeps the camera plane safely out of the walls
const double margin = 0.3;
double newX = player.x + moveStepX;
// Check the edge of our hitbox, not just the center point
int checkX = (moveStepX > 0)
? (newX + margin).toInt()
: (newX - margin).toInt();
// Try to move along the X axis
if (currentLevel[player.y.toInt()][checkX] == 0) {
player = (x: newX, y: player.y);
}
double newY = player.y + moveStepY;
int checkY = (moveStepY > 0)
? (newY + margin).toInt()
: (newY - margin).toInt();
// Try to move along the Y axis
if (currentLevel[checkY][player.x.toInt()] == 0) {
player = (x: player.x, y: newY);
}
// 4. DOOR INTERACTION LOGIC
bool isSpacePressed = pressedKeys.contains(LogicalKeyboardKey.space);
if (isSpacePressed && !_spaceWasPressed) {
int targetX = (player.x + math.cos(playerAngle)).toInt();
int targetY = (player.y + math.sin(playerAngle)).toInt();
if (targetY > 0 &&
targetY < currentLevel.length &&
targetX > 0 &&
targetX < currentLevel[0].length) {
int targetBlock = currentLevel[targetY][targetX];
if (targetBlock >= 90) {
currentLevel[targetY][targetX] = 0;
}
}
}
_spaceWasPressed = isSpacePressed;
setState(() {});
}