2014-09-19 18:46:02 -04:00
|
|
|
###
|
|
|
|
* SpriteStage (WebGL Canvas)
|
|
|
|
** Land texture
|
|
|
|
** Ground-based selection/target marks, range radii
|
|
|
|
** Walls/obstacles
|
|
|
|
** Paths and target pieces (and ghosts?)
|
|
|
|
** Normal Thangs, bots, wizards (z-indexing based on World-determined sprite.thang.pos.z/y, mainly, instead of sprite-map-determined sprite.z, which we rename to... something)
|
|
|
|
** Above-thang marks (blood, highlight) and health bars
|
|
|
|
|
|
|
|
* Stage (Regular Canvas)
|
|
|
|
** Camera border
|
|
|
|
** surfaceTextLayer (speech, names)
|
|
|
|
** screenLayer
|
|
|
|
*** Letterbox
|
|
|
|
**** Letterbox top and bottom
|
|
|
|
*** FPS display, maybe grid axis labels, coordinate hover
|
|
|
|
|
|
|
|
** Grid lines--somewhere--we will figure it out, do not really need it at first
|
|
|
|
###
|
|
|
|
|
2014-09-12 19:33:01 -04:00
|
|
|
SpriteBuilder = require 'lib/sprites/SpriteBuilder'
|
2014-09-16 18:36:59 -04:00
|
|
|
CocoClass = require 'lib/CocoClass'
|
2014-09-19 17:56:40 -04:00
|
|
|
SegmentedSprite = require './SegmentedSprite'
|
|
|
|
SingularSprite = require './SingularSprite'
|
2014-09-16 18:36:59 -04:00
|
|
|
|
2014-09-19 17:56:40 -04:00
|
|
|
NEVER_RENDER_ANYTHING = false # set to true to test placeholders
|
2014-09-19 16:50:14 -04:00
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
module.exports = LayerAdapter = class LayerAdapter extends CocoClass
|
|
|
|
|
|
|
|
# Intermediary between a Surface Stage and a top-level static normal Container or hot-swapped WebGL SpriteContainer.
|
|
|
|
# It handles zooming in different ways and, if webGL, creating and assigning spriteSheets.
|
2014-09-16 18:36:59 -04:00
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
@TRANSFORM_CHILD: 'child' # Layer transform is managed by its parents
|
|
|
|
@TRANSFORM_SURFACE: 'surface' # Layer moves/scales/zooms with the Surface of the World
|
|
|
|
@TRANSFORM_SURFACE_TEXT: 'surface_text' # Layer moves with the Surface but is size-independent
|
|
|
|
@TRANSFORM_SCREEN: 'screen' # Layer stays fixed to the screen (different from child?)
|
2014-09-12 19:33:01 -04:00
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
# WebGL properties
|
2014-09-12 19:33:01 -04:00
|
|
|
actionRenderState: null
|
|
|
|
needToRerender: false
|
|
|
|
toRenderBundles: null
|
|
|
|
willRender: false
|
2014-09-16 18:36:59 -04:00
|
|
|
buildAutomatically: true
|
|
|
|
buildAsync: true
|
2014-09-12 19:33:01 -04:00
|
|
|
resolutionFactor: SPRITE_RESOLUTION_FACTOR
|
|
|
|
defaultActions: ['idle', 'die', 'move', 'move_back', 'move_side', 'move_fore', 'attack']
|
2014-09-16 18:36:59 -04:00
|
|
|
numThingsLoading: 0
|
|
|
|
cocoSprites: null
|
|
|
|
spriteSheet: null
|
2014-09-19 18:46:02 -04:00
|
|
|
container: null
|
2014-09-24 15:08:55 -04:00
|
|
|
customGraphics: null
|
2014-09-19 18:46:02 -04:00
|
|
|
|
|
|
|
subscriptions:
|
|
|
|
'camera:zoom-updated': 'onZoomUpdated'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
2014-09-17 18:47:25 -04:00
|
|
|
super()
|
2014-09-19 18:46:02 -04:00
|
|
|
options ?= {}
|
|
|
|
@name = options.name ? 'Unnamed'
|
2014-09-24 15:08:55 -04:00
|
|
|
@customGraphics = {}
|
2014-09-19 18:46:02 -04:00
|
|
|
@layerPriority = options.layerPriority ? 0
|
|
|
|
@transformStyle = options.transform ? LayerAdapter.TRANSFORM_CHILD
|
|
|
|
@camera = options.camera
|
|
|
|
@updateLayerOrder = _.throttle @updateLayerOrder, 1000 / 30 # Don't call multiple times in one frame; 30 FPS is probably good enough
|
|
|
|
|
|
|
|
@webGL = !!options.webGL
|
|
|
|
if @webGL
|
|
|
|
@initializing = true
|
|
|
|
@spriteSheet = @_renderNewSpriteSheet(false) # builds an empty spritesheet
|
|
|
|
@container = new createjs.SpriteContainer(@spriteSheet)
|
|
|
|
@actionRenderState = {}
|
|
|
|
@toRenderBundles = []
|
|
|
|
@cocoSprites = []
|
|
|
|
@initializing = false
|
|
|
|
|
|
|
|
else
|
|
|
|
@container = new createjs.Container()
|
|
|
|
|
|
|
|
toString: -> "<Layer #{@layerPriority}: #{@name}>"
|
|
|
|
|
|
|
|
#- Layer ordering
|
2014-09-12 19:33:01 -04:00
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
updateLayerOrder: ->
|
|
|
|
@container.sortChildren @layerOrderComparator
|
|
|
|
|
|
|
|
layerOrderComparator: (a, b) ->
|
|
|
|
# Optimize
|
|
|
|
alp = a.layerPriority or 0
|
|
|
|
blp = b.layerPriority or 0
|
|
|
|
return alp - blp if alp isnt blp
|
|
|
|
# TODO: remove this z stuff
|
|
|
|
az = a.z or 1000
|
|
|
|
bz = b.z or 1000
|
|
|
|
if aSprite = a.sprite
|
|
|
|
if aThang = aSprite.thang
|
|
|
|
aPos = aThang.pos
|
|
|
|
if aThang.health < 0
|
|
|
|
--az
|
|
|
|
if bSprite = b.sprite
|
|
|
|
if bThang = bSprite.thang
|
|
|
|
bPos = bThang.pos
|
|
|
|
if bThang.health < 0
|
|
|
|
--bz
|
|
|
|
if az is bz
|
|
|
|
return 0 unless aPos and bPos
|
|
|
|
return (bPos.y - aPos.y) or (bPos.x - aPos.x)
|
|
|
|
return az - bz
|
2014-09-12 19:33:01 -04:00
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
#- Zoom updating
|
|
|
|
|
|
|
|
onZoomUpdated: (e) ->
|
|
|
|
return unless e.camera is @camera
|
|
|
|
if @transformStyle in [LayerAdapter.TRANSFORM_SURFACE, LayerAdapter.TRANSFORM_SURFACE_TEXT, LayerAdapter.TRANSFORM_CHILD]
|
|
|
|
change = @container.scaleX / e.zoom
|
|
|
|
@container.scaleX = @container.scaleY = e.zoom
|
|
|
|
@container.regX = e.surfaceViewport.x
|
|
|
|
@container.regY = e.surfaceViewport.y
|
|
|
|
if @transformStyle is LayerAdapter.TRANSFORM_SURFACE_TEXT
|
|
|
|
for child in @container.children
|
|
|
|
child.scaleX *= change
|
|
|
|
child.scaleY *= change
|
|
|
|
|
|
|
|
#- Caching
|
|
|
|
|
|
|
|
cache: ->
|
|
|
|
return if @webGL
|
|
|
|
return unless @children.length
|
|
|
|
bounds = @container.getBounds()
|
|
|
|
return unless bounds
|
|
|
|
@container.cache bounds.x, bounds.y, bounds.width, bounds.height, 2
|
|
|
|
|
|
|
|
#- Container-like child functions
|
|
|
|
|
|
|
|
addChild: (children...) ->
|
2014-09-24 15:08:55 -04:00
|
|
|
@container.addChild children...
|
|
|
|
if @transformStyle is LayerAdapter.TRANSFORM_SURFACE_TEXT
|
2014-09-19 18:46:02 -04:00
|
|
|
for child in children
|
|
|
|
child.scaleX /= @scaleX
|
|
|
|
child.scaleY /= @scaleY
|
|
|
|
|
|
|
|
removeChild: (children...) ->
|
2014-09-24 15:08:55 -04:00
|
|
|
@container.removeChild children...
|
2014-09-19 18:46:02 -04:00
|
|
|
# TODO: Do we actually need to scale children that were removed?
|
2014-09-24 15:08:55 -04:00
|
|
|
if @transformStyle is LayerAdapter.TRANSFORM_SURFACE_TEXT
|
2014-09-19 18:46:02 -04:00
|
|
|
for child in children
|
|
|
|
child.scaleX *= @scaleX
|
|
|
|
child.scaleY *= @scaleY
|
|
|
|
|
|
|
|
#- Adding, removing children for WebGL layers.
|
|
|
|
|
2014-09-12 19:33:01 -04:00
|
|
|
addCocoSprite: (cocoSprite) ->
|
2014-09-17 19:49:31 -04:00
|
|
|
cocoSprite.options.resolutionFactor = @resolutionFactor
|
2014-09-18 13:03:13 -04:00
|
|
|
if cocoSprite.layer
|
|
|
|
console.warn 'CocoSprite being re-added to a layer?'
|
|
|
|
|
|
|
|
cocoSprite.layer = @
|
2014-09-18 15:19:52 -04:00
|
|
|
@listenTo(cocoSprite, 'action-needs-render', @onActionNeedsRender)
|
2014-09-16 18:36:59 -04:00
|
|
|
@cocoSprites.push cocoSprite
|
|
|
|
@loadThangType(cocoSprite.thangType)
|
|
|
|
@addDefaultActionsToRender(cocoSprite)
|
|
|
|
@setImageObjectToCocoSprite(cocoSprite)
|
2014-09-19 18:46:02 -04:00
|
|
|
@updateLayerOrder()
|
2014-09-24 19:53:38 -04:00
|
|
|
cocoSprite.addHealthBar()
|
2014-09-19 18:46:02 -04:00
|
|
|
|
2014-09-18 13:03:13 -04:00
|
|
|
removeCocoSprite: (cocoSprite) ->
|
2014-09-18 15:19:52 -04:00
|
|
|
@stopListening(cocoSprite)
|
2014-09-18 13:03:13 -04:00
|
|
|
cocoSprite.imageObject.parent.removeChild cocoSprite.imageObject
|
|
|
|
@cocoSprites = _.without @cocoSprites, cocoSprite
|
2014-09-16 18:36:59 -04:00
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
#- Loading network resources dynamically
|
|
|
|
|
2014-09-16 18:36:59 -04:00
|
|
|
loadThangType: (thangType) ->
|
|
|
|
if not thangType.isFullyLoaded()
|
|
|
|
thangType.setProjection null
|
|
|
|
thangType.fetch() unless thangType.loading
|
|
|
|
@numThingsLoading++
|
|
|
|
@listenToOnce(thangType, 'sync', @somethingLoaded)
|
|
|
|
else if thangType.get('raster') and not thangType.loadedRaster
|
|
|
|
thangType.loadRasterImage()
|
|
|
|
@listenToOnce(thangType, 'raster-image-loaded', @somethingLoaded)
|
|
|
|
@numThingsLoading++
|
|
|
|
|
|
|
|
somethingLoaded: (thangType) ->
|
|
|
|
@numThingsLoading--
|
|
|
|
@loadThangType(thangType) # might need to load the raster image object
|
|
|
|
for cocoSprite in @cocoSprites
|
|
|
|
if cocoSprite.thangType is thangType
|
|
|
|
@addDefaultActionsToRender(cocoSprite)
|
|
|
|
@renderNewSpriteSheet()
|
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
#- Adding to the list of things we need to render
|
|
|
|
|
|
|
|
onActionNeedsRender: (cocoSprite, action) ->
|
|
|
|
@upsertActionToRender(cocoSprite.thangType, action.name, cocoSprite.options.colorConfig)
|
|
|
|
|
2014-09-16 18:36:59 -04:00
|
|
|
addDefaultActionsToRender: (cocoSprite) ->
|
2014-09-17 18:47:25 -04:00
|
|
|
needToRender = false
|
2014-09-12 19:33:01 -04:00
|
|
|
if cocoSprite.thangType.get('raster')
|
|
|
|
@upsertActionToRender(cocoSprite.thangType)
|
|
|
|
else
|
|
|
|
for action in _.values(cocoSprite.thangType.getActions())
|
|
|
|
continue unless action.name in @defaultActions
|
|
|
|
@upsertActionToRender(cocoSprite.thangType, action.name, cocoSprite.options.colorConfig)
|
2014-09-16 18:36:59 -04:00
|
|
|
|
2014-09-12 19:33:01 -04:00
|
|
|
upsertActionToRender: (thangType, actionName, colorConfig) ->
|
|
|
|
groupKey = @renderGroupingKey(thangType, actionName, colorConfig)
|
2014-09-17 18:47:25 -04:00
|
|
|
return false if @actionRenderState[groupKey] isnt undefined
|
2014-09-12 19:33:01 -04:00
|
|
|
@actionRenderState[groupKey] = 'need-to-render'
|
|
|
|
@toRenderBundles.push({thangType: thangType, actionName: actionName, colorConfig: colorConfig})
|
2014-09-17 18:47:25 -04:00
|
|
|
return true if @willRender or not @buildAutomatically
|
2014-09-16 18:36:59 -04:00
|
|
|
@willRender = _.defer => @renderNewSpriteSheet()
|
2014-09-17 18:47:25 -04:00
|
|
|
return true
|
2014-09-24 13:55:33 -04:00
|
|
|
|
2014-09-24 15:08:55 -04:00
|
|
|
addCustomGraphic: (key, graphic, bounds) ->
|
|
|
|
return false if @customGraphics[key]
|
|
|
|
@customGraphics[key] = { graphic: graphic, bounds: new createjs.Rectangle(bounds...) }
|
|
|
|
return true if @willRender or not @buildAutomatically
|
|
|
|
@_renderNewSpriteSheet(false)
|
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
#- Rendering sprite sheets
|
|
|
|
|
2014-09-12 19:33:01 -04:00
|
|
|
renderNewSpriteSheet: ->
|
|
|
|
@willRender = false
|
2014-09-16 18:36:59 -04:00
|
|
|
return if @numThingsLoading
|
|
|
|
@_renderNewSpriteSheet()
|
|
|
|
|
|
|
|
_renderNewSpriteSheet: (async) ->
|
|
|
|
async ?= @buildAsync
|
2014-09-12 19:33:01 -04:00
|
|
|
builder = new createjs.SpriteSheetBuilder()
|
|
|
|
groups = _.groupBy(@toRenderBundles, ((bundle) -> @renderGroupingKey(bundle.thangType, '', bundle.colorConfig)), @)
|
2014-09-18 14:56:49 -04:00
|
|
|
|
2014-09-19 16:50:14 -04:00
|
|
|
# The first frame is always the 'loading', ie placeholder, image.
|
|
|
|
placeholder = @createPlaceholder()
|
2014-09-24 13:55:33 -04:00
|
|
|
dimension = @resolutionFactor * SPRITE_PLACEHOLDER_WIDTH
|
2014-09-19 16:50:14 -04:00
|
|
|
placeholder.setBounds(0, 0, dimension, dimension)
|
|
|
|
builder.addFrame(placeholder)
|
2014-09-24 13:55:33 -04:00
|
|
|
|
2014-09-24 15:08:55 -04:00
|
|
|
# Add custom graphics
|
|
|
|
for key, graphic of @customGraphics
|
|
|
|
frame = builder.addFrame(graphic.graphic, graphic.bounds, @resolutionFactor)
|
|
|
|
builder.addAnimation(key, [frame], false)
|
|
|
|
|
|
|
|
# Render ThangTypes
|
2014-09-19 16:50:14 -04:00
|
|
|
groups = {} if NEVER_RENDER_ANYTHING
|
2014-09-12 19:33:01 -04:00
|
|
|
for bundleGrouping in _.values(groups)
|
|
|
|
thangType = bundleGrouping[0].thangType
|
|
|
|
colorConfig = bundleGrouping[0].colorConfig
|
|
|
|
actionNames = (bundle.actionName for bundle in bundleGrouping)
|
|
|
|
args = [thangType, colorConfig, actionNames, builder]
|
|
|
|
if thangType.get('raw')
|
2014-09-19 17:56:40 -04:00
|
|
|
if thangType.get('spriteType') is 'segmented'
|
2014-09-19 18:46:02 -04:00
|
|
|
@renderSegmentedThangType(args...)
|
2014-09-12 19:33:01 -04:00
|
|
|
else
|
2014-09-19 18:46:02 -04:00
|
|
|
@renderSingularThangType(args...)
|
2014-09-12 19:33:01 -04:00
|
|
|
else
|
2014-09-19 18:46:02 -04:00
|
|
|
@renderRasterThangType(thangType, builder)
|
2014-09-16 18:36:59 -04:00
|
|
|
|
|
|
|
if async
|
|
|
|
builder.buildAsync()
|
|
|
|
builder.on 'complete', @onBuildSpriteSheetComplete, @, true, builder
|
|
|
|
else
|
2014-09-17 18:47:25 -04:00
|
|
|
sheet = builder.build()
|
2014-09-24 15:08:55 -04:00
|
|
|
@onBuildSpriteSheetComplete({async:async}, builder)
|
2014-09-17 18:47:25 -04:00
|
|
|
return sheet
|
2014-09-19 16:50:14 -04:00
|
|
|
|
2014-09-16 18:36:59 -04:00
|
|
|
onBuildSpriteSheetComplete: (e, builder) ->
|
2014-09-17 18:47:25 -04:00
|
|
|
return if @initializing
|
2014-09-17 20:08:24 -04:00
|
|
|
|
|
|
|
if builder.spriteSheet._images.length > 1
|
|
|
|
@resolutionFactor *= 0.9
|
|
|
|
console.debug('Sprite sheet is too large... re-rendering at', @resolutionFactor.toFixed(2))
|
2014-09-24 15:08:55 -04:00
|
|
|
@_renderNewSpriteSheet(e.async)
|
2014-09-17 20:08:24 -04:00
|
|
|
return
|
|
|
|
|
2014-09-17 18:47:25 -04:00
|
|
|
@spriteSheet = builder.spriteSheet
|
2014-09-18 17:36:05 -04:00
|
|
|
@spriteSheet.resolutionFactor = @resolutionFactor
|
2014-09-19 18:46:02 -04:00
|
|
|
oldLayer = @container
|
|
|
|
@container = new createjs.SpriteContainer(@spriteSheet)
|
2014-09-17 18:47:25 -04:00
|
|
|
for cocoSprite in @cocoSprites
|
|
|
|
@setImageObjectToCocoSprite(cocoSprite)
|
|
|
|
for prop in ['scaleX', 'scaleY', 'regX', 'regY']
|
2014-09-19 18:46:02 -04:00
|
|
|
@container[prop] = oldLayer[prop]
|
2014-09-17 18:47:25 -04:00
|
|
|
if parent = oldLayer.parent
|
|
|
|
index = parent.getChildIndex(oldLayer)
|
|
|
|
parent.removeChildAt(index)
|
2014-09-19 18:46:02 -04:00
|
|
|
parent.addChildAt(@container, index)
|
|
|
|
@camera?.updateZoom(true)
|
|
|
|
@updateLayerOrder()
|
2014-09-17 20:08:24 -04:00
|
|
|
for cocoSprite in @cocoSprites
|
|
|
|
cocoSprite.options.resolutionFactor = @resolutionFactor
|
|
|
|
cocoSprite.updateScale()
|
2014-09-18 12:03:33 -04:00
|
|
|
cocoSprite.updateRotation()
|
2014-09-17 18:47:25 -04:00
|
|
|
@trigger 'new-spritesheet'
|
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
#- Placeholder
|
|
|
|
|
|
|
|
createPlaceholder: ->
|
|
|
|
# TODO: Experiment with this. Perhaps have rectangles if default layer is obstacle or floor,
|
|
|
|
# and different colors for different layers.
|
|
|
|
g = new createjs.Graphics()
|
|
|
|
g.setStrokeStyle(5)
|
|
|
|
g.beginStroke(createjs.Graphics.getRGB(64,64,64))
|
|
|
|
g.beginFill(createjs.Graphics.getRGB(64,64,64,0.7))
|
2014-09-24 13:55:33 -04:00
|
|
|
radius = @resolutionFactor * SPRITE_PLACEHOLDER_WIDTH / 2
|
2014-09-19 18:46:02 -04:00
|
|
|
g.drawCircle(radius, radius, radius)
|
|
|
|
new createjs.Shape(g)
|
|
|
|
|
|
|
|
#- Rendering containers for segmented thang types
|
|
|
|
|
|
|
|
renderSegmentedThangType: (thangType, colorConfig, actionNames, spriteSheetBuilder) ->
|
2014-09-12 19:33:01 -04:00
|
|
|
containersToRender = {}
|
|
|
|
for actionName in actionNames
|
|
|
|
action = _.find(thangType.getActions(), {name: actionName})
|
|
|
|
if action.container
|
|
|
|
containersToRender[action.container] = true
|
|
|
|
else if action.animation
|
2014-09-15 18:08:02 -04:00
|
|
|
animationContainers = @getContainersForAnimation(thangType, action.animation)
|
2014-09-12 19:33:01 -04:00
|
|
|
containersToRender[container.gn] = true for container in animationContainers
|
|
|
|
|
|
|
|
spriteBuilder = new SpriteBuilder(thangType, {colorConfig: colorConfig})
|
|
|
|
for containerGlobalName in _.keys(containersToRender)
|
|
|
|
containerKey = @renderGroupingKey(thangType, containerGlobalName, colorConfig)
|
2014-09-18 17:36:05 -04:00
|
|
|
if @spriteSheet?.resolutionFactor is @resolutionFactor and containerKey in @spriteSheet.getAnimations()
|
|
|
|
container = new createjs.Sprite(@spriteSheet)
|
|
|
|
container.gotoAndStop(containerKey)
|
|
|
|
frame = spriteSheetBuilder.addFrame(container)
|
|
|
|
else
|
|
|
|
container = spriteBuilder.buildContainerFromStore(containerGlobalName)
|
|
|
|
frame = spriteSheetBuilder.addFrame(container, null, @resolutionFactor * (thangType.get('scale') or 1))
|
2014-09-12 19:33:01 -04:00
|
|
|
spriteSheetBuilder.addAnimation(containerKey, [frame], false)
|
|
|
|
|
2014-09-15 18:08:02 -04:00
|
|
|
getContainersForAnimation: (thangType, animation) ->
|
|
|
|
containers = thangType.get('raw').animations[animation].containers
|
|
|
|
for animation in thangType.get('raw').animations[animation].animations
|
|
|
|
containers = containers.concat(@getContainersForAnimation(thangType, animation.gn))
|
|
|
|
return containers
|
2014-09-19 18:46:02 -04:00
|
|
|
|
|
|
|
#- Rendering sprite sheets for singular thang types
|
2014-09-12 19:33:01 -04:00
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
renderSingularThangType: (thangType, colorConfig, actionNames, spriteSheetBuilder) ->
|
2014-09-12 19:33:01 -04:00
|
|
|
actionObjects = _.values(thangType.getActions())
|
|
|
|
animationActions = []
|
|
|
|
for a in actionObjects
|
|
|
|
continue unless a.animation
|
|
|
|
continue unless a.name in actionNames
|
|
|
|
animationActions.push(a)
|
|
|
|
|
|
|
|
spriteBuilder = new SpriteBuilder(thangType, {colorConfig: colorConfig})
|
|
|
|
|
|
|
|
animationGroups = _.groupBy animationActions, (action) -> action.animation
|
|
|
|
for animationName, actions of animationGroups
|
|
|
|
renderAll = _.any actions, (action) -> action.frames is undefined
|
|
|
|
scale = actions[0].scale or thangType.get('scale') or 1
|
2014-09-18 17:36:05 -04:00
|
|
|
|
|
|
|
actionKeys = (@renderGroupingKey(thangType, action.name, colorConfig) for action in actions)
|
|
|
|
if @spriteSheet?.resolutionFactor is @resolutionFactor and _.all(actionKeys, (key) => key in @spriteSheet.getAnimations())
|
|
|
|
framesNeeded = _.uniq(_.flatten((@spriteSheet.getAnimation(key)).frames for key in actionKeys))
|
|
|
|
framesMap = {}
|
|
|
|
for frame in framesNeeded
|
|
|
|
sprite = new createjs.Sprite(@spriteSheet)
|
|
|
|
sprite.gotoAndStop(frame)
|
|
|
|
framesMap[frame] = spriteSheetBuilder.addFrame(sprite)
|
|
|
|
for key, index in actionKeys
|
|
|
|
action = actions[index]
|
|
|
|
frames = (framesMap[f] for f in @spriteSheet.getAnimation(key).frames)
|
|
|
|
next = @nextForAction(action)
|
|
|
|
spriteSheetBuilder.addAnimation(key, frames, next)
|
|
|
|
continue
|
|
|
|
|
2014-09-12 19:33:01 -04:00
|
|
|
mc = spriteBuilder.buildMovieClip(animationName, null, null, null, {'temp':0})
|
|
|
|
|
|
|
|
if renderAll
|
|
|
|
res = spriteSheetBuilder.addMovieClip(mc, null, scale * @resolutionFactor)
|
|
|
|
frames = spriteSheetBuilder._animations['temp'].frames
|
|
|
|
framesMap = _.zipObject _.range(frames.length), frames
|
|
|
|
else
|
|
|
|
framesMap = {}
|
|
|
|
framesToRender = _.uniq(_.flatten((a.frames.split(',') for a in actions)))
|
|
|
|
for frame in framesToRender
|
|
|
|
frame = parseInt(frame)
|
|
|
|
f = _.bind(mc.gotoAndStop, mc, frame)
|
|
|
|
framesMap[frame] = spriteSheetBuilder.addFrame(mc, null, scale * @resolutionFactor, f)
|
|
|
|
|
|
|
|
for action in actions
|
|
|
|
name = @renderGroupingKey(thangType, action.name, colorConfig)
|
|
|
|
|
|
|
|
if action.frames
|
|
|
|
frames = (framesMap[parseInt(frame)] for frame in action.frames.split(','))
|
|
|
|
else
|
|
|
|
frames = _.values(framesMap).sort()
|
2014-09-18 17:36:05 -04:00
|
|
|
next = @nextForAction(action)
|
2014-09-12 19:33:01 -04:00
|
|
|
spriteSheetBuilder.addAnimation(name, frames, next)
|
|
|
|
|
|
|
|
containerActions = []
|
|
|
|
for a in actionObjects
|
|
|
|
continue unless a.container
|
|
|
|
continue unless a.name in actionNames
|
|
|
|
containerActions.push(a)
|
|
|
|
|
|
|
|
containerGroups = _.groupBy containerActions, (action) -> action.container
|
|
|
|
for containerName, actions of containerGroups
|
|
|
|
container = spriteBuilder.buildContainerFromStore(containerName)
|
|
|
|
scale = actions[0].scale or thangType.get('scale') or 1
|
|
|
|
frame = spriteSheetBuilder.addFrame(container, null, scale * @resolutionFactor)
|
|
|
|
for action in actions
|
|
|
|
name = @renderGroupingKey(thangType, action.name, colorConfig)
|
|
|
|
spriteSheetBuilder.addAnimation(name, [frame], false)
|
|
|
|
|
2014-09-18 17:36:05 -04:00
|
|
|
nextForAction: (action) ->
|
|
|
|
next = true
|
|
|
|
next = action.goesTo if action.goesTo
|
|
|
|
next = false if action.loops is false
|
|
|
|
return next
|
2014-09-19 18:46:02 -04:00
|
|
|
|
|
|
|
#- Rendering frames for raster thang types
|
2014-09-18 17:36:05 -04:00
|
|
|
|
2014-09-19 18:46:02 -04:00
|
|
|
renderRasterThangType: (thangType, spriteSheetBuilder) ->
|
2014-09-12 19:33:01 -04:00
|
|
|
unless thangType.rasterImage
|
2014-09-19 17:59:29 -04:00
|
|
|
console.error("Cannot render the LayerAdapter SpriteSheet until the raster image for <#{thangType.get('name')}> is loaded.")
|
2014-09-12 19:33:01 -04:00
|
|
|
|
|
|
|
bm = new createjs.Bitmap(thangType.rasterImage[0])
|
|
|
|
scale = thangType.get('scale') or 1
|
|
|
|
frame = spriteSheetBuilder.addFrame(bm, null, scale)
|
2014-09-16 18:36:59 -04:00
|
|
|
spriteSheetBuilder.addAnimation(@renderGroupingKey(thangType), [frame], false)
|
2014-09-19 18:46:02 -04:00
|
|
|
|
|
|
|
#- Distributing new Segmented/Singular/RasterSprites to CocoSprites
|
2014-09-16 18:36:59 -04:00
|
|
|
|
|
|
|
setImageObjectToCocoSprite: (cocoSprite) ->
|
|
|
|
if not cocoSprite.thangType.isFullyLoaded()
|
|
|
|
# just give a placeholder
|
|
|
|
sprite = new createjs.Sprite(@spriteSheet)
|
|
|
|
|
|
|
|
else if cocoSprite.thangType.get('raster')
|
|
|
|
sprite = new createjs.Sprite(@spriteSheet)
|
|
|
|
reg = cocoSprite.getOffset 'registration'
|
|
|
|
sprite.regX = -reg.x
|
|
|
|
sprite.regY = -reg.y
|
|
|
|
sprite.gotoAndStop(@renderGroupingKey(cocoSprite.thangType))
|
|
|
|
|
|
|
|
else
|
2014-09-19 17:56:40 -04:00
|
|
|
SpriteClass = if cocoSprite.thangType.get('spriteType') is 'segmented' then SegmentedSprite else SingularSprite
|
2014-09-16 18:36:59 -04:00
|
|
|
prefix = @renderGroupingKey(cocoSprite.thangType, null, cocoSprite.colorConfig) + '.'
|
2014-09-19 17:56:40 -04:00
|
|
|
sprite = new SpriteClass(@spriteSheet, cocoSprite.thangType, prefix, @resolutionFactor)
|
2014-09-16 18:36:59 -04:00
|
|
|
|
|
|
|
sprite.sprite = cocoSprite
|
2014-09-24 19:53:38 -04:00
|
|
|
sprite.camera = @camera
|
2014-09-16 18:36:59 -04:00
|
|
|
sprite.layerPriority = cocoSprite.thang?.layerPriority ? cocoSprite.thangType.get 'layerPriority'
|
|
|
|
sprite.name = cocoSprite.thang?.spriteName or cocoSprite.thangType.get 'name'
|
2014-09-17 18:47:25 -04:00
|
|
|
cocoSprite.setImageObject(sprite)
|
|
|
|
cocoSprite.update(true)
|
2014-09-19 18:46:02 -04:00
|
|
|
@container.addChild(sprite)
|
|
|
|
|
|
|
|
renderGroupingKey: (thangType, grouping, colorConfig) ->
|
|
|
|
key = thangType.get('slug')
|
|
|
|
if colorConfig?.team
|
|
|
|
key += "(#{colorConfig.team.hue},#{colorConfig.team.saturation},#{colorConfig.team.lightness})"
|
|
|
|
key += '.'+grouping if grouping
|
|
|
|
key
|
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
child.destroy?() for child in @container.children
|
|
|
|
super()
|