Resource -> Res, Ref->Com

This commit is contained in:
Carter Anderson 2020-05-13 17:52:47 -07:00
parent fb8f9e8636
commit 6381611e89
32 changed files with 153 additions and 154 deletions

View file

@ -1,4 +1,4 @@
use legion::prelude::{ResourceMut, Resources}; use legion::prelude::{ResMut, Resources};
use std::marker::PhantomData; use std::marker::PhantomData;
struct EventInstance<T> { struct EventInstance<T> {
@ -205,7 +205,7 @@ impl<T> Events<T> {
} }
/// A system that calls [Events::update] once per frame. /// A system that calls [Events::update] once per frame.
pub fn update_system(mut events: ResourceMut<Self>) { pub fn update_system(mut events: ResMut<Self>) {
events.update(); events.update();
} }

View file

@ -76,8 +76,8 @@ impl<T> Assets<T> {
} }
pub fn asset_event_system( pub fn asset_event_system(
mut events: ResourceMut<Events<AssetEvent<T>>>, mut events: ResMut<Events<AssetEvent<T>>>,
mut assets: ResourceMut<Assets<T>>, mut assets: ResMut<Assets<T>>,
) { ) {
events.extend(assets.events.drain()) events.extend(assets.events.drain())
} }

View file

@ -32,10 +32,10 @@ impl Time {
} }
} }
pub fn start_timer_system(mut time: ResourceMut<Time>) { pub fn start_timer_system(mut time: ResMut<Time>) {
time.start(); time.start();
} }
pub fn stop_timer_system(mut time: ResourceMut<Time>) { pub fn stop_timer_system(mut time: ResMut<Time>) {
time.stop(); time.stop();
} }

View file

