Skip to content

Commit

Permalink
typed resource access
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Aug 13, 2022
1 parent 63d262c commit 13f8f1f
Show file tree
Hide file tree
Showing 6 changed files with 638 additions and 10 deletions.
13 changes: 10 additions & 3 deletions assets/scripts/breakout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ function filterComponentInfos(infos: ComponentInfo[], prefix: string): string[]

let firstIteration = true;
let i = 0;

type Scoreboard = {
score: number;
};
const Scoreboard: BevyType<Scoreboard> = { typeName: "breakout::Scoreboard" };

function run() {
if (firstIteration) {
firstIteration = false;
Expand All @@ -17,10 +23,11 @@ function run() {
}



i++;
if (i % 60 == 0) {
let timeId = world.resources.find(info => info.name == "bevy_time::time::Time").id;
let time = world.resource(timeId);
info(time.delta_seconds.toString());
let time = world.resource(Scoreboard);
time.score += 1;
info(time.score);
}
}
2 changes: 2 additions & 0 deletions examples/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn main() {
.register_type::<Collider>()
.register_type::<Brick>()
.register_type::<Time>()
.register_type::<Scoreboard>()
.run();
}

Expand Down Expand Up @@ -175,6 +176,7 @@ impl WallBundle {
}

// This resource tracks the game's score
#[derive(Reflect)]
struct Scoreboard {
score: usize,
}
Expand Down
9 changes: 4 additions & 5 deletions src/runtime/ecs/resource.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use bevy::ecs::component::ComponentId;
use bevy_ecs_dynamic::reflect_value_ref::EcsValueRef;
use deno_core::{error::AnyError, op, v8, OpState, ResourceId};

use crate::runtime::WorldResource;

use super::{
types::JsComponentId,
types::ComponentIdOrBevyType,
v8_utils::{create_value_ref_object, ValueRefObject},
};

Expand All @@ -14,15 +13,15 @@ pub fn op_world_get_resource(
state: &mut OpState,
scope: &mut v8::HandleScope,
world_rid: ResourceId,
component_id: JsComponentId,
component_id: ComponentIdOrBevyType,
) -> Result<Option<ValueRefObject<'static>>, AnyError> {
let world = state.resource_table.get::<WorldResource>(world_rid)?;
let world = world.world.borrow();

let component_id = ComponentId::from(&component_id);
let component_id = component_id.component_id(&world)?;

// todo: implement world.contains_resource_by_id
if world.get_resource_by_id(component_id).is_none() {
dbg!();
return Ok(None);
}
let value_ref = EcsValueRef::resource(&world, component_id)?;
Expand Down
37 changes: 35 additions & 2 deletions src/runtime/ecs/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
use bevy::{
ecs::component::{ComponentId, ComponentInfo},
prelude::Entity,
prelude::{Entity, World},
};
use bevy_reflect::TypeRegistryArc;
use deno_core::error::AnyError;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum ComponentIdOrBevyType {
ComponentId(JsComponentId),
Type {
#[serde(rename = "typeName")]
type_name: String,
},
}

impl ComponentIdOrBevyType {
pub fn component_id(self, world: &World) -> Result<ComponentId, AnyError> {
match self {
ComponentIdOrBevyType::ComponentId(id) => Ok(ComponentId::from(&id)),
ComponentIdOrBevyType::Type { type_name } => {
let type_registry = world.resource::<TypeRegistryArc>().read();
let registration = type_registry.get_with_name(&type_name).ok_or_else(|| {
anyhow::anyhow!("`{type_name}` does not exist in the type registry")
})?;
let type_id = registration.type_id();
let component_id = world
.components()
.get_id(type_id)
.or_else(|| world.components().get_resource_id(type_id))
.ok_or_else(|| anyhow::anyhow!("`{type_name}` is not a component"))?;
Ok(component_id)
}
}
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct JsComponentId {
pub index: usize,
}
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ interface Value {
[path: string | number]: Value | Primitive | undefined,
}

type BevyType<T> = {
typeName: string;
};

declare class World {
get components(): ComponentInfo[];
get resources(): ComponentInfo[];
get entities(): Entity[];

resource(componentId: ComponentId): Value | null;
resource<T>(type: BevyType<T>): T | null;

query(descriptor: QueryDescriptor): QueryItem[];
}
Expand Down
Loading

0 comments on commit 13f8f1f

Please sign in to comment.