mai3-phaser-sdk
Version:
A UI component library based on the Phaser game engine
297 lines (296 loc) • 13 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 _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
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);
}
(_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);
this.addChild(this.instance);
this.RefreshBounds();
this.setDepth((_s = (_r = this._config) === null || _r === void 0 ? void 0 : _r.depth) !== null && _s !== void 0 ? _s : 1);
if (this._config.isGroup) {
this.reDrawGroup();
}
this.handleUpdateListener();
};
Sprite.prototype.handleUpdateListener = function () {
var _this = this;
// Remove old listener
this.removeUpdateListener();
// Monitor sprite velocity changes
if (this._config.gravity && this._config.gravity > 0) {
this._updateListener = function () {
var _a;
if ((_a = _this.instance) === null || _a === void 0 ? void 0 : _a.body) {
// When vertical velocity is close to 0 and sprite is on the ground, it means falling has stopped
if (Math.abs(_this.instance.body.velocity.y) <= 0 && _this.instance.body.blocked.down) {
_this.updatePosition();
_this.removeUpdateListener();
}
}
};
this.scene.events.on('update', this._updateListener);
}
};
Sprite.prototype.removeUpdateListener = function () {
if (this._updateListener) {
this.scene.events.off('update', this._updateListener);
this._updateListener = undefined;
}
};
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;
var _b = Utils.getWorldPosition(this.instance), x = _b.x, y = _b.y;
this.setPosition(x, y);
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setPosition(0, 0);
this.RefreshBounds();
};
Sprite.prototype.moveLeft = function (velocity) {
var _a, _b;
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';
this.updatePosition();
}
};
Sprite.prototype.moveRight = function (velocity) {
var _a, _b;
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';
this.updatePosition();
}
};
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';
// this.directionX = 'none';
this.updatePosition();
}
};
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';
// this.directionX = 'none';
this.updatePosition();
}
};
Sprite.prototype.stopHorizontal = function () {
var _a;
if (this._config.enableMove) {
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setVelocityX(0);
this.updatePosition();
this.handleUpdateListener();
}
};
Sprite.prototype.stopVertical = function () {
var _a;
if (this._config.enableMove) {
(_a = this.instance) === null || _a === void 0 ? void 0 : _a.setVelocityY(0);
this.updatePosition();
this.handleUpdateListener();
}
};
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();
};
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) {
// Remove update event listener
this.removeUpdateListener();
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 };