-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
543 additions
and
471 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use bevy::{ecs::component::ComponentId, prelude::*, utils::HashSet}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use crate::runtime::{ | ||
types::{JsComponentInfo, JsEntity}, | ||
wasm::{BevyModJsScripting, WORLD_RID}, | ||
}; | ||
|
||
#[wasm_bindgen] | ||
impl BevyModJsScripting { | ||
pub fn op_world_tostring(&self, rid: u32) -> String { | ||
assert_eq!(rid, WORLD_RID); | ||
let state = self.state(); | ||
let world = &state.world; | ||
|
||
format!("{world:?}") | ||
} | ||
|
||
pub fn op_world_components(&self, rid: u32) -> Result<JsValue, JsValue> { | ||
assert_eq!(rid, WORLD_RID); | ||
let state = self.state(); | ||
let world = &state.world; | ||
|
||
let resource_components: HashSet<ComponentId> = | ||
world.archetypes().resource().components().collect(); | ||
|
||
let infos = world | ||
.components() | ||
.iter() | ||
.filter(|info| !resource_components.contains(&info.id())) | ||
.map(JsComponentInfo::from) | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(serde_wasm_bindgen::to_value(&infos)?) | ||
} | ||
|
||
pub fn op_world_resources(&self, rid: u32) -> Result<JsValue, JsValue> { | ||
assert_eq!(rid, WORLD_RID); | ||
let state = self.state(); | ||
let world = &state.world; | ||
|
||
let infos = world | ||
.archetypes() | ||
.resource() | ||
.components() | ||
.map(|id| world.components().get_info(id).unwrap()) | ||
.map(JsComponentInfo::from) | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(serde_wasm_bindgen::to_value(&infos)?) | ||
} | ||
|
||
pub fn op_world_entities(&self, rid: u32) -> Result<JsValue, JsValue> { | ||
assert_eq!(rid, WORLD_RID); | ||
let mut state = self.state(); | ||
let world = &mut state.world; | ||
|
||
let entities = world | ||
.query::<Entity>() | ||
.iter(world) | ||
.map(JsEntity::from) | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(serde_wasm_bindgen::to_value(&entities)?) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
mod info; | ||
mod query; | ||
mod resource; | ||
mod value; |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use bevy::ecs::component::ComponentId; | ||
use bevy_ecs_dynamic::reflect_value_ref::{query::EcsValueRefQuery, ReflectValueRef}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use crate::runtime::{ | ||
types::QueryDescriptor, | ||
wasm::{BevyModJsScripting, JsQueryItem, JsRuntimeState, JsValueRef, WORLD_RID}, | ||
}; | ||
|
||
#[wasm_bindgen] | ||
impl BevyModJsScripting { | ||
pub fn op_world_query(&self, rid: u32, query: JsValue) -> Result<JsValue, JsValue> { | ||
assert_eq!(rid, WORLD_RID); | ||
let mut state = self.state(); | ||
let JsRuntimeState { | ||
world, value_refs, .. | ||
} = &mut *state; | ||
|
||
let descriptor: QueryDescriptor = serde_wasm_bindgen::from_value(query)?; | ||
|
||
let components: Vec<ComponentId> = descriptor | ||
.components | ||
.iter() | ||
.map(ComponentId::from) | ||
.collect(); | ||
|
||
let mut query = EcsValueRefQuery::new(world, &components); | ||
let results = query | ||
.iter(world) | ||
.map(|item| { | ||
let components = item | ||
.items | ||
.into_iter() | ||
.map(|value| JsValueRef { | ||
key: value_refs.insert(ReflectValueRef::ecs_ref(value)), | ||
function: None, | ||
}) | ||
.collect(); | ||
|
||
JsQueryItem { | ||
entity: item.entity.into(), | ||
components, | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(serde_wasm_bindgen::to_value(&results)?) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use bevy_ecs_dynamic::reflect_value_ref::{EcsValueRef, ReflectValueRef}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use crate::runtime::{ | ||
types::ComponentIdOrBevyType, | ||
wasm::{BevyModJsScripting, JsRuntimeState, JsValueRef, WORLD_RID}, | ||
ToJsErr, | ||
}; | ||
|
||
#[wasm_bindgen] | ||
impl BevyModJsScripting { | ||
pub fn op_world_get_resource( | ||
&self, | ||
rid: u32, | ||
component_id: JsValue, | ||
) -> Result<JsValue, JsValue> { | ||
assert_eq!(rid, WORLD_RID); | ||
let mut state = self.state(); | ||
let JsRuntimeState { | ||
world, value_refs, .. | ||
} = &mut *state; | ||
|
||
let component_id: ComponentIdOrBevyType = serde_wasm_bindgen::from_value(component_id)?; | ||
let component_id = component_id.component_id(world).to_js_error()?; | ||
|
||
let value_ref = EcsValueRef::resource(world, component_id).to_js_error()?; | ||
|
||
let value_ref = JsValueRef { | ||
key: value_refs.insert(ReflectValueRef::ecs_ref(value_ref)), | ||
function: None, | ||
}; | ||
|
||
Ok(serde_wasm_bindgen::to_value(&value_ref)?) | ||
} | ||
} |
Oops, something went wrong.