Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 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.  
  100. this._pickUpItem = function (itemName, object) {
  101. if (__game._isPlayerCodeRunning()) { throw 'Forbidden method call: player._pickUpItem()';}
  102.  
  103. var player = this;
  104.  
  105. __game.addToInventory(itemName);
  106. __map._removeItemFromMap(__x, __y, itemName);
  107. __map.refresh();
  108. __game.sound.playSound('pickup');
  109.  
  110. if (object.onPickUp) {
  111. __game.validateCallback(function () {
  112. setTimeout(function () {
  113. object.onPickUp(player);
  114. }, 100);
  115. // timeout is so that written text is not immediately overwritten
  116. // TODO: play around with Display.writeStatus so that this is
  117. // not necessary
  118. });
  119. }
  120. };
  121.  
  122. /* exposed methods */
  123.  
  124. this.atLocation = wrapExposedMethod(function (x, y) {
  125. return (__x === x && __y === y);
  126. }, this);
  127.  
  128. this.move = wrapExposedMethod(function (direction, fromKeyboard) {
  129. if (!this._canMove) { // mainly for key delay
  130. return false;
  131. }
  132.  
  133. if (__map._overrideKeys[direction] && fromKeyboard) {
  134. try {
  135. __game.validateCallback(__map._overrideKeys[direction], true);
  136.  
  137. __map.refresh();
  138. this._canMove = false;
  139. __map._reenableMovementForPlayer(this); // (key delay can vary by map)
  140. this._afterMove(__x, __y);
  141. } catch (e) {
  142. }
  143.  
  144. return;
  145. }
  146.  
  147. var new__x;
  148. var new__y;
  149. if (direction === 'up') {
  150. new__x = __x;
  151. new__y = __y - 1;
  152. }
  153. else if (direction === 'down') {
  154. new__x = __x;
  155. new__y = __y + 1;
  156. }
  157. else if (direction === 'left') {
  158. new__x = __x - 1;
  159. new__y = __y;
  160. }
  161. else if (direction === 'right') {
  162. new__x = __x + 1;
  163. new__y = __y;
  164. }
  165. else if (direction === 'rest') {
  166. new__x = __x;
  167. new__y = __y;
  168. }
  169. else if (direction === 'funcPhone') {
  170. __game.usePhone();
  171. return;
  172. }
  173.  
  174. if (__map._canMoveTo(new__x, new__y)) {
  175. __x = new__x;
  176. __y = new__y;
  177.  
  178. __map.refresh();
  179.  
  180. this._canMove = false;
  181.  
  182. __lastMoveDirection = direction;
  183. this._afterMove(__x, __y);
  184.  
  185. __map._reenableMovementForPlayer(this); // (key delay can vary by map)
  186. } else {
  187. // play bump sound
  188. __game.sound.playSound('select');
  189. }
  190. }, this);
  191.  
  192. this.killedBy = wrapExposedMethod(function (killer) {
  193. __game.sound.playSound('hurt');
  194. __game._restartLevel();
  195.  
  196. __map.displayChapter('You have been killed by \n' + killer + '!', 'death');
  197. }, this);
  198.  
  199. this.hasItem = wrapExposedMethod(function (itemName) {
  200. return __game.checkInventory(itemName);
  201. }, this);
  202.  
  203. this.removeItem = wrapExposedMethod(function (itemName) {
  204. var object = __game.objects[itemName];
  205.  
  206. __game.removeFromInventory(itemName);
  207. __game.sound.playSound('blip');
  208. }, this);
  209.  
  210. this.setPhoneCallback = wrapExposedMethod(function(func) {
  211. this._phoneFunc = func;
  212. }, this);
  213.  
  214. this.setPhoneCallback(function(){__game._moveToNextLevel()});
  215.  
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement