Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser Runtime Implementation #11

Merged
merged 14 commits into from
Sep 2, 2022
Merged
Prev Previous commit
Next Next commit
Implement More WASM Runtime Ops
Implements all the remaining ops for the browser runtime, other than
op_value_ref_call.
zicklag committed Aug 15, 2022
commit 6981dd0f5f06f62e7babb49af3f3e205b057db54
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ wasm_mutex = "0.1.4"
js-sys = "0.3.59"
wasm-bindgen = { version = "0.2.82", features = ["enable-interning"] }
serde-wasm-bindgen = "0.4.3"
slab = "0.4.7"

[dev-dependencies]
bevy = { version = "0.8.0", default-features = false, features = ["render", "bevy_winit", "x11", "filesystem_watcher"] }
28 changes: 16 additions & 12 deletions assets/scripts/breakout.ts
Original file line number Diff line number Diff line change
@@ -15,17 +15,21 @@ type Scoreboard = {
};
const Scoreboard: BevyType<Scoreboard> = { typeName: "breakout::Scoreboard" };

export default {
update() {
i++;
function run() {
i++;
if (i % 60 == 0) {
let score = world.resource(Scoreboard);
score.score += 1;
info(score.score);
}

if (firstIteration) {
firstIteration = false;
// info("Components: " + filterComponentInfos(world.components, "bevy_transform::"));
// info("Resources: " + filterComponentInfos(world.resources, "breakout::").join(", "));
}
}

if (firstIteration) {
firstIteration = false;
for (const entity of world.entities) {
info!(entity);
}
// info("Components: " + filterComponentInfos(world.components, "bevy_transform::"));
// info("Resources: " + filterComponentInfos(world.resources, "breakout::").join(", "));
}
},
export default {
update: run,
};
39 changes: 20 additions & 19 deletions assets/scripts/headless.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
function filterComponentInfos(
infos: ComponentInfo[],
prefix: string
): string[] {
return infos
.filter((info) => info.name.startsWith(prefix))
.map((info) => info.name.replace(prefix, ""));
}
let i = 0;

function componentId(name) {
let id = world.components.find((info) => info.name === name);
if (!id) throw new Error(`component id for ${name} not found`);
return id.id;
}
type Scoreboard = {
score: number;
extra: ExtraData;
};
type ExtraData = {
name: string;
};
const Scoreboard: BevyType<Scoreboard> = { typeName: "headless::Scoreboard" };

let firstIteration = true;
info("Loaded");

export default {
update() {
if (firstIteration) {
firstIteration = false;
if (i == 0) {
info(world.resources);
}

for (const entity of world.entities) {
info(entity);
}
if (i % 3 == 0) {
let score = world.resource(Scoreboard);
info(score.toString());
info(score.score);
info(score.extra.name);
}

i++;
},
};
25 changes: 25 additions & 0 deletions examples/headless.rs
Original file line number Diff line number Diff line change
@@ -24,9 +24,34 @@ fn main() {
.add_startup_system(setup)
.add_js_system("scripts/headless.ts")
.register_type::<TestComponent>()
.register_type::<Scoreboard>()
.insert_resource(Scoreboard::default())
.run();
}

// This resource tracks the game's score
#[derive(Reflect)]
struct Scoreboard {
score: usize,
extra: ExtraData,
}

#[derive(Reflect)]
struct ExtraData {
name: String,
}

impl Default for Scoreboard {
fn default() -> Self {
Self {
score: 7,
extra: ExtraData {
name: "Awesome".into(),
},
}
}
}

#[derive(Component, Reflect)]
struct TestComponent {
value: String,
1 change: 1 addition & 0 deletions src/runtime/js/ecs.js
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@
).map(({ entity, components }) => ({
entity,
components: components.map(wrapValueRef),
test: components,
}));
}
}
7 changes: 1 addition & 6 deletions src/runtime/native/ecs/query.rs
Original file line number Diff line number Diff line change
@@ -2,17 +2,12 @@ use super::{
v8_utils::{create_value_ref_object, ValueRefObject},
WorldResource,
};
use crate::runtime::types::{JsComponentId, JsEntity};
use crate::runtime::types::{JsComponentId, JsEntity, QueryDescriptor};
use bevy::ecs::component::ComponentId;
use bevy_ecs_dynamic::reflect_value_ref::query::EcsValueRefQuery;
use deno_core::{error::AnyError, op, v8, OpState, ResourceId};
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
pub struct QueryDescriptor {
components: Vec<JsComponentId>,
}

#[derive(Serialize)]
pub struct JsQueryItem {
entity: JsEntity,
5 changes: 5 additions & 0 deletions src/runtime/types.rs
Original file line number Diff line number Diff line change
@@ -82,3 +82,8 @@ impl From<Entity> for JsEntity {
}
}
}

#[derive(Deserialize)]
pub struct QueryDescriptor {
pub components: Vec<JsComponentId>,
}
Loading