Skip to content

Commit

Permalink
refactor: use shared resources for game inputs. (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Sep 29, 2023
1 parent 5dc6959 commit d26c70a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
27 changes: 11 additions & 16 deletions framework_crates/bones_bevy_renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ impl BonesBevyRenderer {
let mut bones_image_ids = BonesImageIds::default();
let mut bones_egui_textures = bones::EguiTextures::default();
'asset_load: {
let Some(mut asset_server) = self.game.shared_resource::<bones::AssetServer>() else {
let Some(mut asset_server) = self.game.shared_resource_mut::<bones::AssetServer>()
else {
break 'asset_load;
};

Expand Down Expand Up @@ -556,14 +557,11 @@ fn step_bones_game(

let bevy_time = world.resource::<Time>();

let mouse_inputs = bones::AtomicResource::new(mouse_inputs);
let keyboard_inputs = bones::AtomicResource::new(keyboard_inputs);
let gamepad_inputs = bones::AtomicResource::new(gamepad_inputs);

// Reload assets if necessary
if let Some(mut asset_server) = game.shared_resource::<bones::AssetServer>() {
if let Some(mut asset_server) = game.shared_resource_mut::<bones::AssetServer>() {
asset_server.handle_asset_changes(|asset_server, handle| {
let mut bones_egui_textures = game.shared_resource::<bones::EguiTextures>().unwrap();
let mut bones_egui_textures =
game.shared_resource_mut::<bones::EguiTextures>().unwrap();
let asset = asset_server.get_untyped_mut(handle).unwrap();

// TODO: hot reload changed fonts.
Expand All @@ -580,16 +578,13 @@ fn step_bones_game(
})
}

// Add the game inputs
game.insert_shared_resource(mouse_inputs);
game.insert_shared_resource(keyboard_inputs);
game.insert_shared_resource(gamepad_inputs);

// Step the game simulation
game.step(
bevy_time.last_update().unwrap_or_else(Instant::now),
|bones_world| {
// Update the inputs.
bones_world.resources.insert_cell(mouse_inputs.clone());
bones_world.resources.insert_cell(keyboard_inputs.clone());
bones_world.resources.insert_cell(gamepad_inputs.clone());
},
);
game.step(bevy_time.last_update().unwrap_or_else(Instant::now));

world.insert_resource(data);
world.insert_resource(bones_image_ids);
Expand Down
26 changes: 10 additions & 16 deletions framework_crates/bones_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,16 @@ impl Game {
plugin.install(self);
self
}
/// Get the shared resource of a given type out of this [`Game`]s shared resources.
pub fn shared_resource<T: HasSchema>(&self) -> Option<Ref<T>> {
self.shared_resources
.iter()
.find(|x| x.schema() == T::schema())
.map(|x| x.borrow().typed())
}

/// Get the shared resource of a given type out of this [`Game`]s shared resources.
pub fn shared_resource<T: HasSchema>(&self) -> Option<RefMut<T>> {
pub fn shared_resource_mut<T: HasSchema>(&self) -> Option<RefMut<T>> {
self.shared_resources
.iter()
.find(|x| x.schema() == T::schema())
Expand All @@ -219,7 +226,7 @@ impl Game {
{
self.insert_shared_resource(T::default());
}
self.shared_resource::<T>().unwrap()
self.shared_resource_mut::<T>().unwrap()
}

/// Insert a resource that will be shared across all game sessions.
Expand All @@ -238,17 +245,7 @@ impl Game {
}

/// Step the game simulation.
///
/// `apply_input` is a function that will be called once for every active [`Session`], allowing
/// you to update the world with the current frame's input, whatever form that may come in.
///
/// Usually this will be used to:
/// - assign the player input to a resource so that the game can respond to player controls.
/// - assign the window information to a resource, so that the game can respond to the window
/// size.
/// - setup other important resources such as the UI context and the asset server, if
/// applicable.
pub fn step<F: FnMut(&mut World)>(&mut self, now: instant::Instant, mut apply_input: F) {
pub fn step(&mut self, now: instant::Instant) {
// Pull out the game systems so that we can run them on the game
let mut game_systems = std::mem::take(&mut self.systems);

Expand Down Expand Up @@ -308,9 +305,6 @@ impl Game {
visible: current_session.visible,
});

// Apply the game input
apply_input(&mut current_session.world);

// Insert the other sessions into the current session's world
{
let mut sessions = current_session.world.resource_mut::<Sessions>();
Expand Down

0 comments on commit d26c70a

Please sign in to comment.