cleanup 3d stuff

This commit is contained in:
Magnus von Wachenfeldt 2022-08-11 13:43:48 +02:00
parent b46a22539e
commit de769004af
Signed by: magnus
GPG Key ID: A469F7D71D09F795
3 changed files with 18 additions and 37 deletions

View File

@ -116,7 +116,6 @@ fn player_input(
keyboard_input: Res<Input<KeyCode>>,
mut player_input: ResMut<PlayerInput>,
mouse_button_input: Res<Input<MouseButton>>,
target_query: Query<&Transform, With<Target>>,
mut player_commands: EventWriter<PlayerCommand>,
most_recent_tick: Res<MostRecentTick>,
) {
@ -129,7 +128,7 @@ fn player_input(
if mouse_button_input.just_pressed(MouseButton::Left) {
player_commands.send(PlayerCommand::BasicAttack {
cast_at: Vec2::default(), // TODO: spawn projectiles correctly
fired_at: Vec2::default(), // TODO: spawn projectiles correctly
});
}
}
@ -152,8 +151,6 @@ fn client_send_player_commands(
fn client_sync_players(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut client: ResMut<RenetClient>,
mut lobby: ResMut<ClientLobby>,
mut network_mapping: ResMut<NetworkMapping>,
@ -165,11 +162,7 @@ fn client_sync_players(
let server_message = bincode::deserialize(&message).unwrap();
match server_message {
ServerMessages::PlayerCreate {
id,
translation,
entity,
} => {
ServerMessages::PlayerCreate { id, entity } => {
println!("Player {} connected.", id);
let mut client_entity = commands.spawn_bundle(SpriteBundle {
@ -208,7 +201,7 @@ fn client_sync_players(
}
ServerMessages::SpawnProjectile {
entity,
location,
position,
direction,
} => {
let projectile_entity = commands.spawn_bundle(SpriteBundle {
@ -256,6 +249,3 @@ fn client_sync_players(
fn setup_camera(mut commands: Commands) {
commands.spawn_bundle(Camera2dBundle::default());
}
#[derive(Component)]
struct Target;

View File

@ -80,13 +80,11 @@ fn main() {
fn server_update_system(
mut server_events: EventReader<ServerEvent>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut lobby: ResMut<ServerLobby>,
mut server: ResMut<RenetServer>,
mut visualizer: ResMut<RenetServerVisualizer<200>>,
mut client_ticks: ResMut<ClientTicks>,
players: Query<(Entity, &Player, &Transform)>,
players: Query<(Entity, &Player)>,
) {
for event in server_events.iter() {
match event {
@ -96,19 +94,16 @@ fn server_update_system(
visualizer.add_client(*id);
// Initialize other players for this new client
for (entity, player, transform) in players.iter() {
let translation: [f32; 3] = transform.translation.into();
for (entity, player) in players.iter() {
let message = bincode::serialize(&ServerMessages::PlayerCreate {
id: player.id,
entity,
translation,
})
.unwrap();
server.send_message(*id, ServerChannel::ServerMessages.id(), message);
}
// Spawn new player
let transform = Transform::from_xyz(0., 0.51, 0.);
let player_entity = commands
.spawn_bundle(SpriteBundle {
sprite: Sprite {
@ -125,17 +120,15 @@ fn server_update_system(
.insert(Velocity::default())
.insert(Player {
id: *id,
location: Vec2::new(10., 10.),
position: Vec2::new(10., 10.),
})
.id();
lobby.players.insert(*id, player_entity);
let translation: [f32; 3] = transform.translation.into();
let message = bincode::serialize(&ServerMessages::PlayerCreate {
id: *id,
entity: player_entity,
translation,
})
.unwrap();
@ -164,23 +157,23 @@ fn server_update_system(
let command: PlayerCommand = bincode::deserialize(&message).unwrap();
match command {
PlayerCommand::BasicAttack { mut cast_at } => {
PlayerCommand::BasicAttack { mut fired_at } => {
println!(
"Received basic attack from client {}: {:?}",
client_id, cast_at
client_id, fired_at
);
if let Some(player_entity) = lobby.players.get(&client_id) {
if let Ok((_, _, player_transform)) = players.get(*player_entity) {
cast_at[1] = player_transform.translation[1];
if let Ok((_, player)) = players.get(*player_entity) {
fired_at = player.position;
let projectile_entity =
spawn_projectile(&mut commands, cast_at, cast_at);
spawn_projectile(&mut commands, fired_at, fired_at);
let message = ServerMessages::SpawnProjectile {
entity: projectile_entity,
location: cast_at,
direction: cast_at,
position: fired_at,
direction: fired_at,
};
let message = bincode::serialize(&message).unwrap();
@ -255,10 +248,9 @@ fn move_players_system(mut query: Query<(&mut Velocity, &PlayerInput)>) {
for (mut velocity, input) in query.iter_mut() {
let x = (input.right as i8 - input.left as i8) as f32;
let y = (input.down as i8 - input.up as i8) as f32;
let direction = Vec2::new(x, y).normalize_or_zero();
velocity.linvel.x = direction.x * PLAYER_MOVE_SPEED;
velocity.linvel.y = direction.y * PLAYER_MOVE_SPEED;
velocity.linvel.x = x * PLAYER_MOVE_SPEED;
velocity.linvel.y = y * PLAYER_MOVE_SPEED;
}
}

View File

@ -17,7 +17,7 @@ pub const PROTOCOL_ID: u64 = 7;
#[derive(Debug, Component)]
pub struct Player {
pub id: u64,
pub location: Vec2,
pub position: Vec2,
}
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)]
@ -31,7 +31,7 @@ pub struct PlayerInput {
#[derive(Debug, Serialize, Deserialize, Component)]
pub enum PlayerCommand {
BasicAttack { cast_at: Vec2 },
BasicAttack { fired_at: Vec2 },
}
pub enum ClientChannel {
@ -49,14 +49,13 @@ pub enum ServerMessages {
PlayerCreate {
entity: Entity,
id: u64,
translation: [f32; 3],
},
PlayerRemove {
id: u64,
},
SpawnProjectile {
entity: Entity,
location: Vec2,
position: Vec2,
direction: Vec2,
},
DespawnProjectile {