Skip to content

Commit

Permalink
feat: inventory commands
Browse files Browse the repository at this point in the history
  • Loading branch information
umut-sahin committed Jan 31, 2024
1 parent aac4ec4 commit 444dcc4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
18 changes: 18 additions & 0 deletions game/src/inventory/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::prelude::*;

/// Controls the inventory.
#[derive(ConsoleCommand, Parser)]
#[command(name = "inventory")]
#[command(disable_help_flag = true)]
pub struct InventoryCommand {
#[clap(subcommand)]
pub subcommand: InventoryCommands,
}

#[derive(Debug, Subcommand)]
pub enum InventoryCommands {
/// Lists the items in the inventory.
List,
/// Adds an item to the inventory.
Add { item: String },
}
1 change: 1 addition & 0 deletions game/src/inventory/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod commands;
pub mod plugin;
pub mod resources;
pub mod systems;
8 changes: 7 additions & 1 deletion game/src/inventory/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{
inventory::systems::*,
inventory::{
commands::*,
systems::*,
},
prelude::*,
};

Expand All @@ -11,6 +14,9 @@ impl Plugin for InventoryPlugin {
// Insert resources.
app.init_resource::<Inventory>();

// Add console commands.
app.add_console_command::<InventoryCommand, _>(apply_command);

// Add systems.
app.add_systems(
OnEnter(GameState::Initialization),
Expand Down
39 changes: 38 additions & 1 deletion game/src/inventory/systems.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
use crate::prelude::*;
use crate::{
inventory::commands::*,
prelude::*,
};


/// Applies the inventory console commands.
pub fn apply_command(
mut command: ConsoleCommand<InventoryCommand>,
mut inventory: ResMut<Inventory>,
item_registry: Res<ItemRegistry>,
) {
if let Some(Ok(InventoryCommand { subcommand })) = command.take() {
match subcommand {
InventoryCommands::List => {
for (i, item) in inventory.iter().enumerate() {
reply!(command, "{}) {}", i + 1, item.id());
}
},
InventoryCommands::Add { item } => {
match item_registry.find_item_by_id(&item) {
Some(item) => {
inventory.add(item.instantiate());
reply!(command, "Added.");
},
None => {
reply!(
command,
"Failed to add {:?} to the inventory as it doesn't exist.",
item,
);
},
}
},
}
reply!(command, "");
}
}


/// Adds the items specified in the inventory argument to the inventory.
Expand Down
5 changes: 5 additions & 0 deletions game/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ pub use {
},
},
bevy_console::{
reply,
AddConsoleCommand as _,
ConsoleCommand,
ConsoleConfiguration,
ConsoleOpen as ConsoleState,
ConsolePlugin as BevyConsolePlugin,
ConsoleSet,
},
bevy_persistent::prelude::*,
bevy_prng::ChaCha8Rng,
Expand All @@ -111,6 +115,7 @@ pub use {
self,
CommandFactory,
Parser,
Subcommand,
},
core::num::NonZeroU8,
leafwing_input_manager::{
Expand Down

0 comments on commit 444dcc4

Please sign in to comment.