-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed HTML5/GWT platform to use web worker requestAnimationFrame to…
… trigger AGI animation ticks, rather than relying on speed of postMessage calls.
- Loading branch information
1 parent
5f3ba96
commit b6be45a
Showing
2 changed files
with
47 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,11 @@ public class AgileWebWorker extends DedicatedWorkerEntryPoint implements Message | |
*/ | ||
private OPFSGameFiles opfsGameFiles; | ||
|
||
/** | ||
* The total tick count at the time of the last animation tick. | ||
*/ | ||
private int lastTotalTickCount; | ||
|
||
/** | ||
* Incoming messages from the UI thread are for two purposes: One is to set things | ||
* up, and then once both sides are up and running, the UI thread then starts sending | ||
|
@@ -122,25 +127,46 @@ public void onMessage(MessageEvent event) { | |
interpreter = new Interpreter( | ||
game, userInput, wavePlayer, savedGameStore, | ||
pixelData, variableData); | ||
break; | ||
|
||
case "Tick": | ||
try { | ||
// Perform one animation tick. | ||
interpreter.animationTick(); | ||
// Then notify the UI thread that the tick is complete. | ||
postObject("TickComplete", JavaScriptObject.createObject()); | ||
} catch (QuitAction qa) { | ||
// The user has quit the game, so notify the UI thread of this. | ||
postObject("QuitGame", JavaScriptObject.createObject()); | ||
} | ||
lastTotalTickCount = variableData.getTotalTicks(); | ||
performAnimationTick(0); | ||
break; | ||
|
||
default: | ||
// Unknown message. Ignore. | ||
} | ||
} | ||
|
||
public void performAnimationTick(double timestamp) { | ||
try { | ||
int currentTotalTicks = variableData.getTotalTicks(); | ||
int numOfTicksToRun = (currentTotalTicks - this.lastTotalTickCount); | ||
this.lastTotalTickCount = currentTotalTicks; | ||
|
||
// Catch up with ticks, if we are behind. | ||
while ((numOfTicksToRun-- > 0) && (variableData.getTotalTicks() == currentTotalTicks)) { | ||
// Perform one animation tick. | ||
interpreter.animationTick(); | ||
} | ||
|
||
requestNextAnimationFrame(); | ||
|
||
} catch (QuitAction qa) { | ||
// The user has quit the game, so notify the UI thread of this. | ||
postObject("QuitGame", JavaScriptObject.createObject()); | ||
} | ||
} | ||
|
||
public native void exportPerformAnimationTick() /*-{ | ||
var that = this; | ||
$self.performAnimationTick = $entry(function(timestamp) { | ||
[email protected]::performAnimationTick(D)(timestamp); | ||
}); | ||
}-*/; | ||
|
||
private native void requestNextAnimationFrame()/*-{ | ||
$self.requestAnimationFrame($self.performAnimationTick); | ||
}-*/; | ||
|
||
private native String getEventType(JavaScriptObject obj)/*-{ | ||
return obj.name; | ||
}-*/; | ||
|
@@ -176,11 +202,17 @@ protected final void setOnMessage(MessageHandler messageHandler) { | |
|
||
@Override | ||
public void onWorkerLoad() { | ||
exportPerformAnimationTick(); | ||
|
||
this.scope = DedicatedWorkerGlobalScope.get(); | ||
this.opfsGameFiles = new OPFSGameFiles(); | ||
|
||
this.importScript("/opfs-saved-games.js"); | ||
|
||
this.setOnMessage(this); | ||
} | ||
|
||
private final native void logToJSConsole(String message)/*-{ | ||
console.log(message); | ||
}-*/; | ||
} |