Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export type Dimensions = {
- width: number,
- height: number,
- layers: number
- }
- export type EuclideanCoordinates = {
- x:number,
- y:number,
- z:number
- }
- export type DungeonOptions = {
- dimensions: Dimensions;
- fill: boolean
- }
- export type RoomOptions = {
- dungeon: Dungeon,
- coordinates: EuclideanCoordinates
- }
- export type DObjectOptions = {
- location?: Occupiable
- };
- export interface Occupier{
- location: Occupiable|null;
- }
- export interface Occupiable{
- add(occupier:Occupier): void;
- remove(occupier:Occupier): void;
- contains(occupier:Occupier): boolean;
- allowEnter(location:Occupier): boolean;
- allowExit(location:Occupier): boolean;
- contents: Occupier[];
- }
- export interface Movable{
- canMove(location:Occupiable): boolean;
- canEnter(location:Occupiable): boolean;
- canExit(location:Occupiable): boolean;
- move(location:Occupiable): boolean;
- }
- export class Dungeon{
- rooms: Room[] = [];
- grid!: (Room|null)[][][];
- dimensions: Dimensions;
- constructor(options: DungeonOptions){
- this.dimensions = options.dimensions;
- this.buildGrid(options.fill);
- }
- add(room:Room): void{
- if(this.rooms.indexOf(room) != -1) return;
- this.rooms.push(room);
- }
- remove(room:Room): void{
- let i:number = this.rooms.indexOf(room);
- if(i == -1) return;
- this.rooms.splice(i, 1);
- }
- contains(room:Room): boolean{
- return (this.rooms.indexOf(room) != -1);
- }
- buildGrid(fill:boolean){
- this.grid = new Array(this.dimensions.layers);
- for(let z=0;z<this.dimensions.layers;z++){
- this.grid[z] = new Array(this.dimensions.height);
- for(let y=0;y<this.dimensions.height;y++){
- this.grid[z][y] = new Array(this.dimensions.width);
- if(fill) for(let x=0;x<this.dimensions.width;x++) this.createRoom({x:x,y:y,z:z});
- }
- }
- }
- createRoom(coordinates:EuclideanCoordinates): Room{
- if(coordinates.x < 0 || coordinates.x >= this.dimensions.width) throw new Error("Invidate X coordinate.");
- if(coordinates.y < 0 || coordinates.y >= this.dimensions.height) throw new Error("Invidate Y coordinate.");
- if(coordinates.z < 0 || coordinates.z >= this.dimensions.layers) throw new Error("Invidate Z coordinate.");
- let room:Room = new Room({coordinates:coordinates, dungeon:this});
- this.grid[coordinates.z][coordinates.y][coordinates.x] = room;
- return room;
- }
- getRoom(coordinates:EuclideanCoordinates): Room|null{
- if(coordinates.x < 0 || coordinates.x >= this.dimensions.width) throw new Error("Invidate X coordinate.");
- if(coordinates.y < 0 || coordinates.y >= this.dimensions.height) throw new Error("Invidate Y coordinate.");
- if(coordinates.z < 0 || coordinates.z >= this.dimensions.layers) throw new Error("Invidate Z coordinate.");
- return this.grid[coordinates.z][coordinates.y][coordinates.x];
- }
- }
- export class Room implements Occupiable{
- private _dungeon: Dungeon;
- private _coordinates: EuclideanCoordinates;
- private _contents: Occupier[] = [];
- constructor(options: RoomOptions){
- this._dungeon = options.dungeon;
- this._coordinates = options.coordinates;
- }
- get dungeon(): Dungeon{
- return this._dungeon;
- }
- get coordinates(): EuclideanCoordinates{
- return this._coordinates;
- }
- get contents(): Occupier[]{
- return this._contents;
- }
- add(occupier:Occupier): void{
- if(this.contents.indexOf(occupier) != -1) return;
- this.contents.push(occupier);
- if(occupier.location != this) occupier.location = this;
- }
- remove(occupier:Occupier): void{
- let i:number = this.contents.indexOf(occupier);
- if(i == -1) return;
- this.contents.splice(i, 1);
- if(occupier.location == this) occupier.location = null;
- }
- contains(occupier:Occupier): boolean{
- return (this.contents.indexOf(occupier) != -1);
- }
- allowEnter(occupier:Occupier): boolean{
- return true;
- }
- allowExit(occupier:Occupier): boolean{
- return true;
- }
- }
- export class DObject implements Occupiable, Occupier{
- private _location: Occupiable|null = null;
- private _contents: Occupier[] = [];
- constructor(options?:DObjectOptions){
- if(!options) return;
- if(options.location) this.location = options.location;
- }
- get location(): Occupiable|null{
- return this._location;
- }
- set location(location:Occupiable|null){
- if(this.location) {
- let olocation: Occupiable = this.location;
- this._location = null;
- olocation.remove(this);
- }
- if(!location) return;
- this._location = location;
- location.add(this);
- }
- get contents(): Occupier[]{
- return this._contents;
- }
- add(occupier:Occupier): void{
- if(this.contents.indexOf(occupier) != -1) return;
- this.contents.push(occupier);
- if(occupier.location != this) occupier.location = this;
- }
- remove(occupier:Occupier): void{
- let i:number = this.contents.indexOf(occupier);
- if(i == -1) return;
- this.contents.splice(i, 1);
- if(occupier.location == this) occupier.location = null;
- }
- contains(occupier:Occupier): boolean{
- return (this.contents.indexOf(occupier) != -1);
- }
- allowEnter(location:Occupier): boolean{
- return true;
- }
- allowExit(location:Occupier): boolean{
- return true;
- }
- }
- export class Mob extends DObject implements Movable{
- canMove(location:Occupiable): boolean{
- if(this.location && !this.canExit(this.location)) return false;
- if(!this.canEnter(location)) return false;
- return true;
- }
- canEnter(location:Occupiable): boolean{
- if(!location.allowEnter(this)) return false;
- return true;
- }
- canExit(location:Occupiable): boolean{
- if(!location.allowExit(this)) return false;
- return true;
- }
- move(location:Occupiable|null): boolean{
- if(location && !this.canMove(location)) return false;
- this.location = location;
- return true;
- }
- }
- export class Item extends DObject implements Movable{
- canMove(location:Occupiable): boolean{
- if(this.location && !this.canExit(this.location)) return false;
- if(!this.canEnter(location)) return false;
- return true;
- }
- canEnter(location:Occupiable): boolean{
- if(!location.allowEnter(this)) return false;
- return true;
- }
- canExit(location:Occupiable): boolean{
- if(!location.allowExit(this)) return false;
- return true;
- }
- move(location:Occupiable|null): boolean{
- if(location && !this.canMove(location)) return false;
- this.location = location;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement