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