server side attack. TODO: fix origin on shooter
This commit is contained in:
parent
a8e204e60c
commit
615d9d7080
@ -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.),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@ -167,26 +167,17 @@ 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) {
|
|
||||||
fired_at = Vec2::new(
|
|
||||||
player.transform.translation.x,
|
|
||||||
player.transform.translation.y,
|
|
||||||
);
|
|
||||||
|
|
||||||
let projectile_entity =
|
|
||||||
spawn_projectile(&mut commands, fired_at, fired_at);
|
|
||||||
|
|
||||||
let message = ServerMessages::SpawnProjectile {
|
let message = ServerMessages::SpawnProjectile {
|
||||||
entity: projectile_entity,
|
entity,
|
||||||
position: fired_at,
|
origin,
|
||||||
direction: fired_at,
|
direction,
|
||||||
};
|
};
|
||||||
|
|
||||||
let message = bincode::serialize(&message).unwrap();
|
let message = bincode::serialize(&message).unwrap();
|
||||||
@ -195,8 +186,6 @@ fn server_update_system(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while let Some(message) = server.receive_message(client_id, ClientChannel::Input.id()) {
|
while let Some(message) = server.receive_message(client_id, ClientChannel::Input.id()) {
|
||||||
let input: PlayerInput = bincode::deserialize(&message).unwrap();
|
let input: PlayerInput = bincode::deserialize(&message).unwrap();
|
||||||
|
@ -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,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user