Home Reference Source Repository
import Dungeon from 'roguelike/app/dungeons/Dungeon.js'
public class | source

Dungeon

Extends:

Observable → Dungeon

An explorable dungeon in the game. Contains a grid of tiles and the high-level logic for advancing the game state. Optionally contains a set of victory and defeat conditions

Constructor Summary

Public Constructor
public

constructor(width: number, height: number)

Initializes an empty dungeon with the specified width

Method Summary

Public Methods
public

Tells if the dungeon's state can advance.

public

Dispatches a GameEvent to all observers

public

Performs an operation with each tile in the map

public

Gets the creature whose turn it is to move.

public
public
public
public
public

getRng(): Random

Gets an RNG for use with random effects.

public

getTile(param1: Creature | number, param2: number): *

Gets a tile from coordinates or a creature

public

getTiles(filter: function): *

Gets a 1-D array of the tiles in the map, optionally filtered

public
public

Tells if the game has ended through either defeat or victory

public

isConnected(isTraversable: function): *

Tells whether the dungeon is traversable.

public

moveCreature(creature: Creature, x: number, y: number)

Positions a creature within the Dungeon.

public

removeCreature(param1: Creature | number, param2: number)

Removes a creature from the dungeon, either by using a reference to the creature, or coordinates

public

Advances the dungeon by one timestep.

public

Advances the game state until the player's next move or the game is over. This contains the game loop.

public

Sets the conditions for victory and defeat in the Dungeon

public

setTile(tile: Tile, x: number, y: number)

Changes the tile at the specified position

Inherited Summary

From class Observable
public

addObserver(observer: *)

public

removeObserver(observer: *)

Public Constructors

public constructor(width: number, height: number) source

Initializes an empty dungeon with the specified width

Override:

Observable#constructor

Params:

NameTypeAttributeDescription
width number

The number of columns in the dungeon

height number

The number of rows in the dungeon

Public Methods

public canAdvance(): boolean source

Tells if the dungeon's state can advance.

Return:

boolean

false if the game has ended or if the game is blocked waiting for the player's move; true otherwise.

public fireEvent(event: GameEvent) source

Dispatches a GameEvent to all observers

Params:

NameTypeAttributeDescription
event GameEvent

An event representing something that happened in the Dungeon

public forEachTile(func: function) source

Performs an operation with each tile in the map

Params:

NameTypeAttributeDescription
func function

A function that will be called for each tile. The first param is the Tile. The second is the x-coordinate; the third, the y-coordinate

public getActiveCreature(): Creature source

Gets the creature whose turn it is to move. This will be a creature whose speed counter has reached 0. Ties are broken with the creature's base speed.

Return:

Creature

The creature that can move next.

public getCreatures(): Array<Creature> source

Return:

Array<Creature>

The creatures in the dungeon

public getCurrentTimestep(): number source

Return:

number

The number of timesteps that have occurred

public getHeight(): number source

Return:

number

The number of rows in the dungeon

public getPlayableCharacter(): PlayableCharacter source

Return:

PlayableCharacter

The player's character, or null if none exists.

public getRng(): Random source

Gets an RNG for use with random effects. All creatures behaving randomly should use this RNG.

Return:

Random

An RNG for random effects within the dungeon

public getTile(param1: Creature | number, param2: number): * source

Gets a tile from coordinates or a creature

Params:

NameTypeAttributeDescription
param1 Creature | number
param2 number
  • optional

Return:

*

public getTiles(filter: function): * source

Gets a 1-D array of the tiles in the map, optionally filtered

Params:

NameTypeAttributeDescription
filter function

An Array.filter predicate. The first parameter will be a Tile

Return:

*

public getWidth(): number source

Return:

number

The number of columns in the dungeon

public hasEnded(): boolean source

Tells if the game has ended through either defeat or victory

Return:

boolean

true if the player has won or lost; false otherwise

public isConnected(isTraversable: function): * source

Tells whether the dungeon is traversable.

Params:

NameTypeAttributeDescription
isTraversable function
  • optional

Optional predicate for determining if a tile counts as traversable. Defaults to testing if tile has a floor and isn't solid

Return:

*

public moveCreature(creature: Creature, x: number, y: number) source

Positions a creature within the Dungeon. If the creature is already present, it will be removed from it's previous location first

Params:

NameTypeAttributeDescription
creature Creature

The creature to add or reposition

x number

The x-coordinate

y number

The y-coordinate

public removeCreature(param1: Creature | number, param2: number) source

Removes a creature from the dungeon, either by using a reference to the creature, or coordinates

Params:

NameTypeAttributeDescription
param1 Creature | number
param2 number
  • optional

public resolveNextStep() source

Advances the dungeon by one timestep. During this time, multiple creatures can move (if their speed counters have reached 0).

public resolveUntilBlocked() source

Advances the game state until the player's next move or the game is over. This contains the game loop.

public setGameConditions(conditions: GameConditions) source

Sets the conditions for victory and defeat in the Dungeon

Params:

NameTypeAttributeDescription
conditions GameConditions

public setTile(tile: Tile, x: number, y: number) source

Changes the tile at the specified position

Params:

NameTypeAttributeDescription
tile Tile

The new tile

x number

The x coordinate

y number

The y coordinate