jumping and stuff

This commit is contained in:
Magnus von Wachenfeldt 2022-08-14 21:05:27 +02:00
parent f24d13458a
commit 8391811448
Signed by: magnus
GPG Key ID: A469F7D71D09F795
3 changed files with 57 additions and 12 deletions

View File

@ -122,8 +122,8 @@ fn player_input(
player_input.left = keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left); player_input.left = keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left);
player_input.right = player_input.right =
keyboard_input.pressed(KeyCode::D) || keyboard_input.pressed(KeyCode::Right); keyboard_input.pressed(KeyCode::D) || keyboard_input.pressed(KeyCode::Right);
player_input.up = keyboard_input.pressed(KeyCode::W) || keyboard_input.pressed(KeyCode::Up); player_input.space = keyboard_input.pressed(KeyCode::Space);
player_input.down = keyboard_input.pressed(KeyCode::S) || keyboard_input.pressed(KeyCode::Down);
player_input.most_recent_tick = most_recent_tick.0; player_input.most_recent_tick = most_recent_tick.0;
if mouse_button_input.just_pressed(MouseButton::Left) { if mouse_button_input.just_pressed(MouseButton::Left) {

View File

@ -6,15 +6,15 @@ use bevy::{
}; };
use bevy_egui::{EguiContext, EguiPlugin}; use bevy_egui::{EguiContext, EguiPlugin};
use bevy_rapier2d::prelude::*; use bevy_rapier2d::{prelude::*, rapier::prelude::CollisionEventFlags};
use bevy_renet::{ use bevy_renet::{
renet::{RenetServer, ServerAuthentication, ServerConfig, ServerEvent}, renet::{RenetServer, ServerAuthentication, ServerConfig, ServerEvent},
RenetServerPlugin, RenetServerPlugin,
}; };
use daggmask_shared::{ use daggmask_shared::{
server_connection_config, setup_level, spawn_projectile, ClientChannel, NetworkFrame, Player, server_connection_config, setup_level, spawn_projectile, ClientChannel, Jumper, NetworkFrame,
PlayerCommand, PlayerInput, Projectile, ServerChannel, ServerMessages, PROTOCOL_ID, Player, PlayerCommand, PlayerInput, Projectile, ServerChannel, ServerMessages, PROTOCOL_ID,
}; };
use renet_visualizer::RenetServerVisualizer; use renet_visualizer::RenetServerVisualizer;
@ -65,6 +65,8 @@ fn main() {
app.add_system(server_update_system); app.add_system(server_update_system);
app.add_system(server_network_sync); app.add_system(server_network_sync);
app.add_system(move_players_system); app.add_system(move_players_system);
app.add_system(jump_system);
app.add_system(jump_reset);
app.add_system(update_projectiles_system); app.add_system(update_projectiles_system);
app.add_system(update_visualizer_system); app.add_system(update_visualizer_system);
app.add_system(despawn_projectile_system); app.add_system(despawn_projectile_system);
@ -116,13 +118,19 @@ fn server_update_system(
..Default::default() ..Default::default()
}) })
.insert(RigidBody::Dynamic) .insert(RigidBody::Dynamic)
.insert(LockedAxes::ROTATION_LOCKED)
.insert(Collider::cuboid(5., 5.)) .insert(Collider::cuboid(5., 5.))
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(PlayerInput::default()) .insert(PlayerInput::default())
.insert(Velocity::default()) .insert(Velocity::default())
.insert(Player { .insert(Player {
id: *id, id: *id,
transform: Transform::from_xyz(10., 10., 0.), transform: Transform::from_xyz(10., 10., 0.),
}) })
.insert(Jumper {
jump_impulse: 80.,
is_jumping: false,
})
.id(); .id();
lobby.players.insert(*id, player_entity); lobby.players.insert(*id, player_entity);
@ -251,18 +259,49 @@ fn server_network_sync(
fn move_players_system(mut query: Query<(&mut Velocity, &PlayerInput)>) { fn move_players_system(mut query: Query<(&mut Velocity, &PlayerInput)>) {
for (mut velocity, input) in query.iter_mut() { for (mut velocity, input) in query.iter_mut() {
let x_axis = -(input.left as i8) + input.right as i8; let x_axis = -(input.left as i8) + input.right as i8;
let y_axis = -(input.down as i8) + input.up as i8;
let mut move_delta = Vec2::new(x_axis as f32, y_axis as f32); let mut move_delta = Vec2::new(x_axis as f32, 0.);
if move_delta != Vec2::ZERO { if move_delta != Vec2::ZERO {
move_delta /= move_delta.length(); move_delta /= move_delta.length();
} }
velocity.linvel = move_delta * PLAYER_MOVE_SPEED; velocity.linvel.x = move_delta.x * PLAYER_MOVE_SPEED;
} }
} }
pub fn setup_simple_camera(mut commands: Commands) { fn jump_system(mut players: Query<(&mut Jumper, &mut Velocity, &PlayerInput)>) {
for (mut jumper, mut velocity, input) in players.iter_mut() {
if input.space && !jumper.is_jumping {
velocity.linvel.y = jumper.jump_impulse;
jumper.is_jumping = true;
}
}
}
fn jump_reset(
mut query: Query<(Entity, &mut Jumper)>,
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
}
}
}
fn setup_simple_camera(mut commands: Commands) {
commands.spawn_bundle(Camera2dBundle::default()); commands.spawn_bundle(Camera2dBundle::default());
} }
@ -310,5 +349,6 @@ fn spawn_floor(mut commands: Commands) {
..Default::default() ..Default::default()
}) })
.insert(RigidBody::Fixed) .insert(RigidBody::Fixed)
.insert(Collider::cuboid(width / 2., height / 2.)); .insert(Collider::cuboid(width / 2., height / 2.))
.insert(ActiveEvents::COLLISION_EVENTS);
} }

View File

@ -23,10 +23,15 @@ pub struct Player {
#[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>,
pub up: bool,
pub down: bool,
pub left: bool, pub left: bool,
pub right: bool, pub right: 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)]