spawn projectiles at player location

This commit is contained in:
Magnus von Wachenfeldt 2022-08-16 15:23:29 +02:00
parent 5ddbf6fc7d
commit 56fdaabb1a
Signed by: magnus
GPG Key ID: A469F7D71D09F795
3 changed files with 40 additions and 30 deletions

View File

@ -13,8 +13,8 @@ use bevy_renet::{
}; };
use daggmask_shared::{ use daggmask_shared::{
client_connection_config, setup_level, ClientChannel, NetworkFrame, PlayerCommand, PlayerInput, client_connection_config, setup_level, ClientChannel, NetworkFrame, PlayerCommand, PlayerId,
ServerChannel, ServerMessages, PROTOCOL_ID, PlayerInput, ServerChannel, ServerMessages, PROTOCOL_ID,
}; };
use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle}; use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle};
@ -123,14 +123,14 @@ fn update_visulizer_system(
} }
fn player_input( fn player_input(
controlled_player_query: Query<(&Transform, &Aimer)>, controlled_player_query: Query<&Aimer>,
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, aimer) = controlled_player_query.single(); let aimer = 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 =
@ -140,13 +140,9 @@ 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!( info!("player fired at {}", aimer.aiming_at);
"player at origin {} fired at {}",
transform.translation, aimer.aiming_at
);
player_commands.send(PlayerCommand::BasicAttack { player_commands.send(PlayerCommand::BasicAttack {
origin: Vec2::new(transform.translation.x, transform.translation.y),
direction: aimer.aiming_at, direction: aimer.aiming_at,
}); });
} }
@ -218,9 +214,12 @@ fn client_sync_players(
}); });
if client_id == id { if client_id == id {
client_entity.insert(ControlledPlayer).insert(Aimer { client_entity
.insert(ControlledPlayer)
.insert(Aimer {
aiming_at: Vec2::new(0., 0.), aiming_at: Vec2::new(0., 0.),
}); })
.insert(PlayerId { id });
} }
let player_info = PlayerInfo { let player_info = PlayerInfo {

View File

@ -84,6 +84,7 @@ fn main() {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn server_update_system( fn server_update_system(
mut server_events: EventReader<ServerEvent>, mut server_events: EventReader<ServerEvent>,
pos_query: Query<(&Transform, &Player)>,
mut commands: Commands, mut commands: Commands,
mut lobby: ResMut<ServerLobby>, mut lobby: ResMut<ServerLobby>,
mut server: ResMut<RenetServer>, mut server: ResMut<RenetServer>,
@ -124,10 +125,7 @@ fn server_update_system(
.insert(ActiveEvents::COLLISION_EVENTS) .insert(ActiveEvents::COLLISION_EVENTS)
.insert(PlayerInput::default()) .insert(PlayerInput::default())
.insert(Velocity::default()) .insert(Velocity::default())
.insert(Player { .insert(Player { id: *id })
id: *id,
transform: Transform::from_xyz(10., 10., 0.),
})
.insert(Jumper { .insert(Jumper {
jump_impulse: 80., jump_impulse: 80.,
is_jumping: false, is_jumping: false,
@ -167,9 +165,14 @@ fn server_update_system(
let command: PlayerCommand = bincode::deserialize(&message).unwrap(); let command: PlayerCommand = bincode::deserialize(&message).unwrap();
match command { match command {
PlayerCommand::BasicAttack { origin, direction } => { PlayerCommand::BasicAttack { direction } => {
for (transform, player) in pos_query.iter() {
if player.id == client_id {
let origin =
Vec2::new(transform.translation.x, transform.translation.y);
info!( info!(
"spawned attack on server at origin {} and direction {}", "spawned attack on server at origin {} with direction {}",
origin, direction origin, direction
); );
@ -181,6 +184,8 @@ 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();

View File

@ -17,7 +17,11 @@ 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 transform: Transform, }
#[derive(Debug, Component)]
pub struct PlayerId {
pub id: u64,
} }
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)] #[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)]
@ -36,7 +40,7 @@ pub struct Jumper {
#[derive(Debug, Serialize, Deserialize, Component)] #[derive(Debug, Serialize, Deserialize, Component)]
pub enum PlayerCommand { pub enum PlayerCommand {
BasicAttack { origin: Vec2, direction: Vec2 }, BasicAttack { direction: Vec2 },
} }
pub enum ClientChannel { pub enum ClientChannel {
@ -144,12 +148,15 @@ pub fn setup_level(mut _commands: Commands) {
pub fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2) -> Entity { pub fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2) -> Entity {
commands commands
.spawn() .spawn()
.insert(Transform {
translation: Vec3::new(origin.x, origin.y, 0.),
..Default::default()
})
.insert(Collider::ball(0.1)) .insert(Collider::ball(0.1))
.insert(Velocity::linear(direction * 10.)) .insert(Velocity::linear(direction * 10.))
.insert(ActiveEvents::COLLISION_EVENTS) .insert(ActiveEvents::COLLISION_EVENTS)
.insert(Projectile { .insert(Projectile {
duration: Timer::from_seconds(1.5, false), duration: Timer::from_seconds(3.5, false),
origin,
direction, direction,
}) })
.id() .id()
@ -158,6 +165,5 @@ pub fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2)
#[derive(Debug, Component)] #[derive(Debug, Component)]
pub struct Projectile { pub struct Projectile {
pub duration: Timer, pub duration: Timer,
pub origin: Vec2,
pub direction: Vec2, pub direction: Vec2,
} }