Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // sprite_parallax.js v1.0
- // by aporokizzu@gmail
- // released under MIT license
- //=============================================================================
- //-----------------------------------------------------------------------------
- // Sprite_Parallax
- //
- // The sprite for displaying a looping, tiling image.
- //
- // Usage:
- // const starrySkies = ImageManager.loadParallax(`StarrySky`);
- // starrySkies.addLoadListener(
- // function(bitmap) {
- // const _parallax = new Sprite_Parallax();
- // _parallax.setBmp(bitmap);
- // SceneManager._scene.addChild(_parallax);
- // }.bind(this)
- // );
- //
- class Sprite_Parallax extends TilingSprite {
- initialize(bitmap) {
- TilingSprite.prototype.initialize.apply(this, bitmap);
- this.initParallax();
- this.move(0, 0, Graphics.width, Graphics.height);
- };
- initParallax() {
- this._parallaxY = 0;
- this._parallaxX = 0;
- this._tileWidth = Game_Map.prototype.tileWidth();
- this._tileHeight = Game_Map.prototype.tileHeight();
- this._speedX = 1;
- this._speedY = 1;
- this._parallaxEnabled = true;
- this._loopX = true;
- this._loopY = true;
- };
- enable() {
- this.enableLoopX();
- this.enableLoopY();
- this._parallaxEnabled = true;
- };
- disable() {
- this._parallaxEnabled = false;
- };
- enableLoopX() {
- this._loopX = true;
- };
- enableLoopY() {
- this._loopY = true;
- };
- disableLoopX() {
- this._loopX = false;
- };
- enableLoopY() {
- this._loopY = false;
- };
- disableLoopY() {
- this._loopY = false;
- };
- isEnabled() {
- return (this._parallaxEnabled == true);
- };
- isLoopingX() {
- return (this._loopX == true);
- };
- isLoopingY() {
- return (this._loopY == true);
- };
- setSpeedY(speed) {
- this._speedX = speed;
- };
- speedY() {
- return this._speedY;
- };
- setSpeedX(speed) {
- this._speedX = speed;
- };
- speedX() {
- return this._speedX;
- };
- setSpeed(speed) {
- this.setSpeedX(speed);
- this.setSpeedY(speed);
- };
- tileWidth() {
- return this._tileWidth;
- };
- tileHeight() {
- return this._tileHeight;
- };
- parallaxX() {
- return this._parallaxX;
- };
- parallaxY() {
- return this._parallaxY;
- };
- setBmp(bmp) {
- if (bmp.isString()) {
- const buffer = ImageManager.loadParallax(bmp);
- buffer.addLoadListener(function(){
- this.bitmap = buffer;
- }.bind(this));
- } else {
- this.bitmap = bmp;
- };
- };
- update() {
- TilingSprite.prototype.update.call(this, arguments);
- (this.isEnabled()) && this.updateParallax();
- };
- updateParallax() {
- const updateParallaxOrigins = () => {
- this.origin.x = this.parallax0x();
- this.origin.y = this.parallax0y();
- };
- this.incParallaxY();
- this.incParallaxX();
- updateParallaxOrigins();
- };
- incParallaxY() {
- const check = this.isLoopingY();
- this._parallaxY += (check) ?
- (function(){
- const tw = this.tileWidth();
- const sy = this.speedY();
- const ad = (sy / tw / 2);
- return ad;
- }.bind(this))() :
- 0;
- };
- incParallaxX() {
- const check = this.isLoopingX();
- this._parallaxX += (check) ?
- (function(){
- const th = this.tileHeight();
- const sx = this.speedX();
- const ad = (sx / th / 2);
- return ad;
- }.bind(this))() :
- 0;
- };
- parallax0x() {
- const check = this.isLoopingX();
- const retVal = (check) ?
- (function(){
- const px = this.parallaxX();
- const tw = this.tileWidth();
- const rv = px * tw / 2;
- return rv;
- }.bind(this))() :
- 0;
- return retVal;
- };
- parallax0y() {
- const check = this.isLoopingY();
- const retVal = (check) ?
- (function(){
- const py = this.parallaxY();
- const th = this.tileHeight();
- const rv = py * th / 2;
- return rv;
- }.bind(this))() :
- 0;
- return retVal;
- };
- shuffle() {
- const chance = Math.random();
- const speed = Math.random() + Math.randomIntBetween(3,4);
- (chance > 0.5) && this.setSpeedY(speed) || this.setSpeedX(speed);
- };
- };
Add Comment
Please, Sign In to add comment