Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Recursive function fails for no specified reason, just exits the application. If I had a breakpoint that it hit it says, "The debugger's worker process (msvsmon.exe) unexpectedly exited. Debugging will be aborted."
- class Floor {
- public grid: Array<Array<GridTile>>;
- private loop(x: number, y: number, dir: number): void {
- var mag: number = 0;
- // Set mag (length of wall)
- if (dir == 0) mag = 16 - y;
- else if (dir == 1) mag = 16 - x;
- else if (dir == 2) mag = y;
- else mag = x;
- // Make a wall until you hit another wall in the specified direction
- while (!this.grid[x][y].wall && mag > 4) {
- this.grid[x][y].wall = !this.grid[x][y].wall;
- if (dir == 0) y++;
- else if (dir == 1) x++;
- else if (dir == 2) y--;
- else if (dir == 3) x--;
- }
- // Make a door in the middle-ish
- var door = Math.ceil((Math.random() * (mag * (2 / 3))) + (mag / 6));
- if (dir == 0 || dir == 2) this.grid[x][door].door = true;
- else if (dir == 1 || dir == 3) this.grid[door][y].door = true;
- var newCord1: number = door;
- var newCord2: number = door;
- // Get new coords
- for (var i: number = 0; newCord1 == door && newCord2 == door && i < 50; i++) {
- newCord1 = Math.ceil((Math.random() * (mag * (2 / 3))) + (mag / 6));
- newCord2 = Math.ceil((Math.random() * (mag * (2 / 3))) + (mag / 6));
- }
- // Do the recursion
- if (mag > 4) {
- // Recursive x
- if (dir == 0 || dir == 2) {
- // Recurse right
- this.loop(x + 1, newCord1, 1);
- // Recurse left
- this.loop(x - 1, newCord2, 3);
- }
- // Recursive y
- else if (dir == 1 || dir == 3) {
- // Recurse up
- this.loop(newCord1, y + 1, 0); // If commented it at least runs, needs to be refined but it's better
- // Recurse down
- this.loop(newCord2, y - 1, 2);
- }
- }
- }
- constructor() {
- this.grid = [];
- for (var j: number = 0; j <= 17; j++) {
- this.grid[j] = [];
- for (var k: number = 0; k <= 17; k++) {
- this.grid[j][k] = new GridTile();
- this.grid[j][k].wall = (j == 0 || j == 17) ? true : (k == 0 || k == 17) ? true : false;
- }
- }
- var dir = Math.round(Math.random());
- var x: number = (dir == 0) ? Math.floor(Math.random() * 9) + 4 : 1;
- var y: number = (dir == 1) ? Math.floor(Math.random() * 9) + 4 : 1;
- this.loop(x, y, dir);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment