From 106cabe95157653e3aca15ab1967f954b244d936 Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Sat, 6 Apr 2024 11:24:32 +0000 Subject: [PATCH] Add resources system (#111) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes: #71 Reviewed-on: https://git.tipragot.fr/fish-cannard/border-wars/pulls/111 Reviewed-by: Raphaƫl Co-authored-by: CoCo_Sol007 Co-committed-by: CoCo_Sol007 --- crates/border-wars/src/lib.rs | 1 + crates/border-wars/src/main.rs | 2 ++ crates/border-wars/src/resources.rs | 52 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 crates/border-wars/src/resources.rs diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index 366b111..10112b8 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize}; pub mod camera; pub mod map; pub mod networking; +pub mod resources; pub mod scenes; pub mod ui; diff --git a/crates/border-wars/src/main.rs b/crates/border-wars/src/main.rs index a7c9a63..9abc85c 100644 --- a/crates/border-wars/src/main.rs +++ b/crates/border-wars/src/main.rs @@ -7,6 +7,7 @@ use border_wars::map::ownership::OwnershipPlugin; use border_wars::map::renderer::RendererPlugin; use border_wars::map::selected_tile::SelectTilePlugin; use border_wars::networking::NetworkingPlugin; +use border_wars::resources::ResourcesPlugin; use border_wars::scenes::ScenesPlugin; use border_wars::ui::UiPlugin; @@ -21,5 +22,6 @@ fn main() { .add_plugins(MapGenerationPlugin) .add_plugins(UiPlugin) .add_plugins(OwnershipPlugin) + .add_plugins(ResourcesPlugin) .run(); } diff --git a/crates/border-wars/src/resources.rs b/crates/border-wars/src/resources.rs new file mode 100644 index 0000000..b5b5b7c --- /dev/null +++ b/crates/border-wars/src/resources.rs @@ -0,0 +1,52 @@ +//! All program related to the resources of the game. + +use bevy::prelude::*; + +/// The plugin that manage the resources. +pub struct ResourcesPlugin; + +impl Plugin for ResourcesPlugin { + fn build(&self, app: &mut App) { + app.add_event::() + .insert_resource(Resources::initial()) + .add_systems(Update, handle_reset_resources); + } +} + +/// The resources of the game. +#[derive(Resource, Default)] +pub struct Resources { + /// The stone resource. + pub stone: u32, + + /// The wood resource. + pub wood: u32, + + /// The food resource. + pub food: u32, +} + +impl Resources { + /// Returns the initial resources of the game. + const fn initial() -> Self { + Self { + stone: 100, + wood: 100, + food: 100, + } + } +} + +/// An event send to reset the resources of the game. +#[derive(Event)] +pub struct ResetResources; + +/// Handles the reset resources event. +fn handle_reset_resources( + mut reset_resources_event: EventReader, + mut resources: ResMut, +) { + for _ in reset_resources_event.read() { + *resources = Resources::initial(); + } +}