@koreez/phaser2-animate
Version:
330 lines • 12.1 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var AnimateFrameElementSymbolInstanceColorMode;
(function (AnimateFrameElementSymbolInstanceColorMode) {
AnimateFrameElementSymbolInstanceColorMode["Alpha"] = "a";
AnimateFrameElementSymbolInstanceColorMode["Advanced"] = "ad";
AnimateFrameElementSymbolInstanceColorMode["Tint"] = "t";
})(AnimateFrameElementSymbolInstanceColorMode || (AnimateFrameElementSymbolInstanceColorMode = {}));
var AnimateElementColor = /** @class */ (function () {
function AnimateElementColor(color) {
this._alpha = AnimateElementColor.calculateAlpha(color);
this._tint = AnimateElementColor.calculateTint(color);
}
Object.defineProperty(AnimateElementColor.prototype, "alpha", {
get: function () {
return this._alpha;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AnimateElementColor.prototype, "tint", {
get: function () {
return this._tint;
},
enumerable: true,
configurable: true
});
AnimateElementColor.rgbToHex = function (r, g, b) {
// tslint:disable-next-line:no-bitwise
return +("0x" + ((1 << 24) + (Math.round(r) << 16) + (Math.round(g) << 8) + Math.round(b)).toString(16).slice(1));
};
AnimateElementColor.calculateAlpha = function (color) {
switch (color.m) {
case AnimateFrameElementSymbolInstanceColorMode.Alpha:
return color.am;
case AnimateFrameElementSymbolInstanceColorMode.Tint:
return 1;
default:
return color.am + color.ao;
}
};
AnimateElementColor.calculateTint = function (color) {
switch (color.m) {
case AnimateFrameElementSymbolInstanceColorMode.Alpha:
return AnimateElementColor.rgbToHex(255, 255, 255);
case AnimateFrameElementSymbolInstanceColorMode.Tint:
return Number.parseInt(color.tc.replace("#", "0x"), 16) * color.tm;
default:
return AnimateElementColor.rgbToHex(255 * color.rm + color.ro, 255 * color.gm + color.go, 255 * color.bm + color.bo);
}
};
return AnimateElementColor;
}());
var AnimateElement = /** @class */ (function () {
function AnimateElement(elementData, symbolNames, instanceNames) {
this.position = { x: 0, y: 0 };
this.rotation = 0;
this.scaling = { x: 1, y: 1 };
var pSplit = elementData.t.p.split("|");
this.position.x = Number.parseInt(pSplit[0], 10);
this.position.y = Number.parseInt(pSplit[1], 10);
if (elementData.t.r) {
this.rotation = elementData.t.r;
}
if (elementData.t.s) {
var sSplit = elementData.t.s.split("|");
if (sSplit[0].length > 0) {
this.scaling.x = Number.parseFloat(sSplit[0]);
}
if (sSplit[1].length > 0) {
this.scaling.y = Number.parseFloat(sSplit[1]);
}
}
this.name = symbolNames[elementData.s];
this.key = "" + this.name + (elementData.i !== undefined ? ":" + instanceNames[elementData.i] : "");
if (elementData.c) {
this.color = new AnimateElementColor(elementData.c);
}
}
return AnimateElement;
}());
var AnimateFrame = /** @class */ (function () {
function AnimateFrame(frameData, symbolNames, instanceNames) {
var _this = this;
this.elements = [];
frameData.e.forEach(function (e) {
_this.elements.push(new AnimateElement(e.s, symbolNames, instanceNames));
});
}
return AnimateFrame;
}());
var AnimateLayer = /** @class */ (function () {
function AnimateLayer(layerData, symbolNames, instanceNames) {
var _this = this;
this.images = new Map();
this.frames = [];
layerData.f.forEach(function (f) {
_this.frames.push(new AnimateFrame(f, symbolNames, instanceNames));
});
}
return AnimateLayer;
}());
var Animate = /** @class */ (function (_super) {
__extends(Animate, _super);
function Animate(game, x, y, animationData, textureKey, loop) {
if (loop === void 0) { loop = false; }
var _this = _super.call(this, game, x, y) || this;
_this._layers = [];
_this._currentFrame = 0;
_this._loop = false;
_this._textureKey = textureKey;
_this._loop = loop;
_this._layers = Animate.parse(animationData);
_this._layers.reverse();
_this._totalFrames = Math.max.apply(Math, _this._layers.map(function (layer) { return layer.frames.length; }));
_this._startFrame = 0;
_this._endFrame = _this._totalFrames - 1;
_this.gotoAndStop(0);
return _this;
}
Object.defineProperty(Animate.prototype, "currentFrame", {
/**
* Specifies the number of the frame in which the playhead is located in
* the timeline of the Animate instance.
*/
get: function () {
return this._currentFrame;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Animate.prototype, "totalFrames", {
/**
* The total number of frames in the Animate instance.
*/
get: function () {
return this._totalFrames;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Animate.prototype, "loop", {
get: function () {
return this._loop;
},
set: function (value) {
this._loop = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Animate.prototype, "isPlaying", {
/**
* A Boolean value that indicates whether a movie clip is curently playing.
*/
get: function () {
return !this._paused;
},
enumerable: true,
configurable: true
});
/**
* Starts playing the Animate file at the specified frame.
*
* @param frame A number representing the frame number, to which the playhead is sent.
* @param endFrame A number representing the frame number, to which the playhead is stop.
*
*/
Animate.prototype.gotoAndPlay = function (frame, endFrame) {
if (endFrame === void 0) { endFrame = this.totalFrames - 1; }
this.playFrames(frame, endFrame, false);
};
/**
* Brings the playhead to the specified frame of the Animate and stops it there.
*
* @param frame A number representing the frame number, to which the playhead is sent.
*/
Animate.prototype.gotoAndStop = function (frame) {
this.goTo(frame, true);
};
/**
* Brings the playhead to the specified frame.
*
* @param startFrame A number representing the frame number, to which the playhead is sent.
* @param endFrame A number representing the frame number, to which the playhead is loop.
*/
Animate.prototype.gotoAndLoop = function (startFrame, endFrame) {
this.playFrames(startFrame, endFrame, true);
};
/**
* Sends the playhead to the next frame and stops it.
*
*/
Animate.prototype.nextFrame = function () {
this.gotoAndStop(this.currentFrame + 1);
};
/**
* Sends the playhead to the previous frame and stops it.
*
*/
Animate.prototype.prevFrame = function () {
this.gotoAndStop(this.currentFrame - 1);
};
/**
* Moves the playhead in the timeline of the Animate.
*
*/
// @ts-ignore
Animate.prototype.play = function () {
this._paused = false;
};
/**
* Stops the playhead in the Animate.
*
*/
Animate.prototype.stop = function () {
this._paused = true;
};
Animate.prototype.update = function () {
_super.prototype.update.call(this);
if (this._paused) {
return;
}
this.onNextFrame();
};
Animate.prototype.onNextFrame = function (movesThePlayhead) {
var _this = this;
if (movesThePlayhead === void 0) { movesThePlayhead = true; }
this._layers.forEach(function (layer) {
var prevFrame = _this._currentFrame - 1;
if (prevFrame === _this._startFrame - 1) {
prevFrame = _this._endFrame;
}
_this.clearFrames(layer, prevFrame);
_this.drawFrames(layer, _this._currentFrame);
});
if (!movesThePlayhead) {
return;
}
if (this._currentFrame === this._endFrame) {
this._currentFrame = this._startFrame;
if (!this._loop) {
this._paused = true;
}
}
else {
this._currentFrame += 1;
}
};
Animate.prototype.clearFrames = function (layer, frame) {
var images = layer.images;
if (!layer.frames[frame]) {
return;
}
layer.frames[frame].elements.forEach(function (element) {
var image = images.get(element.key);
if (image) {
image.visible = false;
}
});
};
Animate.prototype.drawFrames = function (layer, frame) {
var _this = this;
var images = layer.images;
if (!layer.frames[frame]) {
return;
}
layer.frames[frame].elements.forEach(function (element) {
var image = images.get(element.key);
if (!image) {
image = _this._textureKey
? _this.game.add.image(0, 0, _this._textureKey, element.name)
: _this.game.add.image(0, 0, element.name);
image.anchor.setTo(0.5);
images.set(element.key, image);
_this.addChild(image);
}
image.position.setTo(element.position.x, element.position.y);
image.scale.setTo(element.scaling.x, element.scaling.y);
image.rotation = element.rotation;
if (element.color) {
image.tint = element.color.tint;
image.alpha = element.color.alpha;
}
else {
image.tint = 0xffffff;
image.alpha = 1;
}
image.visible = true;
});
};
Animate.prototype.goTo = function (frame, paused) {
var _this = this;
this._layers.forEach(function (layer) { return _this.clearFrames(layer, _this._currentFrame); });
this._currentFrame = Math.min(Math.max(0, frame), this.totalFrames - 1);
this.onNextFrame(false);
this._paused = paused;
};
Animate.prototype.playFrames = function (frame, endFrame, loop) {
this.goTo(frame, false);
this.loop = loop;
this._startFrame = Math.min(Math.max(0, frame), this.totalFrames - 1);
this._endFrame = Math.min(Math.max(0, endFrame), this.totalFrames - 1);
};
Animate.parse = function (animationData) {
var layers = [];
animationData.l.forEach(function (l) {
if (l.f.length > 0) {
layers.push(new AnimateLayer(l, animationData.s, animationData.i));
}
});
return layers;
};
return Animate;
}(Phaser.Sprite));
exports.Animate = Animate;
//# sourceMappingURL=Animate.js.map