-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GameMap, a random map generator, and collisions.
- Loading branch information
Showing
4 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Struct, Rand } from "malwoden"; | ||
|
||
export enum TileType { | ||
// all tiles should be truthy | ||
Floor = 1, | ||
Wall, | ||
} | ||
|
||
export class GameMap { | ||
tiles: Struct.Table<TileType>; | ||
width: number; | ||
height: number; | ||
|
||
constructor(tiles: Struct.Table<TileType>, width: number, height: number) { | ||
this.tiles = tiles; | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
static CreateRandom(width: number, height: number): GameMap { | ||
const tiles = new Struct.Table<TileType>(width, height); | ||
const rng = new Rand.AleaRNG(); | ||
const randomWalls = 200; | ||
|
||
// fill the whole map with floor tiles | ||
tiles.fill(TileType.Floor); | ||
|
||
// fill the top and bottom with walls | ||
for (let x = 0; x < width; x++) { | ||
tiles.set({ x, y: 0 }, TileType.Wall); | ||
tiles.set({ x, y: height - 1 }, TileType.Wall); | ||
} | ||
|
||
// fill the left and right with walls | ||
for (let y = 0; y < height; y++) { | ||
tiles.set({ x: 0, y }, TileType.Wall); | ||
tiles.set({ x: width - 1, y }, TileType.Wall); | ||
} | ||
|
||
// fill random spots with walls | ||
for (let i = 0; i < randomWalls; i++) { | ||
const x = rng.nextInt(1, width - 2); | ||
const y = rng.nextInt(1, height - 2); | ||
|
||
tiles.set({ x, y }, TileType.Wall); | ||
} | ||
|
||
// return the new GameMap | ||
return new GameMap(tiles, width, height); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters