aporokizzu

Sprite_Parallax

Mar 6th, 2021 (edited)
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // sprite_parallax.js v1.0
  3. // by aporokizzu@gmail
  4. // released under MIT license
  5. //=============================================================================
  6.  
  7. //-----------------------------------------------------------------------------
  8. // Sprite_Parallax
  9. //
  10. // The sprite for displaying a looping, tiling image.
  11. //
  12. // Usage:
  13. //      const starrySkies = ImageManager.loadParallax(`StarrySky`);
  14. //      starrySkies.addLoadListener(
  15. //          function(bitmap) {
  16. //              const _parallax = new Sprite_Parallax();
  17. //              _parallax.setBmp(bitmap);
  18. //              SceneManager._scene.addChild(_parallax);
  19. //          }.bind(this)
  20. //      );
  21. //      
  22.  
  23. class Sprite_Parallax extends TilingSprite {
  24.  
  25.     initialize(bitmap) {
  26.         TilingSprite.prototype.initialize.apply(this, bitmap);
  27.         this.initParallax();
  28.         this.move(0, 0, Graphics.width, Graphics.height);
  29.     };
  30.  
  31.     initParallax() {
  32.         this._parallaxY          = 0;
  33.         this._parallaxX          = 0;
  34.         this._tileWidth          = Game_Map.prototype.tileWidth();
  35.         this._tileHeight         = Game_Map.prototype.tileHeight();
  36.         this._speedX             = 1;
  37.         this._speedY             = 1;
  38.         this._parallaxEnabled    = true;
  39.         this._loopX              = true;
  40.         this._loopY              = true;
  41.     };
  42.  
  43.     enable() {
  44.         this.enableLoopX();
  45.         this.enableLoopY();
  46.         this._parallaxEnabled = true;
  47.     };
  48.  
  49.     disable() {
  50.         this._parallaxEnabled = false;
  51.     };
  52.  
  53.     enableLoopX() {
  54.         this._loopX = true;
  55.     };
  56.  
  57.     enableLoopY() {
  58.         this._loopY = true;
  59.     };
  60.  
  61.     disableLoopX() {
  62.         this._loopX = false;
  63.     };
  64.  
  65.     enableLoopY() {
  66.         this._loopY = false;
  67.     };
  68.  
  69.     disableLoopY() {
  70.         this._loopY = false;
  71.     };
  72.  
  73.     isEnabled() {
  74.         return (this._parallaxEnabled == true);
  75.     };
  76.  
  77.     isLoopingX() {
  78.         return (this._loopX == true);
  79.     };
  80.  
  81.     isLoopingY() {
  82.         return (this._loopY == true);
  83.     };
  84.  
  85.     setSpeedY(speed) {
  86.         this._speedX = speed;
  87.     };
  88.  
  89.     speedY() {
  90.         return this._speedY;
  91.     };
  92.  
  93.     setSpeedX(speed) {
  94.         this._speedX = speed;
  95.     };
  96.  
  97.     speedX() {
  98.         return this._speedX;
  99.     };
  100.  
  101.     setSpeed(speed) {
  102.         this.setSpeedX(speed);
  103.         this.setSpeedY(speed);
  104.     };
  105.  
  106.     tileWidth() {
  107.         return this._tileWidth;
  108.     };
  109.  
  110.     tileHeight() {
  111.         return this._tileHeight;
  112.     };
  113.  
  114.     parallaxX() {
  115.         return this._parallaxX;
  116.     };
  117.  
  118.     parallaxY() {
  119.         return this._parallaxY;
  120.     };
  121.  
  122.     setBmp(bmp) {
  123.         if (bmp.isString()) {
  124.             const buffer = ImageManager.loadParallax(bmp);
  125.             buffer.addLoadListener(function(){
  126.                 this.bitmap = buffer;
  127.             }.bind(this));
  128.         } else {
  129.             this.bitmap = bmp;
  130.         };
  131.     };
  132.  
  133.     update() {
  134.         TilingSprite.prototype.update.call(this, arguments);
  135.         (this.isEnabled()) && this.updateParallax();
  136.     };
  137.  
  138.     updateParallax() {
  139.         const updateParallaxOrigins = () => {
  140.             this.origin.x = this.parallax0x();
  141.             this.origin.y = this.parallax0y();
  142.         };
  143.         this.incParallaxY();
  144.         this.incParallaxX();
  145.         updateParallaxOrigins();
  146.     };
  147.  
  148.     incParallaxY() {
  149.         const check = this.isLoopingY();
  150.         this._parallaxY += (check) ?
  151.             (function(){
  152.                 const tw = this.tileWidth();
  153.                 const sy = this.speedY();
  154.                 const ad = (sy / tw / 2);
  155.                 return ad;
  156.             }.bind(this))() :
  157.             0;
  158.     };
  159.  
  160.     incParallaxX() {
  161.         const check = this.isLoopingX();
  162.         this._parallaxX += (check) ?
  163.             (function(){
  164.                 const th = this.tileHeight();
  165.                 const sx = this.speedX();
  166.                 const ad = (sx / th / 2);
  167.                 return ad;
  168.             }.bind(this))() :
  169.             0;
  170.     };
  171.  
  172.     parallax0x() {
  173.         const check  = this.isLoopingX();
  174.         const retVal = (check) ?
  175.             (function(){
  176.                 const px = this.parallaxX();
  177.                 const tw = this.tileWidth();
  178.                 const rv = px * tw / 2;
  179.                 return rv;
  180.             }.bind(this))() :
  181.             0;
  182.         return retVal;
  183.     };
  184.  
  185.     parallax0y() {
  186.         const check  = this.isLoopingY();
  187.         const retVal = (check) ?
  188.             (function(){
  189.                 const py = this.parallaxY();
  190.                 const th = this.tileHeight();
  191.                 const rv = py * th / 2;
  192.                 return rv;
  193.             }.bind(this))() :
  194.             0;
  195.         return retVal;
  196.     };
  197.  
  198.     shuffle() {
  199.         const chance = Math.random();
  200.         const speed  = Math.random() + Math.randomIntBetween(3,4);
  201.         (chance > 0.5) && this.setSpeedY(speed) || this.setSpeedX(speed);
  202.     };
  203.  
  204. };
Add Comment
Please, Sign In to add comment