collision detection stuff

This commit is contained in:
Magnus von Wachenfeldt 2022-08-17 21:05:36 +02:00
parent 7fa8c25b54
commit 0fdf5b5952
Signed by: magnus
GPG Key ID: A469F7D71D09F795
3 changed files with 66 additions and 19 deletions

8
README.md Normal file
View File

@ -0,0 +1,8 @@
# daggmask
Should not really be worms, project name subject to change.
## How to run
cargo run --bin daggmask-server
cargo run --bin daggmask

View File

@ -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);
@ -139,8 +139,6 @@ fn player_input(
player_input.most_recent_tick = most_recent_tick.0;
if mouse_button_input.just_pressed(MouseButton::Left) {
info!("player fired at {}", aimer.aiming_at);
player_commands.send(PlayerCommand::BasicAttack {
direction: aimer.aiming_at,
});

View File

@ -53,6 +53,38 @@ struct ClientTicks(HashMap<u64, Option<u32>>);
const PLAYER_MOVE_SPEED: f32 = 50.0;
#[derive(PartialEq, Eq, Clone, Copy, Component)]
enum ShooterProjectileTag {
Id(u64),
}
struct ShooterProjectileFilter;
impl<'a> PhysicsHooksWithQuery<&'a ShooterProjectileTag> for ShooterProjectileFilter {
fn filter_contact_pair(
&self,
context: PairFilterContextView,
user_data: &Query<&'a ShooterProjectileTag>,
) -> Option<SolverFlags> {
let result = match user_data.get(context.collider1()).ok() {
Some(ShooterProjectileTag::Id(id1)) => match user_data.get(context.collider2()).ok() {
Some(ShooterProjectileTag::Id(id2)) => {
// HACK: bullet hitting yourself shouldn't do damage when you shoot
// TODO: find out how to be able to have bullets damage you after some time
if id1 == id2 {
None
} else {
Some(SolverFlags::COMPUTE_IMPULSES)
}
}
_ => None,
},
_ => Some(SolverFlags::COMPUTE_IMPULSES),
};
result
}
}
fn new_renet_server() -> RenetServer {
let server_addr = "127.0.0.1:5000".parse().unwrap();
let socket = UdpSocket::bind(server_addr).unwrap();
@ -70,9 +102,9 @@ fn main() {
app.add_plugins(DefaultPlugins);
app.add_plugin(RenetServerPlugin);
app.add_plugin(RapierPhysicsPlugin::<NoUserData>::default());
app.add_plugin(RapierPhysicsPlugin::<&ShooterProjectileTag>::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);
@ -92,6 +124,7 @@ fn main() {
app.add_system(despawn_projectile_system);
app.add_system_to_stage(CoreStage::PostUpdate, projectile_on_removal_system);
app.add_startup_system(setup_physics);
app.add_startup_system(setup_level);
app.add_startup_system(setup_simple_camera);
@ -118,7 +151,7 @@ fn server_update_system(
visualizer.add_client(*id);
// Initialize other players for this new client
// initialize other players for this new client
for (entity, player) in players.iter() {
let message = bincode::serialize(&ServerMessages::PlayerCreate {
id: player.id,
@ -128,7 +161,7 @@ fn server_update_system(
server.send_message(*id, ServerChannel::ServerMessages.id(), message);
}
// Spawn new player
// spawn new player
let player_entity = commands
.spawn_bundle(SpriteBundle {
sprite: Sprite {
@ -142,10 +175,12 @@ fn server_update_system(
.insert(RigidBody::Dynamic)
.insert(LockedAxes::ROTATION_LOCKED)
.insert(Collider::cuboid(5., 5.))
.insert(ActiveHooks::FILTER_CONTACT_PAIRS)
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(PlayerInput::default())
.insert(Velocity::default())
.insert(Player { id: *id })
.insert(ShooterProjectileTag::Id(*id))
.insert(Jumper {
jump_impulse: 80.,
is_jumping: false,
@ -191,12 +226,8 @@ fn server_update_system(
let origin =
Vec2::new(transform.translation().x, transform.translation().y);
info!(
"spawned attack on server at origin {} with direction {}",
origin, direction
);
let entity = spawn_projectile(&mut commands, origin, direction);
let entity =
spawn_projectile(&mut commands, player.id, origin, direction);
let message = ServerMessages::SpawnProjectile { entity, origin };
let message = bincode::serialize(&message).unwrap();
@ -295,16 +326,13 @@ fn jump_reset(
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() {
if e1.id() == entity.id() || e2.id() == entity.id() {
jumper.is_jumping = false
}
}
}
CollisionEvent::Stopped(e1, e2, _) => {
info!("collision stopped: {}, {}", e1.id(), e2.id());
}
CollisionEvent::Stopped(_e1, _e2, _) => {}
}
}
}
@ -343,7 +371,12 @@ fn projectile_on_removal_system(
}
}
fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2) -> Entity {
fn spawn_projectile(
commands: &mut Commands,
player_id: u64,
origin: Vec2,
direction: Vec2,
) -> Entity {
let normalized_direction = direction.normalize();
commands
@ -356,7 +389,9 @@ fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2) -> E
.insert(Collider::ball(0.1))
.insert(RigidBody::Dynamic)
.insert(Velocity::linear(normalized_direction * 130.))
.insert(ActiveHooks::FILTER_CONTACT_PAIRS)
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(ShooterProjectileTag::Id(player_id))
.insert(Projectile {
duration: Timer::from_seconds(3.5, false),
})
@ -379,11 +414,17 @@ 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(ActiveHooks::FILTER_CONTACT_PAIRS)
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(Floor);
}
pub fn setup_physics(mut commands: Commands) {
commands.insert_resource(PhysicsHooksWithQueryResource(Box::new(
ShooterProjectileFilter {},
)));
}
// set up the level
pub fn setup_level(mut _commands: Commands) {
fn setup_level(mut _commands: Commands) {
info!("bygger level...");
}