Skip to content

Commit

Permalink
Stairs to descend will spawn.
Browse files Browse the repository at this point in the history
  • Loading branch information
avinashv committed Sep 6, 2023
1 parent 86a59e3 commit 5f37593
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,21 @@ export class Viewshed extends Component<Viewshed> {
}

/**
* BlocksTile component.
* `BlocksTile` component.
* Attached to `Entity` that exclusively occupies the space in a map tile from other entities that have the same tag.
* Initiates `MapIndexingSystem`.
*/
export class BlocksTile extends Component<BlocksTile> {
}

/**
* `CanDescend` component.
* Attached to `Entity` that allows us to descend a level.
* Initiates `LevelSystem`
*/
export class CanDescend extends Component<CanDescend> {
}

/**
* `Attributes` component.
* Attached to `Entity` that might be in combat to initiate `MeleeCombatSystem`, `HealingSystem`, and `DamageSystem`.
Expand Down
29 changes: 22 additions & 7 deletions src/level-gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, World } from "ecsy";
import { Rand, Vector2 } from "malwoden";
import { Rand, Struct, Vector2 } from "malwoden";

import * as Components from "./components.ts";
import * as Prefabs from "./prefabs.ts";
Expand Down Expand Up @@ -56,6 +56,19 @@ export class LevelGenerator {
return player;
}

/**
* Get a random position within a specified room `Rect`.
* @param room Room `Rect`.
* @returns Vector2 Random position in the room
*/
getRandomRoomPosition(room: Struct.Rect): Vector2 {
// get a random x and y coordinate
const x = this.rng.nextInt(room.v1.x, room.v2.x + 1);
const y = this.rng.nextInt(room.v1.y, room.v2.y + 1);

return { x, y };
}

/**
* Generate a level given configuration information.
* @param config Map size and game world.
Expand All @@ -78,19 +91,21 @@ export class LevelGenerator {
// choose a creature type
const creatureType = this.rng.nextInt(0, 100);
if (creatureType < 50)
Prefabs.placeEntity(Prefabs.goblin(this.world), room.center());
Prefabs.placeEntity(Prefabs.goblin(this.world), this.getRandomRoomPosition(room));
else
Prefabs.placeEntity(Prefabs.orc(this.world), room.center());
Prefabs.placeEntity(Prefabs.orc(this.world), this.getRandomRoomPosition(room));

// create items
if (this.rng.next() < 0.2) {
// get a random spot in the room
const randomX = this.rng.nextInt(room.v1.x, room.v2.x + 1);
const randomY = this.rng.nextInt(room.v1.y, room.v2.y + 1);
Prefabs.placeEntity(Prefabs.bandage(this.world), { x: randomX, y: randomY });
Prefabs.placeEntity(Prefabs.bandage(this.world), this.getRandomRoomPosition(room));
}
}

// create stairs in a room that isn't a player room
const stairRoomIndex = this.rng.nextInt(1, map.rooms.length);
const stairRoom = map.rooms[stairRoomIndex];
Prefabs.placeEntity(Prefabs.stairs(this.world), this.getRandomRoomPosition(stairRoom));

return {
map,
playerStart,
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class Game {
.registerComponent(Components.Consumable)
.registerComponent(Components.IncomingHealing)
.registerComponent(Components.Description)
.registerComponent(Components.CanDescend)

// systems - order matters
.registerSystem(Systems.VisibilitySystem, this)
Expand Down
15 changes: 15 additions & 0 deletions src/prefabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,19 @@ export function bandage(world: World): Entity {
.addComponent(Components.Description, {
text: "Will patch up minor injuries."
});
}

/**
* Create a stairs `Entity`.
* @param world Game world object.
* @return Entity Stairs `Entity`.
*/
export function stairs(world: World): Entity {
return world
.createEntity()
.addComponent(Components.Renderable, {
glyph: Glyph.fromCharCode(CharCode.blackDownPointingTriangle, Color.Cyan),
})
.addComponent(Components.Name, { name: "Stairs" })
.addComponent(Components.CanDescend);
}

0 comments on commit 5f37593

Please sign in to comment.