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

View File

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

View File

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