Fixed . Refactored scaling for sprites a bit.

This commit is contained in:
Scott Erickson 2014-05-19 12:56:13 -07:00
parent 3c3fbf3836
commit 8ae14e21ae
3 changed files with 40 additions and 32 deletions
app
lib/surface
models
views/editor/thang

View file

@ -37,12 +37,16 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
possessed: false
flipped: false
flippedCount: 0
originalScaleX: null
originalScaleY: null
actionQueue: null
actions: null
rotation: 0
# Scale numbers
baseScaleX: 1 # scale + flip (for current action) / resolutionFactor.
baseScaleY: 1 # These numbers rarely change, so keep them around.
scaleFactor: 1 # Current scale adjustment. This can change rapidly.
targetScaleFactor: 1 # What the scaleFactor is going toward during a tween.
# ACTION STATE
# Actions have relations. If you say 'move', 'move_side' may play because of a direction
# relationship, and if you say 'cast', 'cast_begin' may happen first, or 'cast_end' after.
@ -71,7 +75,6 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@ranges = []
@handledDisplayEvents = {}
@age = 0
@scaleFactor = @targetScaleFactor = 1
if @thangType.isFullyLoaded()
@setupSprite()
else
@ -94,6 +97,7 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
finishSetup: ->
return unless @thang
@updateBaseScale()
@update true # Reflect initial scale and other state
setUpRasterImage: ->
@ -102,8 +106,6 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@setImageObject image
$(image.image).one 'load', => @updateScale?()
@configureMouse()
@originalScaleX = image.scaleX
@originalScaleY = image.scaleY
@imageObject.sprite = @
@imageObject.layerPriority = @thangType.get 'layerPriority'
@imageObject.name = @thang?.spriteName or @thangType.get 'name'
@ -112,14 +114,6 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@imageObject.regY = -reg.y
@finishSetup()
destroy: ->
mark.destroy() for name, mark of @marks
label.destroy() for name, label of @labels
p.removeChild @healthBar if p = @healthBar?.parent
@imageObject?.off 'animationend', @playNextAction
clearInterval @effectInterval if @effectInterval
super()
toString: -> "<CocoSprite: #{@thang?.id}>"
buildSpriteSheet: ->
@ -139,17 +133,11 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
sprite = new createjs.Sprite(spriteSheet)
else
sprite = new createjs.Shape()
sprite.scaleX = sprite.scaleY = 1 / @options.resolutionFactor
# temp, until these are re-exported with perspective
if @options.camera and @thangType.get('name') in ['Dungeon Floor', 'Indoor Floor', 'Grass', 'Goal Trigger', 'Obstacle']
sprite.scaleY *= @options.camera.y2x
@setImageObject sprite
@addHealthBar()
@configureMouse()
# TODO: generalize this later?
@originalScaleX = sprite.scaleX
@originalScaleY = sprite.scaleY
@imageObject.sprite = @
@imageObject.layerPriority = @thangType.get 'layerPriority'
@imageObject.name = @thang?.spriteName or @thangType.get 'name'
@ -183,6 +171,7 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@currentAction = action
return @hide() unless action.animation or action.container or action.relatedActions
@show()
@updateBaseScale()
return @updateActionDirection() unless action.animation or action.container
m = if action.container then "gotoAndStop" else "gotoAndPlay"
@imageObject.framerate = action.framerate or 20
@ -307,6 +296,18 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
[@imageObject.x, @imageObject.y] = [sup.x, sup.y]
@lastPos = p1.copy?() or _.clone(p1)
@hasMoved = true
updateBaseScale: ->
scale = 1
scale = @thangType.get('scale') or 1 if @isRaster
scale /= @options.resolutionFactor unless @isRaster
@baseScaleX = @baseScaleY = scale
@baseScaleX *= -1 if @getActionProp 'flipX'
@baseScaleY *= -1 if @getActionProp 'flipY'
# temp, until these are re-exported with perspective
floors = ['Dungeon Floor', 'Indoor Floor', 'Grass', 'Goal Trigger', 'Obstacle']
if @options.camera and @thangType.get('name') in floors
@baseScaleY *= @options.camera.y2x
updateScale: ->
return unless @imageObject
@ -326,8 +327,9 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@imageObject.scaleY *= @thangType.get('scale') ? 1
[@lastThangWidth, @lastThangHeight] = [@thang.width, @thang.height]
return
scaleX = if @getActionProp 'flipX' then -1 else 1
scaleY = if @getActionProp 'flipY' then -1 else 1
scaleX = scaleY = 1
if @thangType.get('name') in ['Arrow', 'Spear']
# Scales the arrow so it appears longer when flying parallel to horizon.
# To do that, we convert angle to [0, 90] (mirroring half-planes twice), then make linear function out of it:
@ -342,16 +344,10 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
angle = 180 - angle if angle > 90
scaleX = 0.5 + 0.5 * (90 - angle) / 90
if @isRaster # scale is worked into building the sprite sheet for animations
scale = @thangType.get('scale') or 1
scaleX *= scale
scaleY *= scale
console.error "No thang for", @ unless @thang
scaleFactorX = @thang.scaleFactorX ? @scaleFactor
scaleFactorY = @thang.scaleFactorY ? @scaleFactor
@imageObject.scaleX = @originalScaleX * scaleX * scaleFactorX
@imageObject.scaleY = @originalScaleY * scaleY * scaleFactorY
# TODO: support using scaleFactorX/Y from the thang object
@imageObject.scaleX = @baseScaleX * @scaleFactor * scaleX
@imageObject.scaleY = @baseScaleY * @scaleFactor * scaleY
if (@thang.scaleFactor or 1) isnt @targetScaleFactor
createjs.Tween.removeTweens(@)
@ -783,3 +779,11 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
return unless @healthBar
@healthBar.x = @imageObject.x
@healthBar.y = @imageObject.y
destroy: ->
mark.destroy() for name, mark of @marks
label.destroy() for name, label of @labels
p.removeChild @healthBar if p = @healthBar?.parent
@imageObject?.off 'animationend', @playNextAction
clearInterval @effectInterval if @effectInterval
super()

View file

@ -30,7 +30,7 @@ module.exports = class ThangType extends CocoModel
isFullyLoaded: ->
# TODO: Come up with a better way to identify when the model doesn't have everything needed to build the sprite. ie when it's a projection without all the required data.
return @get('actions') or @get('raster') # needs one of these two things
getActions: ->
return {} unless @isFullyLoaded()
return @actions or @buildActions()

View file

@ -284,7 +284,11 @@ module.exports = class ThangTypeEditView extends View
fixed = scaleValue.toFixed(1)
@scale = scaleValue
@$el.find('.scale-label').text " #{fixed}x "
@currentObject.scaleX = @currentObject.scaleY = scaleValue / resValue if @currentObject?
if @currentSprite
@currentSprite.scaleFactor = scaleValue
@currentSprite.updateScale()
else if @currentObject?
@currentObject.scaleX = @currentObject.scaleY = scaleValue / resValue
@updateGrid()
@updateDots()