@mai3/phaser-sdk
Version:
A UI component library based on the Phaser game engine
300 lines (299 loc) • 14.1 kB
JavaScript
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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import Utils from "../utils";
import { BaseButton } from "./BaseButton";
var Sprite = /** @class */ (function (_super) {
__extends(Sprite, _super);
function Sprite(scene, config) {
var _a;
var _this = _super.call(this, scene, config, "Sprite") || this;
_this.directionX = 'none';
_this.directionY = 'none';
_this._useCount = 0;
_this._config = config;
_this.reDraw(config);
_this.setEventInteractive();
(_a = _this._config.animConfigs) === null || _a === void 0 ? void 0 : _a.forEach(function (animconfig) {
_this.createAnimsSprite(animconfig.key, animconfig);
});
return _this;
}
Sprite.prototype.reDraw = function (config) {
var _this = this;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
this._config = config;
this._config.width = (_a = config.width) !== null && _a !== void 0 ? _a : 0;
this._config.height = (_b = config.height) !== null && _b !== void 0 ? _b : 0;
this._config.leftVelocity = (_c = config.leftVelocity) !== null && _c !== void 0 ? _c : 0;
this._config.rightVelocity = (_d = config.rightVelocity) !== null && _d !== void 0 ? _d : 0;
this._config.upVelocity = (_e = config.upVelocity) !== null && _e !== void 0 ? _e : 0;
this._config.downVelocity = (_f = config.downVelocity) !== null && _f !== void 0 ? _f : 0;
this._config.repeat = (_g = config.repeat) !== null && _g !== void 0 ? _g : 0;
if (this.instance) {
this.instance.destroy();
this.instance = undefined;
}
this.instance = this.scene.physics.add.sprite(0, 0, (_h = this._config.key) !== null && _h !== void 0 ? _h : "", (_j = this._config.frame) !== null && _j !== void 0 ? _j : 0);
if (this._config.isStatic) {
(_k = this.instance) === null || _k === void 0 ? void 0 : _k.setImmovable(true);
}
if (this._config.isCameraFollow) {
this.scene.cameras.main.startFollow(this.instance, true);
}
(_l = this.instance) === null || _l === void 0 ? void 0 : _l.setGravityY((_m = this._config.gravity) !== null && _m !== void 0 ? _m : 0);
(_o = this.instance) === null || _o === void 0 ? void 0 : _o.setCollideWorldBounds(true);
(_p = this.instance) === null || _p === void 0 ? void 0 : _p.setDisplaySize(this._config.width, this._config.height);
(_q = this.instance) === null || _q === void 0 ? void 0 : _q.setOrigin(0);
if (this._config.flipX) {
(_r = this.instance) === null || _r === void 0 ? void 0 : _r.setScale(-Math.abs(this.instance.scaleX), this.instance.scaleY);
(_s = this.instance) === null || _s === void 0 ? void 0 : _s.setPosition(this._config.width, 0);
}
this.addChild(this.instance);
this.RefreshBounds();
this.setDepth((_u = (_t = this._config) === null || _t === void 0 ? void 0 : _t.depth) !== null && _u !== void 0 ? _u : 1);
this.setScrollFactor(this._config.isScrollFactor ? 0 : 1);
if (this._config.isGroup) {
this.reDrawGroup();
}
this.instance.on("animationstart", function () {
_this.resetBody();
});
this.instance.on("animationcomplete", function () {
_this.resetBody();
});
this.resetBody();
};
Sprite.prototype.resetBody = function () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
if (this.instance && ((_b = (_a = this._config) === null || _a === void 0 ? void 0 : _a.height) !== null && _b !== void 0 ? _b : 0) < (((_c = this.instance) === null || _c === void 0 ? void 0 : _c.height) * this.instance.scaleY)) {
if ((_d = this.instance.body) === null || _d === void 0 ? void 0 : _d.blocked.down) {
this.setPosition((_e = this._config.x) !== null && _e !== void 0 ? _e : 0, ((_f = this._config.y) !== null && _f !== void 0 ? _f : 0) - (this.instance.height * this.instance.scaleY - ((_h = (_g = this._config) === null || _g === void 0 ? void 0 : _g.height) !== null && _h !== void 0 ? _h : 0)));
}
}
(_k = (_j = this.instance) === null || _j === void 0 ? void 0 : _j.body) === null || _k === void 0 ? void 0 : _k.setSize(this.instance.width, this.instance.height);
if (this._config.flipX) {
(_m = (_l = this.instance) === null || _l === void 0 ? void 0 : _l.body) === null || _m === void 0 ? void 0 : _m.setOffset(-1 * this.instance.width, 0);
}
};
Sprite.prototype.reDrawGroup = function () {
var _a, _b;
if (this.group) {
this.group.destroy();
this.group = undefined;
}
this.group = this.scene.physics.add.group({
defaultKey: (_a = this._config.key) !== null && _a !== void 0 ? _a : "",
maxSize: (_b = this._config.repeat) !== null && _b !== void 0 ? _b : 0
});
};
Sprite.prototype.createAnimsSprite = function (animKey, config) {
var _a;
if (this.scene.anims.exists(animKey))
return;
if (Array.isArray(config.frames)) {
this.scene.anims.create({
key: animKey,
frames: this.scene.anims.generateFrameNumbers((_a = config.frameKey) !== null && _a !== void 0 ? _a : "", { frames: config.frames }),
frameRate: config.frameRate,
repeat: config.repeat,
});
}
else if (Array.isArray(config.keys)) {
this.scene.anims.create({
key: animKey,
frames: config.keys.map(function (key) { return ({ key: key }); }),
frameRate: config.frameRate,
repeat: config.repeat,
});
}
};
Sprite.prototype.play = function (key, ignoreIfPlaying) {
var _a;
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.play(key, ignoreIfPlaying);
};
Sprite.prototype.getGroupChild = function (x, y) {
var _this = this;
var _a, _b;
if (!this._config.isGroup) {
console.log('not sprite group');
return;
}
if (!this.group) {
console.log('not group');
return;
}
// Check if exceeded repeat limit
if (this._config.repeat !== -1 && this._useCount >= this._config.repeat) {
return;
}
var child = (_a = this.group) === null || _a === void 0 ? void 0 : _a.get();
this._useCount++;
if (child) {
child.setActive(true);
child.setVisible(true);
child.body.reset(x, y);
child.setDisplaySize(this._config.width, this._config.height);
// Copy animation from instance if it exists
if ((_b = this.instance) === null || _b === void 0 ? void 0 : _b.anims.currentAnim) {
var currentAnim = this.instance.anims.currentAnim;
child.play(currentAnim.key, {
frameRate: currentAnim.frameRate,
repeat: currentAnim.repeat
});
}
// Add update check to recycle when out of bounds
this.scene.events.on('update', function () {
var _a, _b, _c;
if (child.active) {
var bounds = (_c = (_b = (_a = _this.scene) === null || _a === void 0 ? void 0 : _a.physics) === null || _b === void 0 ? void 0 : _b.world) === null || _c === void 0 ? void 0 : _c.bounds;
if (bounds) {
if (child.x > bounds.width ||
child.x < bounds.x ||
child.y > bounds.height ||
child.y < bounds.y) {
child.setActive(false);
child.setVisible(false);
child.destroy();
}
}
}
});
return child;
}
return;
};
Sprite.prototype.updatePosition = function () {
var _a, _b, _c;
if (!this.instance)
return;
var _d = Utils.getWorldPosition(this.instance), x = _d.x, y = _d.y;
var xOffset = this.config.flipX ? -((_a = this.config.width) !== null && _a !== void 0 ? _a : 0) : 0;
var roundedX = Math.round(x + xOffset);
var roundedY = Math.round(y);
this.setPosition(roundedX, roundedY);
if (this._config.flipX) {
(_b = this.instance) === null || _b === void 0 ? void 0 : _b.setPosition(this._config.width, 0);
}
else {
(_c = this.instance) === null || _c === void 0 ? void 0 : _c.setPosition(0, 0);
}
this.RefreshBounds();
};
Sprite.prototype.moveLeft = function (velocity) {
var _a, _b, _c, _d;
if (this._config.enableMove) {
if (velocity)
this._config.leftVelocity = velocity;
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setVelocityX(-((_b = this._config.leftVelocity) !== null && _b !== void 0 ? _b : 0));
this.directionX = 'left';
this.directionY = 'none';
(_c = this.instance) === null || _c === void 0 ? void 0 : _c.setDragX(Math.abs(((_d = this._config.leftVelocity) !== null && _d !== void 0 ? _d : 0) * 3));
}
};
Sprite.prototype.moveRight = function (velocity) {
var _a, _b, _c, _d;
if (this._config.enableMove) {
if (velocity)
this._config.rightVelocity = velocity;
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setVelocityX((_b = this._config.rightVelocity) !== null && _b !== void 0 ? _b : 0);
this.directionX = 'right';
this.directionY = 'none';
(_c = this.instance) === null || _c === void 0 ? void 0 : _c.setDragX(Math.abs(((_d = this._config.rightVelocity) !== null && _d !== void 0 ? _d : 0) * 3));
}
};
Sprite.prototype.moveUpward = function (velocity) {
var _a, _b;
if (this._config.enableMove) {
if (velocity)
this._config.upVelocity = velocity;
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setVelocityY(-((_b = this._config.upVelocity) !== null && _b !== void 0 ? _b : 0));
this.directionY = 'up';
}
};
Sprite.prototype.moveDownward = function (velocity) {
var _a, _b;
if (this._config.enableMove) {
if (velocity)
this._config.downVelocity = velocity;
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setVelocityY((_b = this._config.downVelocity) !== null && _b !== void 0 ? _b : 0);
this.directionY = 'down';
}
};
Sprite.prototype.stopHorizontal = function () {
var _a;
if (this._config.enableMove) {
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setVelocityX(0);
}
};
Sprite.prototype.stopVertical = function () {
var _a;
if (this._config.enableMove) {
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setVelocityY(0);
}
};
Sprite.prototype.setImmovable = function (immovable) {
var _a;
if (immovable === void 0) { immovable = true; }
if ((_a = this.instance) === null || _a === void 0 ? void 0 : _a.body) {
this.instance.body.enable = !immovable;
}
};
Sprite.prototype.setBounce = function () {
var _a, _b;
if (this._config.enableMove) {
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setBounce((_b = this._config.bounce) !== null && _b !== void 0 ? _b : 0);
}
};
Sprite.prototype.setGravity = function () {
var _a, _b;
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setGravityY((_b = this._config.gravity) !== null && _b !== void 0 ? _b : 0);
};
Sprite.prototype.stop = function () {
var _a;
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.stop();
this.updatePosition();
};
Sprite.prototype.setFlipX = function (flip) {
var _a;
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setFlipX(flip);
};
Sprite.prototype.getData = function (key) {
var _a;
return (_a = this.instance) === null || _a === void 0 ? void 0 : _a.getData(key);
};
Sprite.prototype.setData = function (key, data) {
var _a;
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setData(key, data);
return this;
};
Sprite.prototype.destroy = function (fromScene) {
// if (this.group) {
// this.group.clear(true, true);
// this.group.destroy(true);
// this.group = undefined;
// }
if (this.instance) {
if (this.instance.body) {
this.instance.body.enable = false;
}
this.instance.destroy();
this.instance = undefined;
}
_super.prototype.destroy.call(this, fromScene);
};
return Sprite;
}(BaseButton));
export { Sprite };