Set up marks to all load dynamically, and set up effect marks, which appear above a thang's head and rotate between them if there are multiple.

This commit is contained in:
Scott Erickson 2014-03-06 15:52:09 -08:00
parent a03403de94
commit 041f4512e8
4 changed files with 74 additions and 23 deletions

View file

@ -82,6 +82,7 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@imageObject?.off 'animationend', @playNextAction @imageObject?.off 'animationend', @playNextAction
@playNextAction = null @playNextAction = null
@displayObject?.off() @displayObject?.off()
clearInterval @effectInterval if @effectInterval
super() super()
toString: -> "<CocoSprite: #{@thang?.id}>" toString: -> "<CocoSprite: #{@thang?.id}>"
@ -375,18 +376,55 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
scale *= @options.resolutionFactor if prop is 'registration' scale *= @options.resolutionFactor if prop is 'registration'
pos.x *= scale pos.x *= scale
pos.y *= scale pos.y *= scale
if @thang
scaleFactor = @thang.scaleFactor ? 1
pos.x *= @thang.scaleFactorX ? scaleFactor
pos.y *= @thang.scaleFactorY ? scaleFactor
pos pos
updateMarks: -> updateMarks: ->
return unless @options.camera return unless @options.camera
@addMark 'repair', null, @options.markThangTypes.repair if @thang?.errorsOut @addMark 'repair', null, 'repair' if @thang?.errorsOut
@marks.repair?.toggle @thang?.errorsOut @marks.repair?.toggle @thang?.errorsOut
@addMark('bounds').toggle true if @thang?.drawsBounds @addMark('bounds').toggle true if @thang?.drawsBounds
@addMark('shadow').toggle true unless @thangType.get('shadow') is 0 @addMark('shadow').toggle true unless @thangType.get('shadow') is 0
mark.update() for name, mark of @marks mark.update() for name, mark of @marks
# @thang.effectNames = ['berserk', 'confused', 'controlled', 'cursed', 'fear', 'poison', 'paralyzed', 'regeneration', 'sleep', 'slowed', 'speed']
@updateEffectMarks() if @thang?.effectNames?.length
updateEffectMarks: ->
return if _.isEqual @thang.effectNames, @previousEffectNames
for effect in @thang.effectNames
mark = @addMark effect, @options.floatingLayer, effect
mark.statusEffect = true
mark.toggle 'on'
mark.show()
if @previousEffectNames
for effect in @previousEffectNames
mark = @marks[effect]
mark.toggle 'off'
if @thang.effectNames.length > 1 and not @effectInterval
@rotateEffect()
@effectInterval = setInterval @rotateEffect, 1500
else if @effectInterval and @thang.effectNames.length <= 1
@clearInterval @effectInterval
@effectInterval = null
@previousEffectNames = @thang.effectNames
rotateEffect: =>
effects = (m.name for m in _.values(@marks) when m.on and m.statusEffect and m.mark)
effects.sort()
@effectIndex ?= 0
@effectIndex = (@effectIndex + 1) % effects.length
@marks[effect].hide() for effect in effects
@marks[effects[@effectIndex]].show()
setHighlight: (to, delay) -> setHighlight: (to, delay) ->
@addMark 'highlight', @options.floatingLayer, @options.markThangTypes.highlight if to @addMark 'highlight', @options.floatingLayer, 'highlight' if to
@marks.highlight?.highlightDelay = delay @marks.highlight?.highlightDelay = delay
@marks.highlight?.toggle to and not @dimmed @marks.highlight?.toggle to and not @dimmed

View file

