cleanup 3d stuff
This commit is contained in:
parent
b46a22539e
commit
de769004af
@ -116,7 +116,6 @@ fn player_input(
|
|||||||
keyboard_input: Res<Input<KeyCode>>,
|
keyboard_input: Res<Input<KeyCode>>,
|
||||||
mut player_input: ResMut<PlayerInput>,
|
mut player_input: ResMut<PlayerInput>,
|
||||||
mouse_button_input: Res<Input<MouseButton>>,
|
mouse_button_input: Res<Input<MouseButton>>,
|
||||||
target_query: Query<&Transform, With<Target>>,
|
|
||||||
mut player_commands: EventWriter<PlayerCommand>,
|
mut player_commands: EventWriter<PlayerCommand>,
|
||||||
most_recent_tick: Res<MostRecentTick>,
|
most_recent_tick: Res<MostRecentTick>,
|
||||||
) {
|
) {
|
||||||
@ -129,7 +128,7 @@ fn player_input(
|
|||||||
|
|
||||||
if mouse_button_input.just_pressed(MouseButton::Left) {
|
if mouse_button_input.just_pressed(MouseButton::Left) {
|
||||||
player_commands.send(PlayerCommand::BasicAttack {
|
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(
|
fn client_sync_players(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
||||||
mut client: ResMut<RenetClient>,
|
mut client: ResMut<RenetClient>,
|
||||||
mut lobby: ResMut<ClientLobby>,
|
mut lobby: ResMut<ClientLobby>,
|
||||||
mut network_mapping: ResMut<NetworkMapping>,
|
mut network_mapping: ResMut<NetworkMapping>,
|
||||||
@ -165,11 +162,7 @@ fn client_sync_players(
|
|||||||
let server_message = bincode::deserialize(&message).unwrap();
|
let server_message = bincode::deserialize(&message).unwrap();
|
||||||
|
|
||||||
match server_message {
|
match server_message {
|
||||||
ServerMessages::PlayerCreate {
|
ServerMessages::PlayerCreate { id, entity } => {
|
||||||
id,
|
|
||||||
translation,
|
|
||||||
entity,
|
|
||||||
} => {
|
|
||||||
println!("Player {} connected.", id);
|
println!("Player {} connected.", id);
|
||||||
|
|
||||||
let mut client_entity = commands.spawn_bundle(SpriteBundle {
|
let mut client_entity = commands.spawn_bundle(SpriteBundle {
|
||||||
@ -208,7 +201,7 @@ fn client_sync_players(
|
|||||||
}
|
}
|
||||||
ServerMessages::SpawnProjectile {
|
ServerMessages::SpawnProjectile {
|
||||||
entity,
|
entity,
|
||||||
location,
|
position,
|
||||||
direction,
|
direction,
|
||||||
} => {
|
} => {
|
||||||
let projectile_entity = commands.spawn_bundle(SpriteBundle {
|
let projectile_entity = commands.spawn_bundle(SpriteBundle {
|
||||||
@ -256,6 +249,3 @@ fn client_sync_players(
|
|||||||
fn setup_camera(mut commands: Commands) {
|
fn setup_camera(mut commands: Commands) {
|
||||||
commands.spawn_bundle(Camera2dBundle::default());
|
commands.spawn_bundle(Camera2dBundle::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
struct Target;
|
|
||||||
|
@ -80,13 +80,11 @@ fn main() {
|
|||||||
fn server_update_system(
|
fn server_update_system(
|
||||||
mut server_events: EventReader<ServerEvent>,
|
mut server_events: EventReader<ServerEvent>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
||||||
mut lobby: ResMut<ServerLobby>,
|
mut lobby: ResMut<ServerLobby>,
|
||||||
mut server: ResMut<RenetServer>,
|
mut server: ResMut<RenetServer>,
|
||||||
mut visualizer: ResMut<RenetServerVisualizer<200>>,
|
mut visualizer: ResMut<RenetServerVisualizer<200>>,
|
||||||
mut client_ticks: ResMut<ClientTicks>,
|
mut client_ticks: ResMut<ClientTicks>,
|
||||||
players: Query<(Entity, &Player, &Transform)>,
|
players: Query<(Entity, &Player)>,
|
||||||
) {
|
) {
|
||||||
for event in server_events.iter() {
|
for event in server_events.iter() {
|
||||||
match event {
|
match event {
|
||||||
@ -96,19 +94,16 @@ fn server_update_system(
|
|||||||
visualizer.add_client(*id);
|
visualizer.add_client(*id);
|
||||||
|
|
||||||
// Initialize other players for this new client
|
// Initialize other players for this new client
|
||||||
for (entity, player, transform) in players.iter() {
|
for (entity, player) in players.iter() {
|
||||||
let translation: [f32; 3] = transform.translation.into();
|
|
||||||
let message = bincode::serialize(&ServerMessages::PlayerCreate {
|
let message = bincode::serialize(&ServerMessages::PlayerCreate {
|
||||||
id: player.id,
|
id: player.id,
|
||||||
entity,
|
entity,
|
||||||
translation,
|
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
server.send_message(*id, ServerChannel::ServerMessages.id(), message);
|
server.send_message(*id, ServerChannel::ServerMessages.id(), message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spawn new player
|
// Spawn new player
|
||||||
let transform = Transform::from_xyz(0., 0.51, 0.);
|
|
||||||
let player_entity = commands
|
let player_entity = commands
|
||||||
.spawn_bundle(SpriteBundle {
|
.spawn_bundle(SpriteBundle {
|
||||||
sprite: Sprite {
|
sprite: Sprite {
|
||||||
@ -125,17 +120,15 @@ fn server_update_system(
|
|||||||
.insert(Velocity::default())
|
.insert(Velocity::default())
|
||||||
.insert(Player {
|
.insert(Player {
|
||||||
id: *id,
|
id: *id,
|
||||||
location: Vec2::new(10., 10.),
|
position: Vec2::new(10., 10.),
|
||||||
})
|
})
|
||||||
.id();
|
.id();
|
||||||
|
|
||||||
lobby.players.insert(*id, player_entity);
|
lobby.players.insert(*id, player_entity);
|
||||||
|
|
||||||
let translation: [f32; 3] = transform.translation.into();
|
|
||||||
let message = bincode::serialize(&ServerMessages::PlayerCreate {
|
let message = bincode::serialize(&ServerMessages::PlayerCreate {
|
||||||
id: *id,
|
id: *id,
|
||||||
entity: player_entity,
|
entity: player_entity,
|
||||||
translation,
|
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -164,23 +157,23 @@ fn server_update_system(
|
|||||||
let command: PlayerCommand = bincode::deserialize(&message).unwrap();
|
let command: PlayerCommand = bincode::deserialize(&message).unwrap();
|
||||||
|
|
||||||
match command {
|
match command {
|
||||||
PlayerCommand::BasicAttack { mut cast_at } => {
|
PlayerCommand::BasicAttack { mut fired_at } => {
|
||||||
println!(
|
println!(
|
||||||
"Received basic attack from client {}: {:?}",
|
"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 Some(player_entity) = lobby.players.get(&client_id) {
|
||||||
if let Ok((_, _, player_transform)) = players.get(*player_entity) {
|
if let Ok((_, player)) = players.get(*player_entity) {
|
||||||
cast_at[1] = player_transform.translation[1];
|
fired_at = player.position;
|
||||||
|
|
||||||
let projectile_entity =
|
let projectile_entity =
|
||||||
spawn_projectile(&mut commands, cast_at, cast_at);
|
spawn_projectile(&mut commands, fired_at, fired_at);
|
||||||
|
|
||||||
let message = ServerMessages::SpawnProjectile {
|
let message = ServerMessages::SpawnProjectile {
|
||||||
entity: projectile_entity,
|
entity: projectile_entity,
|
||||||
location: cast_at,
|
position: fired_at,
|
||||||
direction: cast_at,
|
direction: fired_at,
|
||||||
};
|
};
|
||||||
|
|
||||||
let message = bincode::serialize(&message).unwrap();
|
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() {
|
for (mut velocity, input) in query.iter_mut() {
|
||||||
let x = (input.right as i8 - input.left as i8) as f32;
|
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 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.x = x * PLAYER_MOVE_SPEED;
|
||||||
velocity.linvel.y = direction.y * PLAYER_MOVE_SPEED;
|
velocity.linvel.y = y * PLAYER_MOVE_SPEED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ pub const PROTOCOL_ID: u64 = 7;
|
|||||||
#[derive(Debug, Component)]
|
#[derive(Debug, Component)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub location: Vec2,
|
pub position: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)]
|
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)]
|
||||||
@ -31,7 +31,7 @@ pub struct PlayerInput {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Component)]
|
#[derive(Debug, Serialize, Deserialize, Component)]
|
||||||
pub enum PlayerCommand {
|
pub enum PlayerCommand {
|
||||||
BasicAttack { cast_at: Vec2 },
|
BasicAttack { fired_at: Vec2 },
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ClientChannel {
|
pub enum ClientChannel {
|
||||||
@ -49,14 +49,13 @@ pub enum ServerMessages {
|
|||||||
PlayerCreate {
|
PlayerCreate {
|
||||||
entity: Entity,
|
entity: Entity,
|
||||||
id: u64,
|
id: u64,
|
||||||
translation: [f32; 3],
|
|
||||||
},
|
},
|
||||||
PlayerRemove {
|
PlayerRemove {
|
||||||
id: u64,
|
id: u64,
|
||||||
},
|
},
|
||||||
SpawnProjectile {
|
SpawnProjectile {
|
||||||
entity: Entity,
|
entity: Entity,
|
||||||
location: Vec2,
|
position: Vec2,
|
||||||
direction: Vec2,
|
direction: Vec2,
|
||||||
},
|
},
|
||||||
DespawnProjectile {
|
DespawnProjectile {
|
||||||
|
Loading…
Reference in New Issue
Block a user