1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
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::*;
use bevy::sprite::{MaterialMesh2dBundle, Mesh2dHandle};
use lightyear::client::input::InputSystemSet;
use lightyear::prelude::*;
mod network;
pub fn main(transport: TransportConfig) {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(ClientPlugin { transport })
.run();
}
struct ClientPlugin {
pub transport: TransportConfig,
}
impl Plugin for ClientPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(NetworkPlugin {
transport: self.transport.clone(),
})
.add_systems(Startup, setup)
.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),
);
}
}
fn setup(mut client: ClientMut, mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
client.connect().unwrap();
}
fn render_players(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
players: Query<(Entity, &PlayerPosition, &PlayerColor), Added<PlayerId>>,
) {
for (entity, position, color) in players.iter() {
commands.entity(entity).insert(MaterialMesh2dBundle {
mesh: Mesh2dHandle(meshes.add(Circle { radius: 10. })),
material: materials.add(color.0),
transform: Transform::from_xyz(position.0.x, position.0.y, 0.),
..Default::default()
});
}
}
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.);
}
}
fn buffer_input(
attackables: Query<(&PlayerId, &PlayerPosition)>,
cameras: Query<(&Camera, &GlobalTransform)>,
mouse_input: Res<ButtonInput<MouseButton>>,
mut client: ClientMut,
windows: Query<&Window>,
) {
let window = windows.single();
let (camera, camera_transform) = cameras.single();
if mouse_input.just_pressed(MouseButton::Left) {
if let Some(cursor_position) = window.cursor_position() {
if let Some(world_position) =
camera.viewport_to_world_2d(camera_transform, cursor_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)));
}
}
}
}
}
|