refactoring - move type declarations

This commit is contained in:
2022-08-17 20:03:21 +02:00
parent 164f248066
commit 7fa8c25b54
3 changed files with 69 additions and 75 deletions

View File

@@ -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<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)]
struct NetworkTick(u32);
@@ -53,7 +72,7 @@ fn main() {
app.add_plugin(RenetServerPlugin);
app.add_plugin(RapierPhysicsPlugin::<NoUserData>::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<CollisionEvent>,
) {
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...");
}