@fuwu-yuan/bgew
Version:
Baguette Game Engine Web is a french 2D Web game engine
164 lines (163 loc) • 5.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpriteSheetImage = void 0;
const image_1 = require("./image");
class SpriteSheetImage extends image_1.Image {
constructor(src, spritesCountX, spritesCountY, x, y, width, height, defaultSpritePosition = { x: 0, y: 0 }) {
super(src, x, y, width, height);
this.speedTimer = 0;
this._animations = [];
this._currentAnimation = null;
this._currentAnimationFrameIndex = 0;
this._pause = false;
this._spriteSheetWidth = 0;
this._spriteSheetHeight = 0;
this._spriteX = 0;
this._spriteY = 0;
this._spritePaddingX = 0;
this._spritePaddingY = 0;
this._spritesCountX = spritesCountX;
this._spritesCountY = spritesCountY;
this._spriteX = defaultSpritePosition.x;
this._spriteY = defaultSpritePosition.y;
this.update(0);
}
update(delta) {
super.update(delta);
if (this._spriteSheetWidth === 0 || this._spriteSheetHeight === 0) {
this._spriteSheetWidth = this.image.width;
this._spriteSheetHeight = this.image.height;
this._sourceW = (this._spriteSheetWidth - (this._spritePaddingX * (this._spritesCountX + 1))) / this._spritesCountX;
this._sourceH = (this._spriteSheetHeight - (this._spritePaddingY * (this._spritesCountY + 1))) / this._spritesCountY;
}
if (this._sourceW && this._sourceH) {
this._sourceX = this._spriteX * (this._sourceW + this._spritePaddingX) + this._spritePaddingX;
this._sourceY = this._spriteY * (this._sourceH + this._spritePaddingY) + this._spritePaddingY;
}
if (this.currentAnimation && !this.pause) {
if (this.speedTimer === 0 || this.speedTimer >= this.currentAnimation.speed / this.currentAnimation.frames.length) {
this.nextFrame();
this.speedTimer = 0;
}
this.speedTimer += delta;
}
}
nextFrame() {
if (this.currentAnimation) {
this._spriteX = this.currentAnimation.frames[this._currentAnimationFrameIndex].x;
this._spriteY = this.currentAnimation.frames[this._currentAnimationFrameIndex].y;
this._currentAnimationFrameIndex++;
if (this._currentAnimationFrameIndex > this.currentAnimation.frames.length - 1) {
this._currentAnimationFrameIndex = 0;
}
}
}
/**
* Animate the spritesheet with the registered animation
* To register an animation see {@link SpriteSheetImage.addAnimation}
* @param name
* @param speed
*/
animate(name, speed = null) {
this._currentAnimationFrameIndex = 0;
this._currentAnimation = null;
let animation = this._animations.find((a) => {
return a.name === name;
});
if (animation) {
this._currentAnimation = animation;
if (speed)
this._currentAnimation.speed = speed;
this.nextFrame();
}
return this._currentAnimation;
}
/**
* Register a new animation
* Animation can be started by calling {@link SpriteSheetImage.animate} with the animation name
*
* @param name
* @param fromSpriteY
* @param fromSpriteX
* @param toSpriteY
* @param toSpriteX
* @param speed
*/
addAnimation(name, fromSpriteY, fromSpriteX, toSpriteY, toSpriteX, speed) {
let frames = [];
for (let y = fromSpriteY; y <= toSpriteY; y++) {
let x = fromSpriteX;
let maxX = toSpriteX;
if (y > fromSpriteY) {
x = 0;
}
if (y < toSpriteY) {
maxX = this._spritesCountX - 1;
}
while (x <= maxX) {
frames.push({ y: y, x: x });
x++;
}
}
this._animations.push({
name: name,
speed: speed,
frames: frames
});
}
/**
* @param name Name of the animation to remove
* @return Index of removed animation
*/
removeAnimation(name) {
let i = this.animations.findIndex((a) => {
return a.name === name;
});
if (i > -1) {
this.animations.splice(i, 1);
}
return -1;
}
get animations() {
return this._animations;
}
get currentAnimation() {
return this._currentAnimation;
}
stopAnimation() {
this._currentAnimation = null;
}
get pause() {
return this._pause;
}
set pause(value) {
this._pause = value;
}
get spritePaddingX() {
return this._spritePaddingX;
}
set spritePaddingX(value) {
this._spritePaddingX = value;
this._sourceW = (this._spriteSheetWidth - (this._spritePaddingX * (this._spritesCountX + 1))) / this._spritesCountX;
}
get spritePaddingY() {
return this._spritePaddingY;
}
set spritePaddingY(value) {
this._spritePaddingY = value;
this._sourceH = (this._spriteSheetHeight - (this._spritePaddingY * (this._spritesCountY + 1))) / this._spritesCountY;
}
get spriteX() {
return this._spriteX;
}
set spriteX(value) {
this._spriteX = value;
}
get spriteY() {
return this._spriteY;
}
set spriteY(value) {
this._spriteY = value;
}
}
exports.SpriteSheetImage = SpriteSheetImage;