Resources

Struct Resources 

Source
pub struct Resources { /* private fields */ }

Implementations§

Source§

impl Resources

Source

pub fn new() -> Self

Source

pub fn insert<T: 'static>(&mut self, resource: T)

Examples found in repository?
examples/shooter_game.rs (line 42)
36fn setup(engine: &mut Engine, resources: &mut Resources) -> EngineResult<()> {
37    // Spawn player
38    let player = engine.entities.spawn()
39        .at_position(400.0, 500.0)
40        .with_colored_square(Color::BLUE, 32.0)
41        .build();
42    resources.insert(player);
43    Ok(())
44}
Source

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}
Source

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}
Source

pub fn remove<T: 'static>(&mut self) -> Option<T>

Source

pub fn contains<T: 'static>(&self) -> bool

Trait Implementations§

Source§

impl Debug for Resources

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Resources

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

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>

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)

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)

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more