Advertisement
Guest User

Untitled

a guest
Jul 9th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export type Dimensions = {
  2.     width: number,
  3.     height: number,
  4.     layers: number
  5. }
  6.  
  7. export type EuclideanCoordinates = {
  8.     x:number,
  9.     y:number,
  10.     z:number
  11. }
  12.  
  13. export type DungeonOptions = {
  14.     dimensions: Dimensions;
  15.     fill: boolean
  16. }
  17.  
  18. export type RoomOptions = {
  19.     dungeon: Dungeon,
  20.     coordinates: EuclideanCoordinates
  21. }
  22.  
  23. export type DObjectOptions = {
  24.     location?: Occupiable
  25. };
  26.  
  27. export interface Occupier{
  28.     location: Occupiable|null;
  29. }
  30.  
  31. export interface Occupiable{
  32.     add(occupier:Occupier): void;
  33.     remove(occupier:Occupier): void;
  34.     contains(occupier:Occupier): boolean;
  35.     allowEnter(location:Occupier): boolean;
  36.     allowExit(location:Occupier): boolean;
  37.     contents: Occupier[];
  38. }
  39.  
  40. export interface Movable{
  41.     canMove(location:Occupiable): boolean;
  42.     canEnter(location:Occupiable): boolean;
  43.     canExit(location:Occupiable): boolean;
  44.     move(location:Occupiable): boolean;
  45. }
  46.  
  47. export class Dungeon{
  48.     rooms: Room[] = [];
  49.     grid!: (Room|null)[][][];
  50.     dimensions: Dimensions;
  51.     constructor(options: DungeonOptions){
  52.         this.dimensions = options.dimensions;
  53.         this.buildGrid(options.fill);
  54.     }
  55.  
  56.     add(room:Room): void{
  57.         if(this.rooms.indexOf(room) != -1) return;
  58.         this.rooms.push(room);
  59.     }
  60.  
  61.     remove(room:Room): void{
  62.         let i:number = this.rooms.indexOf(room);
  63.         if(i == -1) return;
  64.         this.rooms.splice(i, 1);
  65.     }
  66.  
  67.     contains(room:Room): boolean{
  68.         return (this.rooms.indexOf(room) != -1);
  69.     }
  70.  
  71.     buildGrid(fill:boolean){
  72.         this.grid = new Array(this.dimensions.layers);
  73.         for(let z=0;z<this.dimensions.layers;z++){
  74.             this.grid[z] = new Array(this.dimensions.height);
  75.             for(let y=0;y<this.dimensions.height;y++){
  76.                 this.grid[z][y] = new Array(this.dimensions.width);
  77.                 if(fill) for(let x=0;x<this.dimensions.width;x++) this.createRoom({x:x,y:y,z:z});
  78.             }
  79.         }
  80.     }
  81.  
  82.     createRoom(coordinates:EuclideanCoordinates): Room{
  83.         if(coordinates.x < 0 || coordinates.x >= this.dimensions.width) throw new Error("Invidate X coordinate.");
  84.         if(coordinates.y < 0 || coordinates.y >= this.dimensions.height) throw new Error("Invidate Y coordinate.");
  85.         if(coordinates.z < 0 || coordinates.z >= this.dimensions.layers) throw new Error("Invidate Z coordinate.");
  86.         let room:Room = new Room({coordinates:coordinates, dungeon:this});
  87.         this.grid[coordinates.z][coordinates.y][coordinates.x] = room;
  88.         return room;
  89.     }
  90.  
  91.     getRoom(coordinates:EuclideanCoordinates): Room|null{
  92.         if(coordinates.x < 0 || coordinates.x >= this.dimensions.width) throw new Error("Invidate X coordinate.");
  93.         if(coordinates.y < 0 || coordinates.y >= this.dimensions.height) throw new Error("Invidate Y coordinate.");
  94.         if(coordinates.z < 0 || coordinates.z >= this.dimensions.layers) throw new Error("Invidate Z coordinate.");
  95.         return this.grid[coordinates.z][coordinates.y][coordinates.x];
  96.     }
  97. }
  98.  
  99. export class Room implements Occupiable{
  100.     private _dungeon: Dungeon;
  101.     private _coordinates: EuclideanCoordinates;
  102.     private _contents: Occupier[] = [];
  103.     constructor(options: RoomOptions){
  104.         this._dungeon = options.dungeon;
  105.         this._coordinates = options.coordinates;
  106.     }
  107.  
  108.     get dungeon(): Dungeon{
  109.         return this._dungeon;
  110.     }
  111.  
  112.     get coordinates(): EuclideanCoordinates{
  113.         return this._coordinates;
  114.     }
  115.  
  116.     get contents(): Occupier[]{
  117.         return this._contents;
  118.     }
  119.  
  120.     add(occupier:Occupier): void{
  121.         if(this.contents.indexOf(occupier) != -1) return;
  122.         this.contents.push(occupier);
  123.         if(occupier.location != this) occupier.location = this;
  124.     }
  125.  
  126.     remove(occupier:Occupier): void{
  127.         let i:number = this.contents.indexOf(occupier);
  128.         if(i == -1) return;
  129.         this.contents.splice(i, 1);
  130.         if(occupier.location == this) occupier.location = null;
  131.     }
  132.  
  133.     contains(occupier:Occupier): boolean{
  134.         return (this.contents.indexOf(occupier) != -1);
  135.     }
  136.  
  137.     allowEnter(occupier:Occupier): boolean{
  138.         return true;
  139.     }
  140.  
  141.     allowExit(occupier:Occupier): boolean{
  142.         return true;
  143.     }
  144. }
  145.  
  146. export class DObject implements Occupiable, Occupier{
  147.     private _location: Occupiable|null = null;
  148.     private _contents: Occupier[] = [];
  149.     constructor(options?:DObjectOptions){
  150.         if(!options) return;
  151.         if(options.location) this.location = options.location;
  152.     }
  153.  
  154.     get location(): Occupiable|null{
  155.         return this._location;
  156.     }
  157.  
  158.     set location(location:Occupiable|null){
  159.         if(this.location) {
  160.             let olocation: Occupiable = this.location;
  161.             this._location = null;
  162.             olocation.remove(this);
  163.         }
  164.  
  165.         if(!location) return;
  166.         this._location = location;
  167.         location.add(this);
  168.     }
  169.  
  170.     get contents(): Occupier[]{
  171.         return this._contents;
  172.     }
  173.  
  174.     add(occupier:Occupier): void{
  175.         if(this.contents.indexOf(occupier) != -1) return;
  176.         this.contents.push(occupier);
  177.         if(occupier.location != this) occupier.location = this;
  178.     }
  179.  
  180.     remove(occupier:Occupier): void{
  181.         let i:number = this.contents.indexOf(occupier);
  182.         if(i == -1) return;
  183.         this.contents.splice(i, 1);
  184.         if(occupier.location == this) occupier.location = null;
  185.     }
  186.  
  187.     contains(occupier:Occupier): boolean{
  188.         return (this.contents.indexOf(occupier) != -1);
  189.     }
  190.  
  191.     allowEnter(location:Occupier): boolean{
  192.         return true;
  193.     }
  194.  
  195.     allowExit(location:Occupier): boolean{
  196.         return true;
  197.     }
  198. }
  199.  
  200. export class Mob extends DObject implements Movable{
  201.     canMove(location:Occupiable): boolean{
  202.         if(this.location && !this.canExit(this.location)) return false;
  203.         if(!this.canEnter(location)) return false;
  204.         return true;
  205.     }
  206.  
  207.     canEnter(location:Occupiable): boolean{
  208.         if(!location.allowEnter(this)) return false;
  209.         return true;
  210.     }
  211.  
  212.     canExit(location:Occupiable): boolean{
  213.         if(!location.allowExit(this)) return false;
  214.         return true;
  215.     }
  216.  
  217.     move(location:Occupiable|null): boolean{
  218.         if(location && !this.canMove(location)) return false;
  219.         this.location = location;
  220.         return true;
  221.     }
  222. }
  223.  
  224. export class Item extends DObject implements Movable{
  225.     canMove(location:Occupiable): boolean{
  226.         if(this.location && !this.canExit(this.location)) return false;
  227.         if(!this.canEnter(location)) return false;
  228.         return true;
  229.     }
  230.  
  231.     canEnter(location:Occupiable): boolean{
  232.         if(!location.allowEnter(this)) return false;
  233.         return true;
  234.     }
  235.  
  236.     canExit(location:Occupiable): boolean{
  237.         if(!location.allowExit(this)) return false;
  238.         return true;
  239.     }
  240.  
  241.     move(location:Occupiable|null): boolean{
  242.         if(location && !this.canMove(location)) return false;
  243.         this.location = location;
  244.         return true;
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement