fix: wrong format of forest template

feat: some functions in RMG
This commit is contained in:
Renge 2022-04-06 18:01:23 -04:00
parent feb9fb824f
commit e74b36585f
5 changed files with 560 additions and 437 deletions

View File

@ -1,6 +1,4 @@
{
"maptemplate": [
{
"columns":8,
"tileheight":16,
"tilewidth":16,
@ -11,7 +9,7 @@
"margin":0,
"spacing":0,
"name":"forest_tileset",
"image":"forest_tileset.png",
"image":"shattered_sword_assets/tilemaps/forest_tileset.png",
"entrance": {
"width": 30,
"height": 20,
@ -425,7 +423,6 @@
]
}
],
"background": 0
}
]
"background": 0,
"minroom": 10
}

30
package-lock.json generated
View File

@ -9,6 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"random-seed": "^0.3.0",
"typescript-require": "^0.3.0"
},
"devDependencies": {
@ -2660,6 +2661,11 @@
"integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=",
"dev": true
},
"node_modules/json-stringify-safe": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz",
"integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus="
},
"node_modules/jsonify": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz",
@ -3635,6 +3641,17 @@
"node": ">=0.4.x"
}
},
"node_modules/random-seed": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/random-seed/-/random-seed-0.3.0.tgz",
"integrity": "sha1-2UXy4fOPSejViRNDG4v2u5N1Vs0=",
"dependencies": {
"json-stringify-safe": "^5.0.1"
},
"engines": {
"node": ">= 0.6.0"
}
},
"node_modules/randombytes": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
@ -7275,6 +7292,11 @@
"integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=",
"dev": true
},
"json-stringify-safe": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz",
"integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus="
},
"jsonify": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz",
@ -8075,6 +8097,14 @@
"integrity": "sha1-nsYfeQSYdXB9aUFFlv2Qek1xHnM=",
"dev": true
},
"random-seed": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/random-seed/-/random-seed-0.3.0.tgz",
"integrity": "sha1-2UXy4fOPSejViRNDG4v2u5N1Vs0=",
"requires": {
"json-stringify-safe": "^5.0.1"
}
},
"randombytes": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",

View File

@ -19,6 +19,7 @@
"watchify": "^3.11.1"
},
"dependencies": {
"random-seed": "^0.3.0",
"typescript-require": "^0.3.0"
}
}

View File

@ -20,6 +20,7 @@ export default class MainMenu extends Scene {
private mainMenu: Layer;
private about: Layer;
private control: Layer;
private rmg: RandomMapGenerator;
animatedSprite: AnimatedSprite;
@ -42,10 +43,10 @@ export default class MainMenu extends Scene {
console.log(this.save.getLevel());
this.rmg = new RandomMapGenerator("shattered_sword_assets/jsons/forest_template.json", 114514);
this.rmg.getMap();
new RandomMapGenerator("shattered_sword_assets/jsons/forest_template.json");
// Scene has started, so start playing music
//this.emitter.fireEvent(GameEventType.PLAY_SOUND, {key: "menu", loop: true, holdReference: true});

View File

@ -1,6 +1,6 @@
import { TiledTilemapData } from "../../Wolfie2D/DataTypes/Tilesets/TiledData";
import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
import MapTemplate, { Entrance } from "./DataTypes/MapTemplate";
import MapTemplate, { Entrance, RoomTemplate } from "./DataTypes/MapTemplate";
export default class RandomMapGenerator {
private template: MapTemplate;
@ -9,17 +9,104 @@ export default class RandomMapGenerator {
private minY: number;
private maxY: number;
private map: TiledTilemapData;
private roomWithLeftEntrance: Array<RoomTemplate>;
private roomWithLeftEntranceWeight: number;
private roomWithRightEntrance: Array<RoomTemplate>;
private roomWithRightEntranceWeight: number;
private roomWithUpEntrance: Array<RoomTemplate>;
private roomWithUpEntranceWeight: number;
private roomWithDownEntrance: Array<RoomTemplate>;
private roomWithDownEntranceWeight: number;
private rooms: Array<Room>
private gen: any;
constructor(JSONFilePath: string) {
constructor(JSONFilePath: string, seed: any) {
let xhr = new XMLHttpRequest();
xhr.overrideMimeType("application/json");
xhr.open('GET', JSONFilePath, false);
xhr.send(null);
this.template = JSON.parse(xhr.responseText);
this.roomWithLeftEntrance = new Array();
this.roomWithRightEntrance = new Array();
this.roomWithUpEntrance = new Array();
this.roomWithDownEntrance = new Array();
this.minX = this.minY = this.maxX = this.maxY =
this.roomWithLeftEntranceWeight = this.roomWithRightEntranceWeight =
this.roomWithUpEntranceWeight = this.roomWithDownEntranceWeight = 0;
this.map = new TiledTilemapData();
this.rooms = new Array();
let gen = require('random-seed');
this.gen = new gen(seed);
this.template.rooms.forEach((room) => {
let left = false, right = false, up = false, down = false;
room.entrances.forEach((entrance) => {
let facing = this.getEntranceFacing(entrance, room.width);
switch (facing) {
case Facing.LEFT:
left = true;
break;
case Facing.RIGHT:
right = true;
break;
case Facing.UP:
up = true;
break;
case Facing.DOWN:
down = true;
break;
default:
break;
}
})
if (left) {
this.roomWithLeftEntrance.push(room);
this.roomWithLeftEntranceWeight += room.weight;
}
if (right) {
this.roomWithRightEntrance.push(room);
this.roomWithRightEntranceWeight += room.weight;
}
if (up) {
this.roomWithUpEntrance.push(room);
this.roomWithUpEntranceWeight += room.weight;
}
if (down) {
this.roomWithDownEntrance.push(room);
this.roomWithDownEntranceWeight += room.weight;
}
})
}
printRoom() {
console.log(this.template);
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);
return this.map;
}
private getEntranceFacing(entrance: Entrance, width: number): Facing {
if (entrance.x === 0)
return Facing.LEFT;
else if (entrance.x === width - 1)
return Facing.RIGHT
else if (entrance.y === 0)
return Facing.UP;
return Facing.DOWN;
}
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];
return room;
}
}
@ -29,3 +116,10 @@ class Room {
topLayer: Array<number>;
bottomLayer: Array<number>;
}
enum Facing {
LEFT = "left",
RIGHT = "right",
UP = "up",
DOWN = "down"
}