Fixes for world simulation without Web Workers

Primarily for IE9, which is still blocked by box2d issues with these
changes.

Forcing vendor.js instead of vendor_with_box2d.js in main.html allows
you to load a level in IE9, but without Collision-based movement.

Work in progress.
This commit is contained in:
Matt Lott 2014-09-28 23:24:18 -07:00
parent bdb3fd4b92
commit 136af8a0b4
4 changed files with 54 additions and 37 deletions

View file

@ -210,6 +210,11 @@ module.exports = class Angel extends CocoClass
@hireWorker() if rehire
hireWorker: ->
unless Worker?
unless @initialized
@initialized = true
@doWork()
return null
return if @worker
@say 'Hiring worker.'
@worker = new Worker @shared.workerCode
@ -243,11 +248,12 @@ module.exports = class Angel extends CocoClass
# If performance was really a priority in IE9, we would rework things to be able to skip this step.
goalStates = testGM?.getGoalStates()
serialized = testWorld.serialize().serializedWorld
work.testWorld.goalManager.worldGenerationEnded() if work.testWorld.ended
serialized = testWorld.serialize()
window.BOX2D_ENABLED = false
World.deserialize serialized, @angelsShare.worldClassMap, @shared.lastSerializedWorldFrames, @finishBeholdingWorld(goalStates)
World.deserialize serialized.serializedWorld, @shared.worldClassMap, @shared.lastSerializedWorldFrames, @finishBeholdingWorld(goalStates), serialized.startFrame, serialized.endFrame
window.BOX2D_ENABLED = true
@shared.lastSerializedWorldFrames = serialized.frames
@shared.lastSerializedWorldFrames = serialized.serializedWorld.frames
doSimulateWorld: (work) ->
work.t1 = now()
@ -258,6 +264,7 @@ module.exports = class Angel extends CocoClass
i = 0
while i < work.testWorld.totalFrames
frame = work.testWorld.getFrame i++
Backbone.Mediator.publish 'god:world-load-progress-changed', progress: 1
work.testWorld.ended = true
system.finish work.testWorld.thangs for system in work.testWorld.systems
work.t2 = now()

View file

@ -110,19 +110,22 @@ module.exports = class Spell
unless aether
console.error @toString(), 'couldn\'t find a spellThang with aether of', @thangs
cb false
workerMessage =
function: 'hasChangedSignificantly'
a: (newSource ? @originalSource)
spellKey: @spellKey
b: (currentSource ? @source)
careAboutLineNumbers: true
careAboutLint: true
@worker.addEventListener 'message', (e) =>
workerData = JSON.parse e.data
if workerData.function is 'hasChangedSignificantly' and workerData.spellKey is @spellKey
@worker.removeEventListener 'message', arguments.callee, false
cb(workerData.hasChanged)
@worker.postMessage JSON.stringify(workerMessage)
if @worker
workerMessage =
function: 'hasChangedSignificantly'
a: (newSource ? @originalSource)
spellKey: @spellKey
b: (currentSource ? @source)
careAboutLineNumbers: true
careAboutLint: true
@worker.addEventListener 'message', (e) =>
workerData = JSON.parse e.data
if workerData.function is 'hasChangedSignificantly' and workerData.spellKey is @spellKey
@worker.removeEventListener 'message', arguments.callee, false
cb(workerData.hasChanged)
@worker.postMessage JSON.stringify(workerMessage)
else
cb(aether.hasChangedSignificantly((newSource ? @originalSource), (currentSource ? @source), true, true))
createAether: (thang) ->
aceConfig = me.get('aceConfig') ? {}
@ -130,11 +133,12 @@ module.exports = class Spell
skipProtectAPI = @skipProtectAPI or not writable
aetherOptions = createAetherOptions functionName: @name, codeLanguage: @language, functionParameters: @parameters, skipProtectAPI: skipProtectAPI
aether = new Aether aetherOptions
workerMessage =
function: 'createAether'
spellKey: @spellKey
options: aetherOptions
@worker.postMessage JSON.stringify workerMessage
if @worker
workerMessage =
function: 'createAether'
spellKey: @spellKey
options: aetherOptions
@worker.postMessage JSON.stringify workerMessage
aether
updateLanguageAether: (@language) ->
@ -142,10 +146,11 @@ module.exports = class Spell
spellThang.aether?.setLanguage @language
spellThang.castAether = null
Backbone.Mediator.publish 'tome:spell-changed-language', spell: @, language: @language
workerMessage =
function: 'updateLanguageAether'
newLanguage: @language
@worker.postMessage JSON.stringify workerMessage
if @worker
workerMessage =
function: 'updateLanguageAether'
newLanguage: @language
@worker.postMessage JSON.stringify workerMessage
@transpile()
toString: ->

View file

@ -400,19 +400,23 @@ module.exports = class SpellView extends CocoView
@clearAetherDisplay()
if codeHasChangedSignificantly and not codeIsAsCast
workerMessage =
function: 'transpile'
spellKey: @spell.spellKey
source: source
if @worker
workerMessage =
function: 'transpile'
spellKey: @spell.spellKey
source: source
@worker.addEventListener 'message', (e) =>
workerData = JSON.parse e.data
if workerData.function is 'transpile' and workerData.spellKey is @spell.spellKey
@worker.removeEventListener 'message', arguments.callee, false
aether.problems = workerData.problems
aether.raw = source
finishUpdatingAether(aether)
@worker.postMessage JSON.stringify(workerMessage)
@worker.addEventListener 'message', (e) =>
workerData = JSON.parse e.data
if workerData.function is 'transpile' and workerData.spellKey is @spell.spellKey
@worker.removeEventListener 'message', arguments.callee, false
aether.problems = workerData.problems
aether.raw = source
finishUpdatingAether(aether)
@worker.postMessage JSON.stringify(workerMessage)
else
aether.transpile source
finishUpdatingAether(aether)
else
finishUpdatingAether(aether)

View file

@ -89,6 +89,7 @@ module.exports = class TomeView extends CocoView
@cast()
createWorker: ->
return null unless Worker?
return new Worker('/javascripts/workers/aether_worker.js')
generateTeamSpellMap: (spellObject) ->