Skip to content

Commit

Permalink
use bevy_ggrs main
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeder committed Oct 23, 2023
1 parent e05d4a5 commit 3662e71
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 54 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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"] }
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,9 +21,10 @@ use winit::window::Icon;
fn main() {
let mut app = App::new();

GgrsPlugin::<GGRSConfig>::new()
.with_update_frequency(FPS)
.with_input_system(input)
// TODO: move GGRS plugin setup out of mains
app.add_plugins(GgrsPlugin::<GGRSConfig>::default())
.set_rollback_schedule_fps(FPS)
.add_systems(ReadInputs, input)
.register_rollback_component::<Checksum>()
.register_rollback_component::<Edible>()
.register_rollback_component::<EdibleTarget>()
Expand All @@ -45,8 +46,7 @@ fn main() {
.register_rollback_component::<PlayerPoopTimer>()
.register_rollback_component::<RoundComponent>()
.register_rollback_component::<Transform>()
.register_rollback_resource::<EdibleSpawnTimer>()
.build(&mut app);
.register_rollback_resource::<EdibleSpawnTimer>();

app.insert_resource(Msaa::Off)
.insert_resource(ClearColor(Color::rgb(0.0, 0.3, 0.0)))
Expand Down
54 changes: 30 additions & 24 deletions src/player/input.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<ggrs::PlayerHandle>, keys: Res<Input<KeyCode>>) -> PlayerInput {
let mut input = 0u8;
pub fn input(mut commands: Commands, keys: Res<Input<KeyCode>>, local_players: Res<LocalPlayers>) {
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::<GGRSConfig>(local_inputs));
}
34 changes: 15 additions & 19 deletions src/player/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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)
}
}

0 comments on commit 3662e71

Please sign in to comment.