Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 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, game) called when player moves over the object
  6. onPickUp: function (player, game) 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.  
  76.  
  77. return {
  78. // special
  79.  
  80. 'empty' : {
  81. 'symbol': ' ',
  82. 'impassableFor': ['raft']
  83. },
  84.  
  85. 'player' : {
  86. 'symbol': '@',
  87. 'color': '#0f0'
  88. },
  89.  
  90. 'exit' : {
  91. 'symbol' : String.fromCharCode(0x2395), // ⎕
  92. 'color': '#0ff',
  93. 'onCollision': function (player) {
  94. if (game.map.finalLevel) {
  95. game._moveToNextLevel();
  96. }
  97. }
  98. },
  99.  
  100. // obstacles
  101.  
  102. 'block': {
  103. 'symbol': '#',
  104. 'color': '#999',
  105. 'impassable': true
  106. },
  107.  
  108. 'tree': {
  109. 'symbol': '♣',
  110. 'color': '#080',
  111. 'impassable': true
  112. },
  113.  
  114. 'mine': {
  115. 'symbol': ' ',
  116. 'onCollision': function (player) {
  117. player.killedBy('a hidden mine');
  118. }
  119. },
  120.  
  121. 'trap': {
  122. 'type': 'dynamic',
  123. 'symbol': '*',
  124. 'color': '#f00',
  125. 'onCollision': function (player, me) {
  126. player.killedBy('a trap');
  127. },
  128. 'behavior': null
  129. },
  130.  
  131. 'teleporter': {
  132. 'type': 'dynamic',
  133. 'symbol' : String.fromCharCode(0x2395), // ⎕
  134. 'color': '#f0f',
  135. 'onCollision': function (player, me) {
  136. if (!player._hasTeleported) {
  137. if (me.target) {
  138. game._callUnexposedMethod(function () {
  139. player._moveTo(me.target);
  140. });
  141. } else {
  142. throw 'TeleporterError: Missing target for teleporter'
  143. }
  144. }
  145. player._hasTeleported = true;
  146. },
  147. 'behavior': null
  148. },
  149.  
  150. // items
  151.  
  152. 'computer': {
  153. 'type': 'item',
  154. 'symbol': String.fromCharCode(0x2318), // ⌘
  155. 'color': '#ccc',
  156. 'onPickUp': function (player) {
  157. $('#editorPane').fadeIn();
  158. game.editor.refresh();
  159. game.map.writeStatus('You have picked up the computer!');
  160. },
  161. 'onDrop': function () {
  162. $('#editorPane').hide();
  163. }
  164. },
  165.  
  166. 'phone': {
  167. 'type': 'item',
  168. 'symbol': String.fromCharCode(0x260E), // ☎
  169. 'onPickUp': function (player) {
  170. game.map.writeStatus('You have picked up the function phone!');
  171. $('#phoneButton').show();
  172. },
  173. 'onDrop': function () {
  174. $('#phoneButton').hide();
  175. }
  176. },
  177.  
  178. 'redKey': {
  179. 'type': 'item',
  180. 'symbol': 'k',
  181. 'color': 'red',
  182. 'onPickUp': function (player) {
  183. game.map.writeStatus('You have picked up a red key!');
  184. }
  185. },
  186.  
  187. 'greenKey': {
  188. 'type': 'item',
  189. 'symbol': 'k',
  190. 'color': '#0f0',
  191. 'onPickUp': function (player) {
  192. game.map.writeStatus('You have picked up a green key!');
  193. }
  194. },
  195.  
  196. 'blueKey': {
  197. 'type': 'item',
  198. 'symbol': 'k',
  199. 'color': '#06f',
  200. 'onPickUp': function (player) {
  201. game.map.writeStatus('You have picked up a blue key!');
  202. }
  203. },
  204.  
  205. 'yellowKey': {
  206. 'type': 'item',
  207. 'symbol': 'k',
  208. 'color': 'yellow',
  209. 'onPickUp': function (player) {
  210. game.map.writeStatus('You have picked up a yellow key!');
  211. }
  212. },
  213.  
  214. 'theAlgorithm': {
  215. 'type': 'item',
  216. 'symbol': 'A',
  217. 'color': 'white',
  218. 'onPickUp': function (player) {
  219. game.map.writeStatus('You have picked up the Algorithm!');
  220. },
  221. 'onDrop': function () {
  222. game.map.writeStatus('You have lost the Algorithm!');
  223. }
  224. },
  225.  
  226. // used by bonus levels 01 through 04
  227. 'eye': {
  228. 'type': 'dynamic',
  229. 'symbol': 'E',
  230. 'color': 'red',
  231. 'behavior': function (me) {
  232. followAndKeepDistance(me, 'player');
  233. killPlayerIfTooFar(me);
  234. },
  235. 'onCollision': function (player) {
  236. player.killedBy('"the eye"');
  237. },
  238. },
  239.  
  240. // used by bonus levels 01 through 04
  241. 'guard': {
  242. 'type': 'dynamic',
  243. 'symbol': 'd',
  244. 'color': 'red',
  245. 'behavior': function (me) {
  246. moveToward(me, 'player');
  247. },
  248. 'onCollision': function (player) {
  249. player.killedBy('a guard drone');
  250. },
  251. }
  252. };
  253. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement