2014-02-24 17:40:28 -05:00
|
|
|
CocoClass = require 'lib/CocoClass'
|
|
|
|
|
|
|
|
module.exports = class CastingScreen extends CocoClass
|
|
|
|
subscriptions:
|
|
|
|
'tome:cast-spells': 'onCastingBegins'
|
|
|
|
'god:new-world-created': 'onCastingEnds'
|
2014-02-28 14:05:41 -05:00
|
|
|
'god:world-load-progress-changed': 'onWorldLoadProgressChanged'
|
2014-02-24 17:40:28 -05:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super()
|
|
|
|
options ?= {}
|
|
|
|
@camera = options.camera
|
|
|
|
@layer = options.layer
|
2014-06-30 22:16:26 -04:00
|
|
|
console.error @toString(), 'needs a camera.' unless @camera
|
|
|
|
console.error @toString(), 'needs a layer.' unless @layer
|
2014-02-24 17:40:28 -05:00
|
|
|
@build()
|
2014-05-11 20:42:32 -04:00
|
|
|
|
|
|
|
onCastingBegins: (e) -> @show() unless e.preload
|
|
|
|
onCastingEnds: (e) -> @hide()
|
2014-02-24 17:40:28 -05:00
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
toString: -> '<CastingScreen>'
|
2014-02-24 17:40:28 -05:00
|
|
|
|
|
|
|
build: ->
|
|
|
|
@dimLayer = new createjs.Container()
|
|
|
|
@dimLayer.mouseEnabled = @dimLayer.mouseChildren = false
|
|
|
|
@dimLayer.layerIndex = -11
|
|
|
|
@dimLayer.addChild @dimScreen = new createjs.Shape()
|
2014-06-30 22:16:26 -04:00
|
|
|
@dimScreen.graphics.beginFill('rgba(0,0,0,0.5)').rect 0, 0, @camera.canvasWidth, @camera.canvasHeight
|
2014-02-24 17:40:28 -05:00
|
|
|
@dimLayer.alpha = 0
|
|
|
|
@layer.addChild @dimLayer
|
2014-02-28 14:05:41 -05:00
|
|
|
@dimLayer.addChild @makeProgressBar()
|
|
|
|
@dimLayer.addChild @makeCastingText()
|
|
|
|
|
|
|
|
onWorldLoadProgressChanged: (e) ->
|
|
|
|
if new Date().getTime() - @t0 > 500
|
|
|
|
createjs.Tween.removeTweens @progressBar
|
2014-06-30 22:16:26 -04:00
|
|
|
createjs.Tween.get(@progressBar).to({scaleX: e.progress}, 200)
|
2014-02-28 14:05:41 -05:00
|
|
|
|
|
|
|
makeProgressBar: ->
|
|
|
|
BAR_PIXEL_HEIGHT = 3
|
|
|
|
BAR_PCT_WIDTH = .75
|
|
|
|
pixelWidth = parseInt(@camera.canvasWidth * BAR_PCT_WIDTH)
|
|
|
|
pixelMargin = (@camera.canvasWidth - (@camera.canvasWidth * BAR_PCT_WIDTH)) / 2
|
|
|
|
barY = 3 * (@camera.canvasHeight / 5)
|
|
|
|
|
|
|
|
g = new createjs.Graphics()
|
2014-06-30 22:16:26 -04:00
|
|
|
g.beginFill(createjs.Graphics.getRGB(255, 255, 255))
|
|
|
|
g.drawRoundRect(0, 0, pixelWidth, BAR_PIXEL_HEIGHT, 3)
|
2014-02-28 14:05:41 -05:00
|
|
|
@progressBar = new createjs.Shape(g)
|
|
|
|
@progressBar.x = pixelMargin
|
|
|
|
@progressBar.y = barY
|
|
|
|
@progressBar.scaleX = 0
|
|
|
|
@dimLayer.addChild(@progressBar)
|
|
|
|
|
|
|
|
makeCastingText: ->
|
|
|
|
size = @camera.canvasHeight / 15
|
2014-06-30 22:16:26 -04:00
|
|
|
text = new createjs.Text('Casting', "#{size}px cursive", '#aaaaaa')
|
2014-02-28 14:05:41 -05:00
|
|
|
text.regX = text.getMeasuredWidth() / 2
|
|
|
|
text.regY = text.getMeasuredHeight() / 2
|
|
|
|
text.x = @camera.canvasWidth / 2
|
|
|
|
text.y = @camera.canvasHeight / 2
|
|
|
|
@text = text
|
|
|
|
return text
|
2014-02-24 17:40:28 -05:00
|
|
|
|
|
|
|
show: ->
|
2014-02-28 14:05:41 -05:00
|
|
|
return if @showing
|
|
|
|
@showing = true
|
|
|
|
@t0 = new Date().getTime()
|
|
|
|
|
|
|
|
@progressBar.scaleX = 0
|
2014-02-24 17:40:28 -05:00
|
|
|
@dimLayer.alpha = 0
|
|
|
|
createjs.Tween.removeTweens @dimLayer
|
2014-06-30 22:16:26 -04:00
|
|
|
createjs.Tween.get(@dimLayer).to({alpha: 1}, 500)
|
2014-02-24 17:40:28 -05:00
|
|
|
|
|
|
|
hide: ->
|
2014-02-28 14:05:41 -05:00
|
|
|
return unless @showing
|
|
|
|
@showing = false
|
|
|
|
|
|
|
|
createjs.Tween.removeTweens @progressBar
|
2014-02-24 17:40:28 -05:00
|
|
|
createjs.Tween.removeTweens @dimLayer
|
2014-06-30 22:16:26 -04:00
|
|
|
createjs.Tween.get(@dimLayer).to({alpha: 0}, 500)
|