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.
 
 
 

174 regels
3.8 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. 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. name: this.closed ? 'open door' : 'close door',
  76. action: {
  77. targetId: this.obj.id,
  78. cpn: 'door',
  79. method: 'unlock'
  80. }
  81. });
  82. }
  83. this.obj.instance.syncer.queue('onGetAnnouncement', {
  84. src: this.obj.id,
  85. msg: msg
  86. }, [obj.serverId]);
  87. },
  88. unlock: function (msg) {
  89. if (!msg.sourceId)
  90. return;
  91. let obj = this.obj.instance.objects.objects.find(o => o.serverId === msg.sourceId);
  92. if ((!obj) || (!obj.player))
  93. return;
  94. let thisObj = this.obj;
  95. if ((Math.abs(thisObj.x - obj.x) > 1) || (Math.abs(thisObj.y - obj.y) > 1))
  96. return;
  97. else if ((thisObj.x === obj.x) && (thisObj.y === obj.y))
  98. return;
  99. let syncO = thisObj.syncer.o;
  100. if ((this.locked) && (this.closed)) {
  101. if (this.autoClose)
  102. this.autoCloseCd = this.autoClose;
  103. let key = obj.inventory.items.find(i => ((i.keyId === this.key) || (i.keyId === 'world')));
  104. if (!key)
  105. return;
  106. if (((key.singleUse) || (this.destroyKey)) && (key.keyId !== 'world')) {
  107. obj.inventory.destroyItem(key.id, 1);
  108. obj.instance.syncer.queue('onGetMessages', {
  109. id: obj.id,
  110. messages: [{
  111. class: 'color-redA',
  112. message: 'The ' + key.name + ' disintegrates on use',
  113. type: 'info'
  114. }]
  115. }, [obj.serverId]);
  116. }
  117. }
  118. if (this.closed) {
  119. thisObj.cell = this.openSprite;
  120. syncO.cell = this.openSprite;
  121. this.obj.instance.physics.setCollision(thisObj.x, thisObj.y, false);
  122. this.obj.instance.objects.notifyCollisionChange(thisObj.x, thisObj.y, false);
  123. this.closed = false;
  124. this.enterArea(obj);
  125. } else {
  126. thisObj.cell = this.closedSprite;
  127. syncO.cell = this.closedSprite;
  128. this.obj.instance.physics.setCollision(thisObj.x, thisObj.y, true);
  129. this.obj.instance.objects.notifyCollisionChange(thisObj.x, thisObj.y, true);
  130. this.closed = true;
  131. this.enterArea(obj);
  132. }
  133. },
  134. update: function () {
  135. if (!this.autoCloseCd)
  136. return;
  137. this.autoCloseCd--;
  138. if (this.autoCloseCd === 0) {
  139. this.obj.cell = this.closedSprite;
  140. this.obj.syncer.o.cell = this.closedSprite;
  141. this.obj.instance.physics.setCollision(this.obj.x, this.obj.y, true);
  142. this.closed = true;
  143. }
  144. }
  145. };