Refactored bobbing.

This commit is contained in:
Nick Winter 2014-01-31 10:21:32 -08:00
parent d79d66c175
commit 6303d28554
5 changed files with 32 additions and 28 deletions

View file

@ -61,7 +61,7 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@actionQueue = []
@marks = {}
@labels = {}
@ticker = 0
@age = 0
@displayObject = new createjs.Container()
if @thangType.get('actions')
@onThangTypeLoaded()
@ -124,7 +124,7 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@playNextAction()
onActionEnd: (e) => @playNextAction()
onSurfaceTicked: -> @ticker += 1
onSurfaceTicked: (e) -> @age += e.dt
playNextAction: ->
@playAction(@actionQueue.splice(0,1)[0]) if @actionQueue.length
@ -159,16 +159,21 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@displayObject.cache 0, 0, bounds.width, bounds.height
#console.log "just cached", @thang.id, "which was at", @imageObject.x, @imageObject.y, bounds.width, bounds.height, "with scale", Math.max(@imageObject.scaleX, @imageObject.scaleY)
getBobOffset: ->
return 0 unless @thang.bobHeight
@thang.bobHeight * (1 + Math.sin(@age * Math.PI / @thang.bobTime))
updatePosition: ->
return unless @thang?.pos and @options.camera?
if @thang.bobHeight
@thang.pos.z = @thang.pos.z + (Math.sin @ticker / @thang.bobTime) * 0.1 * @thang.bobHeight
[p0, p1] = [@lastPos, @thang.pos]
if bobOffset = @getBobOffset()
p1 = p1.copy?() or _.clone(p1)
p1.z += bobOffset
return if p0 and p0.x is p1.x and p0.y is p1.y and p0.z is p1.z and not @options.camera.tweeningZoomTo
wop = x: p1.x, y: p1.y, z: if @thang.isLand then 0 else p1.z - @thang.depth / 2
wop = x: p1.x, y: p1.y, z: if @thang.isLand then 0 else p1.z - @thang.depth
sup = @options.camera.worldToSurface wop
[@displayObject.x, @displayObject.y] = [sup.x, sup.y]
@lastPos = _.clone(p1)
@lastPos = p1.copy?() or _.clone(p1)
@hasMoved = true
updateScale: ->

View file

@ -137,7 +137,7 @@ module.exports = class Mark extends CocoClass
if @name in ['shadow', 'debug']
pos = @camera.worldToSurface x: @sprite.thang.pos.x, y: @sprite.thang.pos.y
if @name is 'shadow'
worldZ = @sprite.thang.pos.z - @sprite.thang.depth / 2
worldZ = @sprite.thang.pos.z - @sprite.thang.depth / 2 + @sprite.getBobOffset()
@mark.alpha = 0.451 / Math.sqrt(worldZ / 2 + 1)
else
pos ?= @sprite?.displayObject

View file

@ -106,15 +106,15 @@ module.exports = Surface = class Surface extends CocoClass
@hidePathFinding()
@showingPathFinding = not @showingPathFinding
if @showingPathFinding then @showPathFinding() else @hidePathFinding()
hidePathFinding: ->
@surfaceLayer.removeChild @navRectangles if @navRectangles
@surfaceLayer.removeChild @navPaths if @navPaths
@navRectangles = @navPaths = null
showPathFinding: ->
@hidePathFinding()
mesh = _.values(@world.navMeshes or {})[0]
return unless mesh
@navRectangles = new createjs.Container()
@ -122,7 +122,7 @@ module.exports = Surface = class Surface extends CocoClass
@addMeshRectanglesToContainer mesh, @navRectangles
@surfaceLayer.addChild @navRectangles
@surfaceLayer.updateLayerOrder()
graph = _.values(@world.graphs or {})[0]
return @surfaceLayer.updateLayerOrder() unless graph
@navPaths = new createjs.Container()
@ -130,7 +130,7 @@ module.exports = Surface = class Surface extends CocoClass
@addNavPathsToContainer graph, @navPaths
@surfaceLayer.addChild @navPaths
@surfaceLayer.updateLayerOrder()
addMeshRectanglesToContainer: (mesh, container) ->
for rect in mesh
shape = new createjs.Shape()
@ -455,7 +455,7 @@ module.exports = Surface = class Surface extends CocoClass
@drawCurrentFrame()
@onFrameChanged()
@updatePaths() if (@totalFramesDrawn % 2) is 0 or createjs.Ticker.getMeasuredFPS() > createjs.Ticker.getFPS() - 5
Backbone.Mediator.publish('surface:ticked', {})
Backbone.Mediator.publish('surface:ticked', {dt: @world.dt})
mib = @stage.mouseInBounds
if @mouseInBounds isnt mib
Backbone.Mediator.publish('surface:mouse-' + (if mib then "over" else "out"), {})

View file

@ -3,8 +3,6 @@ Camera = require './Camera'
{me} = require 'lib/auth'
module.exports = class WizardSprite extends IndieSprite
ticker: 0
# Wizard targets are constantly changing, so a simple tween doesn't work.
# Instead, the wizard stores its origin point and the (possibly) moving target.
# Then it figures out its current position based on tween percentage and
@ -16,12 +14,11 @@ module.exports = class WizardSprite extends IndieSprite
reachedTarget: true
spriteXOffset: 4 # meters from target sprite
spriteYOffset: 0 # meters from target sprite
spriteZOffset: 1 # meters from ground (in middle of float)
subscriptions:
'bus:player-states-changed': 'onPlayerStatesChanged'
'me:synced': 'onMeSynced'
'surface:sprite-selected': 'onSpriteSelected'
'surface:sprite-selected': 'onSpriteSelected'
'echo-self-wizard-sprite': 'onEchoSelfWizardSprite'
'echo-all-wizard-sprites': 'onEchoAllWizardSprites'
@ -38,6 +35,8 @@ module.exports = class WizardSprite extends IndieSprite
makeIndieThang: (thangType, thangID, pos) ->
thang = super thangType, thangID, pos
thang.isSelectable = false
thang.bobHeight = 1.5
thang.bobTime = 2
thang
onPlayerStatesChanged: (e) ->
@ -102,8 +101,7 @@ module.exports = class WizardSprite extends IndieSprite
onEchoSelfWizardSprite: (e) -> e.payload = @ if @isSelf
onEchoAllWizardSprites: (e) -> e.payload.push @
defaultPos: -> x: 35, y: 24, z: @thang.depth / 2 + @spriteZOffset
getZOffset: -> @thang.depth / 2 + @spriteZOffset + Math.sin @ticker / 20 # Cloud bobbing.
defaultPos: -> x: 35, y: 24, z: @thang.depth / 2 + @bobHeight
move: (pos, duration) -> @setTarget(pos, duration)
setTarget: (newTarget, duration) ->
@ -185,7 +183,7 @@ module.exports = class WizardSprite extends IndieSprite
return unless @options.camera
@thang.pos = @getCurrentPosition()
@faceTarget()
sup = @options.camera.worldToSurface x: @thang.pos.x, y: @thang.pos.y, z: @thang.pos.z - @thang.depth / 2
sup = @options.camera.worldToSurface x: @thang.pos.x, y: @thang.pos.y, z: @thang.pos.z - @thang.depth / 2 + @getBobOffset()
@displayObject.x = sup.x
@displayObject.y = sup.y
@ -196,7 +194,7 @@ module.exports = class WizardSprite extends IndieSprite
"""
@targetPos = @targetSprite.thang.pos if @targetSprite
pos = _.clone(@targetPos)
pos.z = @getZOffset()
pos.z += @thang.bobHeight
@adjustPositionToSideOfTarget(pos) if @targetSprite # be off to the side depending on placement in world
return pos if @reachedTarget # stick like glue

View file

@ -43,10 +43,10 @@ module.exports = class ThangsTabView extends View
'sprite:mouse-up': 'onSpriteMouseUp'
'sprite:double-clicked': 'onSpriteDoubleClicked'
'surface:stage-mouse-down': 'onStageMouseDown'
events:
'click #extant-thangs-filter button': 'onFilterExtantThangs'
shortcuts:
'esc': -> @selectAddThang()
@ -56,7 +56,7 @@ module.exports = class ThangsTabView extends View
val = button.val()
@thangsTreema.$el.removeClass(@lastHideClass) if @lastHideClass
@thangsTreema.$el.addClass(@lastHideClass = "hide-except-#{val}") if val
constructor: (options) ->
super options
@ -76,11 +76,12 @@ module.exports = class ThangsTabView extends View
context = super(context)
thangTypes = (thangType.attributes for thangType in @supermodel.getModels(ThangType))
thangTypes = _.uniq thangTypes, false, 'original'
thangTypes = _.reject thangTypes, kind: 'Mark'
groupMap = {}
for thangType in thangTypes
groupMap[thangType.kind] ?= []
groupMap[thangType.kind].push thangType
groups = []
for groupName in Object.keys(groupMap).sort()
someThangTypes = groupMap[groupName]
@ -89,7 +90,7 @@ module.exports = class ThangsTabView extends View
name: groupName
thangs: someThangTypes
groups.push group
context.thangTypes = thangTypes
context.groups = groups
context
@ -100,7 +101,7 @@ module.exports = class ThangsTabView extends View
$('.tab-content').click @selectAddThang
$('#thangs-list').bind 'mousewheel', @preventBodyScrollingInThangList
@$el.find('#extant-thangs-filter button:first').button('toggle')
# TODO: move these into the shortcuts list
key 'left', _.bind @moveAddThangSelection, @, -1
key 'right', _.bind @moveAddThangSelection, @, 1
@ -208,7 +209,7 @@ module.exports = class ThangsTabView extends View
else if @addThangSprite
# We clicked on the background when we had an add Thang selected, so add it
@addThang @addThangType, @addThangSprite.thang.pos
# Commented out this bit so the extant thangs treema editor can select invisible thangs like arrows.
# Couldn't spot any bugs... But if there are any, better come up with a better solution.
# else