@ -1,7 +1,7 @@
use crate::{Diagnostic, DiagnosticId, Diagnostics}; use crate::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_app::AppPlugin; use bevy_app::AppPlugin;
use bevy_core::time::Time; use bevy_core::time::Time;
use legion::prelude::{IntoSystem, Resource, ResourceMut}; use legion::prelude::{IntoSystem, Res, ResMut};
#[derive(Default)] #[derive(Default)]
pub struct FrameTimeDiagnosticsPlugin; pub struct FrameTimeDiagnosticsPlugin;
@ -18,12 +18,12 @@ impl FrameTimeDiagnosticsPlugin {
pub const FRAME_TIME: DiagnosticId = pub const FRAME_TIME: DiagnosticId =
DiagnosticId::from_u128(54021991829115352065418785002088010276); DiagnosticId::from_u128(54021991829115352065418785002088010276);
pub fn setup_system(mut diagnostics: ResourceMut<Diagnostics>) { pub fn setup_system(mut diagnostics: ResMut<Diagnostics>) {
diagnostics.add(Diagnostic::new(Self::FRAME_TIME, "frame_time", 10)); diagnostics.add(Diagnostic::new(Self::FRAME_TIME, "frame_time", 10));
diagnostics.add(Diagnostic::new(Self::FPS, "fps", 10)); diagnostics.add(Diagnostic::new(Self::FPS, "fps", 10));
} }
pub fn diagnostic_system(mut diagnostics: ResourceMut<Diagnostics>, time: Resource<Time>) { pub fn diagnostic_system(mut diagnostics: ResMut<Diagnostics>, time: Res<Time>) {
if time.delta_seconds_f64 == 0.0 { if time.delta_seconds_f64 == 0.0 {
return; return;
} }

View file

@ -65,9 +65,9 @@ impl PrintDiagnosticsPlugin {
} }
pub fn print_diagnostics_system( pub fn print_diagnostics_system(
mut state: ResourceMut<PrintDiagnosticsState>, mut state: ResMut<PrintDiagnosticsState>,
time: Resource<Time>, time: Res<Time>,
diagnostics: Resource<Diagnostics>, diagnostics: Res<Diagnostics>,
) { ) {
state.elapsed += time.delta_seconds_f64; state.elapsed += time.delta_seconds_f64;
if state.elapsed >= state.wait_seconds { if state.elapsed >= state.wait_seconds {
@ -87,9 +87,9 @@ impl PrintDiagnosticsPlugin {
} }
pub fn print_diagnostics_debug_system( pub fn print_diagnostics_debug_system(
mut state: ResourceMut<PrintDiagnosticsState>, mut state: ResMut<PrintDiagnosticsState>,
time: Resource<Time>, time: Res<Time>,
diagnostics: Resource<Diagnostics>, diagnostics: Res<Diagnostics>,
) { ) {
state.elapsed += time.delta_seconds_f64; state.elapsed += time.delta_seconds_f64;
if state.elapsed >= state.wait_seconds { if state.elapsed >= state.wait_seconds {

View file

@ -4,8 +4,8 @@ use legion::prelude::*;
pub fn exit_on_esc_system(resources: &mut Resources) -> Box<dyn Schedulable> { pub fn exit_on_esc_system(resources: &mut Resources) -> Box<dyn Schedulable> {
let mut keyboard_input_event_reader = resources.get_event_reader::<KeyboardInput>(); let mut keyboard_input_event_reader = resources.get_event_reader::<KeyboardInput>();
(move |keyboard_input_events: Resource<Events<KeyboardInput>>, (move |keyboard_input_events: Res<Events<KeyboardInput>>,
mut app_exit_events: ResourceMut<Events<AppExit>>| { mut app_exit_events: ResMut<Events<AppExit>>| {
for event in keyboard_input_event_reader.iter(&keyboard_input_events) { for event in keyboard_input_event_reader.iter(&keyboard_input_events) {
if let Some(virtual_key_code) = event.virtual_key_code { if let Some(virtual_key_code) = event.virtual_key_code {
if event.state == ElementState::Pressed if event.state == ElementState::Pressed

View file

@ -21,8 +21,7 @@ mod zip;
pub mod prelude { pub mod prelude {
pub use crate::{ pub use crate::{
// used by system_fn borrow::{Ref as Com, RefMut as ComMut},
borrow::{Ref, RefMut},
command::CommandBuffer, command::CommandBuffer,
entity::Entity, entity::Entity,
event::Event, event::Event,

View file

@ -8,7 +8,7 @@ mod system_fn_types;
pub use bit_set; pub use bit_set;
pub use system::*; pub use system::*;
pub use system_fn::*; pub use system_fn::*;
pub use system_fn_types::{Resource, ResourceMut}; pub use system_fn_types::{Res, ResMut};
pub mod prelude { pub mod prelude {
pub use crate::{ pub use crate::{
@ -17,8 +17,8 @@ pub mod prelude {
resource::{ResourceSet, Resources}, resource::{ResourceSet, Resources},
schedule::{Executor, Runnable, Schedulable, Schedule}, schedule::{Executor, Runnable, Schedulable, Schedule},
IntoSystem, IntoSystem,
Resource, Res,
ResourceMut, ResMut,
System, System,
SystemBuilder, SystemBuilder,
}; };

View file

@ -269,7 +269,7 @@ impl_system_variants![(R1, r1), (R2, r2), (R3, r3), (R4, r4), (R5, r5), (R6, r6)
mod tests { mod tests {
use crate::{ use crate::{
resource::Resources, resource::Resources,
system_fn_types::{Resource, ResourceMut}, system_fn_types::{Res, ResMut},
IntoSystem, IntoSystem,
}; };
use legion_core::{ use legion_core::{
@ -306,7 +306,7 @@ mod tests {
} }
({ ({
|x: Resource<A>, y: Ref<Y>, mut z: RefMut<A>| { |x: Res<A>, y: Ref<Y>, mut z: RefMut<A>| {
z.0 += 1; z.0 += 1;
println!("{} {} {}", x.0, y.0, z.0); println!("{} {} {}", x.0, y.0, z.0);
} }
@ -316,7 +316,7 @@ mod tests {
let mut system = read_write_system.system(); let mut system = read_write_system.system();
system.run(&mut world, &mut resources); system.run(&mut world, &mut resources);
fn resource_system(a: Resource<A>, x: Ref<X>, y: Ref<Y>) { fn resource_system(a: Res<A>, x: Ref<X>, y: Ref<Y>) {
println!("{} {} {}", a.0, x.0, y.0); println!("{} {} {}", a.0, x.0, y.0);
} }
@ -330,14 +330,14 @@ mod tests {
let mut system = empty_system_mut.system(); let mut system = empty_system_mut.system();
system.run(&mut world, &mut resources); system.run(&mut world, &mut resources);
fn resource_system_mut(mut a: ResourceMut<A>, x: Ref<X>, y: Ref<Y>) { fn resource_system_mut(mut a: ResMut<A>, x: Ref<X>, y: Ref<Y>) {
a.0 += 1; a.0 += 1;
println!("{} {} {}", a.0, x.0, y.0); println!("{} {} {}", a.0, x.0, y.0);
} }
let mut system = resource_system_mut.system(); let mut system = resource_system_mut.system();
system.run(&mut world, &mut resources); system.run(&mut world, &mut resources);
fn command_buffer_system(command_buffer: &mut CommandBuffer, mut a: ResourceMut<A>) { fn command_buffer_system(command_buffer: &mut CommandBuffer, mut a: ResMut<A>) {
a.0 += 1; a.0 += 1;
command_buffer.insert((), vec![(X(1), Y(1)), (X(2), Y(2))]); command_buffer.insert((), vec![(X(1), Y(1)), (X(2), Y(2))]);
println!("{}", a.0); println!("{}", a.0);
@ -348,7 +348,7 @@ mod tests {
#[test] #[test]
fn test_resource_system_fn() { fn test_resource_system_fn() {
fn my_system(mut a: ResourceMut<A>, x: Ref<X>, mut y: RefMut<Y>) { fn my_system(mut a: ResMut<A>, x: Ref<X>, mut y: RefMut<Y>) {
if a.0 == 0 { if a.0 == 0 {
assert_eq!(*a, A(0)); assert_eq!(*a, A(0));
assert_eq!(*x, X(2)); assert_eq!(*x, X(2));

View file

@ -17,21 +17,21 @@ use std::{
}; };
use tracing::{debug, info, span, Level}; use tracing::{debug, info, span, Level};
#[derive(Debug)] #[derive(Debug)]
pub struct Resource<'a, T: 'a> { pub struct Res<'a, T: 'a> {
#[allow(dead_code)] #[allow(dead_code)]
// held for drop impl // held for drop impl
_marker: PhantomData<&'a ()>, _marker: PhantomData<&'a ()>,
value: *const T, value: *const T,
} }
unsafe impl<'a, T: resource::Resource> Send for Resource<'a, T> {} unsafe impl<'a, T: resource::Resource> Send for Res<'a, T> {}
unsafe impl<'a, T: resource::Resource> Sync for Resource<'a, T> {} unsafe impl<'a, T: resource::Resource> Sync for Res<'a, T> {}
impl<'a, T: 'a> Clone for Resource<'a, T> { impl<'a, T: 'a> Clone for Res<'a, T> {
#[inline(always)] #[inline(always)]
fn clone(&self) -> Self { Resource::new(self.value) } fn clone(&self) -> Self { Res::new(self.value) }
} }
impl<'a, T: 'a> Resource<'a, T> { impl<'a, T: 'a> Res<'a, T> {
#[inline(always)] #[inline(always)]
fn new(resource: *const T) -> Self { fn new(resource: *const T) -> Self {
Self { Self {
@ -41,37 +41,37 @@ impl<'a, T: 'a> Resource<'a, T> {
} }
#[inline(always)] #[inline(always)]
pub fn map<K: 'a, F: FnMut(&T) -> &K>(&self, mut f: F) -> Resource<'a, K> { pub fn map<K: 'a, F: FnMut(&T) -> &K>(&self, mut f: F) -> Res<'a, K> {
Resource::new(f(&self)) Res::new(f(&self))
} }
} }
impl<'a, T: 'a> Deref for Resource<'a, T> { impl<'a, T: 'a> Deref for Res<'a, T> {
type Target = T; type Target = T;
#[inline(always)] #[inline(always)]
fn deref(&self) -> &Self::Target { unsafe { &*self.value } } fn deref(&self) -> &Self::Target { unsafe { &*self.value } }
} }
impl<'a, T: 'a> AsRef<T> for Resource<'a, T> { impl<'a, T: 'a> AsRef<T> for Res<'a, T> {
#[inline(always)] #[inline(always)]
fn as_ref(&self) -> &T { unsafe { &*self.value } } fn as_ref(&self) -> &T { unsafe { &*self.value } }
} }
impl<'a, T: 'a> std::borrow::Borrow<T> for Resource<'a, T> { impl<'a, T: 'a> std::borrow::Borrow<T> for Res<'a, T> {
#[inline(always)] #[inline(always)]
fn borrow(&self) -> &T { unsafe { &*self.value } } fn borrow(&self) -> &T { unsafe { &*self.value } }
} }
impl<'a, T> PartialEq for Resource<'a, T> impl<'a, T> PartialEq for Res<'a, T>
where where
T: 'a + PartialEq, T: 'a + PartialEq,
{ {
fn eq(&self, other: &Self) -> bool { self.value == other.value } fn eq(&self, other: &Self) -> bool { self.value == other.value }
} }
impl<'a, T> Eq for Resource<'a, T> where T: 'a + Eq {} impl<'a, T> Eq for Res<'a, T> where T: 'a + Eq {}
impl<'a, T> PartialOrd for Resource<'a, T> impl<'a, T> PartialOrd for Res<'a, T>
where where
T: 'a + PartialOrd, T: 'a + PartialOrd,
{ {
@ -79,48 +79,48 @@ where
self.value.partial_cmp(&other.value) self.value.partial_cmp(&other.value)
} }
} }
impl<'a, T> Ord for Resource<'a, T> impl<'a, T> Ord for Res<'a, T>
where where
T: 'a + Ord, T: 'a + Ord,
{ {
fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.value.cmp(&other.value) } fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.value.cmp(&other.value) }
} }
impl<'a, T> Hash for Resource<'a, T> impl<'a, T> Hash for Res<'a, T>
where where
T: 'a + Hash, T: 'a + Hash,
{ {
fn hash<H: Hasher>(&self, state: &mut H) { self.value.hash(state); } fn hash<H: Hasher>(&self, state: &mut H) { self.value.hash(state); }
} }
impl<'a, T: resource::Resource> ResourceSet for Resource<'a, T> { impl<'a, T: resource::Resource> ResourceSet for Res<'a, T> {
type PreparedResources = Resource<'a, T>; type PreparedResources = Res<'a, T>;
unsafe fn fetch_unchecked(resources: &Resources) -> Self::PreparedResources { unsafe fn fetch_unchecked(resources: &Resources) -> Self::PreparedResources {
let resource = resources let resource = resources
.get::<T>() .get::<T>()
.unwrap_or_else(|| panic!("Failed to fetch resource!: {}", std::any::type_name::<T>())); .unwrap_or_else(|| panic!("Failed to fetch resource!: {}", std::any::type_name::<T>()));
Resource::new(resource.deref() as *const T) Res::new(resource.deref() as *const T)
} }
fn read_types() -> Vec<ResourceTypeId> { vec![ResourceTypeId::of::<T>()] } fn read_types() -> Vec<ResourceTypeId> { vec![ResourceTypeId::of::<T>()] }
fn write_types() -> Vec<ResourceTypeId> { Vec::new() } fn write_types() -> Vec<ResourceTypeId> { Vec::new() }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct ResourceMut<'a, T: 'a> { pub struct ResMut<'a, T: 'a> {
// held for drop impl // held for drop impl
_marker: PhantomData<&'a mut ()>, _marker: PhantomData<&'a mut ()>,
value: *mut T, value: *mut T,
} }
unsafe impl<'a, T: resource::Resource> Send for ResourceMut<'a, T> {} unsafe impl<'a, T: resource::Resource> Send for ResMut<'a, T> {}
unsafe impl<'a, T: resource::Resource> Sync for ResourceMut<'a, T> {} unsafe impl<'a, T: resource::Resource> Sync for ResMut<'a, T> {}
impl<'a, T: 'a> Clone for ResourceMut<'a, T> { impl<'a, T: 'a> Clone for ResMut<'a, T> {
#[inline(always)] #[inline(always)]
fn clone(&self) -> Self { ResourceMut::new(self.value) } fn clone(&self) -> Self { ResMut::new(self.value) }
} }
impl<'a, T: 'a> ResourceMut<'a, T> { impl<'a, T: 'a> ResMut<'a, T> {
#[inline(always)] #[inline(always)]
fn new(resource: *mut T) -> Self { fn new(resource: *mut T) -> Self {
Self { Self {
@ -130,42 +130,42 @@ impl<'a, T: 'a> ResourceMut<'a, T> {
} }
#[inline(always)] #[inline(always)]
pub fn map_into<K: 'a, F: FnMut(&mut T) -> K>(mut self, mut f: F) -> ResourceMut<'a, K> { pub fn map_into<K: 'a, F: FnMut(&mut T) -> K>(mut self, mut f: F) -> ResMut<'a, K> {
ResourceMut::new(&mut f(&mut self)) ResMut::new(&mut f(&mut self))
} }
} }
impl<'a, T: 'a> Deref for ResourceMut<'a, T> { impl<'a, T: 'a> Deref for ResMut<'a, T> {
type Target = T; type Target = T;
#[inline(always)] #[inline(always)]
fn deref(&self) -> &Self::Target { unsafe { &*self.value } } fn deref(&self) -> &Self::Target { unsafe { &*self.value } }
} }
impl<'a, T: 'a> DerefMut for ResourceMut<'a, T> { impl<'a, T: 'a> DerefMut for ResMut<'a, T> {
#[inline(always)] #[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut *self.value } } fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut *self.value } }
} }
impl<'a, T: 'a> AsRef<T> for ResourceMut<'a, T> { impl<'a, T: 'a> AsRef<T> for ResMut<'a, T> {
#[inline(always)] #[inline(always)]
fn as_ref(&self) -> &T { unsafe { &*self.value } } fn as_ref(&self) -> &T { unsafe { &*self.value } }
} }
impl<'a, T: 'a> std::borrow::Borrow<T> for ResourceMut<'a, T> { impl<'a, T: 'a> std::borrow::Borrow<T> for ResMut<'a, T> {
#[inline(always)] #[inline(always)]
fn borrow(&self) -> &T { unsafe { &*self.value } } fn borrow(&self) -> &T { unsafe { &*self.value } }
} }
impl<'a, T> PartialEq for ResourceMut<'a, T> impl<'a, T> PartialEq for ResMut<'a, T>
where where
T: 'a + PartialEq, T: 'a + PartialEq,
{ {
fn eq(&self, other: &Self) -> bool { self.value == other.value } fn eq(&self, other: &Self) -> bool { self.value == other.value }
} }
impl<'a, T> Eq for ResourceMut<'a, T> where T: 'a + Eq {} impl<'a, T> Eq for ResMut<'a, T> where T: 'a + Eq {}
impl<'a, T> PartialOrd for ResourceMut<'a, T> impl<'a, T> PartialOrd for ResMut<'a, T>
where where
T: 'a + PartialOrd, T: 'a + PartialOrd,
{ {
@ -173,28 +173,28 @@ where
self.value.partial_cmp(&other.value) self.value.partial_cmp(&other.value)
} }
} }
impl<'a, T> Ord for ResourceMut<'a, T> impl<'a, T> Ord for ResMut<'a, T>
where where
T: 'a + Ord, T: 'a + Ord,
{ {
fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.value.cmp(&other.value) } fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.value.cmp(&other.value) }
} }
impl<'a, T> Hash for ResourceMut<'a, T> impl<'a, T> Hash for ResMut<'a, T>
where where
T: 'a + Hash, T: 'a + Hash,
{ {
fn hash<H: Hasher>(&self, state: &mut H) { self.value.hash(state); } fn hash<H: Hasher>(&self, state: &mut H) { self.value.hash(state); }
} }
impl<'a, T: resource::Resource> ResourceSet for ResourceMut<'a, T> { impl<'a, T: resource::Resource> ResourceSet for ResMut<'a, T> {
type PreparedResources = ResourceMut<'a, T>; type PreparedResources = ResMut<'a, T>;
unsafe fn fetch_unchecked(resources: &Resources) -> Self::PreparedResources { unsafe fn fetch_unchecked(resources: &Resources) -> Self::PreparedResources {
let mut resource = resources let mut resource = resources
.get_mut::<T>() .get_mut::<T>()
.unwrap_or_else(|| panic!("Failed to fetch resource!: {}", std::any::type_name::<T>())); .unwrap_or_else(|| panic!("Failed to fetch resource!: {}", std::any::type_name::<T>()));
ResourceMut::new(resource.deref_mut() as *mut T) ResMut::new(resource.deref_mut() as *mut T)
} }
fn read_types() -> Vec<ResourceTypeId> { vec![ResourceTypeId::of::<T>()] } fn read_types() -> Vec<ResourceTypeId> { vec![ResourceTypeId::of::<T>()] }
fn write_types() -> Vec<ResourceTypeId> { Vec::new() } fn write_types() -> Vec<ResourceTypeId> { Vec::new() }

View file

@ -19,7 +19,7 @@ pub trait AsUniforms: Send + Sync + 'static {
fn get_vertex_buffer_descriptor() -> Option<&'static VertexBufferDescriptor>; fn get_vertex_buffer_descriptor() -> Option<&'static VertexBufferDescriptor>;
} }
pub fn shader_def_system<T>(uniforms: Ref<T>, mut renderable: RefMut<Renderable>) pub fn shader_def_system<T>(uniforms: Com<T>, mut renderable: ComMut<Renderable>)
where where
T: AsUniforms + Send + Sync + 'static, T: AsUniforms + Send + Sync + 'static,
{ {
@ -34,9 +34,9 @@ where
} }
pub fn asset_shader_def_system<T>( pub fn asset_shader_def_system<T>(
assets: Resource<Assets<T>>, assets: Res<Assets<T>>,
asset_handle: Ref<Handle<T>>, asset_handle: Com<Handle<T>>,
mut renderable: RefMut<Renderable>, mut renderable: ComMut<Renderable>,
) where ) where
T: AsUniforms + Send + Sync + 'static, T: AsUniforms + Send + Sync + 'static,
{ {

View file

@ -2,7 +2,7 @@ use crate::renderer::WgpuRenderResourceContext;
use bevy_app::{AppBuilder, AppPlugin}; use bevy_app::{AppBuilder, AppPlugin};
use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics}; use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_render::renderer::RenderResources; use bevy_render::renderer::RenderResources;
use legion::prelude::{IntoSystem, Resource, ResourceMut}; use legion::prelude::{IntoSystem, Res, ResMut};
#[derive(Default)] #[derive(Default)]
pub struct WgpuResourceDiagnosticsPlugin; pub struct WgpuResourceDiagnosticsPlugin;
@ -51,7 +51,7 @@ impl WgpuResourceDiagnosticsPlugin {
pub const RENDER_PIPELINES: DiagnosticId = pub const RENDER_PIPELINES: DiagnosticId =
DiagnosticId::from_u128(278527620040377353875091478462209885377); DiagnosticId::from_u128(278527620040377353875091478462209885377);
pub fn setup_system(mut diagnostics: ResourceMut<Diagnostics>) { pub fn setup_system(mut diagnostics: ResMut<Diagnostics>) {
diagnostics.add(Diagnostic::new( diagnostics.add(Diagnostic::new(
Self::WINDOW_SURFACES, Self::WINDOW_SURFACES,
"window_surfaces", "window_surfaces",
@ -93,8 +93,8 @@ impl WgpuResourceDiagnosticsPlugin {
} }
pub fn diagnostic_system( pub fn diagnostic_system(
mut diagnostics: ResourceMut<Diagnostics>, mut diagnostics: ResMut<Diagnostics>,
render_resources: Resource<RenderResources>, render_resources: Res<RenderResources>,
) { ) {
let render_resource_context = render_resources let render_resource_context = render_resources
.context .context

View file

@ -9,8 +9,8 @@ fn main() {
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut textures: ResourceMut<Assets<Texture>>, mut textures: ResMut<Assets<Texture>>,
mut materials: ResourceMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
let texture_path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/branding/icon.png"); let texture_path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/branding/icon.png");
let texture = Texture::load(TextureType::Png(texture_path.to_string())); let texture = Texture::load(TextureType::Png(texture_path.to_string()));

View file

@ -9,8 +9,8 @@ fn main() {
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
// load the mesh // load the mesh
let model_path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/models/Monkey.gltf"); let model_path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/models/Monkey.gltf");

View file

@ -11,15 +11,15 @@ fn main() {
} }
/// rotates the parent, which will result in the child also rotating /// rotates the parent, which will result in the child also rotating
fn rotator_system(time: Resource<Time>, _rotator: RefMut<Rotator>, mut rotation: RefMut<Rotation>) { fn rotator_system(time: Res<Time>, _rotator: ComMut<Rotator>, mut rotation: ComMut<Rotation>) {
rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds); rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds);
} }
/// set up a simple scene with a "parent" cube and a "child" cube /// set up a simple scene with a "parent" cube and a "child" cube
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let cube_handle = meshes.add(Mesh::from(shape::Cube)); let cube_handle = meshes.add(Mesh::from(shape::Cube));
let cube_material_handle = materials.add(StandardMaterial { let cube_material_handle = materials.add(StandardMaterial {

View file

@ -10,8 +10,8 @@ fn main() {
/// set up a simple scene /// set up a simple scene
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
// create a cube and a plane mesh // create a cube and a plane mesh
let cube_handle = meshes.add(Mesh::from(shape::Cube)); let cube_handle = meshes.add(Mesh::from(shape::Cube));

View file

@ -15,10 +15,10 @@ fn main() {
} }
fn move_cubes( fn move_cubes(
time: Resource<Time>, time: Res<Time>,
mut materials: ResourceMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
mut translation: RefMut<Translation>, mut translation: ComMut<Translation>,
material_handle: Ref<Handle<StandardMaterial>>, material_handle: Com<Handle<StandardMaterial>>,
) { ) {
let material = materials.get_mut(&material_handle).unwrap(); let material = materials.get_mut(&material_handle).unwrap();
translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds; translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds;
@ -27,8 +27,8 @@ fn move_cubes(
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let cube_handle = meshes.add(Mesh::from(shape::Cube)); let cube_handle = meshes.add(Mesh::from(shape::Cube));
let plane_handle = meshes.add(Mesh::from(shape::Plane { size: 10.0 })); let plane_handle = meshes.add(Mesh::from(shape::Plane { size: 10.0 }));

View file

@ -10,9 +10,9 @@ fn main() {
/// sets up a scene with textured entities /// sets up a scene with textured entities
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut textures: ResourceMut<Assets<Texture>>, mut textures: ResMut<Assets<Texture>>,
mut materials: ResourceMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
// load a texture // load a texture
let texture_path = concat!( let texture_path = concat!(

View file

@ -11,8 +11,8 @@ impl AppPlugin for ExamplePlugin {
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let cube_handle = meshes.add(Mesh::from(shape::Cube)); let cube_handle = meshes.add(Mesh::from(shape::Cube));
let cube_material_handle = materials.add(StandardMaterial { let cube_material_handle = materials.add(StandardMaterial {

View file

@ -40,7 +40,7 @@ struct PrintMessageState {
elapsed_time: f32, elapsed_time: f32,
} }
fn print_message_system(time: Resource<Time>, mut state: ResourceMut<PrintMessageState>) { fn print_message_system(time: Res<Time>, mut state: ResMut<PrintMessageState>) {
state.elapsed_time += time.delta_seconds; state.elapsed_time += time.delta_seconds;
if state.elapsed_time > state.duration.as_secs_f32() { if state.elapsed_time > state.duration.as_secs_f32() {
println!("{}", state.message); println!("{}", state.message);

View file

@ -17,7 +17,7 @@ fn main() {
pub const SYSTEM_ITERATION_COUNT: DiagnosticId = pub const SYSTEM_ITERATION_COUNT: DiagnosticId =
DiagnosticId::from_u128(337040787172757619024841343456040760896); DiagnosticId::from_u128(337040787172757619024841343456040760896);
fn setup_diagnostic_system(mut diagnostics: ResourceMut<Diagnostics>) { fn setup_diagnostic_system(mut diagnostics: ResMut<Diagnostics>) {
// Diagnostics must be initialized before measurements can be added. // Diagnostics must be initialized before measurements can be added.
// In general it's a good idea to set them up in a "startup system". // In general it's a good idea to set them up in a "startup system".
diagnostics.add(Diagnostic::new( diagnostics.add(Diagnostic::new(
@ -27,7 +27,7 @@ fn setup_diagnostic_system(mut diagnostics: ResourceMut<Diagnostics>) {
)); ));
} }
fn my_system(mut diagnostics: ResourceMut<Diagnostics>) { fn my_system(mut diagnostics: ResMut<Diagnostics>) {
// Add a measurement of 10.0 for our diagnostic each time this system runs // Add a measurement of 10.0 for our diagnostic each time this system runs
diagnostics.add_measurement(SYSTEM_ITERATION_COUNT, 10.0); diagnostics.add_measurement(SYSTEM_ITERATION_COUNT, 10.0);
} }

View file

@ -71,8 +71,8 @@ fn print_message_system() {
// Systems can also read and modify resources. This system starts a new "round" on each update: // Systems can also read and modify resources. This system starts a new "round" on each update:
// NOTE: "mut" denotes that the resource is "mutable" // NOTE: "mut" denotes that the resource is "mutable"
// Resource<GameRules> is read-only. ResourceMut<GameState> can modify the resource // Res<GameRules> is read-only. ResMut<GameState> can modify the resource
fn new_round_system(game_rules: Resource<GameRules>, mut game_state: ResourceMut<GameState>) { fn new_round_system(game_rules: Res<GameRules>, mut game_state: ResMut<GameState>) {
game_state.current_round += 1; game_state.current_round += 1;
println!( println!(
"Begin round {} of {}", "Begin round {} of {}",
@ -81,8 +81,8 @@ fn new_round_system(game_rules: Resource<GameRules>, mut game_state: ResourceMut
} }
// This system runs once for each entity with both the "Player" and "Score" component. // This system runs once for each entity with both the "Player" and "Score" component.
// NOTE: Ref<Player> is a read-only reference, whereas RefMut<Score> can modify the component // NOTE: Com<Player> is a read-only component Comerence, whereas ComMut<Score> can modify the component
fn score_system(player: Ref<Player>, mut score: RefMut<Score>) { fn score_system(player: Com<Player>, mut score: ComMut<Score>) {
let scored_a_point = random::<bool>(); let scored_a_point = random::<bool>();
if scored_a_point { if scored_a_point {
score.value += 1; score.value += 1;
@ -104,10 +104,10 @@ fn score_system(player: Ref<Player>, mut score: RefMut<Score>) {
// accesses the "GameRules" resource to determine if a player has won. // accesses the "GameRules" resource to determine if a player has won.
// NOTE: resources must always come before components in system functions // NOTE: resources must always come before components in system functions
fn score_check_system( fn score_check_system(
game_rules: Resource<GameRules>, game_rules: Res<GameRules>,
mut game_state: ResourceMut<GameState>, mut game_state: ResMut<GameState>,
player: Ref<Player>, player: Com<Player>,
score: Ref<Score>, score: Com<Score>,
) { ) {
if score.value == game_rules.winning_score { if score.value == game_rules.winning_score {
game_state.winning_player = Some(player.name.clone()); game_state.winning_player = Some(player.name.clone());
@ -117,9 +117,9 @@ fn score_check_system(
// This system ends the game if we meet the right conditions. This fires an AppExit event, which tells our // This system ends the game if we meet the right conditions. This fires an AppExit event, which tells our
// App to quit. Check out the "event.rs" example if you want to learn more about using events. // App to quit. Check out the "event.rs" example if you want to learn more about using events.
fn game_over_system( fn game_over_system(
game_rules: Resource<GameRules>, game_rules: Res<GameRules>,
game_state: Resource<GameState>, game_state: Res<GameState>,
mut app_exit_events: ResourceMut<Events<AppExit>>, mut app_exit_events: ResMut<Events<AppExit>>,
) { ) {
if let Some(ref player) = game_state.winning_player { if let Some(ref player) = game_state.winning_player {
println!("{} won the game!", player); println!("{} won the game!", player);
@ -175,8 +175,8 @@ fn startup_system(world: &mut World, resources: &mut Resources) {
// NOTE: Command buffers must always come before resources and components in system functions // NOTE: Command buffers must always come before resources and components in system functions
fn new_player_system( fn new_player_system(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
game_rules: Resource<GameRules>, game_rules: Res<GameRules>,
mut game_state: ResourceMut<GameState>, mut game_state: ResMut<GameState>,
) { ) {
// Randomly add a new player // Randomly add a new player
let add_new_player = random::<bool>(); let add_new_player = random::<bool>();
@ -232,7 +232,7 @@ fn thread_local_system(world: &mut World, resources: &mut Resources) {
#[allow(dead_code)] #[allow(dead_code)]
fn closure_system() -> Box<dyn Schedulable> { fn closure_system() -> Box<dyn Schedulable> {
let mut counter = 0; let mut counter = 0;
(move |player: Ref<Player>, score: Ref<Score>| { (move |player: Com<Player>, score: Com<Score>| {
println!("processed: {} {}", player.name, score.value); println!("processed: {} {}", player.name, score.value);
println!("this ran {} times", counter); println!("this ran {} times", counter);
counter += 1; counter += 1;
@ -250,7 +250,7 @@ struct State {
// NOTE: this doesn't do anything relevant to our game, it is just here for illustrative purposes // NOTE: this doesn't do anything relevant to our game, it is just here for illustrative purposes
#[allow(dead_code)] #[allow(dead_code)]
fn stateful_system(mut state: RefMut<State>, player: Ref<Player>, score: RefMut<Score>) { fn stateful_system(mut state: ComMut<State>, player: Com<Player>, score: ComMut<Score>) {
println!("processed: {} {}", player.name, score.value); println!("processed: {} {}", player.name, score.value);
println!("this ran {} times", state.counter); println!("this ran {} times", state.counter);
state.counter += 1; state.counter += 1;
@ -267,7 +267,7 @@ fn complex_system(resources: &mut Resources) -> Box<dyn Schedulable> {
SystemBuilder::new("complex_system") SystemBuilder::new("complex_system")
.read_resource::<GameState>() .read_resource::<GameState>()
.write_resource::<GameRules>() .write_resource::<GameRules>()
// this query is equivalent to the system we saw above: system(player: Ref<Player>, mut score: RefMut<Score>) // this query is equivalent to the system we saw above: system(player: Com<Player>, mut score: ComMut<Score>)
.with_query(<(Read<Player>, Write<Score>)>::query()) .with_query(<(Read<Player>, Write<Score>)>::query())
// this query only returns entities with a Player component that has changed since the last update // this query only returns entities with a Player component that has changed since the last update
.with_query(<Read<Player>>::query().filter(changed::<Player>())) .with_query(<Read<Player>>::query().filter(changed::<Player>()))
@ -307,7 +307,7 @@ fn main() {
.add_startup_system(startup_system) .add_startup_system(startup_system)
// my_system.system() calls converts normal rust functions into ECS systems: // my_system.system() calls converts normal rust functions into ECS systems:
.add_system(print_message_system.system()) .add_system(print_message_system.system())
// Systems that need a reference to Resources to be constructed can be added using "init_system": // Systems that need a Comerence to Resources to be constructed can be added using "init_system":
// .init_system(complex_system) // .init_system(complex_system)
// //
// SYSTEM EXECUTION ORDER // SYSTEM EXECUTION ORDER
@ -316,7 +316,7 @@ fn main() {
// For example, we want our "game over" system to execute after all other systems to ensure we don't // For example, we want our "game over" system to execute after all other systems to ensure we don't
// accidentally run the game for an extra round. // accidentally run the game for an extra round.
// //
// First, if a system writes a component or resource (RefMut / ResourceMut), it will force a synchronization. // First, if a system writes a component or resource (ComMut / ResMut), it will force a synchronization.
// Any systems that access the data type and were registered BEFORE the system will need to finish first. // Any systems that access the data type and were registered BEFORE the system will need to finish first.
// Any systems that were registered _after_ the system will need to wait for it to finish. This is a great // Any systems that were registered _after_ the system will need to wait for it to finish. This is a great
// default that makes everything "just work" as fast as possible without us needing to think about it ... provided // default that makes everything "just work" as fast as possible without us needing to think about it ... provided

View file

@ -22,9 +22,9 @@ struct EventTriggerState {
// sends MyEvent every second // sends MyEvent every second
fn event_trigger_system( fn event_trigger_system(
mut state: ResourceMut<EventTriggerState>, mut state: ResMut<EventTriggerState>,
mut my_events: ResourceMut<Events<MyEvent>>, mut my_events: ResMut<Events<MyEvent>>,
time: Resource<Time>, time: Res<Time>,
) { ) {
state.elapsed += time.delta_seconds; state.elapsed += time.delta_seconds;
if state.elapsed > 1.0 { if state.elapsed > 1.0 {
@ -43,8 +43,8 @@ struct EventListenerState {
// prints events as they come in // prints events as they come in
fn event_listener_system( fn event_listener_system(
mut state: ResourceMut<EventListenerState>, mut state: ResMut<EventListenerState>,
my_events: Resource<Events<MyEvent>>, my_events: Res<Events<MyEvent>>,
) { ) {
for my_event in state.my_event_reader.iter(&my_events) { for my_event in state.my_event_reader.iter(&my_events) {
println!("{}", my_event.message); println!("{}", my_event.message);

View file

@ -22,8 +22,8 @@ struct State {
/// adjusts move state based on keyboard input /// adjusts move state based on keyboard input
fn collect_input( fn collect_input(
mut state: ResourceMut<State>, mut state: ResMut<State>,
keyboard_input_events: Resource<Events<KeyboardInput>>, keyboard_input_events: Res<Events<KeyboardInput>>,
) { ) {
for event in state.event_reader.iter(&keyboard_input_events) { for event in state.event_reader.iter(&keyboard_input_events) {
match event { match event {
@ -48,10 +48,10 @@ fn collect_input(
/// moves our cube left when the "left" key is pressed. moves it right when the "right" key is pressed /// moves our cube left when the "left" key is pressed. moves it right when the "right" key is pressed
fn move_on_input( fn move_on_input(
state: Resource<State>, state: Res<State>,
time: Resource<Time>, time: Res<Time>,
mut translation: RefMut<Translation>, mut translation: ComMut<Translation>,
_: Ref<Handle<Mesh>>, _: Com<Handle<Mesh>>,
) { ) {
if state.moving_left { if state.moving_left {
translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds; translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds;
@ -65,8 +65,8 @@ fn move_on_input(
/// creates a simple scene /// creates a simple scene
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let cube_handle = meshes.add(Mesh::from(shape::Cube)); let cube_handle = meshes.add(Mesh::from(shape::Cube));
let cube_material_handle = materials.add(StandardMaterial { let cube_material_handle = materials.add(StandardMaterial {

View file

@ -19,9 +19,9 @@ struct State {
/// prints out mouse events as they come in /// prints out mouse events as they come in
fn mouse_input_system( fn mouse_input_system(
mut state: ResourceMut<State>, mut state: ResMut<State>,
mouse_button_input_events: Ref<Events<MouseButtonInput>>, mouse_button_input_events: Com<Events<MouseButtonInput>>,
mouse_motion_events: Ref<Events<MouseMotionInput>>, mouse_motion_events: Com<Events<MouseMotionInput>>,
) { ) {
for event in state for event in state
.mouse_button_event_reader .mouse_button_event_reader

View file

@ -15,11 +15,11 @@ struct MyMaterial {
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut pipelines: ResourceMut<Assets<PipelineDescriptor>>, mut pipelines: ResMut<Assets<PipelineDescriptor>>,
mut shaders: ResourceMut<Assets<Shader>>, mut shaders: ResMut<Assets<Shader>>,
mut meshes: ResourceMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<MyMaterial>>, mut materials: ResMut<Assets<MyMaterial>>,
mut render_graph: ResourceMut<RenderGraph>, mut render_graph: ResMut<RenderGraph>,
) { ) {
// create new shader pipeline and add to main pass in Render Graph // create new shader pipeline and add to main pass in Render Graph
let pipeline_handle = { let pipeline_handle = {

View file

@ -21,11 +21,11 @@ struct MyMaterial {
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut pipelines: ResourceMut<Assets<PipelineDescriptor>>, mut pipelines: ResMut<Assets<PipelineDescriptor>>,
mut shaders: ResourceMut<Assets<Shader>>, mut shaders: ResMut<Assets<Shader>>,
mut meshes: ResourceMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<MyMaterial>>, mut materials: ResMut<Assets<MyMaterial>>,
mut render_graph: ResourceMut<RenderGraph>, mut render_graph: ResMut<RenderGraph>,
) { ) {
// create new shader pipeline and add to main pass in Render Graph // create new shader pipeline and add to main pass in Render Graph
let pipeline_handle = { let pipeline_handle = {

View file

@ -10,8 +10,8 @@ fn main() {
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut textures: ResourceMut<Assets<Texture>>, mut textures: ResMut<Assets<Texture>>,
mut materials: ResourceMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
let font_path = concat!( let font_path = concat!(
env!("CARGO_MANIFEST_DIR"), env!("CARGO_MANIFEST_DIR"),

View file

@ -9,8 +9,8 @@ fn main() {
fn setup( fn setup(
command_buffer: &mut CommandBuffer, command_buffer: &mut CommandBuffer,
mut textures: ResourceMut<Assets<Texture>>, mut textures: ResMut<Assets<Texture>>,
mut materials: ResourceMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
// TODO: "background" 3D temporarily disabled until depth mismatch is fixed // TODO: "background" 3D temporarily disabled until depth mismatch is fixed
// let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap(); // let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();

View file

@ -10,10 +10,10 @@ fn main() {
} }
fn placement_system( fn placement_system(
time: Resource<Time>, time: Res<Time>,
materials: Resource<Assets<ColorMaterial>>, materials: Res<Assets<ColorMaterial>>,
mut node: RefMut<Node>, mut node: ComMut<Node>,
material_handle: Ref<Handle<ColorMaterial>>, material_handle: Com<Handle<ColorMaterial>>,
) { ) {
let material = materials.get(&material_handle).unwrap(); let material = materials.get(&material_handle).unwrap();
if material.color.r > 0.2 { if material.color.r > 0.2 {
@ -21,7 +21,7 @@ fn placement_system(
} }
} }
fn setup(command_buffer: &mut CommandBuffer, mut materials: ResourceMut<Assets<ColorMaterial>>) { fn setup(command_buffer: &mut CommandBuffer, mut materials: ResMut<Assets<ColorMaterial>>) {
let mut builder = command_buffer.build(); let mut builder = command_buffer.build();
builder.add_entity(Camera2dEntity::default()); builder.add_entity(Camera2dEntity::default());

View file

@ -7,7 +7,7 @@ fn main() {
.run(); .run();
} }
fn setup(mut create_window_events: ResourceMut<Events<CreateWindow>>) { fn setup(mut create_window_events: ResMut<Events<CreateWindow>>) {
// sends out a "CreateWindow" event, which will be received by the windowing backend // sends out a "CreateWindow" event, which will be received by the windowing backend
create_window_events.send(CreateWindow { create_window_events.send(CreateWindow {
descriptor: WindowDescriptor { descriptor: WindowDescriptor {

View file

@ -46,7 +46,7 @@ pub use crate::{
AddDefaultPlugins, AddDefaultPlugins,
}; };
pub use legion::{ pub use legion::{
borrow::{Ref, RefMut}, borrow::{Ref as Com, RefMut as ComMut},
command::CommandBuffer, command::CommandBuffer,
entity::Entity, entity::Entity,
event::Event as LegionEvent, event::Event as LegionEvent,
@ -56,7 +56,7 @@ pub use legion::{
bit_set::BitSet, bit_set::BitSet,
resource::{ResourceSet, Resources}, resource::{ResourceSet, Resources},
schedule::{Executor, Runnable, Schedulable, Schedule}, schedule::{Executor, Runnable, Schedulable, Schedule},
IntoSystem, Resource, ResourceMut, SubWorld, SystemBuilder, IntoSystem, Res, ResMut, SubWorld, SystemBuilder,
}, },
world::{Universe, World}, world::{Universe, World},
}; };