Advertisement
XArnonX

player

Feb 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Player = (function () {
  2.  
  3.   'use strict';
  4.  
  5.  
  6.   function Class(game, map) {
  7.     // extend abstract's properties
  8.     _.extend(this, new AbstractSprite(game, map));
  9.  
  10.     // set properties
  11.     this.className = 'Player';
  12.     this.spriteName = 'player';
  13.     this.spritesheetPath = 'assets/images/sprites/Spieler.png';
  14.   }
  15.  
  16.  
  17.   // public api
  18.   Class.prototype = {
  19.     preload: preload,
  20.     init: init,
  21.     update: update
  22.   }
  23.   // add the abstract's api
  24.   _.extend(Class.prototype, AbstractSprite.prototype);
  25.  
  26.   return Class;
  27.  
  28.  
  29.   // private methods
  30.   function preload() {
  31.     this.gameReference.load.spritesheet(
  32.       this.spriteName,
  33.       this.spritesheetPath,
  34.       SpriteConstants.SIZE,
  35.       SpriteConstants.SIZE
  36.     );
  37.   }
  38.  
  39.   function init(tile) {
  40.     // set position vars
  41.     this.initialTile = tile;
  42.  
  43.     // add sprite
  44.     this.sprite = this.gameReference.add.sprite(
  45.       this.getTileX(1),
  46.       this.getTileY(1),
  47.       //Ursprung
  48.       //this.getTileX(this.initialTile.x),
  49.       //this.getTileY(this.initialTile.y),
  50.       this.spriteName
  51.     );
  52.  
  53.     // set anchor
  54.     this.sprite.anchor.setTo(SpriteConstants.Anchor.X, SpriteConstants.Anchor.Y);
  55.  
  56.     // set movement vars
  57.     this.walkingSpeed = 10;
  58.     this.walkingDirection = SpriteConstants.Direction.DOWN;
  59.  
  60.     // set anim vars
  61.     this.animSpeed = 15,
  62.  
  63.     // add anims
  64.     this.addBasicAnimation();
  65.  
  66.     this.player.physics.arcade.enable(Spieler);
  67.   }
  68.  
  69.   function update(isUpPressed, isRightPressed, isDownPressed, isLeftPressed) {
  70.     if(this.isOnTile()) {
  71.       // clear previous position's collision
  72.       if(this.currentTile) {
  73.         this.mapReference.setCollisionAt(this.currentTile, false);
  74.       }
  75.  
  76.       // get current tile
  77.       this.currentTile = this.getTileFromCurrentPosition();
  78.       this.mapReference.setCollisionAt(this.currentTile, true);
  79.      
  80.       // check surrounding collisions
  81.       this.surroundingCollisions = this.mapReference.getSurroundingCollisionsAt(this.currentTile, true);
  82.  
  83.       if(isUpPressed) {
  84.         this.walkingDirection = SpriteConstants.Direction.UP;
  85.         if(!this.surroundingCollisions.up) {
  86.           this.isWalkingAnim = true;
  87.           this.isMoving = true;
  88.         } else {
  89.           this.isWalkingAnim = false;
  90.           this.isMoving = false;
  91.         }
  92.  
  93.       } else if(isRightPressed) {
  94.         this.walkingDirection = SpriteConstants.Direction.RIGHT;
  95.         if(!this.surroundingCollisions.right) {
  96.           this.isWalkingAnim = true;
  97.           this.isMoving = true;
  98.         } else {
  99.           this.isWalkingAnim = false;
  100.           this.isMoving = false;
  101.         }
  102.  
  103.       } else if(isDownPressed) {
  104.         this.walkingDirection = SpriteConstants.Direction.DOWN;
  105.         if(!this.surroundingCollisions.down) {
  106.           this.isWalkingAnim = true;
  107.           this.isMoving = true;
  108.         } else {
  109.           this.isWalkingAnim = false;
  110.           this.isMoving = false;
  111.         }
  112.  
  113.       } else if(isLeftPressed) {
  114.         this.walkingDirection = SpriteConstants.Direction.LEFT;
  115.         if(!this.surroundingCollisions.left) {
  116.           this.isWalkingAnim = true;
  117.           this.isMoving = true;
  118.         } else {
  119.           this.isWalkingAnim = false;
  120.           this.isMoving = false;
  121.         }
  122.  
  123.       } else {
  124.         this.isWalkingAnim = false;
  125.         this.isMoving = false;
  126.       }
  127.  
  128.     // is not on tile (is moving)
  129.     } else {
  130.       // get next tile
  131.       this.setNextTileFromCurrentDirection();
  132.       this.mapReference.setCollisionAt(this.nextTile, true);
  133.  
  134.       this.surroundingCollisions = this.mapReference.getSurroundingCollisionsAt(this.nextTile, true);
  135.     }
  136.  
  137.     this.setAnim();
  138.     this.move();
  139.   }
  140.  
  141. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement