jumping and stuff
This commit is contained in:
parent
f24d13458a
commit
8391811448
@ -122,8 +122,8 @@ fn player_input(
|
||||
player_input.left = keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left);
|
||||
player_input.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.down = keyboard_input.pressed(KeyCode::S) || keyboard_input.pressed(KeyCode::Down);
|
||||
player_input.space = keyboard_input.pressed(KeyCode::Space);
|
||||
|
||||
player_input.most_recent_tick = most_recent_tick.0;
|
||||
|
||||
if mouse_button_input.just_pressed(MouseButton::Left) {
|
||||
|
@ -6,15 +6,15 @@ use bevy::{
|
||||
};
|
||||
|
||||
use bevy_egui::{EguiContext, EguiPlugin};
|
||||
use bevy_rapier2d::prelude::*;
|
||||
use bevy_rapier2d::{prelude::*, rapier::prelude::CollisionEventFlags};
|
||||
use bevy_renet::{
|
||||
renet::{RenetServer, ServerAuthentication, ServerConfig, ServerEvent},
|
||||
RenetServerPlugin,
|
||||
};
|
||||
|
||||
use daggmask_shared::{
|
||||
server_connection_config, setup_level, spawn_projectile, ClientChannel, NetworkFrame, Player,
|
||||
PlayerCommand, PlayerInput, Projectile, ServerChannel, ServerMessages, PROTOCOL_ID,
|
||||
server_connection_config, setup_level, spawn_projectile, ClientChannel, Jumper, NetworkFrame,
|
||||
Player, PlayerCommand, PlayerInput, Projectile, ServerChannel, ServerMessages, PROTOCOL_ID,
|
||||
};
|
||||
|
||||
use renet_visualizer::RenetServerVisualizer;
|
||||
@ -65,6 +65,8 @@ fn main() {
|
||||
app.add_system(server_update_system);
|
||||
app.add_system(server_network_sync);
|
||||
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_visualizer_system);
|
||||
app.add_system(despawn_projectile_system);
|
||||
@ -116,13 +118,19 @@ fn server_update_system(
|
||||
..Default::default()
|
||||
})
|
||||
.insert(RigidBody::Dynamic)
|
||||
.insert(LockedAxes::ROTATION_LOCKED)
|
||||
.insert(Collider::cuboid(5., 5.))
|
||||
.insert(ActiveEvents::COLLISION_EVENTS)
|
||||
.insert(PlayerInput::default())
|
||||
.insert(Velocity::default())
|
||||
.insert(Player {
|
||||
id: *id,
|
||||
transform: Transform::from_xyz(10., 10., 0.),
|
||||
})
|
||||
.insert(Jumper {
|
||||
jump_impulse: 80.,
|
||||
is_jumping: false,
|
||||
})
|
||||
.id();
|
||||
|
||||
lobby.players.insert(*id, player_entity);
|
||||
@ -251,18 +259,49 @@ fn server_network_sync(
|
||||
fn move_players_system(mut query: Query<(&mut Velocity, &PlayerInput)>) {
|
||||
for (mut velocity, input) in query.iter_mut() {
|
||||
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 {
|
||||
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());
|
||||
}
|
||||
|
||||
@ -310,5 +349,6 @@ fn spawn_floor(mut commands: Commands) {
|
||||
..Default::default()
|
||||
})
|
||||
.insert(RigidBody::Fixed)
|
||||
.insert(Collider::cuboid(width / 2., height / 2.));
|
||||
.insert(Collider::cuboid(width / 2., height / 2.))
|
||||
.insert(ActiveEvents::COLLISION_EVENTS);
|
||||
}
|
||||
|
@ -23,10 +23,15 @@ pub struct Player {
|
||||
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)]
|
||||
pub struct PlayerInput {
|
||||
pub most_recent_tick: Option<u32>,
|
||||
pub up: bool,
|
||||
pub down: bool,
|
||||
pub left: 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)]
|
||||
|
Loading…
Reference in New Issue
Block a user