Advertisement
XArnonX

abstract-sprite

Feb 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var AbstractSprite = (function () {
  2.  
  3.   'use strict';
  4.  
  5.  
  6.   function Class(game, map) {
  7.     // references
  8.     this.gameReference = game;
  9.     this.mapReference = map;
  10.  
  11.     this.className = 'AbstractSprite';
  12.  
  13.     // properties
  14.     this.sprite = null;
  15.     this.spriteName = null;
  16.     this.spritesheetPath = null;
  17.    
  18.     // positions vars
  19.     this.initialTile = null;
  20.     this.currentTile = null;
  21.     this.nextTile = null;
  22.  
  23.     // animation vars
  24.     this.animSpeed = 0;
  25.     this.isWalkingAnim = false;
  26.  
  27.     // movement vars
  28.     this.surroundingCollisions = null;
  29.     this.walkingDirection = null;
  30.     this.walkingSpeed = 0;
  31.     this.isMoving = false;
  32.   }
  33.  
  34.  
  35.   // public api
  36.   Class.prototype = {
  37.     // anim methods
  38.     addBasicAnimation: addBasicAnimation,
  39.     setAnim: setAnim,
  40.     // movement methods
  41.     move: move,
  42.     // tile management methods
  43.     isOnTile: isOnTile,
  44.     getTileFromCurrentPosition: getTileFromCurrentPosition,
  45.     setNextTileFromCurrentDirection: setNextTileFromCurrentDirection,
  46.     getTileX: getTileX,
  47.     getTileY: getTileY
  48.   };
  49.  
  50.   return Class;
  51.  
  52.  
  53.   // private methods
  54.   function addBasicAnimation() {
  55.     this.sprite.animations.add(SpriteConstants.Animation.STILL_DOWN, [0]);
  56.     this.sprite.animations.add(SpriteConstants.Animation.STILL_UP, [2]);
  57.     this.sprite.animations.add(SpriteConstants.Animation.STILL_SIDE, [4]);
  58.     this.sprite.animations.add(SpriteConstants.Animation.WALKING_DOWN, [0, 1], this.animSpeed, true);
  59.     this.sprite.animations.add(SpriteConstants.Animation.WALKING_UP, [2, 3], this.animSpeed, true);
  60.     this.sprite.animations.add(SpriteConstants.Animation.WALKING_SIDE, [4, 5], this.animSpeed, true);
  61.   }
  62.  
  63.   function setAnim() {
  64.     // walking animations
  65.     if(this.isWalkingAnim) {
  66.       switch(this.walkingDirection) {
  67.         case SpriteConstants.Direction.UP:
  68.           this.sprite.animations.play(SpriteConstants.Animation.WALKING_UP);
  69.           break;
  70.  
  71.         case SpriteConstants.Direction.RIGHT:
  72.           this.sprite.scale.x = -1;
  73.           this.sprite.animations.play(SpriteConstants.Animation.WALKING_SIDE);
  74.           break;
  75.  
  76.         case SpriteConstants.Direction.DOWN:
  77.           this.sprite.animations.play(SpriteConstants.Animation.WALKING_DOWN);
  78.           break;
  79.  
  80.         case SpriteConstants.Direction.LEFT:
  81.           this.sprite.scale.x = 1;
  82.           this.sprite.animations.play(SpriteConstants.Animation.WALKING_SIDE);
  83.           break;
  84.       }
  85.  
  86.     // still/idle animations
  87.     } else {
  88.       switch(this.walkingDirection) {
  89.         case SpriteConstants.Direction.UP:
  90.           this.sprite.animations.play(SpriteConstants.Animation.STILL_UP);
  91.           break;
  92.  
  93.         case SpriteConstants.Direction.RIGHT:
  94.           this.sprite.scale.x = -1;
  95.           this.sprite.animations.play(SpriteConstants.Animation.STILL_SIDE);
  96.           break;
  97.  
  98.         case SpriteConstants.Direction.DOWN:
  99.           this.sprite.animations.play(SpriteConstants.Animation.STILL_DOWN);
  100.           break;
  101.  
  102.         case SpriteConstants.Direction.LEFT:
  103.           this.sprite.scale.x = 1;
  104.           this.sprite.animations.play(SpriteConstants.Animation.STILL_SIDE);
  105.           break;
  106.       }
  107.       this.sprite.animations.stop();
  108.     }
  109.   }
  110.  
  111.   function move() {
  112.     if(this.isMoving) {
  113.       switch(this.walkingDirection) {
  114.         case SpriteConstants.Direction.UP:
  115.           this.sprite.y -= this.walkingSpeed;
  116.           break;
  117.  
  118.         case SpriteConstants.Direction.RIGHT:
  119.           this.sprite.x += this.walkingSpeed;
  120.           break;
  121.  
  122.         case SpriteConstants.Direction.DOWN:
  123.           this.sprite.y += this.walkingSpeed;
  124.           break;
  125.  
  126.         case SpriteConstants.Direction.LEFT:
  127.           this.sprite.x -= this.walkingSpeed;
  128.           break;
  129.       }
  130.     }
  131.   }
  132.  
  133.   function isOnTile() {
  134.     var spriteX = this.sprite.x + (SpriteConstants.Anchor.X * SpriteConstants.SIZE),
  135.         spriteY = this.sprite.y,
  136.         isOn = (spriteX % SpriteConstants.SIZE === 0) && (spriteY % SpriteConstants.SIZE === 0);
  137.     return isOn;
  138.   }
  139.  
  140.   function getTileFromCurrentPosition() {
  141.     var spriteX = this.sprite.x - (SpriteConstants.Anchor.X * SpriteConstants.SIZE),
  142.         spriteY = this.sprite.y - SpriteConstants.SIZE,
  143.         tile = {x: spriteX / SpriteConstants.SIZE, y: spriteY / SpriteConstants.SIZE};
  144.     return tile;
  145.   }
  146.  
  147.   function setNextTileFromCurrentDirection() {
  148.     switch(this.walkingDirection) {
  149.       case SpriteConstants.Direction.UP:
  150.         this.nextTile = {x: this.currentTile.x, y: this.currentTile.y - 1};
  151.         break;
  152.  
  153.       case SpriteConstants.Direction.RIGHT:
  154.         this.nextTile = {x: this.currentTile.x + 1, y: this.currentTile.y};
  155.         break;
  156.  
  157.       case SpriteConstants.Direction.DOWN:
  158.         this.nextTile = {x: this.currentTile.x, y: this.currentTile.y + 1};
  159.         break;
  160.  
  161.       case SpriteConstants.Direction.LEFT:
  162.         this.nextTile = {x: this.currentTile.x - 1, y: this.currentTile.y};
  163.         break;
  164.     }
  165.   }
  166.  
  167.   function getTileX(tileXId) {
  168.     return (SpriteConstants.Anchor.X * SpriteConstants.SIZE) + (tileXId * SpriteConstants.SIZE);
  169.   }
  170.  
  171.   function getTileY(tileYId) {
  172.     return (SpriteConstants.Anchor.Y * SpriteConstants.SIZE) + (tileYId * SpriteConstants.SIZE);
  173.   }
  174.  
  175.  
  176. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement