Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

179 righe
3.9 KiB

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