diff options
Diffstat (limited to 'src/client.rs')
-rw-r--r-- | src/client.rs | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/src/client.rs b/src/client.rs index bac597e..7dbe7fa 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,6 +1,7 @@ use crate::client::network::*; use crate::protocol::*; use crate::shared::imperative::*; +use crate::shared::projectile::*; use crate::shared::*; use bevy::input::mouse::MouseButton; use bevy::prelude::*; @@ -27,8 +28,10 @@ impl Plugin for ClientPlugin { transport: self.transport.clone(), }) .add_systems(Startup, setup) - .add_systems(Update, spawn_players) + .add_systems(Update, render_players) + .add_systems(Update, render_projectiles) .add_systems(Update, move_players) + .add_systems(Update, move_projectiles) .add_systems( FixedPreUpdate, buffer_input.in_set(InputSystemSet::BufferInputs), @@ -41,7 +44,7 @@ fn setup(mut client: ClientMut, mut commands: Commands) { client.connect().unwrap(); } -fn spawn_players( +fn render_players( mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>, mut meshes: ResMut<Assets<Mesh>>, @@ -57,6 +60,30 @@ fn spawn_players( } } +fn render_projectiles( + mut commands: Commands, + mut materials: ResMut<Assets<ColorMaterial>>, + mut meshes: ResMut<Assets<Mesh>>, + projectiles: Query<(Entity, &ProjectilePosition), Added<Projectile>>, +) { + for (entity, position) in projectiles.iter() { + commands.entity(entity).insert(MaterialMesh2dBundle { + mesh: Mesh2dHandle(meshes.add(Circle { radius: 2. })), + material: materials.add(Color::RED), + transform: Transform::from_xyz(position.0.x, position.0.y, 1.), + ..Default::default() + }); + } +} + +fn move_projectiles( + mut projectiles: Query<(&mut Transform, &ProjectilePosition), Changed<ProjectilePosition>>, +) { + for (mut transform, projectile_position) in projectiles.iter_mut() { + transform.translation = Vec3::new(projectile_position.0.x, projectile_position.0.y, 0.); + } +} + fn move_players(mut players: Query<(&mut Transform, &PlayerPosition), Changed<PlayerPosition>>) { for (mut transform, player_position) in players.iter_mut() { transform.translation = Vec3::new(player_position.0.x, player_position.0.y, 0.); @@ -64,6 +91,7 @@ fn move_players(mut players: Query<(&mut Transform, &PlayerPosition), Changed<Pl } fn buffer_input( + attackables: Query<(&PlayerId, &PlayerPosition)>, cameras: Query<(&Camera, &GlobalTransform)>, mouse_input: Res<ButtonInput<MouseButton>>, mut client: ClientMut, @@ -76,7 +104,17 @@ fn buffer_input( if let Some(world_position) = camera.viewport_to_world_2d(camera_transform, cursor_position) { - client.add_input(Inputs::Imperative(Imperative::WalkTo(world_position))); + let mut attackable_player = None; + for (player, position) in attackables.iter() { + if position.0.distance(world_position) < 20. { + attackable_player = Some(player); + } + } + if let Some(target_player) = attackable_player { + client.add_input(Inputs::Imperative(Imperative::Attack(*target_player))); + } else { + client.add_input(Inputs::Imperative(Imperative::WalkTo(world_position))); + } } } } |