diff --git a/Cargo.lock b/Cargo.lock index 3b58126..00c41c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -881,15 +881,13 @@ dependencies = [ [[package]] name = "bevy_ggrs" version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10aba92b574e9af76a1b4514fa010731d0b2bf29b8b0b135a2a5c9bc3c468174" +source = "git+https://github.com/gschup/bevy_ggrs.git?branch=main#9f86dc56985aba912b09ff79696bb603aa203d85" dependencies = [ "bevy", "bytemuck", "ggrs", "instant", "log", - "parking_lot 0.12.1", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 2fc2e1d..5c06bdb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,9 @@ lto = "thin" # WASM requirements [target.'cfg(target_arch = "wasm32")'.dependencies] -bevy_ggrs = { version = "0.13", features = ["wasm-bindgen"] } +bevy_ggrs = { git = "https://github.com/gschup/bevy_ggrs.git", branch = "main", features = [ + "wasm-bindgen", +] } js-sys = { version = "0.3" } wasm-bindgen = { version = "0.2.84" } wasm-bindgen-futures = { version = "0.4.34" } @@ -45,7 +47,7 @@ bevy = { version = "0.11.3", default-features = false, features = [ ] } bevy_kira_audio = { git = "https://github.com/NiklasEi/bevy_kira_audio.git", branch = "bevy_main" } bevy_asset_loader = { version = "0.17" } -bevy_ggrs = { version = "0.13" } +bevy_ggrs = { git = "https://github.com/gschup/bevy_ggrs.git", branch = "main" } bevy_matchbox = { version = "0.7.0", features = ["ggrs"] } bevy-inspector-egui = "0.19" bytemuck = { version = "1.7.3", features = ["derive"] } diff --git a/src/lib.rs b/src/lib.rs index f912ae7..b20105e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,7 @@ use bevy::diagnostic::LogDiagnosticsPlugin; pub const ASPECT_RATIO: f32 = 16.0 / 9.0; pub const MAP_HEIGHT: f32 = 768.0; pub const TILE_SIZE: f32 = 32.0; +pub const HEALTH_BAR_Y_OFFSET: f32 = TILE_SIZE + 10.; pub const FPS: usize = 60; pub const FIXED_TICK_MS: u64 = 1000 / FPS as u64; // use fixed duration tick delta to keep in sync with GGRSSchedule diff --git a/src/main.rs b/src/main.rs index 515eed9..f84ef28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use bevy::prelude::*; use bevy::window::PrimaryWindow; use bevy::winit::WinitWindows; use bevy::DefaultPlugins; -use bevy_ggrs::GgrsPlugin; +use bevy_ggrs::{GgrsApp, GgrsPlugin, ReadInputs}; use std::io::Cursor; use turtle_time::npc::components::{EdibleTarget, Goose, HasTarget}; use turtle_time::player::checksum::Checksum; @@ -21,9 +21,10 @@ use winit::window::Icon; fn main() { let mut app = App::new(); - GgrsPlugin::::new() - .with_update_frequency(FPS) - .with_input_system(input) + // TODO: move GGRS plugin setup out of mains + app.add_plugins(GgrsPlugin::::default()) + .set_rollback_schedule_fps(FPS) + .add_systems(ReadInputs, input) .register_rollback_component::() .register_rollback_component::() .register_rollback_component::() @@ -45,8 +46,7 @@ fn main() { .register_rollback_component::() .register_rollback_component::() .register_rollback_component::() - .register_rollback_resource::() - .build(&mut app); + .register_rollback_resource::(); app.insert_resource(Msaa::Off) .insert_resource(ClearColor(Color::rgb(0.0, 0.3, 0.0))) diff --git a/src/player/input.rs b/src/player/input.rs index 2b3731b..759433a 100644 --- a/src/player/input.rs +++ b/src/player/input.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{prelude::*, utils::HashMap}; use bevy_ggrs::*; use bevy_matchbox::matchbox_socket::PeerId; use bytemuck::{Pod, Zeroable}; @@ -35,30 +35,36 @@ pub const INPUT_FIRE: u8 = 1 << 4; pub const INPUT_EXIT: u8 = 1 << 5; pub const INPUT_SPRINT: u8 = 1 << 6; -pub fn input(_: In, keys: Res>) -> PlayerInput { - let mut input = 0u8; +pub fn input(mut commands: Commands, keys: Res>, local_players: Res) { + let mut local_inputs = HashMap::new(); - if keys.any_pressed([KeyCode::W]) { - input |= INPUT_UP; - } - if keys.any_pressed([KeyCode::S]) { - input |= INPUT_DOWN; - } - if keys.any_pressed([KeyCode::A]) { - input |= INPUT_LEFT - } - if keys.any_pressed([KeyCode::D]) { - input |= INPUT_RIGHT; - } - if keys.any_pressed([KeyCode::Space, KeyCode::Return]) { - input |= INPUT_FIRE; - } - if keys.any_pressed([KeyCode::Escape, KeyCode::Delete]) { - input |= INPUT_EXIT; - } - if keys.pressed(KeyCode::ShiftLeft) { - input |= INPUT_SPRINT; + for handle in &local_players.0 { + let mut input: u8 = 0; + + if keys.pressed(KeyCode::W) { + input |= INPUT_UP; + } + if keys.pressed(KeyCode::S) { + input |= INPUT_DOWN; + } + if keys.pressed(KeyCode::A) { + input |= INPUT_LEFT + } + if keys.pressed(KeyCode::D) { + input |= INPUT_RIGHT; + } + if keys.any_pressed([KeyCode::Space, KeyCode::Return]) { + input |= INPUT_FIRE; + } + if keys.any_pressed([KeyCode::Escape, KeyCode::Delete]) { + input |= INPUT_EXIT; + } + if keys.pressed(KeyCode::ShiftLeft) { + input |= INPUT_SPRINT; + } + + local_inputs.insert(*handle, PlayerInput { input }); } - PlayerInput { input } + commands.insert_resource(LocalInputs::(local_inputs)); } diff --git a/src/player/systems.rs b/src/player/systems.rs index eafdc49..5b560ed 100644 --- a/src/player/systems.rs +++ b/src/player/systems.rs @@ -25,7 +25,7 @@ use crate::menu::online::PlayerCount; use crate::menu::win::MatchData; use crate::player::components::Expired; use crate::player::resources::PlayersReady; -use crate::{AppState, FIXED_TICK_MS, FPS}; +use crate::{AppState, FIXED_TICK_MS, FPS, HEALTH_BAR_Y_OFFSET}; use crate::{GameState, TILE_SIZE}; use bevy::core::FrameCount; use bevy::math::vec3; @@ -1029,25 +1029,21 @@ pub fn add_player_health_bars( custom_size: Some(Vec2::new(PLAYER_HEALTH_MAX as f32, TILE_SIZE / 4.)), ..default() }, - transform: Transform::from_xyz(0., TILE_SIZE + 10.0, 0.), + transform: Transform::from_xyz(0., HEALTH_BAR_Y_OFFSET, 0.), + ..default() + }); + cb.spawn(SpriteBundle { + // red overlay + sprite: Sprite { + color: Color::RED, + custom_size: Some(Vec2::new(PLAYER_HEALTH_MAX as f32, TILE_SIZE / 8.)), + ..default() + }, + transform: Transform::from_xyz(0., HEALTH_BAR_Y_OFFSET, 0.2), ..default() }) - .with_children(|parent| { - parent - .spawn(SpriteBundle { - // red overlay - sprite: Sprite { - color: Color::RED, - custom_size: Some(Vec2::new(PLAYER_HEALTH_MAX as f32, TILE_SIZE / 8.)), - ..default() - }, - transform: Transform::from_xyz(0., 0., 0.2), - ..default() - }) - .insert(PlayerHealthBar { health_entity }) - .add_rollback(); - }) - .add_rollback(); + // insert component used to track player health in update system + .insert(PlayerHealthBar { health_entity }); }); } trace!("insert HealthBarsAdded"); @@ -1069,6 +1065,6 @@ pub fn update_health_bars( let x_offset = half - half * health_percent; transform.scale = vec3(health_percent as f32, 1.0, 1.0); - transform.translation = vec3(-x_offset, 0., 0.2) + transform.translation = vec3(-x_offset, HEALTH_BAR_Y_OFFSET, 0.2) } }