2014-09-15 16:53:20 -04:00
|
|
|
SpriteBuilder = require 'lib/sprites/SpriteBuilder'
|
|
|
|
|
|
|
|
module.exports = class WebGLSprite extends createjs.SpriteContainer
|
2014-09-17 18:47:25 -04:00
|
|
|
childMovieClips: null
|
2014-09-15 18:08:02 -04:00
|
|
|
|
2014-09-15 16:53:20 -04:00
|
|
|
constructor: (@spriteSheet, @thangType, @spriteSheetPrefix) ->
|
|
|
|
@initialize(@spriteSheet)
|
2014-09-16 18:36:59 -04:00
|
|
|
if @thangType.get('renderStrategy') isnt 'container'
|
|
|
|
@singleChildSprite = new createjs.Sprite(@spriteSheet)
|
|
|
|
@addChild(@singleChildSprite)
|
2014-09-17 18:47:25 -04:00
|
|
|
@addEventListener 'tick', @handleTick
|
|
|
|
|
|
|
|
handleTick: (e) =>
|
|
|
|
if @lastTimeStamp
|
|
|
|
@tick(e.timeStamp - @lastTimeStamp)
|
|
|
|
@lastTimeStamp = e.timeStamp
|
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
@handleTick = undefined
|
|
|
|
@removeAllEventListeners()
|
2014-09-16 18:36:59 -04:00
|
|
|
|
|
|
|
play: ->
|
|
|
|
@singleChildSprite?.play()
|
|
|
|
@paused = false
|
2014-09-15 16:53:20 -04:00
|
|
|
|
2014-09-16 18:36:59 -04:00
|
|
|
stop: ->
|
|
|
|
@singleChildSprite?.stop()
|
|
|
|
@paused = true
|
|
|
|
|
2014-09-15 16:53:20 -04:00
|
|
|
gotoAndPlay: (actionName) -> @goto(actionName, false)
|
|
|
|
gotoAndStop: (actionName) -> @goto(actionName, true)
|
|
|
|
|
|
|
|
goto: (actionName, @paused=true) ->
|
|
|
|
@currentAnimation = actionName
|
2014-09-17 18:47:25 -04:00
|
|
|
@baseMovieClip = @framerate = null
|
|
|
|
|
2014-09-15 16:53:20 -04:00
|
|
|
action = @thangType.getActions()[actionName]
|
2014-09-16 18:36:59 -04:00
|
|
|
randomStart = actionName.startsWith('move')
|
2014-09-17 18:47:25 -04:00
|
|
|
reg = action.positions?.registration or @thangType.get('positions')?.registration or {x:0, y:0}
|
|
|
|
|
2014-09-15 16:53:20 -04:00
|
|
|
if action.animation
|
|
|
|
@framerate = (action.framerate ? 20) * (action.speed ? 1)
|
2014-09-17 18:47:25 -04:00
|
|
|
|
2014-09-16 18:36:59 -04:00
|
|
|
if @singleChildSprite
|
|
|
|
scale = SPRITE_RESOLUTION_FACTOR * (action.scale ? @thangType.get('scale') ? 1)
|
|
|
|
@regX = -reg.x * scale
|
|
|
|
@regY = -reg.y * scale
|
|
|
|
func = if @paused then 'gotoAndStop' else 'gotoAndPlay'
|
|
|
|
animationName = @spriteSheetPrefix + actionName
|
|
|
|
@singleChildSprite[func](animationName)
|
|
|
|
@singleChildSprite.framerate = action.framerate or 20
|
|
|
|
|
|
|
|
if randomStart and frames = @spriteSheet.getAnimation(animationName)?.frames
|
2014-09-17 18:47:25 -04:00
|
|
|
@singleChildSprite.currentAnimationFrame = Math.floor(Math.random() * frames.length)
|
2014-09-16 18:36:59 -04:00
|
|
|
|
|
|
|
else
|
2014-09-17 18:47:25 -04:00
|
|
|
@regX = -reg.x
|
|
|
|
@regY = -reg.y
|
|
|
|
@childMovieClips = []
|
2014-09-16 18:36:59 -04:00
|
|
|
@baseMovieClip = @buildMovieClip(action.animation)
|
2014-09-17 18:47:25 -04:00
|
|
|
@children = @baseMovieClip.children
|
2014-09-16 18:36:59 -04:00
|
|
|
@frames = action.frames
|
2014-09-17 18:47:25 -04:00
|
|
|
@frames = (parseInt(f) for f in @frames.split(',')) if @frames
|
|
|
|
@animLength = if @frames then @frames.length else @baseMovieClip.frameBounds.length
|
|
|
|
@currentFrame = if randomStart then Math.floor(Math.random() * @animLength) else 0
|
|
|
|
@baseMovieClip.gotoAndStop(@currentFrame)
|
2014-09-16 18:36:59 -04:00
|
|
|
@loop = action.loops isnt false
|
|
|
|
@goesTo = action.goesTo
|
|
|
|
|
2014-09-17 18:47:25 -04:00
|
|
|
if action.container
|
|
|
|
if @singleChildSprite
|
|
|
|
scale = SPRITE_RESOLUTION_FACTOR * (action.scale ? @thangType.get('scale') ? 1)
|
|
|
|
@regX = -reg.x * scale
|
|
|
|
@regY = -reg.y * scale
|
|
|
|
animationName = @spriteSheetPrefix + actionName
|
|
|
|
@singleChildSprite.gotoAndStop(animationName)
|
2014-09-16 18:36:59 -04:00
|
|
|
|
2014-09-17 18:47:25 -04:00
|
|
|
else
|
|
|
|
@regX = -reg.x
|
|
|
|
@regY = -reg.y
|
|
|
|
@childMovieClips = []
|
|
|
|
containerName = @spriteSheetPrefix + action.container
|
|
|
|
sprite = new createjs.Sprite(@spriteSheet)
|
|
|
|
sprite.gotoAndStop(containerName)
|
|
|
|
@children = [sprite]
|
|
|
|
|
2014-09-15 16:53:20 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
buildMovieClip: (animationName, mode, startPosition, loops) ->
|
|
|
|
raw = @thangType.get('raw')
|
|
|
|
animData = raw.animations[animationName]
|
|
|
|
movieClip = new createjs.MovieClip()
|
|
|
|
|
|
|
|
locals = {}
|
|
|
|
_.extend locals, @buildMovieClipContainers(animData.containers)
|
|
|
|
_.extend locals, @buildMovieClipAnimations(animData.animations)
|
2014-09-16 18:36:59 -04:00
|
|
|
|
|
|
|
toSkip = {}
|
2014-09-17 18:47:25 -04:00
|
|
|
toSkip[shape.bn] = true for shape in animData.shapes
|
|
|
|
toSkip[graphic.bn] = true for graphic in animData.graphics
|
2014-09-15 16:53:20 -04:00
|
|
|
|
|
|
|
anim = new createjs.MovieClip()
|
|
|
|
anim.initialize(mode ? createjs.MovieClip.INDEPENDENT, startPosition ? 0, loops ? true)
|
|
|
|
|
|
|
|
for tweenData in animData.tweens
|
|
|
|
stopped = false
|
|
|
|
tween = createjs.Tween
|
|
|
|
for func in tweenData
|
|
|
|
args = $.extend(true, [], (func.a))
|
2014-09-16 18:36:59 -04:00
|
|
|
if @dereferenceArgs(args, locals, toSkip) is false
|
2014-09-17 18:47:25 -04:00
|
|
|
console.debug 'Did not dereference args:', args
|
2014-09-15 16:53:20 -04:00
|
|
|
stopped = true
|
|
|
|
break
|
|
|
|
tween = tween[func.n](args...)
|
|
|
|
continue if stopped
|
|
|
|
anim.timeline.addTween(tween)
|
|
|
|
|
|
|
|
anim.nominalBounds = new createjs.Rectangle(animData.bounds...)
|
|
|
|
if animData.frameBounds
|
|
|
|
anim.frameBounds = (new createjs.Rectangle(bounds...) for bounds in animData.frameBounds)
|
|
|
|
return anim
|
|
|
|
|
|
|
|
buildMovieClipContainers: (localContainers) ->
|
|
|
|
map = {}
|
|
|
|
for localContainer in localContainers
|
|
|
|
container = new createjs.Sprite(@spriteSheet)
|
|
|
|
container.gotoAndStop(@spriteSheetPrefix + localContainer.gn)
|
|
|
|
container.setTransform(localContainer.t...)
|
|
|
|
container._off = localContainer.o if localContainer.o?
|
|
|
|
container.alpha = localContainer.al if localContainer.al?
|
|
|
|
map[localContainer.bn] = container
|
|
|
|
return map
|
|
|
|
|
|
|
|
buildMovieClipAnimations: (localAnimations) ->
|
|
|
|
map = {}
|
|
|
|
for localAnimation in localAnimations
|
|
|
|
animation = @buildMovieClip(localAnimation.gn, localAnimation.a...)
|
|
|
|
animation.setTransform(localAnimation.t...)
|
|
|
|
map[localAnimation.bn] = animation
|
2014-09-17 18:47:25 -04:00
|
|
|
@childMovieClips.push(animation)
|
2014-09-15 16:53:20 -04:00
|
|
|
return map
|
|
|
|
|
2014-09-16 18:36:59 -04:00
|
|
|
dereferenceArgs: (args, locals, toSkip) ->
|
2014-09-15 16:53:20 -04:00
|
|
|
for key, val of args
|
|
|
|
if locals[val]
|
|
|
|
args[key] = locals[val]
|
|
|
|
else if val is null
|
|
|
|
args[key] = {}
|
|
|
|
else if _.isString(val) and val.indexOf('createjs.') is 0
|
|
|
|
args[key] = eval(val) # TODO: Security risk
|
|
|
|
else if _.isObject(val) or _.isArray(val)
|
2014-09-16 18:36:59 -04:00
|
|
|
res = @dereferenceArgs(val, locals, toSkip)
|
2014-09-15 16:53:20 -04:00
|
|
|
return res if res is false
|
2014-09-16 18:36:59 -04:00
|
|
|
else if _.isString(val) and toSkip[val]
|
2014-09-15 16:53:20 -04:00
|
|
|
return false
|
|
|
|
return args
|
|
|
|
|
|
|
|
tick: (delta) ->
|
2014-09-17 18:47:25 -04:00
|
|
|
return unless @baseMovieClip and @framerate and not @paused
|
2014-09-15 16:53:20 -04:00
|
|
|
newFrame = @currentFrame + @framerate * delta / 1000
|
|
|
|
|
|
|
|
if newFrame > @animLength
|
|
|
|
if @goesTo
|
|
|
|
@gotoAndPlay(@goesTo)
|
|
|
|
return
|
|
|
|
else if not @loop
|
2014-09-17 18:47:25 -04:00
|
|
|
@paused = true
|
2014-09-15 16:53:20 -04:00
|
|
|
newFrame = @animLength - 1
|
|
|
|
@dispatchEvent('animationend')
|
|
|
|
else
|
|
|
|
newFrame = newFrame % @animLength
|
|
|
|
|
|
|
|
if @frames
|
|
|
|
prevFrame = Math.floor(newFrame)
|
|
|
|
nextFrame = Math.ceil(newFrame)
|
|
|
|
if prevFrame is nextFrame
|
|
|
|
@baseMovieClip.gotoAndStop(@frames[newFrame])
|
|
|
|
else if nextFrame is @frames.length
|
|
|
|
@baseMovieClip.gotoAndStop(@frames[prevFrame])
|
|
|
|
else
|
|
|
|
# interpolate between frames
|
|
|
|
pct = newFrame % 1
|
|
|
|
newFrameIndex = @frames[prevFrame] + (pct * (@frames[nextFrame] - @frames[prevFrame]))
|
|
|
|
@baseMovieClip.gotoAndStop(newFrameIndex)
|
|
|
|
else
|
|
|
|
@baseMovieClip.gotoAndStop(newFrame)
|
|
|
|
|
|
|
|
@currentFrame = newFrame
|
|
|
|
|
2014-09-17 18:47:25 -04:00
|
|
|
# So, originally I thought I'd have to swap in MovieClips for parallel
|
|
|
|
# SpriteContainers between each frame, but turns out that's not the case.
|
|
|
|
# The WebGL rendering system treats the MovieClip like a SpriteContainer,
|
|
|
|
# which makes things simpler for me...
|
|
|
|
|
|
|
|
# For some reason, though, gotoAndStop doesn't seem to advance the children
|
|
|
|
# so I gotta do that manually.
|
|
|
|
movieClip.gotoAndStop(newFrame) for movieClip in @childMovieClips
|
|
|
|
|
2014-09-15 16:53:20 -04:00
|
|
|
getBounds: ->
|
|
|
|
@baseMovieClip.getBounds()
|