From c808a1e7a5248d01bfd2302ebfd6a2972e180cc1 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 7 Apr 2024 17:10:23 +0200 Subject: [PATCH] save --- crates/border-wars/src/lib.rs | 3 ++ crates/border-wars/src/map/hex.rs | 3 +- crates/border-wars/src/map/mod.rs | 3 +- crates/border-wars/src/map/ownership.rs | 3 +- crates/border-wars/src/networking/mod.rs | 3 ++ .../src/networking/reconnection.rs | 36 +++++++++++++++++++ crates/border-wars/src/resources.rs | 5 +-- crates/border-wars/src/scenes/menu.rs | 3 ++ 8 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 crates/border-wars/src/networking/reconnection.rs diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index 10112b8..9663449 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: crate::resources::Resources, } diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 415bdfc..40ab228 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -6,6 +6,7 @@ use std::ops::{ use bevy::prelude::*; use paste::paste; +use serde::{Deserialize, Serialize}; /// Represents a number that can be used in calculations for hexagonal grids. pub trait Number: @@ -99,7 +100,7 @@ number_impl! { /// Represents a position in a hexagonal grid. /// We use the axial coordinate system explained in this /// [documentation](https://www.redblobgames.com/grids/hexagons/#coordinates). -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Component)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Component, Serialize, Deserialize)] pub struct HexPosition(pub T, pub T); /// All possible directions in a hexagonal grid. diff --git a/crates/border-wars/src/map/mod.rs b/crates/border-wars/src/map/mod.rs index 709fae2..a8185ba 100644 --- a/crates/border-wars/src/map/mod.rs +++ b/crates/border-wars/src/map/mod.rs @@ -7,6 +7,7 @@ pub mod renderer; pub mod selected_tile; use bevy::prelude::*; +use serde::{Deserialize, Serialize}; use self::hex::*; @@ -14,7 +15,7 @@ use self::hex::*; pub type TilePosition = HexPosition; /// The tile of the map. -#[derive(Component, Debug)] +#[derive(Component, Debug, Clone, Serialize, Deserialize)] pub enum Tile { /// The breeding tile. Breeding, diff --git a/crates/border-wars/src/map/ownership.rs b/crates/border-wars/src/map/ownership.rs index c563518..6e30dc7 100644 --- a/crates/border-wars/src/map/ownership.rs +++ b/crates/border-wars/src/map/ownership.rs @@ -1,11 +1,12 @@ //! All code related to the ownership of the tiles. use bevy::prelude::*; +use serde::{Deserialize, Serialize}; use crate::Player; /// The owner of a tile. -#[derive(Component, Clone)] +#[derive(Component, Clone, Serialize, Deserialize)] pub struct Owner(pub Player); /// The plugin to render the ownership of the tiles. diff --git a/crates/border-wars/src/networking/mod.rs b/crates/border-wars/src/networking/mod.rs index 9b31654..a98c10d 100644 --- a/crates/border-wars/src/networking/mod.rs +++ b/crates/border-wars/src/networking/mod.rs @@ -6,11 +6,13 @@ use serde::{Deserialize, Serialize}; use self::check_connection::CheckConnectionPlugin; use self::connection::ConnectionPlugin; +use self::reconnection::ReconnectionPlugin; use crate::map::generation::StartMapGeneration; use crate::CurrentScene; pub mod check_connection; pub mod connection; +pub mod reconnection; /// The plugin for the networking. pub struct NetworkingPlugin; @@ -21,6 +23,7 @@ impl Plugin for NetworkingPlugin { .add_plugins(ConnectionPlugin) .add_systems(Update, handle_start_game) .add_network_event::() + .add_plugins(ReconnectionPlugin) .add_plugins(CheckConnectionPlugin); } } diff --git a/crates/border-wars/src/networking/reconnection.rs b/crates/border-wars/src/networking/reconnection.rs new file mode 100644 index 0000000..696d2c0 --- /dev/null +++ b/crates/border-wars/src/networking/reconnection.rs @@ -0,0 +1,36 @@ +//! All program related to reconnection logic. + +use bevnet::NetworkAppExt; +use bevy::prelude::*; +use serde::{Deserialize, Serialize}; + +use crate::map::ownership::Owner; +use crate::map::{Tile, TilePosition}; +use crate::Player; + +/// The plugin for the reconnection. +pub struct ReconnectionPlugin; + +impl Plugin for ReconnectionPlugin { + fn build(&self, app: &mut App) { + app.add_network_event::() + .add_network_event::(); + } +} + +/// The event to request the reconnection. +#[derive(Event, Serialize, Deserialize)] +pub struct ReconnectionRequest; + +/// The event to response to the reconnection. +#[derive(Event, Serialize, Deserialize)] +pub struct ReconnectionResponse { + /// All the players in the game. + pub players: Vec, + + /// The map of the game. + pub map: Vec<(Option, Tile, TilePosition)>, + + /// The current player. + pub current_player: Player, +} diff --git a/crates/border-wars/src/resources.rs b/crates/border-wars/src/resources.rs index b5b5b7c..412a95b 100644 --- a/crates/border-wars/src/resources.rs +++ b/crates/border-wars/src/resources.rs @@ -1,6 +1,7 @@ //! All program related to the resources of the game. use bevy::prelude::*; +use serde::{Deserialize, Serialize}; /// The plugin that manage the resources. pub struct ResourcesPlugin; @@ -14,7 +15,7 @@ impl Plugin for ResourcesPlugin { } /// The resources of the game. -#[derive(Resource, Default)] +#[derive(Resource, Default, Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Resources { /// The stone resource. pub stone: u32, @@ -28,7 +29,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, 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(), }); } });