///
///
///
declare module MapsHandlr {
/**
* A Function to add a map command, such as an after or stretch.
*
* @param thing The raw command to create a Thing, as either a title
* or a JSON object.
* @param index Which command this is, as passed through Array.forEach.
*/
export interface ICommandAdder {
(thing: string | MapsCreatr.IPreThingSettings, index: number): void;
}
/**
* Settings to initialize a new IMapsHandlr.
*/
export interface IMapsHandlrSettings {
/**
* A MapsCreatr used to store and lazily initialize Maps.
*/
MapsCreator: MapsCreatr.IMapsCreatr;
/**
* A MapScreenr used to store attributes of Areas.
*/
MapScreener: MapScreenr.IMapScreenr;
/**
* Function for when a PreThing's Thing should be spawned.
*/
onSpawn?: (prething: MapsCreatr.IPreThing) => void;
/**
* Function for when a PreThing's Thing should be un-spawned.
*/
onUnspawn?: (prething: MapsCreatr.IPreThing) => void;
/**
* Any property names to copy from Areas to MapScreenr.
*/
screenAttributes?: string[];
/**
* Function to add an Area's provided "stretches" commands to stretch
* across an Area.
*/
stretchAdd?: ICommandAdder;
/**
* Function to add an Area provides an "afters" command to add PreThings
* to the end of an Area.
*/
afterAdd?: ICommandAdder;
/**
* An optional scope to call stretchAdd and afterAdd on, if not this.
*/
commandScope?: any;
}
export interface IMapsHandlr {
getMapsCreator(): MapsCreatr.IMapsCreatr;
getMapScreener(): MapScreenr.IMapScreenr;
getScreenAttributes(): string[];
getMapName(): string;
getMap(name?: string): MapsCreatr.IMapsCreatrMap;
getMaps(): { [i: string]: MapsCreatr.IMapsCreatrMap };
getArea(): MapsCreatr.IMapsCreatrArea;
getAreaName(): string;
getLocation(location: string): MapsCreatr.IMapsCreatrLocation;
getLocationEntered(): MapsCreatr.IMapsCreatrLocation;
getPreThings(): { [i: string]: MapsCreatr.IPreThing[] };
setMap(name: string, location?: string): MapsCreatr.IMapsCreatrMap;
setLocation(name: string): void;
setStretches(stretchesRaw: (string | MapsCreatr.IPreThingSettings)[]): void;
setAfters(aftersRaw: (string | MapsCreatr.IPreThingSettings)[]): void;
spawnMap(direction: string, top: number, right: number, bottom: number, left: number): void;
unspawnMap(direction: string, top: number, right: number, bottom: number, left: number): void;
}
}
module MapsHandlr {
"use strict";
/**
* Map manipulator and spawner for GameStartr maps that is the front-end
* counterpart to MapsCreatr. PreThing listings are loaded from Maps stored in a
* MapsCreatr and added or removed from user input. Area properties are given to
* a MapScreenr when a new Area is loaded.
*/
export class MapsHandlr implements IMapsHandlr {
/**
* Directional equivalents for converting from directions to keys.
*/
public static directionKeys: any = {
"xInc": "left",
"xDec": "right",
"yInc": "top",
"yDec": "bottom"
};
/**
* Opposite directions for when finding descending order Arrays.
*/
public static directionOpposites: any = {
"xInc": "xDec",
"xDec": "xInc",
"yInc": "yDec",
"yDec": "yInc"
};
/**
* MapsCreatr container for Maps from which this obtains Thing settings.
*/
private MapsCreator: MapsCreatr.IMapsCreatr;
/**
* MapScreenr container for attributes copied from Areas.
*/
private MapScreener: MapScreenr.IMapScreenr;
/**
* The names of attributes to be copied to the MapScreenr during setLocation.
*/
private screenAttributes: string[];
/**
* The currently referenced Map, set by setMap.
*/
private mapCurrent: MapsCreatr.IMapsCreatrMap;
/**
* The currently referenced Area, set by setLocation.
*/
private areaCurrent: MapsCreatr.IMapsCreatrArea;
/**
* The currently referenced Location, set by setLocation.
*/
private locationEntered: MapsCreatr.IMapsCreatrLocation;
/**
* The name of the currently referenced Area, set by setMap.
*/
private mapName: string;
/**
* The current Area's listing of PreThings.
*/
private prethings: { [i: string]: MapsCreatr.IPreThing[] };
/**
* Function for when a PreThing is to be spawned.
*/
private onSpawn: (prething: MapsCreatr.IPreThing) => void;
/**
* Function for when a PreThing is to be un-spawned.
*/
private onUnspawn: (prething: MapsCreatr.IPreThing) => void;
/**
* Optionally, PreThing settings to stretch across an Area.
*/
private stretches: (string | MapsCreatr.IPreThingSettings)[];
/**
* If stretches exists, a Function to add stretches to an Area.
*/
private stretchAdd: ICommandAdder;
/**
* Optionally, PreThing settings to place at the end of an Area.
*/
private afters: (string | MapsCreatr.IPreThingSettings)[];
/**
* If afters exists, a Function to add afters to an Area.
*/
private afterAdd: ICommandAdder;
/**
* An optional scope to call stretchAdd and afterAdd on, if not this.
*/
private commandScope: any;
/**
* @param {IMapsHandlrSettings} settings
*/
constructor(settings: IMapsHandlrSettings) {
if (!settings) {
throw new Error("No settings given to MapsHandlr.");
}
// Maps themselves should have been created in the MapsCreator object
if (!settings.MapsCreator) {
throw new Error("No MapsCreator provided to MapsHandlr.");
}
this.MapsCreator = settings.MapsCreator;
// Map/Area attributes will need to be stored in a MapScreenr object
if (!settings.MapScreener) {
throw new Error("No MapScreener provided to MapsHandlr.");
}
this.MapScreener = settings.MapScreener;
this.onSpawn = settings.onSpawn;
this.onUnspawn = settings.onUnspawn;
this.screenAttributes = settings.screenAttributes || [];
this.stretchAdd = settings.stretchAdd;
this.afterAdd = settings.afterAdd;
this.commandScope = settings.commandScope;
}
/* Simple gets
*/
/**
* @return {MapsCreatr} The internal MapsCreator.
*/
getMapsCreator(): MapsCreatr.IMapsCreatr {
return this.MapsCreator;
}
/**
* @return {MapScreenr} The internal MapScreener.
*/
getMapScreener(): MapScreenr.IMapScreenr {
return this.MapScreener;
}
/**
* @return {String[]} The attribute names to be copied to MapScreener.
*/
getScreenAttributes(): string[] {
return this.screenAttributes;
}
/**
* @return {String} The key by which the current Map is indexed.
*/
getMapName(): string {
return this.mapName;
}
/**
* Gets the map listed under the given name. If no name is provided, the
* mapCurrent is returned instead.
*
* @param {String} [name] An optional key to find the map under.
* @return {Map}
*/
getMap(name: string = undefined): MapsCreatr.IMapsCreatrMap {
if (typeof name !== "undefined") {
return this.MapsCreator.getMap(name);
} else {
return this.mapCurrent;
}
}
/**
* Simple getter pipe to the internal MapsCreator.getMaps() function.
*
* @return {Object