Maintain state; add getBodyShape

This commit is contained in:
Ketchetwahmeegwun T. Southall 2022-01-29 16:31:15 -08:00
parent 5dc9fb2d40
commit b3ef7a86f0

View File

@ -1,5 +1,5 @@
import assets from '../../../Assets/Sprites/**/*.yaml' import assets from '../../../Assets/Sprites/**/*.yaml'
import { Sprite } from '../data/sprite' import { Shape, Sprite, SpritePart } from '../data/sprite'
import * as PIXI from 'pixi.js' import * as PIXI from 'pixi.js'
export const sprites: Record<string, Sprite> = {} export const sprites: Record<string, Sprite> = {}
@ -10,26 +10,45 @@ for (let [key, value] of Object.entries(assets)) {
export class SpriteInstance { export class SpriteInstance {
container: PIXI.Container container: PIXI.Container
sprite: Sprite
animation?: SpritePart
animationKey: string
set?: SpritePart
setKey: string
subset?: SpritePart
subsetKey: string
frame?: SpritePart
frameIndex: number
constructor(ctor: string) { constructor(ctor: string) {
this.container = new PIXI.Container() this.container = new PIXI.Container()
let [spriteKey, animationKey, setKey, subsetKey, frameKey] = ctor.split('.') let [spriteKey, animationKey, setKey, subsetKey, frameKey] = ctor.split('.')
let frameIndex = Number(frameKey) this.animationKey = animationKey
let sprite = sprites[spriteKey] this.setKey = setKey
if (!sprite) { this.subsetKey = subsetKey
this.frameIndex = Number(frameKey)
this.sprite = sprites[spriteKey]
if (!this.sprite) {
this.container.addChild(new PIXI.Text('missing sprite')) this.container.addChild(new PIXI.Text('missing sprite'))
return return
} }
let animation = sprite.root.children[animationKey] this.animation = this.sprite.root.children[animationKey]
let set = animation.children[setKey] this.set = this.animation.children[setKey]
let subset = set.children[subsetKey] this.subset = this.set.children[subsetKey]
// //
if (subset.frames) { if (this.subset.frames) {
let frame = subset.frames[frameIndex] this.frame = this.subset.frames[this.frameIndex]
let s = new PIXI.Sprite(frame.texture) let s = new PIXI.Sprite(this.frame.texture)
s.x -= frame.originX s.x -= this.frame.originX
s.y -= frame.originY s.y -= this.frame.originY
this.container.addChild(s) this.container.addChild(s)
} }
} }
getBodyShape(): Shape|undefined {
if (this.frame && this.frame.shapes) {
return this.frame.shapes.find(v=>v.key==='body')
}
return
}
} }