import Mesh from "../webgl/mesh" ; import Renderer from "../webgl/renderer" ; import SpineAnimation from "./spine-animation" ; import SpineAtlas from "./spine-atlas" ; import SpineBone from "./spine-bone" ; import SpineData , { AnimationJson } from "./spine-data" ; import SpineDebugMesh from "./spine-debug-mesh" ; import SpineMesh from "./spine-mesh" ; export default class Spine { protected data : SpineData ; protected bones : Array < SpineBone > = [ ] ; protected sortedBones : Array < SpineBone > = [ ] ; protected bonesDict : { [ k ] : SpineBone ; } = { } ; protected meshes : Array < Mesh > = [ ] ; protected spineAnimation : SpineAnimation = null ; public x : number = 0 ; public y : number = 0 ; public constructor ( data ) { this . data = data ; this . createBones ( ) ; this . setupBones ( ) ; } protected createBones ( ) { for ( let b of this . data . json . bones ) { let bone = new SpineBone ( ) ; bone . setJson ( b ) ; this . bones . push ( bone ) ; this . sortedBones . push ( bone ) ; this . bonesDict [ bone . name ] = bone ; } let boneDepthDict = { } ; for ( let bone of this . bones ) { this . calBoneDepth ( bone , boneDepthDict ) ; } this . sortedBones . sort ( function ( a , b ) { return boneDepthDict [ a . name ] - boneDepthDict [ b . name ] ; } ) ; } protected calBoneDepth ( bone , dict ) { if ( dict [ bone . name ] != null ) { return dict [ bone . name ] ; } if ( bone . parent == "" ) { dict [ bone . name ] = 0 ; } else { let parentBone = this . bonesDict [ bone . parent ] ; let parentDepth = this . calBoneDepth ( parentBone , dict ) ; dict [ bone . name ] = parentDepth + 1 ; } return dict [ bone . name ] ; } public getSortedBones ( ) { return this . sortedBones ; } public getBone ( name ) { return this . bonesDict [ name ] ; } public getBoneAt ( index ) { return this . bones [ index ] ; } public createMesh ( renderer , atlas ) { let mesh = new SpineMesh ( renderer ) ; mesh . setSpine ( this ) ; mesh . createFromAtlas ( atlas ) ; this . meshes . push ( mesh ) ; return mesh ; } public createDebugMesh ( renderer ) { let mesh = new SpineDebugMesh ( renderer ) ; mesh . setSpine ( this ) ; this . meshes . push ( mesh ) ; return mesh ; } public getData ( ) { return this . data ; } public setAnimation ( animationName ) { let animationJson = this . data . getAnimationData ( animationName ) ; if ( animationJson != null ) { this . spineAnimation = new SpineAnimation ( this , animationJson ) ; } } protected setupBones ( ) { this . updateBonesTransform ( ) ; } public getAnimation ( ) { return this . spineAnimation ; } public update ( ) { if ( this . spineAnimation == null ) { return ; } this . spineAnimation . update ( ) ; this . updateBonesTransform ( ) ; } public updateBonesTransform ( ) { for ( let bone of this . sortedBones ) { let parent = this . bonesDict [ bone . parent ] ; bone . updateTransform ( parent ) ; } } public draw ( renderer ) { for ( let mesh of this . meshes ) { mesh . draw ( ) ; } } }