Skip to content

Commit

Permalink
feat: item commands
Browse files Browse the repository at this point in the history
  • Loading branch information
umut-sahin committed Jan 31, 2024
1 parent 444dcc4 commit ddcbbb5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions game/src/inventory/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn apply_command(
"Failed to add {:?} to the inventory as it doesn't exist.",
item,
);
reply!(command, "Run \"item list\" to see available items.")
},
}
},
Expand Down
16 changes: 16 additions & 0 deletions game/src/items/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::prelude::*;

/// Controls the items.
#[derive(ConsoleCommand, Parser)]
#[command(name = "item")]
#[command(disable_help_flag = true)]
pub struct ItemCommand {
#[clap(subcommand)]
pub subcommand: ItemCommands,
}

#[derive(Debug, Subcommand)]
pub enum ItemCommands {
/// Lists the available items.
List,
}
2 changes: 2 additions & 0 deletions game/src/items/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod commands;
pub mod components;
pub mod interfaces;
pub mod plugin;
pub mod registry;
pub mod systems;
11 changes: 10 additions & 1 deletion game/src/items/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::prelude::*;
use crate::{
items::{
commands::*,
systems::*,
},
prelude::*,
};

/// Plugin for managing items.
pub struct ItemPlugin;
Expand All @@ -11,5 +17,8 @@ impl Plugin for ItemPlugin {

// Initialize registry.
app.init_resource::<ItemRegistry>();

// Add console commands.
app.add_console_command::<ItemCommand, _>(apply_command);
}
}
19 changes: 19 additions & 0 deletions game/src/items/systems.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::{
items::commands::*,
prelude::*,
};


/// Applies the item console commands.
pub fn apply_command(mut command: ConsoleCommand<ItemCommand>, item_registry: Res<ItemRegistry>) {
if let Some(Ok(ItemCommand { subcommand })) = command.take() {
match subcommand {
ItemCommands::List => {
for (i, entry) in item_registry.iter().enumerate() {
reply!(command, "{}) {}", i + 1, entry.item.id());
}
},
}
reply!(command, "");
}
}

0 comments on commit ddcbbb5

Please sign in to comment.