Skip to content

Commit

Permalink
[phaser] Expose getters for preloaded skeleton data and atlases.
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Apr 25, 2023
1 parent f883760 commit 5662c75
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
39 changes: 19 additions & 20 deletions spine-ts/spine-phaser/example/basic-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -31,22 +46,6 @@ <h1>Basic example</h1>
};

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>
10 changes: 5 additions & 5 deletions spine-ts/spine-phaser/src/SpinePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down
6 changes: 6 additions & 0 deletions spine-ts/spine-phaser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ declare global {
spine (config: SpineGameObjectConfig, addToScene?: boolean): SpineGameObject;
}
}

namespace Phaser {
export interface Scene {
spine: SpinePlugin;
}
}
}

0 comments on commit 5662c75

Please sign in to comment.