From 6dfde673007e206513316ea175dfd1fb0fc7e34c Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 7 Apr 2024 17:34:28 +0200 Subject: [PATCH] Add resources system and update player struct --- crates/border-wars/src/lib.rs | 3 ++ crates/border-wars/src/resources.rs | 48 +++++++++++++++++++++++++-- crates/border-wars/src/scenes/menu.rs | 3 ++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index 10112b8..8e0a6ce 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -43,4 +43,7 @@ pub struct Player { /// The color of the player. pub color: (u8, u8, u8), + + /// The resources of the player. + pub resources: resources::Resources, } diff --git a/crates/border-wars/src/resources.rs b/crates/border-wars/src/resources.rs index b5b5b7c..5a3dfb1 100644 --- a/crates/border-wars/src/resources.rs +++ b/crates/border-wars/src/resources.rs @@ -1,6 +1,10 @@ //! All program related to the resources of the game. +use bevnet::{Receive, SendTo}; use bevy::prelude::*; +use serde::{Deserialize, Serialize}; + +use crate::Player; /// The plugin that manage the resources. pub struct ResourcesPlugin; @@ -9,12 +13,15 @@ impl Plugin for ResourcesPlugin { fn build(&self, app: &mut App) { app.add_event::() .insert_resource(Resources::initial()) - .add_systems(Update, handle_reset_resources); + .add_systems( + Update, + (handle_reset_resources, save_resources, update_resources), + ); } } /// The resources of the game. -#[derive(Resource, Default)] +#[derive(Resource, Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Resources { /// The stone resource. pub stone: u32, @@ -28,7 +35,7 @@ pub struct Resources { impl Resources { /// Returns the initial resources of the game. - const fn initial() -> Self { + pub const fn initial() -> Self { Self { stone: 100, wood: 100, @@ -50,3 +57,38 @@ fn handle_reset_resources( *resources = Resources::initial(); } } + +/// An event send to update the resources of a player. +#[derive(Event, Serialize, Deserialize, Clone, Copy)] +pub struct UpdateResources(pub Resources); + +/// Save the resources of the game when you modify it. +fn save_resources( + mut update_resources_event: EventWriter>, + resources: Res, + players: Query<&Player>, +) { + if !resources.is_changed() { + return; + } + + let event = UpdateResources(*resources); + + for player in players.iter() { + update_resources_event.send(SendTo(player.uuid, event)); + } +} + +/// Update the resources of all player. +fn update_resources( + mut update_resources_event: EventReader>, + mut players: Query<&mut Player>, +) { + for event in update_resources_event.read() { + let Some(mut player) = players.iter_mut().find(|player| player.uuid == event.0) else { + continue; + }; + + player.resources = event.1.0; + } +} diff --git a/crates/border-wars/src/scenes/menu.rs b/crates/border-wars/src/scenes/menu.rs index a13de1a..754b877 100644 --- a/crates/border-wars/src/scenes/menu.rs +++ b/crates/border-wars/src/scenes/menu.rs @@ -6,6 +6,7 @@ use bevy_egui::{egui, EguiContexts}; use crate::networking::connection::RequestJoin; use crate::networking::PlayerRank; +use crate::resources::Resources; use crate::{CurrentScene, Player}; /// The plugin for the menu. @@ -59,6 +60,7 @@ fn menu_ui( rank: PlayerRank::Player, uuid, color: rand::random::<(u8, u8, u8)>(), + resources: Resources::initial(), }), )); } @@ -73,6 +75,7 @@ fn menu_ui( rank: PlayerRank::Admin, uuid, color: rand::random::<(u8, u8, u8)>(), + resources: Resources::initial(), }); } });