codecombat/app/lib/surface/TrailMaster.coffee

123 lines
4.3 KiB
CoffeeScript
Raw Normal View History

2014-11-18 14:46:42 -05:00
PAST_PATH_ALPHA = 0.75
PAST_PATH_WIDTH = 5
FUTURE_PATH_ALPHA = 0.75
FUTURE_PATH_WIDTH = 4
TARGET_ALPHA = 1
TARGET_WIDTH = 10
FUTURE_PATH_INTERVAL_DIVISOR = 4
PAST_PATH_INTERVAL_DIVISOR = 2
2014-11-18 14:46:42 -05:00
Camera = require './Camera'
CocoClass = require 'lib/CocoClass'
module.exports = class TrailMaster extends CocoClass
world: null
2014-11-18 14:46:42 -05:00
constructor: (@camera, @layerAdapter) ->
super()
@tweenedSprites = []
@tweens = []
2014-11-18 14:46:42 -05:00
@listenTo @layerAdapter, 'new-spritesheet', -> @generatePaths(@world, @thang)
generatePaths: (@world, @thang) ->
return if @generatingPaths
@generatingPaths = true
@cleanUp()
@createGraphics()
pathDisplayObject = new createjs.SpriteContainer(@layerAdapter.spriteSheet)
pathDisplayObject.mouseEnabled = pathDisplayObject.mouseChildren = false
pathDisplayObject.addChild @createFuturePath()
# pathDisplayObject.addChild @createPastPath() # Just made the animated path the full path... do we want to have past and future look different again?
pathDisplayObject.addChild @createTargets()
2014-11-18 14:46:42 -05:00
@generatingPaths = false
return pathDisplayObject
2014-11-18 14:46:42 -05:00
cleanUp: ->
createjs.Tween.removeTweens(sprite) for sprite in @tweenedSprites
@tweenedSprites = []
@tweens = []
2014-11-18 14:46:42 -05:00
createGraphics: ->
@targetDotKey = @cachePathDot(TARGET_WIDTH, @colorForThang(@thang.team, TARGET_ALPHA), [0, 0, 0, 1])
@pastDotKey = @cachePathDot(PAST_PATH_WIDTH, @colorForThang(@thang.team, PAST_PATH_ALPHA), [0, 0, 0, 1])
@futureDotKey = @cachePathDot(FUTURE_PATH_WIDTH, [255, 255, 255, FUTURE_PATH_ALPHA], @colorForThang(@thang.team, 1))
cachePathDot: (width, fillColor, strokeColor) ->
key = "path-dot-#{width}-#{fillColor}-#{strokeColor}"
fillColor = createjs.Graphics.getRGB(fillColor...)
strokeColor = createjs.Graphics.getRGB(strokeColor...)
2014-11-18 14:46:42 -05:00
unless key in @layerAdapter.spriteSheet.getAnimations()
circle = new createjs.Shape()
radius = width/2
circle.graphics.setStrokeStyle(width/5).beginFill(fillColor).beginStroke(strokeColor).drawCircle(0, 0, radius)
2014-11-18 14:46:42 -05:00
@layerAdapter.addCustomGraphic(key, circle, [-radius*1.5, -radius*1.5, radius*3, radius*3])
return key
colorForThang: (team, alpha=1.0) ->
rgb = [0, 255, 0]
rgb = [255, 0, 0] if team is 'humans'
rgb = [0, 0, 255] if team is 'ogres'
rgb.push(alpha)
return rgb
2014-11-18 14:46:42 -05:00
createPastPath: ->
return unless points = @world.pointsForThang @thang.id, @camera
interval = Math.max(1, parseInt(@world.frameRate / PAST_PATH_INTERVAL_DIVISOR))
params = { interval: interval, frameKey: @pastDotKey }
2014-11-18 14:46:42 -05:00
return @createPath(points, params)
createFuturePath: ->
return unless points = @world.pointsForThang @thang.id, @camera
interval = Math.max(1, parseInt(@world.frameRate / FUTURE_PATH_INTERVAL_DIVISOR))
2014-11-18 14:46:42 -05:00
params = { interval: interval, animate: true, frameKey: @futureDotKey }
return @createPath(points, params)
createTargets: ->
return unless @thang.allTargets
container = new createjs.SpriteContainer(@layerAdapter.spriteSheet)
for x, i in @thang.allTargets by 2
y = @thang.allTargets[i + 1]
sup = @camera.worldToSurface x: x, y: y
sprite = new createjs.Sprite(@layerAdapter.spriteSheet)
sprite.scaleX = sprite.scaleY = 1 / @layerAdapter.resolutionFactor
sprite.scaleY *= @camera.y2x
2014-11-18 14:46:42 -05:00
sprite.gotoAndStop(@targetDotKey)
sprite.x = sup.x
sprite.y = sup.y
container.addChild(sprite)
return container
2014-11-18 14:46:42 -05:00
createPath: (points, options={}) ->
options = options or {}
interval = options.interval or 8
key = options.frameKey or @pastDotKey
container = new createjs.SpriteContainer(@layerAdapter.spriteSheet)
2014-11-18 14:46:42 -05:00
for x, i in points by interval * 2
y = points[i + 1]
sprite = new createjs.Sprite(@layerAdapter.spriteSheet)
sprite.scaleX = sprite.scaleY = 1 / @layerAdapter.resolutionFactor
sprite.scaleY *= @camera.y2x
2014-11-18 14:46:42 -05:00
sprite.gotoAndStop(key)
sprite.x = x
sprite.y = y
container.addChild(sprite)
if lastSprite and options.animate
tween = createjs.Tween.get(lastSprite, {loop: true}).to({x:x, y:y}, 1000)
2014-11-18 14:46:42 -05:00
@tweenedSprites.push lastSprite
@tweens.push tween
2014-11-18 14:46:42 -05:00
lastSprite = sprite
2014-11-18 14:46:42 -05:00
@logged = true
container
play: ->
tween.setPaused(false) for tween in @tweens
stop: ->
tween.setPaused(true) for tween in @tweens
2014-11-18 14:46:42 -05:00
destroy: ->
@cleanUp()
super()