Add openable doors to generated rooms
This commit is contained in:
@@ -57,8 +57,19 @@ export function generateWorld(floor: number, runState: RunState): { world: World
|
||||
};
|
||||
|
||||
placeEnemies(floor, rooms, actors, random);
|
||||
|
||||
// Place doors for dungeon levels (Uniform/Digger)
|
||||
// Caves (Floors 10+) shouldn't have manufactured doors
|
||||
if (floor <= 9) {
|
||||
placeDoors(width, height, tiles, rooms, random);
|
||||
}
|
||||
|
||||
decorate(width, height, tiles, random, exit);
|
||||
|
||||
// CRITICAL FIX: Ensure player start position is always clear!
|
||||
// Otherwise spawning in Grass (which blocks vision) makes the player blind.
|
||||
tiles[playerY * width + playerX] = TileType.EMPTY;
|
||||
|
||||
return {
|
||||
world: { width, height, tiles, actors, exit },
|
||||
playerId
|
||||
@@ -347,3 +358,40 @@ function placeEnemies(floor: number, rooms: Room[], actors: Map<EntityId, Actor>
|
||||
|
||||
|
||||
export const makeTestWorld = generateWorld;
|
||||
|
||||
function placeDoors(width: number, height: number, tiles: Tile[], rooms: Room[], random: () => number): void {
|
||||
const checkAndPlaceDoor = (x: number, y: number) => {
|
||||
const i = idx({ width, height } as any, x, y);
|
||||
if (tiles[i] === TileType.EMPTY) {
|
||||
// Found a connection (floor tile on perimeter)
|
||||
|
||||
// 50% chance to place a door
|
||||
if (random() < 0.5) {
|
||||
// 90% chance for closed door, 10% for open
|
||||
tiles[i] = random() < 0.9 ? TileType.DOOR_CLOSED : TileType.DOOR_OPEN;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (const room of rooms) {
|
||||
// Scan top and bottom walls
|
||||
const topY = room.y - 1;
|
||||
const bottomY = room.y + room.height;
|
||||
|
||||
// Scan horizontal perimeters (iterate x from left-1 to right+1 to cover corners too if needed,
|
||||
// but usually doors are in the middle segments. Let's cover the full range adjacent to room.)
|
||||
for (let x = room.x; x < room.x + room.width; x++) {
|
||||
if (topY >= 0) checkAndPlaceDoor(x, topY);
|
||||
if (bottomY < height) checkAndPlaceDoor(x, bottomY);
|
||||
}
|
||||
|
||||
// Scan left and right walls
|
||||
const leftX = room.x - 1;
|
||||
const rightX = room.x + room.width;
|
||||
|
||||
for (let y = room.y; y < room.y + room.height; y++) {
|
||||
if (leftX >= 0) checkAndPlaceDoor(leftX, y);
|
||||
if (rightX < width) checkAndPlaceDoor(rightX, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user