server side attack. TODO: fix origin on shooter

This commit is contained in:
Magnus von Wachenfeldt 2022-08-15 22:46:34 +02:00
parent a8e204e60c
commit 615d9d7080
Signed by: magnus
GPG Key ID: A469F7D71D09F795
3 changed files with 40 additions and 36 deletions

View File

@ -23,7 +23,9 @@ use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle};
struct MainCamera; struct MainCamera;
#[derive(Component)] #[derive(Component)]
struct ControlledPlayer; struct ControlledPlayer {
aiming_at: Vec2,
}
#[derive(Default)] #[derive(Default)]
struct NetworkMapping(HashMap<Entity, Entity>); struct NetworkMapping(HashMap<Entity, Entity>);
@ -118,12 +120,15 @@ fn update_visulizer_system(
} }
fn player_input( fn player_input(
controlled_player_query: Query<(&Transform, &ControlledPlayer)>,
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>>,
mut player_commands: EventWriter<PlayerCommand>, mut player_commands: EventWriter<PlayerCommand>,
most_recent_tick: Res<MostRecentTick>, most_recent_tick: Res<MostRecentTick>,
) { ) {
let (transform, controlled_player) = controlled_player_query.single();
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);
@ -132,17 +137,23 @@ fn player_input(
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) {
info!(
"player at origin {} fired at {}",
transform.translation, controlled_player.aiming_at
);
player_commands.send(PlayerCommand::BasicAttack { player_commands.send(PlayerCommand::BasicAttack {
fired_at: Vec2::default(), // TODO: spawn projectiles correctly origin: Vec2::new(transform.translation.x, transform.translation.y),
direction: controlled_player.aiming_at,
}); });
} }
} }
fn cursor_system( fn cursor_system(
wnds: Res<Windows>, wnds: Res<Windows>,
q_camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>, mut q_camera: Query<(&Camera, &mut ControlledPlayer, &GlobalTransform), With<ControlledPlayer>>,
) { ) {
let (camera, camera_transform) = q_camera.single(); let (camera, mut controlled_player, camera_transform) = q_camera.single_mut();
let wnd = if let RenderTarget::Window(id) = camera.target { let wnd = if let RenderTarget::Window(id) = camera.target {
wnds.get(id).unwrap() wnds.get(id).unwrap()
@ -158,7 +169,7 @@ fn cursor_system(
let world_pos = ndc_to_world.project_point3(ndc.extend(-1.0)); let world_pos = ndc_to_world.project_point3(ndc.extend(-1.0));
let world_pos: Vec2 = world_pos.truncate(); let world_pos: Vec2 = world_pos.truncate();
info!("World coords: {}/{}", world_pos.x, world_pos.y); controlled_player.aiming_at = world_pos;
} }
} }
@ -204,7 +215,9 @@ fn client_sync_players(
}); });
if client_id == id { if client_id == id {
client_entity.insert(ControlledPlayer); client_entity.insert(ControlledPlayer {
aiming_at: Vec2::new(0., 0.),
});
} }
let player_info = PlayerInfo { let player_info = PlayerInfo {
@ -230,7 +243,7 @@ fn client_sync_players(
} }
ServerMessages::SpawnProjectile { ServerMessages::SpawnProjectile {
entity, entity,
position, origin,
direction, direction,
} => { } => {
let projectile_entity = commands.spawn_bundle(SpriteBundle { let projectile_entity = commands.spawn_bundle(SpriteBundle {
@ -278,5 +291,7 @@ fn client_sync_players(
fn setup_camera(mut commands: Commands) { fn setup_camera(mut commands: Commands) {
commands commands
.spawn_bundle(Camera2dBundle::default()) .spawn_bundle(Camera2dBundle::default())
.insert(MainCamera); .insert(ControlledPlayer {
aiming_at: Vec2::new(0., 0.),
});
} }

View File

@ -167,33 +167,22 @@ 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 fired_at } => { PlayerCommand::BasicAttack { origin, direction } => {
println!( info!(
"Received basic attack from client {}: {:?}", "spawned attack on server at origin {} and direction {}",
client_id, fired_at origin, direction
); );
if let Some(player_entity) = lobby.players.get(&client_id) { let entity = spawn_projectile(&mut commands, origin, direction);
if let Ok((_, player)) = players.get(*player_entity) { let message = ServerMessages::SpawnProjectile {
fired_at = Vec2::new( entity,
player.transform.translation.x, origin,
player.transform.translation.y, direction,
); };
let projectile_entity = let message = bincode::serialize(&message).unwrap();
spawn_projectile(&mut commands, fired_at, fired_at);
let message = ServerMessages::SpawnProjectile { server.broadcast_message(ServerChannel::ServerMessages.id(), message);
entity: projectile_entity,
position: fired_at,
direction: fired_at,
};
let message = bincode::serialize(&message).unwrap();
server.broadcast_message(ServerChannel::ServerMessages.id(), message);
}
}
} }
} }
} }

View File

@ -36,7 +36,7 @@ pub struct Jumper {
#[derive(Debug, Serialize, Deserialize, Component)] #[derive(Debug, Serialize, Deserialize, Component)]
pub enum PlayerCommand { pub enum PlayerCommand {
BasicAttack { fired_at: Vec2 }, BasicAttack { origin: Vec2, direction: Vec2 },
} }
pub enum ClientChannel { pub enum ClientChannel {
@ -60,7 +60,7 @@ pub enum ServerMessages {
}, },
SpawnProjectile { SpawnProjectile {
entity: Entity, entity: Entity,
position: Vec2, origin: Vec2,
direction: Vec2, direction: Vec2,
}, },
DespawnProjectile { DespawnProjectile {
@ -152,7 +152,7 @@ pub fn setup_level(mut _commands: Commands) {
info!("bygger level..."); info!("bygger level...");
} }
pub fn spawn_projectile(commands: &mut Commands, location: Vec2, direction: Vec2) -> Entity { pub fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2) -> Entity {
commands commands
.spawn() .spawn()
.insert(Collider::ball(0.1)) .insert(Collider::ball(0.1))
@ -160,7 +160,7 @@ pub fn spawn_projectile(commands: &mut Commands, location: Vec2, direction: Vec2
.insert(ActiveEvents::COLLISION_EVENTS) .insert(ActiveEvents::COLLISION_EVENTS)
.insert(Projectile { .insert(Projectile {
duration: Timer::from_seconds(1.5, false), duration: Timer::from_seconds(1.5, false),
location, origin,
direction, direction,
}) })
.id() .id()
@ -169,6 +169,6 @@ pub fn spawn_projectile(commands: &mut Commands, location: Vec2, direction: Vec2
#[derive(Debug, Component)] #[derive(Debug, Component)]
pub struct Projectile { pub struct Projectile {
pub duration: Timer, pub duration: Timer,
pub location: Vec2, pub origin: Vec2,
pub direction: Vec2, pub direction: Vec2,
} }