pub struct Resources { /* private fields */ }Implementations§
Source§impl Resources
impl Resources
pub fn new() -> Self
Sourcepub fn get<T: 'static>(&self) -> Option<&T>
pub fn get<T: 'static>(&self) -> Option<&T>
Examples found in repository?
examples/shooter_game.rs (line 47)
46fn update(engine: &mut Engine, resources: &mut Resources) -> EngineResult<()> {
47 let player = *resources.get::<EntityId>().unwrap();
48 let input = &engine.input;
49 let mut pos = engine.entities.get_transform(player).unwrap().position;
50 let speed = 300.0 * engine.time.delta_time();
51 if input.is_key_pressed(KeyCode::ArrowLeft) || input.is_key_pressed(KeyCode::KeyA) {
52 pos.x -= speed;
53 }
54 if input.is_key_pressed(KeyCode::ArrowRight) || input.is_key_pressed(KeyCode::KeyD) {
55 pos.x += speed;
56 }
57 if input.is_key_pressed(KeyCode::ArrowUp) || input.is_key_pressed(KeyCode::KeyW) {
58 pos.y -= speed;
59 }
60 if input.is_key_pressed(KeyCode::ArrowDown) || input.is_key_pressed(KeyCode::KeyS) {
61 pos.y += speed;
62 }
63 if let Some(transform) = engine.entities.get_transform_mut(player) {
64 transform.position = pos;
65 }
66
67 // Shooting
68 if input.is_key_just_pressed(KeyCode::Space) {
69 engine.entities.spawn()
70 .at_position(pos.x, pos.y - 20.0)
71 .with_colored_square(Color::new(1.0, 1.0, 0.0, 1.0), 8.0)
72 .with_velocity(Velocity::new(0.0, -500.0))
73 .build();
74 }
75
76 // Move bullets
77 // Move bullets (iterate over all entities and update those with velocity)
78 let entity_ids: Vec<_> = engine.entities.entities().to_vec();
79 // Collect (entity, velocity) pairs first
80 let moves: Vec<_> = entity_ids.iter()
81 .filter_map(|&entity| engine.entities.get_velocity(entity).map(|vel| (entity, vel.velocity)))
82 .collect();
83 // Now mutate transforms in a separate loop
84 let mut to_remove = Vec::new();
85 for (entity, velocity) in moves {
86 if let Some(transform) = engine.entities.get_transform_mut(entity) {
87 transform.position.x += velocity.x * engine.time.delta_time();
88 transform.position.y += velocity.y * engine.time.delta_time();
89 if transform.position.y < -10.0 {
90 to_remove.push(entity);
91 }
92 }
93 }
94 for entity in to_remove {
95 engine.entities.remove_entity(entity);
96 }
97
98 // TODO: Spawn/move enemies, collision, scoring
99
100 Ok(())
101}
102
103fn ui_system(engine: &mut Engine, resources: &mut Resources) -> EngineResult<()> {
104 // Split borrows for UiContext and GameState
105 // Clone needed GameState fields first
106 let (volume, player_name, score) = {
107 let state_ref = resources.get::<GameState>().unwrap();
108 (state_ref.volume, state_ref.player_name.clone(), state_ref.score)
109 };
110 let ui = resources.get_mut::<UiContext>().unwrap();
111 ui.text("Volume", Position::new(20.0, 20.0), Color::WHITE);
112 ui.slider(
113 Rect::new(100.0, 20.0, 180.0, 24.0),
114 0.0,
115 1.0,
116 volume,
117 0.01,
118 SliderStyle::default(),
119 );
120 ui.text("Name", Position::new(20.0, 60.0), Color::WHITE);
121 ui.text_input(
122 Rect::new(100.0, 60.0, 180.0, 32.0),
123 &player_name,
124 TextInputStyle::default(),
125 );
126 ui.text(format!("Score: {}", score), Position::new(20.0, 110.0), Color::WHITE);
127 Ok(())
128}Sourcepub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>
Examples found in repository?
examples/shooter_game.rs (line 110)
103fn ui_system(engine: &mut Engine, resources: &mut Resources) -> EngineResult<()> {
104 // Split borrows for UiContext and GameState
105 // Clone needed GameState fields first
106 let (volume, player_name, score) = {
107 let state_ref = resources.get::<GameState>().unwrap();
108 (state_ref.volume, state_ref.player_name.clone(), state_ref.score)
109 };
110 let ui = resources.get_mut::<UiContext>().unwrap();
111 ui.text("Volume", Position::new(20.0, 20.0), Color::WHITE);
112 ui.slider(
113 Rect::new(100.0, 20.0, 180.0, 24.0),
114 0.0,
115 1.0,
116 volume,
117 0.01,
118 SliderStyle::default(),
119 );
120 ui.text("Name", Position::new(20.0, 60.0), Color::WHITE);
121 ui.text_input(
122 Rect::new(100.0, 60.0, 180.0, 32.0),
123 &player_name,
124 TextInputStyle::default(),
125 );
126 ui.text(format!("Score: {}", score), Position::new(20.0, 110.0), Color::WHITE);
127 Ok(())
128}pub fn remove<T: 'static>(&mut self) -> Option<T>
pub fn contains<T: 'static>(&self) -> bool
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Resources
impl !RefUnwindSafe for Resources
impl !Send for Resources
impl !Sync for Resources
impl Unpin for Resources
impl !UnwindSafe for Resources
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.