[phaser] Expose getters for preloaded skeleton data and atlases.

This commit is contained in:
Mario Zechner 2023-04-25 15:13:12 +02:00
parent f8837604f5
commit 5662c75db9
3 changed files with 30 additions and 25 deletions

View File

@ -14,15 +14,30 @@
<h1>Basic example</h1>
</body>
<script>
class BasicScene extends Phaser.Scene {
preload() {
this.load.spineBinary("spineboy-data", "assets/spineboy-pro.skel");
this.load.spineAtlas("spineboy-atlas", "assets/spineboy-pma.atlas");
}
create() {
const spineboy = this.add.spine(400, 500, 'spineboy-data', "spineboy-atlas");
spineboy.scale = 0.5;
spineboy.animationState.setAnimation(0, "walk", true);
const spineboy2 = this.make.spine({
x: 200, y: 500, dataKey: "spineboy-data", atlasKey: "spineboy-atlas"
});
this.add.existing(spineboy2);
}
}
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
type: Phaser.WEBGL,
scene: {
preload: preload,
create: create,
},
scene: [BasicScene],
plugins: {
scene: [
{ key: "spine.SpinePlugin", plugin: spine.SpinePlugin, mapping: "spine" }
@ -31,22 +46,6 @@
};
const game = new Phaser.Game(config);
function preload() {
this.load.spineBinary("spineboy-data", "assets/spineboy-pro.skel");
this.load.spineAtlas("spineboy-atlas", "assets/spineboy-pma.atlas");
}
function create() {
const spineboy = this.add.spine(400, 500, 'spineboy-data', "spineboy-atlas");
spineboy.scale = 0.5;
spineboy.animationState.setAnimation(0, "walk", true);
const spineboy2 = this.make.spine({
x: 200, y: 500, dataKey: "spineboy-data", atlasKey: "spineboy-atlas"
});
this.add(spineboy2);
}
</script>
</html>

View File

@ -112,7 +112,7 @@ export class SpinePlugin extends Phaser.Plugins.ScenePlugin {
return gameObject;
};
let makeSpineGameObject = function (this: Phaser.GameObjects.GameObjectFactory, config: SpineGameObjectConfig, addToScene: boolean) {
let makeSpineGameObject = function (this: Phaser.GameObjects.GameObjectFactory, config: SpineGameObjectConfig, addToScene: boolean = false) {
let x = config.x ? config.x : 0;
let y = config.y ? config.y : 0;
let boundsProvider = config.boundsProvider ? config.boundsProvider : undefined;
@ -184,10 +184,10 @@ export class SpinePlugin extends Phaser.Plugins.ScenePlugin {
}
createSkeleton (dataKey: string, atlasKey: string) {
return new Skeleton(this.createSkeletonData(dataKey, atlasKey));
return new Skeleton(this.getSkeletonData(dataKey, atlasKey));
}
createAtlas(atlasKey: string) {
getAtlas(atlasKey: string) {
let atlas: TextureAtlas;
if (this.atlasCache.exists(atlasKey)) {
atlas = this.atlasCache.get(atlasKey);
@ -210,8 +210,8 @@ export class SpinePlugin extends Phaser.Plugins.ScenePlugin {
return atlas;
}
createSkeletonData(dataKey: string, atlasKey: string) {
const atlas = this.createAtlas(atlasKey)
getSkeletonData(dataKey: string, atlasKey: string) {
const atlas = this.getAtlas(atlasKey)
const combinedKey = dataKey + atlasKey;
let skeletonData: SkeletonData;
if (this.skeletonDataCache.exists(combinedKey)) {

View File

@ -28,4 +28,10 @@ declare global {
spine (config: SpineGameObjectConfig, addToScene?: boolean): SpineGameObject;
}
}
namespace Phaser {
export interface Scene {
spine: SpinePlugin;
}
}
}