From 7fa8c25b54f8bad26591deaf599f1b362576a233 Mon Sep 17 00:00:00 2001 From: Magnus von Wachenfeldt Date: Wed, 17 Aug 2022 20:03:21 +0200 Subject: [PATCH] refactoring - move type declarations --- client/src/main.rs | 20 +++++------- server/src/main.rs | 79 +++++++++++++++++++++++++++++++++++----------- shared/src/lib.rs | 45 -------------------------- 3 files changed, 69 insertions(+), 75 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index 9c85821..2a212cb 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -13,8 +13,8 @@ use bevy_renet::{ }; use daggmask_shared::{ - client_connection_config, setup_level, ClientChannel, NetworkFrame, PlayerCommand, PlayerId, - PlayerInput, ServerChannel, ServerMessages, PROTOCOL_ID, + client_connection_config, ClientChannel, NetworkFrame, PlayerCommand, PlayerInput, + ServerChannel, ServerMessages, PROTOCOL_ID, }; use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle}; @@ -70,7 +70,7 @@ fn main() { app.add_plugins(DefaultPlugins); app.add_plugin(RenetClientPlugin); app.add_plugin(TransformPlugin); - app.add_plugin(FrameTimeDiagnosticsPlugin::default()); + //app.add_plugin(FrameTimeDiagnosticsPlugin::default()); app.add_plugin(LogDiagnosticsPlugin::default()); app.add_plugin(EguiPlugin); @@ -92,7 +92,6 @@ fn main() { app.add_system(client_sync_players.with_run_criteria(run_if_client_connected)); app.add_system(update_visulizer_system); - app.add_startup_system(setup_level); app.add_startup_system(setup_camera); app.add_system(panic_on_error_system); @@ -214,12 +213,9 @@ fn client_sync_players( }); if client_id == id { - client_entity - .insert(ControlledPlayer) - .insert(Aimer { - aiming_at: Vec2::new(0., 0.), - }) - .insert(PlayerId { id }); + client_entity.insert(ControlledPlayer).insert(Aimer { + aiming_at: Vec2::new(0., 0.), + }); } let player_info = PlayerInfo { @@ -247,8 +243,8 @@ fn client_sync_players( let projectile_entity = commands .spawn_bundle(SpriteBundle { sprite: Sprite { - color: Color::rgb(0.25, 0.25, 0.75), - custom_size: Some(Vec2::new(5., 5.0)), + color: Color::rgb(0.75, 0.75, 0.75), + custom_size: Some(Vec2::new(1., 1.)), ..default() }, ..default() diff --git a/server/src/main.rs b/server/src/main.rs index 182616d..f260662 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -14,8 +14,8 @@ use bevy_renet::{ }; use daggmask_shared::{ - server_connection_config, setup_level, spawn_projectile, ClientChannel, Jumper, NetworkFrame, - Player, PlayerCommand, PlayerInput, Projectile, ServerChannel, ServerMessages, PROTOCOL_ID, + server_connection_config, ClientChannel, NetworkFrame, PlayerCommand, PlayerInput, + ServerChannel, ServerMessages, PROTOCOL_ID, }; use renet_visualizer::RenetServerVisualizer; @@ -25,6 +25,25 @@ pub struct ServerLobby { pub players: HashMap, } +#[derive(Debug, Component)] +pub struct Player { + pub id: u64, +} + +#[derive(Debug, Component)] +pub struct Projectile { + pub duration: Timer, +} + +#[derive(Debug, Component)] +pub struct Floor; + +#[derive(Debug, Component)] +pub struct Jumper { + pub jump_impulse: f32, + pub is_jumping: bool, +} + #[derive(Debug, Default)] struct NetworkTick(u32); @@ -53,7 +72,7 @@ fn main() { app.add_plugin(RenetServerPlugin); app.add_plugin(RapierPhysicsPlugin::::default()); app.add_plugin(RapierDebugRenderPlugin::default()); - app.add_plugin(FrameTimeDiagnosticsPlugin::default()); + //app.add_plugin(FrameTimeDiagnosticsPlugin::default()); app.add_plugin(LogDiagnosticsPlugin::default()); app.add_plugin(EguiPlugin); @@ -274,20 +293,18 @@ fn jump_reset( mut collision_events: EventReader, ) { for collision_event in collision_events.iter() { - for (entity, mut jumper) in query.iter_mut() { - set_jumping_false_if_touching_floor(entity, &mut jumper, collision_event); - } - } -} - -fn set_jumping_false_if_touching_floor( - entity: Entity, - jumper: &mut Jumper, - event: &CollisionEvent, -) { - if let CollisionEvent::Started(h1, h2, _) = event { - if h1.id() == entity.id() || h2.id() == entity.id() { - jumper.is_jumping = false + match collision_event { + CollisionEvent::Started(e1, e2, _) => { + info!("collision started: e1: {}, e2: {}", e1.id(), e2.id()); + for (entity, mut jumper) in query.iter_mut() { + if e1.id() == entity.id() || e2.id() == entity.id() { + jumper.is_jumping = false + } + } + } + CollisionEvent::Stopped(e1, e2, _) => { + info!("collision stopped: {}, {}", e1.id(), e2.id()); + } } } } @@ -326,6 +343,26 @@ fn projectile_on_removal_system( } } +fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2) -> Entity { + let normalized_direction = direction.normalize(); + + commands + .spawn() + .insert_bundle(TransformBundle::from(Transform::from_xyz( + origin.x + normalized_direction.x, + origin.y + normalized_direction.y, + 0., + ))) + .insert(Collider::ball(0.1)) + .insert(RigidBody::Dynamic) + .insert(Velocity::linear(normalized_direction * 130.)) + .insert(ActiveEvents::COLLISION_EVENTS) + .insert(Projectile { + duration: Timer::from_seconds(3.5, false), + }) + .id() +} + fn spawn_floor(mut commands: Commands) { let width = 300.; let height = 3.; @@ -342,5 +379,11 @@ fn spawn_floor(mut commands: Commands) { .insert_bundle(TransformBundle::from(Transform::from_xyz(-50., 0., 0.))) .insert(RigidBody::Fixed) .insert(Collider::cuboid(width / 2., height / 2.)) - .insert(ActiveEvents::COLLISION_EVENTS); + .insert(ActiveEvents::COLLISION_EVENTS) + .insert(Floor); +} + +// set up the level +pub fn setup_level(mut _commands: Commands) { + info!("bygger level..."); } diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 24d3ab2..309beeb 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -6,24 +6,11 @@ use bevy_renet::renet::{ NETCODE_KEY_BYTES, }; -use bevy_rapier2d::geometry::Collider; -use bevy_rapier2d::prelude::*; - use serde::{Deserialize, Serialize}; pub const PRIVATE_KEY: &[u8; NETCODE_KEY_BYTES] = b"en grisars katt hund hemlis key."; // 32-bytes pub const PROTOCOL_ID: u64 = 7; -#[derive(Debug, Component)] -pub struct Player { - pub id: u64, -} - -#[derive(Debug, Component)] -pub struct PlayerId { - pub id: u64, -} - #[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)] pub struct PlayerInput { pub most_recent_tick: Option, @@ -32,12 +19,6 @@ pub struct PlayerInput { pub space: bool, } -#[derive(Debug, Component)] -pub struct Jumper { - pub jump_impulse: f32, - pub is_jumping: bool, -} - #[derive(Debug, Serialize, Deserialize, Component)] pub enum PlayerCommand { BasicAttack { direction: Vec2 }, @@ -139,29 +120,3 @@ pub fn server_connection_config() -> RenetConnectionConfig { ..Default::default() } } - -/// set up the level -pub fn setup_level(mut _commands: Commands) { - info!("bygger level..."); -} - -pub fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2) -> Entity { - commands - .spawn() - .insert_bundle(TransformBundle::from(Transform::from_xyz( - origin.x, origin.y, 0., - ))) - //.insert(Collider::ball(0.1)) - .insert(RigidBody::Dynamic) - .insert(Velocity::linear(direction.normalize() * 130.)) - //.insert(ActiveEvents::COLLISION_EVENTS) - .insert(Projectile { - duration: Timer::from_seconds(3.5, false), - }) - .id() -} - -#[derive(Debug, Component)] -pub struct Projectile { - pub duration: Timer, -}