feat: implemented getPlayer and getEnemies, not tested

This commit is contained in:
Renge 2022-04-10 19:46:13 -04:00
parent 417315cf08
commit 9aa1495dd0

View File

@ -23,6 +23,8 @@ export default class RandomMapGenerator {
private minRoom: number; private minRoom: number;
private roomPlaced: number; private roomPlaced: number;
private exitFacing: Facing; private exitFacing: Facing;
private enemies: Array<Enemy>;
private player: Vec2;
constructor(JSONFilePath: string, seed: any) { constructor(JSONFilePath: string, seed: any) {
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
@ -40,6 +42,8 @@ export default class RandomMapGenerator {
this.map = new TiledTilemapData(); this.map = new TiledTilemapData();
this.rooms = new Array(); this.rooms = new Array();
this.enemies = new Array();
this.player = new Vec2();
let gen = require('random-seed'); let gen = require('random-seed');
this.gen = new gen(seed); this.gen = new gen(seed);
this.hasExit = false; this.hasExit = false;
@ -104,6 +108,14 @@ export default class RandomMapGenerator {
return this.map; return this.map;
} }
getPlayer(): Vec2 {
return this.player;
}
getEnemies(): Array<Enemy> {
return this.enemies;
}
private putNextRoom(position: Vec2, facing: Facing): boolean { private putNextRoom(position: Vec2, facing: Facing): boolean {
switch (facing) { switch (facing) {
case Facing.LEFT: case Facing.LEFT:
@ -245,8 +257,17 @@ export default class RandomMapGenerator {
this.map.layers[0].data[(room.topLeft.y + i) * width + room.topLeft.x + j] = room.bottomLayer[i * roomWidth + j]; this.map.layers[0].data[(room.topLeft.y + i) * width + room.topLeft.x + j] = room.bottomLayer[i * roomWidth + j];
this.map.layers[1].data[(room.topLeft.y + i) * width + room.topLeft.x + j] = room.topLayer[i * roomWidth + j]; this.map.layers[1].data[(room.topLeft.y + i) * width + room.topLeft.x + j] = room.topLayer[i * roomWidth + j];
} }
if (room.enemies)
for (let index = 0; index < room.enemies.length; index++) {
const enemy = room.enemies[index];
enemy.position.x -= this.minX;
enemy.position.y -= this.minY;
this.enemies.push(enemy);
} }
} }
this.player.x -= this.minX;
this.player.y -= this.minY;
}
private isValidRoom(topLeft: Vec2, bottomRight: Vec2): boolean { private isValidRoom(topLeft: Vec2, bottomRight: Vec2): boolean {
for (let index = 0; index < this.rooms.length; index++) { for (let index = 0; index < this.rooms.length; index++) {
@ -332,6 +353,22 @@ export default class RandomMapGenerator {
room.bottomRight = new Vec2(posX + old.width - 1, posY + old.height - 1); room.bottomRight = new Vec2(posX + old.width - 1, posY + old.height - 1);
room.topLayer = [...old.topLayer]; room.topLayer = [...old.topLayer];
room.bottomLayer = [...old.bottomLayer]; room.bottomLayer = [...old.bottomLayer];
room.enemies = new Array();
if (old.sprites) {
for (let index = 0; index < old.sprites.length; index++) {
const sprite = old.sprites[index];
if (sprite.type == 'player') {
this.player.x = sprite.x;
this.player.y = sprite.y;
}
else {
let tmp = new Enemy();
tmp.type = sprite.type;
tmp.position = new Vec2(posX + sprite.x, posY + sprite.y);
room.enemies.push(tmp);
}
}
}
if (posX < this.minX) if (posX < this.minX)
this.minX = posX; this.minX = posX;
if (posY < this.minY) if (posY < this.minY)
@ -349,6 +386,12 @@ class Room {
bottomRight: Vec2; bottomRight: Vec2;
topLayer: Array<number>; topLayer: Array<number>;
bottomLayer: Array<number>; bottomLayer: Array<number>;
enemies: Array<Enemy>
}
export class Enemy {
type: String;
position: Vec2;
} }
enum Facing { enum Facing {