Guest User

Untitled

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