daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest May 16th, 2018 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.             __game._getLevel(__game._currentLevel + 1, false, true);
  167.             new__x = __x;
  168.             new__y = __y;
  169.         }
  170.         else if (direction === 'funcPhone') {
  171.             __game.usePhone();
  172.             return;
  173.         }
  174.  
  175.         if (__map._canMoveTo(new__x, new__y)) {
  176.             __x = new__x;
  177.             __y = new__y;
  178.  
  179.             __map.refresh();
  180.  
  181.             this._canMove = false;
  182.  
  183.             __lastMoveDirection = direction;
  184.             this._afterMove(__x, __y);
  185.  
  186.             __map._reenableMovementForPlayer(this); // (key delay can vary by map)
  187.         } else {
  188.             // play bump sound
  189.             __game.sound.playSound('select');
  190.         }
  191.     }, this);
  192.  
  193.     this.killedBy = wrapExposedMethod(function (killer) {
  194.         __game.sound.playSound('hurt');
  195.         __game._restartLevel();
  196.  
  197.         __map.displayChapter('You have been killed by \n' + killer + '!', 'death');
  198.     }, this);
  199.  
  200.     this.hasItem = wrapExposedMethod(function (itemName) {
  201.         return __game.checkInventory(itemName);
  202.     }, this);
  203.  
  204.     this.removeItem = wrapExposedMethod(function (itemName) {
  205.         var object = __game.objects[itemName];
  206.  
  207.         __game.removeFromInventory(itemName);
  208.         __game.sound.playSound('blip');
  209.     }, this);
  210.  
  211.     this.setPhoneCallback = wrapExposedMethod(function(func) {
  212.         this._phoneFunc = func;
  213.     }, this);
  214.    
  215. }
RAW Paste Data
Top