feat: implemented some functions in RandomMapGenerator

This commit is contained in:
Renge 2022-04-06 19:29:17 -04:00
parent d3b2a7324b
commit 1679695b10
3 changed files with 75 additions and 17 deletions

View File

@ -20,7 +20,7 @@ export default class MainMenu extends Scene {
private mainMenu: Layer;
private about: Layer;
private control: Layer;
private rmg: RandomMapGenerator;
// private rmg: RandomMapGenerator;
animatedSprite: AnimatedSprite;
@ -33,18 +33,18 @@ export default class MainMenu extends Scene {
//TODO
startScene(): void{
this.config = new ConfigManager();
this.save = new SaveManager();
console.log(this.config.getVolume());
this.config.setVolume(100);
console.log(this.config.getVolume());
console.log(this.save.getLevel());
this.save.setLevel(10);
// this.config = new ConfigManager();
// this.save = new SaveManager();
// console.log(this.config.getVolume());
// this.config.setVolume(100);
// console.log(this.config.getVolume());
// console.log(this.save.getLevel());
// this.save.setLevel(10);
console.log(this.save.getLevel());
// console.log(this.save.getLevel());
this.rmg = new RandomMapGenerator("shattered_sword_assets/jsons/forest_template.json", 114514);
this.rmg.getMap();
// this.rmg = new RandomMapGenerator("shattered_sword_assets/jsons/forest_template.json", 114514);
// this.rmg.getMap();

View File

@ -1,6 +1,7 @@
import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
import Debug from "../../Wolfie2D/Debug/Debug";
import { GameEventType } from "../../Wolfie2D/Events/GameEventType";
import RandomMapGenerator from "../Tools/RandomMapGenerator";
import GameLevel from "./GameLevel";
@ -10,8 +11,10 @@ export default class Tutorial extends GameLevel{
loadScene(): void {
// Load resources
// this.load.tilemap("forest1", "shattered_sword_assets/tilemaps/Tutorial.json");
let map = localStorage.getItem("map");
this.load.tilemapFromObject("forest1", JSON.parse(map));
// let map = localStorage.getItem("map");
let rmg = new RandomMapGenerator("shattered_sword_assets/jsons/forest_template.json", 114514);
let map = rmg.getMap();
this.load.tilemapFromObject("forest1", map);
this.load.spritesheet("player", "shattered_sword_assets/spritesheets/Hiro.json")
//load music here

View File

@ -1,4 +1,4 @@
import { TiledTilemapData } from "../../Wolfie2D/DataTypes/Tilesets/TiledData";
import { TiledLayerData, TiledTilemapData } from "../../Wolfie2D/DataTypes/Tilesets/TiledData";
import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
import MapTemplate, { Entrance, RoomTemplate } from "./DataTypes/MapTemplate";
@ -89,9 +89,9 @@ export default class RandomMapGenerator {
this.rooms.push(room);
if (!this.hasExit)
throw new Error("Fail to generate a room with exit!");
// if (!this.hasExit)
// throw new Error("Fail to generate a map with exit!");
this.fillData();
return this.map;
}
@ -100,6 +100,61 @@ export default class RandomMapGenerator {
return true;
}
private fillData() {
let width = this.maxX - this.minX + 1;
let height = this.maxY - this.minY + 1;
this.map.layers = new Array(2);
this.map.layers[0] = new TiledLayerData;
this.map.layers[1] = new TiledLayerData;
this.map.width = this.map.layers[0].width = this.map.layers[1].width = width;
this.map.height = this.map.layers[0].height = this.map.layers[1].height = height;
this.map.tileheight = this.template.tileheight;
this.map.tilewidth = this.template.tilewidth;
this.map.orientation = "orthogonal";
this.map.layers[0].x = this.map.layers[0].y = this.map.layers[1].x = this.map.layers[1].y = 0;
this.map.layers[0].opacity = this.map.layers[1].opacity = 1;
this.map.layers[0].visible = this.map.layers[1].visible = true;
this.map.layers[0].type = this.map.layers[1].type = "tilelayer";
this.map.layers[0].name = "Floor";
this.map.layers[1].name = "Wall";
this.map.layers[0].properties = [{
name: "Collidable",
type: "bool",
value: false
}]
this.map.layers[1].properties = [{
name: "Collidable",
type: "bool",
value: true
}]
this.map.tilesets = [{
columns: this.template.columns,
tilewidth: this.template.tilewidth,
tileheight: this.template.tileheight,
tilecount: this.template.tilecount,
firstgid: this.template.firstgid,
imageheight: this.template.imageheight,
imagewidth: this.template.imagewidth,
margin: this.template.margin,
spacing: this.template.spacing,
name: this.template.name,
image: this.template.image
}]
this.map.layers[0].data = new Array(width * height).fill(this.template.background);
this.map.layers[1].data = new Array(width * height);
this.rooms.forEach((room) => {
let roomWidth = room.bottomRight.x - room.topLeft.x + 1;
let roomHeight = room.bottomRight.y - room.topLeft.y + 1;
for (let i = 0; i < roomHeight; i++)
for (let j = 0; j < 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];
}
})
}
private isValidRoom(topLeft: Vec2, bottomRight: Vec2): boolean {
this.rooms.forEach((room) => {
if (room.topLeft.x < bottomRight.x &&