Guest User

Untitled

a guest
May 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. function Player(x, y, __map, __game) {
  2. /* private variables */
  3.  
  4. var __x = x;
  5. var __y = y;
  6. var __color = "#0f0";
  7. var __lastMoveDirection = '';
  8.  
  9. var __display = __map._display;
  10.  
  11. /* unexposed variables */
  12.  
  13. this._canMove = false;
  14.  
  15. /* wrapper */
  16.  
  17. function wrapExposedMethod(f, player) {
  18. return function () {
  19. var args = arguments;
  20. return __game._callUnexposedMethod(function () {
  21. return f.apply(player, args);
  22. });
  23. };
  24. };
  25.  
  26. /* exposed getters/setters */
  27.  
  28. this.getX = function () { return __x; };
  29. this.getY = function () { return __y; };
  30. this.getColor = function () { return __color; };
  31. this.getLastMoveDirection = function() { return __lastMoveDirection; };
  32.  
  33. this.setColor = wrapExposedMethod(function (c) {
  34. __color = c;
  35. __display.drawAll(__map);
  36. });
  37.  
  38. /* unexposed methods */
  39.  
  40. // (used for teleporters)
  41. this._moveTo = function (dynamicObject) {
  42. if (__game._isPlayerCodeRunning()) { throw 'Forbidden method call: player._moveTo()';}
  43.  
  44. // no safety checks or anything
  45. // this method is about as safe as a war zone
  46. __x = dynamicObject.getX();
  47. __y = dynamicObject.getY();
  48. __display.drawAll(__map);
  49.  
  50. // play teleporter sound
  51. __game.sound.playSound('blip');
  52. };
  53.  
  54. this._afterMove = function (x, y) {
  55. if (__game._isPlayerCodeRunning()) { throw 'Forbidden method call: player._afterMove()';}
  56.  
  57. var player = this;
  58.  
  59. this._hasTeleported = false; // necessary to prevent bugs with teleportation
  60.  
  61. __map._hideChapter();
  62. __map._moveAllDynamicObjects();
  63.  
  64. var onTransport = false;
  65.  
  66. // check for collision with transport object
  67. for (var i = 0; i < __map.getDynamicObjects().length; i++) {
  68. var object = __map.getDynamicObjects()[i];
  69. if (object.getX() === x && object.getY() === y) {
  70. var objectDef = __map._getObjectDefinition(object.getType());
  71. if (objectDef.transport) {
  72. onTransport = true;
  73. }
  74. }
  75. }
  76.  
  77. // check for collision with static object UNLESS
  78. // we are on a transport
  79. if (!onTransport) {
  80. var objectName = __map._getGrid()[x][y].type;
  81. var objectDef = __map._getObjectDefinition(objectName);
  82. if (objectDef.type === 'item') {
  83. this._pickUpItem(objectName, objectDef);
  84. } else if (objectDef.onCollision) {
  85. __game.validateCallback(function () {
  86. objectDef.onCollision(player, __game);
  87. }, false, true);
  88. }
  89. }
  90.  
  91. // check for collision with any lines on the map
  92. __map.testLineCollisions(this);
  93.  
  94. // check for nonstandard victory condition (e.g. DOM level)
  95. if (typeof(__game.objective) === 'function' && __game.objective(__map)) {
  96. __game._moveToNextLevel();
  97. }
  98.  
  99. __map.finalLevel = false;
  100. };
  101.  
  102. this._pickUpItem = function (itemName, object) {
  103. if (__game._isPlayerCodeRunning()) { throw 'Forbidden method call: player._pickUpItem()';}
  104.  
  105. var player = this;
  106.  
  107. __game.addToInventory(itemName);
  108. __map._removeItemFromMap(__x, __y, itemName);
  109. __map.refresh();
  110. __game.sound.playSound('pickup');
  111.  
  112. if (object.onPickUp) {
  113. __game.validateCallback(function () {
  114. setTimeout(function () {
  115. object.onPickUp(player);
  116. }, 100);
  117. // timeout is so that written text is not immediately overwritten
  118. // TODO: play around with Display.writeStatus so that this is
  119. // not necessary
  120. });
  121. }
  122. };
  123.  
  124. /* exposed methods */
  125.  
  126. this.atLocation = wrapExposedMethod(function (x, y) {
  127. return (__x === x && __y === y);
  128. }, this);
  129.  
  130. this.move = wrapExposedMethod(function (direction, fromKeyboard) {
  131. if (!this._canMove) { // mainly for key delay
  132. return false;
  133. }
  134.  
  135. if (__map._overrideKeys[direction] && fromKeyboard) {
  136. try {
  137. __game.validateCallback(__map._overrideKeys[direction], true);
  138.  
  139. __map.refresh();
  140. this._canMove = false;
  141. __map._reenableMovementForPlayer(this); // (key delay can vary by map)
  142. this._afterMove(__x, __y);
  143. } catch (e) {
  144. }
  145.  
  146. return;
  147. }
  148.  
  149. var new__x;
  150. var new__y;
  151. if (direction === 'up') {
  152. new__x = __x;
  153. new__y = __y - 1;
  154. }
  155. else if (direction === 'down') {
  156. new__x = __x;
  157. new__y = __y + 1;
  158. }
  159. else if (direction === 'left') {
  160. new__x = __x - 1;
  161. new__y = __y;
  162. }
  163. else if (direction === 'right') {
  164. new__x = __x + 1;
  165. new__y = __y;
  166. }
  167. else if (direction === 'rest') {
  168. new__x = __x;
  169. new__y = __y;
  170. }
  171. else if (direction === 'funcPhone') {
  172. __game.usePhone();
  173. return;
  174. }
  175.  
  176. if (__map._canMoveTo(new__x, new__y)) {
  177. __x = new__x;
  178. __y = new__y;
  179.  
  180. __map.refresh();
  181.  
  182. this._canMove = false;
  183.  
  184. __lastMoveDirection = direction;
  185. this._afterMove(__x, __y);
  186.  
  187. __map._reenableMovementForPlayer(this); // (key delay can vary by map)
  188. } else {
  189. // play bump sound
  190. __game.sound.playSound('select');
  191. }
  192. }, this);
  193.  
  194. this.killedBy = wrapExposedMethod(function (killer) {
  195. __game.sound.playSound('hurt');
  196. __game._restartLevel();
  197.  
  198. __map.displayChapter('You have been killed by \n' + killer + '!', 'death');
  199. }, this);
  200.  
  201. this.hasItem = wrapExposedMethod(function (itemName) {
  202. return __game.checkInventory(itemName);
  203. }, this);
  204.  
  205. this.removeItem = wrapExposedMethod(function (itemName) {
  206. var object = __game.objects[itemName];
  207.  
  208. __game.removeFromInventory(itemName);
  209. __game.sound.playSound('blip');
  210. }, this);
  211.  
  212. this.setPhoneCallback = wrapExposedMethod(function(func) {
  213. this._phoneFunc = func;
  214. }, this);
  215.  
  216. }
Add Comment
Please, Sign In to add comment