Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. /*
  2. Objects can have the following parameters:
  3. color: '#fff' by default
  4. impassable: true if it blocks the player from movement (false by default)
  5. onCollision: function (player) called when player moves over the object
  6. onPickUp: function (player) called when player picks up the item
  7. symbol: Unicode character representing the object
  8. type: 'item' or null
  9. */
  10.  
  11. // used by bonus levels 01 through 04
  12. function moveToward(obj, type) {
  13. var target = obj.findNearest(type);
  14. var leftDist = obj.getX() - target.x;
  15. var upDist = obj.getY() - target.y;
  16.  
  17. var direction;
  18. if (upDist == 0 && leftDist == 0) {
  19. return;
  20. }
  21. if (upDist > 0 && upDist >= leftDist) {
  22. direction = 'up';
  23. } else if (upDist < 0 && upDist < leftDist) {
  24. direction = 'down';
  25. } else if (leftDist > 0 && leftDist >= upDist) {
  26. direction = 'left';
  27. } else {
  28. direction = 'right';
  29. }
  30.  
  31. if (obj.canMove(direction)) {
  32. obj.move(direction);
  33. }
  34. }
  35.  
  36. // used by bonus levels 01 through 04
  37. function followAndKeepDistance(obj, type) {
  38. var target = obj.findNearest(type);
  39. var leftDist = obj.getX() - target.x;
  40. var upDist = obj.getY() - target.y;
  41.  
  42. if (Math.abs(upDist) < 2 && Math.abs(leftDist) < 4
  43. || Math.abs(leftDist) < 2 && Math.abs(upDist) < 4) {
  44. return;
  45. }
  46. var direction;
  47. if (upDist > 0 && upDist >= leftDist) {
  48. direction = 'up';
  49. } else if (upDist < 0 && upDist < leftDist) {
  50. direction = 'down';
  51. } else if (leftDist > 0 && leftDist >= upDist) {
  52. direction = 'left';
  53. } else {
  54. direction = 'right';
  55. }
  56.  
  57. if (obj.canMove(direction)) {
  58. obj.move(direction);
  59. }
  60. }
  61.  
  62. // used by bonus levels 01 through 04
  63. function killPlayerIfTooFar(obj) {
  64. var target = obj.findNearest('player');
  65. var leftDist = obj.getX() - target.x;
  66. var upDist = obj.getY() - target.y;
  67.  
  68. if (Math.abs(upDist) > 8 || Math.abs(leftDist) > 8) {
  69. obj._map.getPlayer().killedBy('"suspicious circumstances"');
  70. }
  71. }
  72.  
  73. Game.prototype.getListOfObjects = function () {
  74. var game = this;
  75. return {
  76. // special
  77.  
  78. 'empty' : {
  79. 'symbol': ' ',
  80. 'impassableFor': ['raft']
  81. },
  82.  
  83. 'player' : {
  84. 'symbol': '@',
  85. 'color': '#0f0'
  86. },
  87.  
  88. 'exit' : {
  89. 'symbol' : String.fromCharCode(0x2395), // ⎕
  90. 'color': '#0ff',
  91. 'onCollision': function (player) {
  92. game._moveToNextLevel();
  93. }
  94. },
  95.  
  96. // obstacles
  97.  
  98. 'block': {
  99. 'symbol': '#',
  100. 'color': '#999',
  101. 'impassable': true
  102. },
  103.  
  104. 'tree': {
  105. 'symbol': '♣',
  106. 'color': '#080',
  107. 'impassable': true
  108. },
  109.  
  110. 'mine': {
  111. 'symbol': ' ',
  112. 'onCollision': function (player) {
  113. player.killedBy('a hidden mine');
  114. }
  115. },
  116.  
  117. 'trap': {
  118. 'type': 'dynamic',
  119. 'symbol': '*',
  120. 'color': '#f00',
  121. 'onCollision': function (player, me) {
  122. player.killedBy('a trap');
  123. },
  124. 'behavior': null
  125. },
  126.  
  127. 'teleporter': {
  128. 'type': 'dynamic',
  129. 'symbol' : String.fromCharCode(0x2395), // ⎕
  130. 'color': '#f0f',
  131. 'onCollision': function (player, me) {
  132. if (!player._hasTeleported) {
  133. if (me.target) {
  134. game._callUnexposedMethod(function () {
  135. player._moveTo(me.target);
  136. });
  137. } else {
  138. throw 'TeleporterError: Missing target for teleporter'
  139. }
  140. }
  141. player._hasTeleported = true;
  142. },
  143. 'behavior': null
  144. },
  145.  
  146. // items
  147.  
  148. 'computer': {
  149. 'type': 'item',
  150. 'symbol': String.fromCharCode(0x2318), // ⌘
  151. 'color': '#ccc',
  152. 'onPickUp': function (player) {
  153. $('#editorPane').fadeIn();
  154. game.editor.refresh();
  155. game.map.writeStatus('You have picked up the computer!');
  156. },
  157. 'onDrop': function () {
  158. $('#editorPane').hide();
  159. }
  160. },
  161.  
  162. 'phone': {
  163. 'type': 'item',
  164. 'symbol': String.fromCharCode(0x260E), // ☎
  165. 'onPickUp': function (player) {
  166. game.map.writeStatus('You have picked up the function phone!');
  167. $('#phoneButton').show();
  168. },
  169. 'onDrop': function () {
  170. $('#phoneButton').hide();
  171. }
  172. },
  173.  
  174. 'redKey': {
  175. 'type': 'item',
  176. 'symbol': 'k',
  177. 'color': 'red',
  178. 'onPickUp': function (player) {
  179. game.map.writeStatus('You have picked up a red key!');
  180. }
  181. },
  182.  
  183. 'greenKey': {
  184. 'type': 'item',
  185. 'symbol': 'k',
  186. 'color': '#0f0',
  187. 'onPickUp': function (player) {
  188. game.map.writeStatus('You have picked up a green key!');
  189. }
  190. },
  191.  
  192. 'blueKey': {
  193. 'type': 'item',
  194. 'symbol': 'k',
  195. 'color': '#06f',
  196. 'onPickUp': function (player) {
  197. game.map.writeStatus('You have picked up a blue key!');
  198. }
  199. },
  200.  
  201. 'yellowKey': {
  202. 'type': 'item',
  203. 'symbol': 'k',
  204. 'color': 'yellow',
  205. 'onPickUp': function (player) {
  206. game.map.writeStatus('You have picked up a yellow key!');
  207. }
  208. },
  209.  
  210. 'theAlgorithm': {
  211. 'type': 'item',
  212. 'symbol': 'A',
  213. 'color': 'white',
  214. 'onPickUp': function (player) {
  215. game.map.writeStatus('You have picked up the Algorithm!');
  216. },
  217. 'onDrop': function () {
  218. game.map.writeStatus('You have lost the Algorithm!');
  219. }
  220. },
  221.  
  222. // used by bonus levels 01 through 04
  223. 'eye': {
  224. 'type': 'dynamic',
  225. 'symbol': 'E',
  226. 'color': 'red',
  227. 'behavior': function (me) {
  228. followAndKeepDistance(me, 'player');
  229. killPlayerIfTooFar(me);
  230. },
  231. 'onCollision': function (player) {
  232. player.killedBy('"the eye"');
  233. },
  234. },
  235.  
  236. // used by bonus levels 01 through 04
  237. 'guard': {
  238. 'type': 'dynamic',
  239. 'symbol': 'd',
  240. 'color': 'red',
  241. 'behavior': function (me) {
  242. moveToward(me, 'player');
  243. },
  244. 'onCollision': function (player) {
  245. player.killedBy('a guard drone');
  246. },
  247. }
  248. };
  249. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement