refactoring - move type declarations

This commit is contained in:
Magnus von Wachenfeldt 2022-08-17 20:03:21 +02:00
parent 164f248066
commit 7fa8c25b54
Signed by: magnus
GPG Key ID: A469F7D71D09F795
3 changed files with 69 additions and 75 deletions

View File

@ -13,8 +13,8 @@ use bevy_renet::{
}; };
use daggmask_shared::{ use daggmask_shared::{
client_connection_config, setup_level, ClientChannel, NetworkFrame, PlayerCommand, PlayerId, client_connection_config, ClientChannel, NetworkFrame, PlayerCommand, PlayerInput,
PlayerInput, ServerChannel, ServerMessages, PROTOCOL_ID, ServerChannel, ServerMessages, PROTOCOL_ID,
}; };
use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle}; use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle};
@ -70,7 +70,7 @@ fn main() {
app.add_plugins(DefaultPlugins); app.add_plugins(DefaultPlugins);
app.add_plugin(RenetClientPlugin); app.add_plugin(RenetClientPlugin);
app.add_plugin(TransformPlugin); app.add_plugin(TransformPlugin);
app.add_plugin(FrameTimeDiagnosticsPlugin::default()); //app.add_plugin(FrameTimeDiagnosticsPlugin::default());
app.add_plugin(LogDiagnosticsPlugin::default()); app.add_plugin(LogDiagnosticsPlugin::default());
app.add_plugin(EguiPlugin); 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(client_sync_players.with_run_criteria(run_if_client_connected));
app.add_system(update_visulizer_system); app.add_system(update_visulizer_system);
app.add_startup_system(setup_level);
app.add_startup_system(setup_camera); app.add_startup_system(setup_camera);
app.add_system(panic_on_error_system); app.add_system(panic_on_error_system);
@ -214,12 +213,9 @@ fn client_sync_players(
}); });
if client_id == id { if client_id == id {
client_entity client_entity.insert(ControlledPlayer).insert(Aimer {
.insert(ControlledPlayer)
.insert(Aimer {
aiming_at: Vec2::new(0., 0.), aiming_at: Vec2::new(0., 0.),
}) });
.insert(PlayerId { id });
} }
let player_info = PlayerInfo { let player_info = PlayerInfo {
@ -247,8 +243,8 @@ fn client_sync_players(
let projectile_entity = commands let projectile_entity = commands
.spawn_bundle(SpriteBundle { .spawn_bundle(SpriteBundle {
sprite: Sprite { sprite: Sprite {
color: Color::rgb(0.25, 0.25, 0.75), color: Color::rgb(0.75, 0.75, 0.75),
custom_size: Some(Vec2::new(5., 5.0)), custom_size: Some(Vec2::new(1., 1.)),
..default() ..default()
}, },
..default() ..default()

View File

@ -14,8 +14,8 @@ use bevy_renet::{
}; };
use daggmask_shared::{ use daggmask_shared::{
server_connection_config, setup_level, spawn_projectile, ClientChannel, Jumper, NetworkFrame, server_connection_config, ClientChannel, NetworkFrame, PlayerCommand, PlayerInput,
Player, PlayerCommand, PlayerInput, Projectile, ServerChannel, ServerMessages, PROTOCOL_ID, ServerChannel, ServerMessages, PROTOCOL_ID,
}; };
use renet_visualizer::RenetServerVisualizer; use renet_visualizer::RenetServerVisualizer;
@ -25,6 +25,25 @@ pub struct ServerLobby {
pub players: HashMap<u64, Entity>, pub players: HashMap<u64, Entity>,
} }
#[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)] #[derive(Debug, Default)]
struct NetworkTick(u32); struct NetworkTick(u32);
@ -53,7 +72,7 @@ fn main() {
app.add_plugin(RenetServerPlugin); app.add_plugin(RenetServerPlugin);
app.add_plugin(RapierPhysicsPlugin::<NoUserData>::default()); app.add_plugin(RapierPhysicsPlugin::<NoUserData>::default());
app.add_plugin(RapierDebugRenderPlugin::default()); app.add_plugin(RapierDebugRenderPlugin::default());
app.add_plugin(FrameTimeDiagnosticsPlugin::default()); //app.add_plugin(FrameTimeDiagnosticsPlugin::default());
app.add_plugin(LogDiagnosticsPlugin::default()); app.add_plugin(LogDiagnosticsPlugin::default());
app.add_plugin(EguiPlugin); app.add_plugin(EguiPlugin);
@ -274,22 +293,20 @@ fn jump_reset(
mut collision_events: EventReader<CollisionEvent>, mut collision_events: EventReader<CollisionEvent>,
) { ) {
for collision_event in collision_events.iter() { for collision_event in collision_events.iter() {
match collision_event {
CollisionEvent::Started(e1, e2, _) => {
info!("collision started: e1: {}, e2: {}", e1.id(), e2.id());
for (entity, mut jumper) in query.iter_mut() { for (entity, mut jumper) in query.iter_mut() {
set_jumping_false_if_touching_floor(entity, &mut jumper, collision_event); if e1.id() == entity.id() || e2.id() == entity.id() {
}
}
}
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 jumper.is_jumping = false
} }
} }
}
CollisionEvent::Stopped(e1, e2, _) => {
info!("collision stopped: {}, {}", e1.id(), e2.id());
}
}
}
} }
fn setup_simple_camera(mut commands: Commands) { fn setup_simple_camera(mut commands: Commands) {
@ -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) { fn spawn_floor(mut commands: Commands) {
let width = 300.; let width = 300.;
let height = 3.; let height = 3.;
@ -342,5 +379,11 @@ fn spawn_floor(mut commands: Commands) {
.insert_bundle(TransformBundle::from(Transform::from_xyz(-50., 0., 0.))) .insert_bundle(TransformBundle::from(Transform::from_xyz(-50., 0., 0.)))
.insert(RigidBody::Fixed) .insert(RigidBody::Fixed)
.insert(Collider::cuboid(width / 2., height / 2.)) .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...");
} }

View File

@ -6,24 +6,11 @@ use bevy_renet::renet::{
NETCODE_KEY_BYTES, NETCODE_KEY_BYTES,
}; };
use bevy_rapier2d::geometry::Collider;
use bevy_rapier2d::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub const PRIVATE_KEY: &[u8; NETCODE_KEY_BYTES] = b"en grisars katt hund hemlis key."; // 32-bytes pub const PRIVATE_KEY: &[u8; NETCODE_KEY_BYTES] = b"en grisars katt hund hemlis key."; // 32-bytes
pub const PROTOCOL_ID: u64 = 7; 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)] #[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)]
pub struct PlayerInput { pub struct PlayerInput {
pub most_recent_tick: Option<u32>, pub most_recent_tick: Option<u32>,
@ -32,12 +19,6 @@ pub struct PlayerInput {
pub space: bool, pub space: bool,
} }
#[derive(Debug, Component)]
pub struct Jumper {
pub jump_impulse: f32,
pub is_jumping: bool,
}
#[derive(Debug, Serialize, Deserialize, Component)] #[derive(Debug, Serialize, Deserialize, Component)]
pub enum PlayerCommand { pub enum PlayerCommand {
BasicAttack { direction: Vec2 }, BasicAttack { direction: Vec2 },
@ -139,29 +120,3 @@ pub fn server_connection_config() -> RenetConnectionConfig {
..Default::default() ..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,
}