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;
struct EventInstance<T> {
@ -205,7 +205,7 @@ impl<T> Events<T> {
}
/// 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();
}

View file

@ -76,8 +76,8 @@ impl<T> Assets<T> {
}
pub fn asset_event_system(
mut events: ResourceMut<Events<AssetEvent<T>>>,
mut assets: ResourceMut<Assets<T>>,
mut events: ResMut<Events<AssetEvent<T>>>,
mut assets: ResMut<Assets<T>>,
) {
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();
}
pub fn stop_timer_system(mut time: ResourceMut<Time>) {
pub fn stop_timer_system(mut time: ResMut<Time>) {
time.stop();
}

View file

@ -1,7 +1,7 @@
use crate::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_app::AppPlugin;
use bevy_core::time::Time;
use legion::prelude::{IntoSystem, Resource, ResourceMut};
use legion::prelude::{IntoSystem, Res, ResMut};
#[derive(Default)]
pub struct FrameTimeDiagnosticsPlugin;
@ -18,12 +18,12 @@ impl FrameTimeDiagnosticsPlugin {
pub const FRAME_TIME: DiagnosticId =
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::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 {
return;
}

View file

@ -65,9 +65,9 @@ impl PrintDiagnosticsPlugin {
}
pub fn print_diagnostics_system(
mut state: ResourceMut<PrintDiagnosticsState>,
time: Resource<Time>,
diagnostics: Resource<Diagnostics>,
mut state: ResMut<PrintDiagnosticsState>,
time: Res<Time>,
diagnostics: Res<Diagnostics>,
) {
state.elapsed += time.delta_seconds_f64;
if state.elapsed >= state.wait_seconds {
@ -87,9 +87,9 @@ impl PrintDiagnosticsPlugin {
}
pub fn print_diagnostics_debug_system(
mut state: ResourceMut<PrintDiagnosticsState>,
time: Resource<Time>,
diagnostics: Resource<Diagnostics>,
mut state: ResMut<PrintDiagnosticsState>,
time: Res<Time>,
diagnostics: Res<Diagnostics>,
) {
state.elapsed += time.delta_seconds_f64;
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> {
let mut keyboard_input_event_reader = resources.get_event_reader::<KeyboardInput>();
(move |keyboard_input_events: Resource<Events<KeyboardInput>>,
mut app_exit_events: ResourceMut<Events<AppExit>>| {
(move |keyboard_input_events: Res<Events<KeyboardInput>>,
mut app_exit_events: ResMut<Events<AppExit>>| {
for event in keyboard_input_event_reader.iter(&keyboard_input_events) {
if let Some(virtual_key_code) = event.virtual_key_code {
if event.state == ElementState::Pressed

View file

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

View file

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

View file

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

View file

@ -17,21 +17,21 @@ use std::{
};
use tracing::{debug, info, span, Level};
#[derive(Debug)]
pub struct Resource<'a, T: 'a> {
pub struct Res<'a, T: 'a> {
#[allow(dead_code)]
// held for drop impl
_marker: PhantomData<&'a ()>,
value: *const T,
}
unsafe impl<'a, T: resource::Resource> Send for Resource<'a, T> {}
unsafe impl<'a, T: resource::Resource> Sync for Resource<'a, T> {}
impl<'a, T: 'a> Clone for Resource<'a, T> {
unsafe impl<'a, T: resource::Resource> Send for Res<'a, T> {}
unsafe impl<'a, T: resource::Resource> Sync for Res<'a, T> {}
impl<'a, T: 'a> Clone for Res<'a, T> {
#[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)]
fn new(resource: *const T) -> Self {
Self {
@ -41,37 +41,37 @@ impl<'a, T: 'a> Resource<'a, T> {
}
#[inline(always)]
pub fn map<K: 'a, F: FnMut(&T) -> &K>(&self, mut f: F) -> Resource<'a, K> {
Resource::new(f(&self))
pub fn map<K: 'a, F: FnMut(&T) -> &K>(&self, mut f: F) -> Res<'a, K> {
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;
#[inline(always)]
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)]
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)]
fn borrow(&self) -> &T { unsafe { &*self.value } }
}
impl<'a, T> PartialEq for Resource<'a, T>
impl<'a, T> PartialEq for Res<'a, T>
where
T: 'a + PartialEq,
{
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
T: 'a + PartialOrd,
{
@ -79,48 +79,48 @@ where
self.value.partial_cmp(&other.value)
}
}
impl<'a, T> Ord for Resource<'a, T>
impl<'a, T> Ord for Res<'a, T>
where
T: 'a + Ord,
{
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
T: 'a + Hash,
{
fn hash<H: Hasher>(&self, state: &mut H) { self.value.hash(state); }
}
impl<'a, T: resource::Resource> ResourceSet for Resource<'a, T> {
type PreparedResources = Resource<'a, T>;
impl<'a, T: resource::Resource> ResourceSet for Res<'a, T> {
type PreparedResources = Res<'a, T>;
unsafe fn fetch_unchecked(resources: &Resources) -> Self::PreparedResources {
let resource = resources
.get::<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 write_types() -> Vec<ResourceTypeId> { Vec::new() }
}
#[derive(Debug)]
pub struct ResourceMut<'a, T: 'a> {
pub struct ResMut<'a, T: 'a> {
// held for drop impl
_marker: PhantomData<&'a mut ()>,
value: *mut T,
}
unsafe impl<'a, T: resource::Resource> Send for ResourceMut<'a, T> {}
unsafe impl<'a, T: resource::Resource> Sync for ResourceMut<'a, T> {}
impl<'a, T: 'a> Clone for ResourceMut<'a, T> {
unsafe impl<'a, T: resource::Resource> Send for ResMut<'a, T> {}
unsafe impl<'a, T: resource::Resource> Sync for ResMut<'a, T> {}
impl<'a, T: 'a> Clone for ResMut<'a, T> {
#[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)]
fn new(resource: *mut T) -> Self {
Self {
@ -130,42 +130,42 @@ impl<'a, T: 'a> ResourceMut<'a, T> {
}
#[inline(always)]
pub fn map_into<K: 'a, F: FnMut(&mut T) -> K>(mut self, mut f: F) -> ResourceMut<'a, K> {
ResourceMut::new(&mut f(&mut self))
pub fn map_into<K: 'a, F: FnMut(&mut T) -> K>(mut self, mut f: F) -> ResMut<'a, K> {
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;
#[inline(always)]
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)]
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)]
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)]
fn borrow(&self) -> &T { unsafe { &*self.value } }
}
impl<'a, T> PartialEq for ResourceMut<'a, T>
impl<'a, T> PartialEq for ResMut<'a, T>
where
T: 'a + PartialEq,
{
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
T: 'a + PartialOrd,
{
@ -173,28 +173,28 @@ where
self.value.partial_cmp(&other.value)
}
}
impl<'a, T> Ord for ResourceMut<'a, T>
impl<'a, T> Ord for ResMut<'a, T>
where
T: 'a + Ord,
{
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
T: 'a + Hash,
{
fn hash<H: Hasher>(&self, state: &mut H) { self.value.hash(state); }
}
impl<'a, T: resource::Resource> ResourceSet for ResourceMut<'a, T> {
type PreparedResources = ResourceMut<'a, T>;
impl<'a, T: resource::Resource> ResourceSet for ResMut<'a, T> {
type PreparedResources = ResMut<'a, T>;
unsafe fn fetch_unchecked(resources: &Resources) -> Self::PreparedResources {
let mut resource = resources
.get_mut::<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 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>;
}
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
T: AsUniforms + Send + Sync + 'static,
{
@ -34,9 +34,9 @@ where
}
pub fn asset_shader_def_system<T>(
assets: Resource<Assets<T>>,
asset_handle: Ref<Handle<T>>,
mut renderable: RefMut<Renderable>,
assets: Res<Assets<T>>,
asset_handle: Com<Handle<T>>,
mut renderable: ComMut<Renderable>,
) where
T: AsUniforms + Send + Sync + 'static,
{

View file

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

View file

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

View file

@ -9,8 +9,8 @@ fn main() {
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// load the mesh
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
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);
}
/// set up a simple scene with a "parent" cube and a "child" cube
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube));
let cube_material_handle = materials.add(StandardMaterial {

View file

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

View file

@ -15,10 +15,10 @@ fn main() {
}
fn move_cubes(
time: Resource<Time>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
mut translation: RefMut<Translation>,
material_handle: Ref<Handle<StandardMaterial>>,
time: Res<Time>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut translation: ComMut<Translation>,
material_handle: Com<Handle<StandardMaterial>>,
) {
let material = materials.get_mut(&material_handle).unwrap();
translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds;
@ -27,8 +27,8 @@ fn move_cubes(
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube));
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
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut textures: ResourceMut<Assets<Texture>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut textures: ResMut<Assets<Texture>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// load a texture
let texture_path = concat!(

View file

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

View file

@ -40,7 +40,7 @@ struct PrintMessageState {
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;
if state.elapsed_time > state.duration.as_secs_f32() {
println!("{}", state.message);

View file

@ -17,7 +17,7 @@ fn main() {
pub const SYSTEM_ITERATION_COUNT: DiagnosticId =
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.
// In general it's a good idea to set them up in a "startup system".
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
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:
// NOTE: "mut" denotes that the resource is "mutable"
// Resource<GameRules> is read-only. ResourceMut<GameState> can modify the resource
fn new_round_system(game_rules: Resource<GameRules>, mut game_state: ResourceMut<GameState>) {
// Res<GameRules> is read-only. ResMut<GameState> can modify the resource
fn new_round_system(game_rules: Res<GameRules>, mut game_state: ResMut<GameState>) {
game_state.current_round += 1;
println!(
"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.
// NOTE: Ref<Player> is a read-only reference, whereas RefMut<Score> can modify the component
fn score_system(player: Ref<Player>, mut score: RefMut<Score>) {
// NOTE: Com<Player> is a read-only component Comerence, whereas ComMut<Score> can modify the component
fn score_system(player: Com<Player>, mut score: ComMut<Score>) {
let scored_a_point = random::<bool>();
if scored_a_point {
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.
// NOTE: resources must always come before components in system functions
fn score_check_system(
game_rules: Resource<GameRules>,
mut game_state: ResourceMut<GameState>,
player: Ref<Player>,
score: Ref<Score>,
game_rules: Res<GameRules>,
mut game_state: ResMut<GameState>,
player: Com<Player>,
score: Com<Score>,
) {
if score.value == game_rules.winning_score {
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
// App to quit. Check out the "event.rs" example if you want to learn more about using events.
fn game_over_system(
game_rules: Resource<GameRules>,
game_state: Resource<GameState>,
mut app_exit_events: ResourceMut<Events<AppExit>>,
game_rules: Res<GameRules>,
game_state: Res<GameState>,
mut app_exit_events: ResMut<Events<AppExit>>,
) {
if let Some(ref player) = game_state.winning_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
fn new_player_system(
command_buffer: &mut CommandBuffer,
game_rules: Resource<GameRules>,
mut game_state: ResourceMut<GameState>,
game_rules: Res<GameRules>,
mut game_state: ResMut<GameState>,
) {
// Randomly add a new player
let add_new_player = random::<bool>();
@ -232,7 +232,7 @@ fn thread_local_system(world: &mut World, resources: &mut Resources) {
#[allow(dead_code)]
fn closure_system() -> Box<dyn Schedulable> {
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!("this ran {} times", counter);
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
#[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!("this ran {} times", state.counter);
state.counter += 1;
@ -267,7 +267,7 @@ fn complex_system(resources: &mut Resources) -> Box<dyn Schedulable> {
SystemBuilder::new("complex_system")
.read_resource::<GameState>()
.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())
// this query only returns entities with a Player component that has changed since the last update
.with_query(<Read<Player>>::query().filter(changed::<Player>()))
@ -307,7 +307,7 @@ fn main() {
.add_startup_system(startup_system)
// my_system.system() calls converts normal rust functions into ECS systems:
.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)
//
// 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
// 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 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

View file

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

View file

@ -22,8 +22,8 @@ struct State {
/// adjusts move state based on keyboard input
fn collect_input(
mut state: ResourceMut<State>,
keyboard_input_events: Resource<Events<KeyboardInput>>,
mut state: ResMut<State>,
keyboard_input_events: Res<Events<KeyboardInput>>,
) {
for event in state.event_reader.iter(&keyboard_input_events) {
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
fn move_on_input(
state: Resource<State>,
time: Resource<Time>,
mut translation: RefMut<Translation>,
_: Ref<Handle<Mesh>>,
state: Res<State>,
time: Res<Time>,
mut translation: ComMut<Translation>,
_: Com<Handle<Mesh>>,
) {
if state.moving_left {
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
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube));
let cube_material_handle = materials.add(StandardMaterial {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -10,10 +10,10 @@ fn main() {
}
fn placement_system(
time: Resource<Time>,
materials: Resource<Assets<ColorMaterial>>,
mut node: RefMut<Node>,
material_handle: Ref<Handle<ColorMaterial>>,
time: Res<Time>,
materials: Res<Assets<ColorMaterial>>,
mut node: ComMut<Node>,
material_handle: Com<Handle<ColorMaterial>>,
) {
let material = materials.get(&material_handle).unwrap();
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();
builder.add_entity(Camera2dEntity::default());

View file

@ -7,7 +7,7 @@ fn main() {
.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
create_window_events.send(CreateWindow {
descriptor: WindowDescriptor {

View file

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