@ -1,5 +1,7 @@
CocoClass = require 'lib/CocoClass' CocoClass = require 'lib/CocoClass'
Camera = require './Camera' Camera = require './Camera'
ThangType = require 'models/ThangType'
markThangTypes = {}
module.exports = class Mark extends CocoClass module.exports = class Mark extends CocoClass
subscriptions: {} subscriptions: {}
@ -20,6 +22,7 @@ module.exports = class Mark extends CocoClass
destroy: -> destroy: ->
@mark?.parent?.removeChild @mark @mark?.parent?.removeChild @mark
@markSprite?.destroy() @markSprite?.destroy()
@thangType?.off 'sync', @onLoadedThangType, @
@sprite = null @sprite = null
super() super()
@ -27,7 +30,9 @@ module.exports = class Mark extends CocoClass
toggle: (to) -> toggle: (to) ->
return @ if to is @on return @ if to is @on
return @toggleTo = to unless @mark
@on = to @on = to
delete @toggleTo
if @on if @on
@layer.addChild @mark @layer.addChild @mark
@layer.updateLayerOrder() @layer.updateLayerOrder()
@ -52,7 +57,7 @@ module.exports = class Mark extends CocoClass
else if @name is 'debug' then @buildDebug() else if @name is 'debug' then @buildDebug()
else if @thangType then @buildSprite() else if @thangType then @buildSprite()
else console.error "Don't know how to build mark for", @name else console.error "Don't know how to build mark for", @name
@mark.mouseEnabled = false @mark?.mouseEnabled = false
@ @
buildBounds: -> buildBounds: ->
@ -126,15 +131,34 @@ module.exports = class Mark extends CocoClass
@mark.graphics.endFill() @mark.graphics.endFill()
buildSprite: -> buildSprite: ->
#console.log "building", @name, "with thangtype", @thangType if _.isString @thangType
thangType = markThangTypes[@thangType]
return @loadThangType() if not thangType
@thangType = thangType
return @thangType.once 'sync', @onLoadedThangType, @ if not @thangType.loaded
CocoSprite = require './CocoSprite' CocoSprite = require './CocoSprite'
markSprite = new CocoSprite @thangType, @thangType.spriteOptions markSprite = new CocoSprite @thangType, @thangType.spriteOptions
markSprite.queueAction 'idle' markSprite.queueAction 'idle'
@mark = markSprite.displayObject @mark = markSprite.displayObject
@markSprite = markSprite @markSprite = markSprite
loadThangType: ->
name = @thangType
@thangType = new ThangType()
@thangType.url = -> "/db/thang.type/#{name}"
@thangType.once 'sync', @onLoadedThangType, @
@thangType.fetch()
markThangTypes[name] = @thangType
window.mtt = markThangTypes
onLoadedThangType: ->
@build()
@toggle(@toggleTo) if @toggleTo?
update: (pos=null) -> update: (pos=null) ->
return false unless @on return false unless @on and @mark
@mark.alpha = if @hidden then 0 else 1
@updatePosition pos @updatePosition pos
@updateRotation() @updateRotation()
@updateScale() @updateScale()
@ -156,10 +180,11 @@ module.exports = class Mark extends CocoClass
pos ?= @sprite?.displayObject pos ?= @sprite?.displayObject
@mark.x = pos.x @mark.x = pos.x
@mark.y = pos.y @mark.y = pos.y
if @name is 'highlight' if @statusEffect or @name is 'highlight'
offset = @sprite.getOffset 'aboveHead' offset = @sprite.getOffset 'aboveHead'
@mark.x += offset.x @mark.x += offset.x
@mark.y += offset.y @mark.y += offset.y
@mark.y -= 3 if @statusEffect
updateRotation: -> updateRotation: ->
if @name is 'debug' or (@name is 'shadow' and @sprite.thang?.shape in ["rectangle", "box"]) if @name is 'debug' or (@name is 'shadow' and @sprite.thang?.shape in ["rectangle", "box"])
@ -187,3 +212,5 @@ module.exports = class Mark extends CocoClass
stop: -> @markSprite?.stop() stop: -> @markSprite?.stop()
play: -> @markSprite?.play() play: -> @markSprite?.play()
hide: -> @hidden = true
show: -> @hidden = false

View file

@ -48,10 +48,6 @@ module.exports = class SpriteBoss extends CocoClass
thangTypeFor: (type) -> thangTypeFor: (type) ->
_.find @options.thangTypes, (m) -> m.get('original') is type or m.get('name') is type _.find @options.thangTypes, (m) -> m.get('original') is type or m.get('name') is type
markThangTypes: ->
highlight: @thangTypeFor "Highlight"
repair: @thangTypeFor "Repair"
createLayers: -> createLayers: ->
@spriteLayers = {} @spriteLayers = {}
for [name, priority] in [ for [name, priority] in [
@ -87,11 +83,11 @@ module.exports = class SpriteBoss extends CocoClass
sprite sprite
createMarks: -> createMarks: ->
@targetMark = new Mark name: 'target', camera: @camera, layer: @spriteLayers["Ground"], thangType: @thangTypeFor("Target") @targetMark = new Mark name: 'target', camera: @camera, layer: @spriteLayers["Ground"], thangType: 'target'
@selectionMark = new Mark name: 'selection', camera: @camera, layer: @spriteLayers["Ground"], thangType: @thangTypeFor("Selection") @selectionMark = new Mark name: 'selection', camera: @camera, layer: @spriteLayers["Ground"], thangType: 'selection'
createSpriteOptions: (options) -> createSpriteOptions: (options) ->
_.extend options, camera: @camera, resolutionFactor: 4, groundLayer: @spriteLayers["Ground"], textLayer: @surfaceTextLayer, floatingLayer: @spriteLayers["Floating"], markThangTypes: @markThangTypes(), spriteSheetCache: @spriteSheetCache, showInvisible: @options.showInvisible _.extend options, camera: @camera, resolutionFactor: 4, groundLayer: @spriteLayers["Ground"], textLayer: @surfaceTextLayer, floatingLayer: @spriteLayers["Floating"], spriteSheetCache: @spriteSheetCache, showInvisible: @options.showInvisible
createIndieSprites: (indieSprites, withWizards) -> createIndieSprites: (indieSprites, withWizards) ->
unless @indieSprites unless @indieSprites

View file

@ -115,15 +115,5 @@ module.exports = class Level extends CocoModel
model = CocoModel.getOrMakeModelFromLink link, shouldLoadProjection model = CocoModel.getOrMakeModelFromLink link, shouldLoadProjection
models.push model if model models.push model if model
else if path is '/' else if path is '/'
# We also we need to make sure we grab the Wizard ThangType and the Marks. Hackitrooooid!
for [type, original] in [
["Highlight", "529f8fdbdacd325127000003"]
["Selection", "52aa5f7520fccb0000000002"]
["Target", "52b32ad97385ec3d03000001"]
["Repair", "52bcc4591f766a891c000003"]
]
link = "/db/thang_type/#{original}/version"
model = CocoModel.getOrMakeModelFromLink link, shouldLoadProjection
models.push model if model
models.push ThangType.loadUniversalWizard() models.push ThangType.loadUniversalWizard()
models models