feat: implement some helper functions in RandomMapGenerator

This commit is contained in:
Renge 2022-04-06 18:47:47 -04:00
parent e74b36585f
commit d3b2a7324b
2 changed files with 75 additions and 5 deletions

View File

@ -22,6 +22,7 @@ export default class MapTemplate {
corners?: [Corner, Corner, Corner, Corner];
// Tile to fill all empty spaces between rooms
background: number;
minroom: number;
}
export class Entrance {

View File

@ -19,6 +19,8 @@ export default class RandomMapGenerator {
private roomWithDownEntranceWeight: number;
private rooms: Array<Room>
private gen: any;
private hasExit: boolean;
private minRoom: number;
constructor(JSONFilePath: string, seed: any) {
let xhr = new XMLHttpRequest();
@ -38,6 +40,8 @@ export default class RandomMapGenerator {
this.rooms = new Array();
let gen = require('random-seed');
this.gen = new gen(seed);
this.hasExit = false;
this.minRoom = this.template.minroom;
this.template.rooms.forEach((room) => {
@ -81,15 +85,32 @@ export default class RandomMapGenerator {
}
getMap(): TiledTilemapData {
this.maxX = this.template.entrance.width - 1;
this.maxY = this.template.entrance.height - 1;
let room = this.copyRoom(this.template.entrance, 0, 0);
console.log(room);
this.rooms.push(room);
if (!this.hasExit)
throw new Error("Fail to generate a room with exit!");
return this.map;
}
private putNextRoom(): boolean {
return true;
}
private isValidRoom(topLeft: Vec2, bottomRight: Vec2): boolean {
this.rooms.forEach((room) => {
if (room.topLeft.x < bottomRight.x &&
room.bottomRight.x > topLeft.x &&
room.topLeft.y < bottomRight.y &&
room.bottomRight.y > topLeft.y)
return true;
})
return false;
}
private getEntranceFacing(entrance: Entrance, width: number): Facing {
if (entrance.x === 0)
return Facing.LEFT;
@ -100,12 +121,60 @@ export default class RandomMapGenerator {
return Facing.DOWN;
}
private getRandomRoom(value: number, facing: Facing): RoomTemplate {
let array = this.getRoomArray(facing), weight = this.getRoomWeight(facing);
if (value >= weight)
throw new Error("Random number " + value + " is larger than total weight " + weight);
array.forEach((room) => {
if (value < room.weight)
return room;
value -= room.weight;
})
throw new Error("Cannot find Room! \nRooms: " + JSON.stringify(array) + "\nValue: " + value);
}
private getRoomArray(facing: Facing): Array<RoomTemplate> {
switch (facing) {
case Facing.LEFT:
return this.roomWithLeftEntrance;
case Facing.RIGHT:
return this.roomWithRightEntrance;
case Facing.UP:
return this.roomWithUpEntrance;
case Facing.DOWN:
return this.roomWithDownEntrance;
}
}
private getRoomWeight(facing: Facing): number {
switch (facing) {
case Facing.LEFT:
return this.roomWithLeftEntranceWeight;
case Facing.RIGHT:
return this.roomWithRightEntranceWeight;
case Facing.UP:
return this.roomWithUpEntranceWeight;
case Facing.DOWN:
return this.roomWithDownEntranceWeight;
}
}
private copyRoom(old: RoomTemplate, posX: number, posY: number): Room {
let room = new Room();
room.topLeft = new Vec2(posX, posY);
room.bottomRight = new Vec2(posX + old.width - 1, posY + old.height - 1);
room.topLayer = [...old.topLayer];
room.bottomLayer = [...old.bottomLayer];
if (posX < this.minX)
this.minX = posX;
if (posY < this.minY)
this.minY = posY;
if (posX + old.width - 1 > this.maxX)
this.maxX = posX + old.width - 1;
if (posY + old.height - 1 > this.maxY)
this.maxY = posY + old.height - 1;
return room;
}
}