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