generated from tipragot/rust
Compare commits
No commits in common. "improve-network" and "main" have entirely different histories.
improve-ne
...
main
|
@ -43,7 +43,4 @@ pub struct Player {
|
||||||
|
|
||||||
/// The color of the player.
|
/// The color of the player.
|
||||||
pub color: (u8, u8, u8),
|
pub color: (u8, u8, u8),
|
||||||
|
|
||||||
/// The resources of the player.
|
|
||||||
pub resources: resources::Resources,
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ use std::ops::{
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use paste::paste;
|
use paste::paste;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
/// Represents a number that can be used in calculations for hexagonal grids.
|
/// Represents a number that can be used in calculations for hexagonal grids.
|
||||||
pub trait Number:
|
pub trait Number:
|
||||||
|
@ -100,7 +99,7 @@ number_impl! {
|
||||||
/// Represents a position in a hexagonal grid.
|
/// Represents a position in a hexagonal grid.
|
||||||
/// We use the axial coordinate system explained in this
|
/// We use the axial coordinate system explained in this
|
||||||
/// [documentation](https://www.redblobgames.com/grids/hexagons/#coordinates).
|
/// [documentation](https://www.redblobgames.com/grids/hexagons/#coordinates).
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Component, Serialize, Deserialize)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Component)]
|
||||||
pub struct HexPosition<T: Number>(pub T, pub T);
|
pub struct HexPosition<T: Number>(pub T, pub T);
|
||||||
|
|
||||||
/// All possible directions in a hexagonal grid.
|
/// All possible directions in a hexagonal grid.
|
||||||
|
|
|
@ -7,7 +7,6 @@ pub mod renderer;
|
||||||
pub mod selected_tile;
|
pub mod selected_tile;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use self::hex::*;
|
use self::hex::*;
|
||||||
|
|
||||||
|
@ -15,7 +14,7 @@ use self::hex::*;
|
||||||
pub type TilePosition = HexPosition<i32>;
|
pub type TilePosition = HexPosition<i32>;
|
||||||
|
|
||||||
/// The tile of the map.
|
/// The tile of the map.
|
||||||
#[derive(Component, Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Component, Debug)]
|
||||||
pub enum Tile {
|
pub enum Tile {
|
||||||
/// The breeding tile.
|
/// The breeding tile.
|
||||||
Breeding,
|
Breeding,
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
//! All code related to the ownership of the tiles.
|
//! All code related to the ownership of the tiles.
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use crate::Player;
|
use crate::Player;
|
||||||
|
|
||||||
/// The owner of a tile.
|
/// The owner of a tile.
|
||||||
#[derive(Component, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Component, Clone)]
|
||||||
pub struct Owner(pub Player);
|
pub struct Owner(pub Player);
|
||||||
|
|
||||||
/// The plugin to render the ownership of the tiles.
|
/// The plugin to render the ownership of the tiles.
|
||||||
|
|
|
@ -11,7 +11,6 @@ use crate::CurrentScene;
|
||||||
|
|
||||||
pub mod check_connection;
|
pub mod check_connection;
|
||||||
pub mod connection;
|
pub mod connection;
|
||||||
pub mod reconnection;
|
|
||||||
|
|
||||||
/// The plugin for the networking.
|
/// The plugin for the networking.
|
||||||
pub struct NetworkingPlugin;
|
pub struct NetworkingPlugin;
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
//! 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::<ReconnectionRequest>()
|
|
||||||
.add_network_event::<ReconnectionResponse>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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<Player>,
|
|
||||||
|
|
||||||
/// The map of the game.
|
|
||||||
pub map: Vec<(Option<Owner>, Tile, TilePosition)>,
|
|
||||||
|
|
||||||
/// The current player.
|
|
||||||
pub current_player: Player,
|
|
||||||
}
|
|
|
@ -1,10 +1,6 @@
|
||||||
//! All program related to the resources of the game.
|
//! All program related to the resources of the game.
|
||||||
|
|
||||||
use bevnet::{NetworkAppExt, Receive, SendTo};
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use crate::Player;
|
|
||||||
|
|
||||||
/// The plugin that manage the resources.
|
/// The plugin that manage the resources.
|
||||||
pub struct ResourcesPlugin;
|
pub struct ResourcesPlugin;
|
||||||
|
@ -13,16 +9,12 @@ impl Plugin for ResourcesPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_event::<ResetResources>()
|
app.add_event::<ResetResources>()
|
||||||
.insert_resource(Resources::initial())
|
.insert_resource(Resources::initial())
|
||||||
.add_network_event::<UpdateResources>()
|
.add_systems(Update, handle_reset_resources);
|
||||||
.add_systems(
|
|
||||||
Update,
|
|
||||||
(handle_reset_resources, save_resources, update_resources),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The resources of the game.
|
/// The resources of the game.
|
||||||
#[derive(Resource, Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Resource, Default)]
|
||||||
pub struct Resources {
|
pub struct Resources {
|
||||||
/// The stone resource.
|
/// The stone resource.
|
||||||
pub stone: u32,
|
pub stone: u32,
|
||||||
|
@ -36,7 +28,7 @@ pub struct Resources {
|
||||||
|
|
||||||
impl Resources {
|
impl Resources {
|
||||||
/// Returns the initial resources of the game.
|
/// Returns the initial resources of the game.
|
||||||
pub const fn initial() -> Self {
|
const fn initial() -> Self {
|
||||||
Self {
|
Self {
|
||||||
stone: 100,
|
stone: 100,
|
||||||
wood: 100,
|
wood: 100,
|
||||||
|
@ -58,42 +50,3 @@ fn handle_reset_resources(
|
||||||
*resources = Resources::initial();
|
*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<SendTo<UpdateResources>>,
|
|
||||||
resources: Res<Resources>,
|
|
||||||
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<Receive<UpdateResources>>,
|
|
||||||
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;
|
|
||||||
println!(
|
|
||||||
"Update resources for player {:?} to {:?}",
|
|
||||||
player.uuid, player.resources
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ use bevy_egui::{egui, EguiContexts};
|
||||||
|
|
||||||
use crate::networking::connection::RequestJoin;
|
use crate::networking::connection::RequestJoin;
|
||||||
use crate::networking::PlayerRank;
|
use crate::networking::PlayerRank;
|
||||||
use crate::resources::Resources;
|
|
||||||
use crate::{CurrentScene, Player};
|
use crate::{CurrentScene, Player};
|
||||||
|
|
||||||
/// The plugin for the menu.
|
/// The plugin for the menu.
|
||||||
|
@ -60,7 +59,6 @@ fn menu_ui(
|
||||||
rank: PlayerRank::Player,
|
rank: PlayerRank::Player,
|
||||||
uuid,
|
uuid,
|
||||||
color: rand::random::<(u8, u8, u8)>(),
|
color: rand::random::<(u8, u8, u8)>(),
|
||||||
resources: Resources::initial(),
|
|
||||||
}),
|
}),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -75,7 +73,6 @@ fn menu_ui(
|
||||||
rank: PlayerRank::Admin,
|
rank: PlayerRank::Admin,
|
||||||
uuid,
|
uuid,
|
||||||
color: rand::random::<(u8, u8, u8)>(),
|
color: rand::random::<(u8, u8, u8)>(),
|
||||||
resources: Resources::initial(),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue