Added MovieClip pooling, improving WebGL performance.
This commit is contained in:
parent
14f3fb6486
commit
8aeb6555ec
1 changed files with 17 additions and 1 deletions
|
@ -4,11 +4,13 @@ module.exports = class SegmentedSprite extends createjs.SpriteContainer
|
||||||
childMovieClips: null
|
childMovieClips: null
|
||||||
|
|
||||||
constructor: (@spriteSheet, @thangType, @spriteSheetPrefix, @resolutionFactor=SPRITE_RESOLUTION_FACTOR) ->
|
constructor: (@spriteSheet, @thangType, @spriteSheetPrefix, @resolutionFactor=SPRITE_RESOLUTION_FACTOR) ->
|
||||||
|
@spriteSheet.mcPool ?= {}
|
||||||
@initialize(@spriteSheet)
|
@initialize(@spriteSheet)
|
||||||
@addEventListener 'tick', @handleTick
|
@addEventListener 'tick', @handleTick
|
||||||
|
|
||||||
destroy: ->
|
destroy: ->
|
||||||
@handleTick = undefined
|
@handleTick = undefined
|
||||||
|
@baseMovieClip.inUse = false if @baseMovieClip
|
||||||
@removeAllEventListeners()
|
@removeAllEventListeners()
|
||||||
|
|
||||||
# CreateJS.Sprite-like interface
|
# CreateJS.Sprite-like interface
|
||||||
|
@ -21,7 +23,10 @@ module.exports = class SegmentedSprite extends createjs.SpriteContainer
|
||||||
goto: (actionName, @paused=true) ->
|
goto: (actionName, @paused=true) ->
|
||||||
@removeAllChildren()
|
@removeAllChildren()
|
||||||
@currentAnimation = actionName
|
@currentAnimation = actionName
|
||||||
@baseMovieClip = @framerate = @animLength = null
|
@baseMovieClip.inUse = false if @baseMovieClip
|
||||||
|
if @childMovieClips
|
||||||
|
mc.inUse = false for mc in @childMovieClips
|
||||||
|
@childMovieClips = @baseMovieClip = @framerate = @animLength = null
|
||||||
@actionNotSupported = false
|
@actionNotSupported = false
|
||||||
|
|
||||||
action = @thangType.getActions()[actionName]
|
action = @thangType.getActions()[actionName]
|
||||||
|
@ -37,6 +42,7 @@ module.exports = class SegmentedSprite extends createjs.SpriteContainer
|
||||||
@framerate = (action.framerate ? 20) * (action.speed ? 1)
|
@framerate = (action.framerate ? 20) * (action.speed ? 1)
|
||||||
@childMovieClips = []
|
@childMovieClips = []
|
||||||
@baseMovieClip = @buildMovieClip(action.animation)
|
@baseMovieClip = @buildMovieClip(action.animation)
|
||||||
|
@baseMovieClip.inUse = true
|
||||||
@frames = action.frames
|
@frames = action.frames
|
||||||
@frames = (parseInt(f) for f in @frames.split(',')) if @frames
|
@frames = (parseInt(f) for f in @frames.split(',')) if @frames
|
||||||
@animLength = if @frames then @frames.length else @baseMovieClip.timeline.duration
|
@animLength = if @frames then @frames.length else @baseMovieClip.timeline.duration
|
||||||
|
@ -87,6 +93,13 @@ module.exports = class SegmentedSprite extends createjs.SpriteContainer
|
||||||
@sprite?.trigger('action-needs-render', @sprite, action)
|
@sprite?.trigger('action-needs-render', @sprite, action)
|
||||||
|
|
||||||
buildMovieClip: (animationName, mode, startPosition, loops) ->
|
buildMovieClip: (animationName, mode, startPosition, loops) ->
|
||||||
|
key = JSON.stringify([@spriteSheetPrefix].concat(arguments))
|
||||||
|
@spriteSheet.mcPool[key] ?= []
|
||||||
|
for mc in @spriteSheet.mcPool[key]
|
||||||
|
if not mc.inUse
|
||||||
|
mc.gotoAndStop(mc.currentFrame+0.01) # just to make sure it has its children back
|
||||||
|
return mc
|
||||||
|
|
||||||
raw = @thangType.get('raw')
|
raw = @thangType.get('raw')
|
||||||
animData = raw.animations[animationName]
|
animData = raw.animations[animationName]
|
||||||
@lastAnimData = animData
|
@lastAnimData = animData
|
||||||
|
@ -119,6 +132,8 @@ module.exports = class SegmentedSprite extends createjs.SpriteContainer
|
||||||
anim.nominalBounds = new createjs.Rectangle(animData.bounds...)
|
anim.nominalBounds = new createjs.Rectangle(animData.bounds...)
|
||||||
if animData.frameBounds
|
if animData.frameBounds
|
||||||
anim.frameBounds = (new createjs.Rectangle(bounds...) for bounds in animData.frameBounds)
|
anim.frameBounds = (new createjs.Rectangle(bounds...) for bounds in animData.frameBounds)
|
||||||
|
|
||||||
|
@spriteSheet.mcPool[key].push(anim)
|
||||||
return anim
|
return anim
|
||||||
|
|
||||||
buildMovieClipContainers: (localContainers) ->
|
buildMovieClipContainers: (localContainers) ->
|
||||||
|
@ -155,6 +170,7 @@ module.exports = class SegmentedSprite extends createjs.SpriteContainer
|
||||||
map = {}
|
map = {}
|
||||||
for localAnimation in localAnimations
|
for localAnimation in localAnimations
|
||||||
animation = @buildMovieClip(localAnimation.gn, localAnimation.a...)
|
animation = @buildMovieClip(localAnimation.gn, localAnimation.a...)
|
||||||
|
animation.inUse = true
|
||||||
animation.setTransform(localAnimation.t...)
|
animation.setTransform(localAnimation.t...)
|
||||||
map[localAnimation.bn] = animation
|
map[localAnimation.bn] = animation
|
||||||
@childMovieClips.push(animation)
|
@childMovieClips.push(animation)
|
||||||
|
|
Reference in a new issue