Skip to content

Commit

Permalink
fix!: Use persistent stats to save command changes
Browse files Browse the repository at this point in the history
  • Loading branch information
PraxTube committed Oct 27, 2023
1 parent c206b20 commit 8a0d6f3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
22 changes: 11 additions & 11 deletions src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ use bevy_console::{
AddConsoleCommand, ConsoleCommand, ConsoleConfiguration, ConsolePlugin, ToggleConsoleKey,
};

use crate::{network::socket::AceSocket, player::Player, GameState, RollbackState};
use crate::{network::socket::AceSocket, player::PersistentPlayerStats, GameState, RollbackState};

#[derive(Resource, Default)]
pub struct CommandQueue {
early_queue: Vec<AceCommands>,
queue: Vec<AceCommands>,
}

pub fn apply_commands(mut command_queque: ResMut<CommandQueue>, mut players: Query<&mut Player>) {
for command in command_queque.early_queue.drain(..) {
pub fn apply_commands(
mut command_queque: ResMut<CommandQueue>,
mut stats: ResMut<PersistentPlayerStats>,
) {
for command in command_queque.queue.drain(..) {
match command {
AceCommands::SetMaxSpeed(handle, value) => {
for mut player in &mut players {
if player.handle == handle {
player.stats.max_speed = value;
}
if handle >= stats.stats.len() {
return;
}

info!("applying command");
stats.stats[handle].max_speed = value;
}
}
}

command_queque.early_queue = command_queque.queue.clone();
command_queque.queue = Vec::default();
}

pub fn receive_commands(mut socket: ResMut<AceSocket>, mut command_queque: ResMut<CommandQueue>) {
Expand Down
13 changes: 10 additions & 3 deletions src/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use bevy_ggrs::*;

use crate::game_logic::Rematch;
use crate::input;
use crate::network::ggrs_config::PLAYER_COUNT;
use crate::network::GgrsConfig;
use crate::RollbackState;

Expand All @@ -36,7 +37,7 @@ pub const P2_COLOR: Color = Color::rgb(
0xD9 as f32 / 255.0,
);

#[derive(Reflect)]
#[derive(Reflect, Clone)]
pub struct PlayerStats {
pub max_speed: f32,
}
Expand All @@ -49,6 +50,11 @@ impl Default for PlayerStats {
}
}

#[derive(Resource, Default)]
pub struct PersistentPlayerStats {
pub stats: [PlayerStats; PLAYER_COUNT],
}

#[derive(Component, Reflect, Default)]
#[reflect(Hash)]
pub struct Player {
Expand All @@ -64,15 +70,15 @@ pub struct Player {
}

impl Player {
pub fn new(handle: usize) -> Player {
pub fn new(handle: usize, stats: PlayerStats) -> Player {
Player {
handle,
current_speed: MIN_SPEED,
health: MAX_HEALTH,
heat: 0,
overheated: false,
dodging: false,
stats: PlayerStats::default(),
stats,
}
}

Expand Down Expand Up @@ -124,6 +130,7 @@ impl Plugin for PlayerPlugin {
),
)
.add_event::<health::PlayerTookDamage>()
.init_resource::<PersistentPlayerStats>()
.add_plugins((shooting::ShootingPlugin, effect::EffectPlugin))
.add_systems(
GgrsSchedule,
Expand Down
14 changes: 11 additions & 3 deletions src/player/spawning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use super::effect::trail::spawn_player_trails;
use super::shooting::bullet::BulletTimer;
use super::shooting::rocket::spawn_player_wing_rockets;
use super::shooting::rocket::RocketTimer;
use super::PersistentPlayerStats;
use super::Player;
use super::PlayerStats;

use crate::audio::RollbackSound;
use crate::debug::DebugTransform;
Expand All @@ -36,11 +38,16 @@ pub fn player_spawn_transform(handle: usize) -> Transform {
}
}

fn spawn_player(commands: &mut Commands, texture: Handle<Image>, handle: usize) -> Entity {
fn spawn_player(
commands: &mut Commands,
texture: Handle<Image>,
handle: usize,
stats: PlayerStats,
) -> Entity {
let transform = player_spawn_transform(handle);
commands
.spawn((
Player::new(handle),
Player::new(handle, stats),
BulletTimer::default(),
RocketTimer::default(),
DodgeTimer::default(),
Expand All @@ -60,11 +67,12 @@ pub fn spawn_players(
mut commands: Commands,
assets: Res<GameAssets>,
mut effects: ResMut<Assets<EffectAsset>>,
stats: Res<PersistentPlayerStats>,
) {
let textures = [assets.player_1.clone(), assets.player_2.clone()];

for (handle, texture) in textures.into_iter().enumerate() {
let player = spawn_player(&mut commands, texture, handle);
let player = spawn_player(&mut commands, texture, handle, stats.stats[handle].clone());
spawn_player_wing_rockets(&mut commands, &assets, player, handle);
spawn_plane_whites(&mut commands, &assets, player, handle);
spawn_player_trails(&mut commands, &mut effects, player);
Expand Down

0 comments on commit 8a0d6f3

Please sign in to comment.