[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> <h1>Basic example</h1>
</body> </body>
<script> <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 = { const config = {
type: Phaser.AUTO, type: Phaser.AUTO,
width: 800, width: 800,
height: 600, height: 600,
type: Phaser.WEBGL, type: Phaser.WEBGL,
scene: { scene: [BasicScene],
preload: preload,
create: create,
},
plugins: { plugins: {
scene: [ scene: [
{ key: "spine.SpinePlugin", plugin: spine.SpinePlugin, mapping: "spine" } { key: "spine.SpinePlugin", plugin: spine.SpinePlugin, mapping: "spine" }
@ -31,22 +46,6 @@
}; };
const game = new Phaser.Game(config); 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> </script>
</html> </html>

View File

@ -112,7 +112,7 @@ export class SpinePlugin extends Phaser.Plugins.ScenePlugin {
return gameObject; 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 x = config.x ? config.x : 0;
let y = config.y ? config.y : 0; let y = config.y ? config.y : 0;
let boundsProvider = config.boundsProvider ? config.boundsProvider : undefined; let boundsProvider = config.boundsProvider ? config.boundsProvider : undefined;
@ -184,10 +184,10 @@ export class SpinePlugin extends Phaser.Plugins.ScenePlugin {
} }
createSkeleton (dataKey: string, atlasKey: string) { 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; let atlas: TextureAtlas;
if (this.atlasCache.exists(atlasKey)) { if (this.atlasCache.exists(atlasKey)) {
atlas = this.atlasCache.get(atlasKey); atlas = this.atlasCache.get(atlasKey);
@ -210,8 +210,8 @@ export class SpinePlugin extends Phaser.Plugins.ScenePlugin {
return atlas; return atlas;
} }
createSkeletonData(dataKey: string, atlasKey: string) { getSkeletonData(dataKey: string, atlasKey: string) {
const atlas = this.createAtlas(atlasKey) const atlas = this.getAtlas(atlasKey)
const combinedKey = dataKey + atlasKey; const combinedKey = dataKey + atlasKey;
let skeletonData: SkeletonData; let skeletonData: SkeletonData;
if (this.skeletonDataCache.exists(combinedKey)) { if (this.skeletonDataCache.exists(combinedKey)) {

View File

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