Advertisement
Guest User

Untitled

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