[oden] Allow a "functional style" init/update/draw

This commit is contained in:
John Doty 2023-08-19 10:45:48 -07:00
parent 7fc786e2e7
commit 642ced45f8

View file

@ -47,6 +47,8 @@ pub struct ScriptContext {
update: Value,
draw: Value,
state: Value,
gfx: graphics::GraphicsAPI,
gfx_receive: Receiver<graphics::GraphicsCommand>,
@ -87,6 +89,8 @@ impl ScriptContext {
.get_export(&context, "draw")
.expect("Unable to fetch draw");
let state = context.undefined();
ScriptContext {
context,
@ -94,6 +98,8 @@ impl ScriptContext {
update,
draw,
state,
gfx,
gfx_receive,
@ -109,7 +115,8 @@ impl ScriptContext {
pub fn init(&mut self) {
let _span = span!("script init");
self.init
self.state = self
.init
.call(&self.context, &[])
.expect("Exception in init");
}
@ -140,8 +147,10 @@ impl ScriptContext {
// Now run the update function.
{
let _span = span!("javascript update");
self.update
.call(&self.context, &[])
let old_state = &self.state;
self.state = self
.update
.call(&self.context, &[old_state])
.expect("Exception in update");
}
}
@ -150,7 +159,7 @@ impl ScriptContext {
let _span = span!("script render");
self.draw
.call(&self.context, &[])
.call(&self.context, &[&self.state])
.expect("Exception in draw");
self.gfx.end_frame();