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.
 
 
 

173 lines
3.7 KiB

  1. module.exports = {
  2. type: 'door',
  3. locked: false,
  4. closed: true,
  5. key: null,
  6. autoCloseCd: 0,
  7. autoClose: null,
  8. destroyKey: false,
  9. openSprite: 157,
  10. closedSprite: 156,
  11. init: function (blueprint) {
  12. this.locked = blueprint.locked;
  13. this.key = blueprint.key;
  14. this.destroyKey = blueprint.destroyKey;
  15. this.autoClose = blueprint.autoClose;
  16. if (this.closed) {
  17. this.obj.instance.physics.setCollision(this.obj.x, this.obj.y, true);
  18. this.obj.instance.objects.notifyCollisionChange(this.obj.x, this.obj.y, true);
  19. }
  20. let o = this.obj.instance.objects.buildObjects([{
  21. properties: {
  22. x: this.obj.x - 1,
  23. y: this.obj.y - 1,
  24. width: 3,
  25. height: 3,
  26. cpnNotice: {
  27. actions: {
  28. enter: {
  29. cpn: 'door',
  30. method: 'enterArea',
  31. targetId: this.obj.id,
  32. args: []
  33. },
  34. exit: {
  35. cpn: 'door',
  36. method: 'exitArea',
  37. targetId: this.obj.id,
  38. args: []
  39. }
  40. }
  41. }
  42. }
  43. }]);
  44. },
  45. exitArea: function (obj) {
  46. if (!obj.player)
  47. return;
  48. obj.syncer.setArray(true, 'serverActions', 'removeActions', {
  49. key: 'u',
  50. action: {
  51. targetId: this.obj.id,
  52. cpn: 'door',
  53. method: 'unlock'
  54. }
  55. });
  56. },
  57. enterArea: function (obj) {
  58. if (!obj.player)
  59. return;
  60. let canAction = true;
  61. let msg = 'Press U to open this door';
  62. if (this.closed) {
  63. if (this.locked) {
  64. let key = obj.inventory.items.find(i => ((i.keyId === this.key) || (i.keyId === 'world')));
  65. if (!key) {
  66. canAction = false;
  67. msg = 'You don\'t have the key to unlock this door';
  68. }
  69. }
  70. } else
  71. msg = 'Press U to close this door';
  72. if (canAction) {
  73. obj.syncer.setArray(true, 'serverActions', 'addActions', {
  74. key: 'u',
  75. action: {
  76. targetId: this.obj.id,
  77. cpn: 'door',
  78. method: 'unlock'
  79. }
  80. });
  81. }
  82. this.obj.instance.syncer.queue('onGetAnnouncement', {
  83. src: this.obj.id,
  84. msg: msg
  85. }, [obj.serverId]);
  86. },
  87. unlock: function (msg) {
  88. if (msg.sourceId === null)
  89. return;
  90. let obj = this.obj.instance.objects.objects.find(o => o.serverId === msg.sourceId);
  91. if ((!obj) || (!obj.player))
  92. return;
  93. let thisObj = this.obj;
  94. if ((Math.abs(thisObj.x - obj.x) > 1) || (Math.abs(thisObj.y - obj.y) > 1))
  95. return;
  96. else if ((thisObj.x === obj.x) && (thisObj.y === obj.y))
  97. return;
  98. let syncO = thisObj.syncer.o;
  99. if ((this.locked) && (this.closed)) {
  100. if (this.autoClose)
  101. this.autoCloseCd = this.autoClose;
  102. let key = obj.inventory.items.find(i => ((i.keyId === this.key) || (i.keyId === 'world')));
  103. if (!key)
  104. return;
  105. if (((key.singleUse) || (this.destroyKey)) && (key.keyId !== 'world')) {
  106. obj.inventory.destroyItem(key.id, 1);
  107. obj.instance.syncer.queue('onGetMessages', {
  108. id: obj.id,
  109. messages: [{
  110. class: 'color-redA',
  111. message: 'The ' + key.name + ' disintegrates on use',
  112. type: 'info'
  113. }]
  114. }, [obj.serverId]);
  115. }
  116. }
  117. if (this.closed) {
  118. thisObj.cell = this.openSprite;
  119. syncO.cell = this.openSprite;
  120. this.obj.instance.physics.setCollision(thisObj.x, thisObj.y, false);
  121. this.obj.instance.objects.notifyCollisionChange(thisObj.x, thisObj.y, false);
  122. this.closed = false;
  123. this.enterArea(obj);
  124. } else {
  125. thisObj.cell = this.closedSprite;
  126. syncO.cell = this.closedSprite;
  127. this.obj.instance.physics.setCollision(thisObj.x, thisObj.y, true);
  128. this.obj.instance.objects.notifyCollisionChange(thisObj.x, thisObj.y, true);
  129. this.closed = true;
  130. this.enterArea(obj);
  131. }
  132. },
  133. update: function () {
  134. if (!this.autoCloseCd)
  135. return;
  136. this.autoCloseCd--;
  137. if (this.autoCloseCd === 0) {
  138. this.obj.cell = this.closedSprite;
  139. this.obj.syncer.o.cell = this.closedSprite;
  140. this.obj.instance.physics.setCollision(this.obj.x, this.obj.y, true);
  141. this.closed = true;
  142. }
  143. }
  144. };