Region.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /// <reference path="Relations/RelationOneToMany.ts" />
  2. class Region {
  3. public name : string;
  4. public constructor (name : string) {
  5. this.name = name;
  6. }
  7. public static RegionRoom = new RelationOneToMany();
  8. public static RegionRegion = new RelationOneToMany();
  9. public static InRelation = new RelationHandlerStrictOneToMany(Region.RegionRegion, Region.RegionRoom);
  10. public place (...rooms : Array<Room | Region>) {
  11. rooms.forEach(room => {
  12. if (room instanceof Room) {
  13. Region.RegionRoom.setRelation(this, room);
  14. } else {
  15. Region.RegionRegion.setRelation(this, room);
  16. }
  17. });
  18. }
  19. public getRooms () {
  20. let rooms = <Array<Room>> Region.RegionRoom.getRight(this);
  21. Region.RegionRegion.getRight(this).forEach((region : Region) => {
  22. (<Array<Room>> Region.RegionRoom.getRight(region)).forEach((room : Room) => {
  23. rooms.push(room);
  24. });
  25. });
  26. return rooms;
  27. }
  28. public containsRoom (room : Room) {
  29. let directlyContained = Region.RegionRoom.getLeft(room) == this;
  30. if (directlyContained) {
  31. return true;
  32. } else if (Region.RegionRegion.getLeft(this) != undefined) {
  33. return this.getRooms().indexOf(room) != -1; // If it's not directly contained, we have to check rooms for every region that contains this one and I don't care enough to do that right now
  34. // TODO: optimize this.
  35. }
  36. return false;
  37. }
  38. public contains (thing : Thing) {
  39. let room = thing.getRoom();
  40. if (room == undefined) {
  41. return false;
  42. } else {
  43. let regions = Region.InRelation.getAllLeft(room);
  44. return regions.indexOf(this) != -1;
  45. }
  46. }
  47. }