You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

106 lines
2.0 KiB

  1. module.exports = {
  2. instance: null,
  3. ent: null,
  4. tiles: [],
  5. oldTiles: {},
  6. load: function (instance, objToAdd, callback) {
  7. this.instance = instance;
  8. this.ent = instance.mapName + '-' + objToAdd.components.find(c => c.type === 'auth').username;
  9. io.get({
  10. ent: this.ent,
  11. field: 'customMap',
  12. callback: this.onLoad.bind(this, callback)
  13. });
  14. },
  15. onLoad: function (callback, result) {
  16. this.tiles = JSON.parse(result || '[]');
  17. this.build(callback);
  18. },
  19. save: async function () {
  20. await io.setAsync({
  21. key: this.ent,
  22. table: 'customMap',
  23. value: this.tiles,
  24. serialize: true
  25. });
  26. },
  27. build: function (callback) {
  28. this.tiles.forEach(function (t) {
  29. t = t.split('|');
  30. this.customize(t[0], t[1], t[2], true);
  31. }, this);
  32. this.save();
  33. callback();
  34. },
  35. customize: function (x, y, tile, noStore) {
  36. let action = null;
  37. if (arguments.length === 1) {
  38. action = x;
  39. let obj = action.obj;
  40. tile = action.tile;
  41. x = obj.x;
  42. y = obj.y;
  43. if (action.destroy) {
  44. x += action.direction.x;
  45. y += action.direction.y;
  46. }
  47. }
  48. let collide = true;
  49. if (!noStore) {
  50. let exists = this.tiles.find(function (t) {
  51. t = t.split('|');
  52. if ((t[0] === x) && (t[1] === y))
  53. return true;
  54. });
  55. if (exists) {
  56. tile = this.oldTiles[x + '|' + y];
  57. collide = false;
  58. this.tiles.spliceWhere(t => t === exists);
  59. } else if (this.instance.map.clientMap.collisionMap[x][y]) {
  60. //Can't build on natural collisions
  61. return;
  62. } else
  63. this.tiles.push(x + '|' + y + '|' + tile);
  64. }
  65. if ((collide) && (!this.oldTiles[x + '|' + y])) {
  66. let oldTile = this.instance.map.clientMap.map[x][y];
  67. this.oldTiles[x + '|' + y] = oldTile - 1;
  68. }
  69. this.instance.map.clientMap.map[x][y] = tile;
  70. this.instance.map.clientMap.collisionMap[x][y] = collide;
  71. this.instance.physics.graph.grid[x][y] = !collide;
  72. if (!noStore)
  73. this.save();
  74. if (action) {
  75. action.result = {
  76. x: x,
  77. y: y,
  78. tile: tile,
  79. collide: collide
  80. };
  81. }
  82. },
  83. placeTile: function (obj) {
  84. this.customize(obj.x, obj.y, 52);
  85. }
  86. };