-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimple_box.rs
306 lines (271 loc) · 9.68 KB
/
simple_box.rs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! A simple demo to showcase how player could send inputs to move a box and server replicates position back.
//! Also demonstrates the single-player and how sever also could be a player.
use std::{
error::Error,
hash::{DefaultHasher, Hash, Hasher},
net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket},
time::SystemTime,
};
use bevy::{
color::palettes::css::GREEN,
prelude::*,
winit::{UpdateMode::Continuous, WinitSettings},
};
use bevy_replicon::prelude::*;
use bevy_replicon_renet::{
RenetChannelsExt, RepliconRenetPlugins,
netcode::{
ClientAuthentication, NetcodeClientTransport, NetcodeServerTransport, ServerAuthentication,
ServerConfig,
},
renet::{ConnectionConfig, RenetClient, RenetServer},
};
use clap::Parser;
use serde::{Deserialize, Serialize};
fn main() {
App::new()
.init_resource::<Cli>() // Parse CLI before creating window.
// Makes the server/client update continuously even while unfocused.
.insert_resource(WinitSettings {
focused_mode: Continuous,
unfocused_mode: Continuous,
})
.add_plugins((
DefaultPlugins,
RepliconPlugins,
RepliconRenetPlugins,
SimpleBoxPlugin,
))
.run();
}
struct SimpleBoxPlugin;
impl Plugin for SimpleBoxPlugin {
fn build(&self, app: &mut App) {
app.replicate::<BoxPosition>()
.replicate::<PlayerBox>()
.add_client_trigger::<MoveBox>(Channel::Ordered)
.add_observer(spawn_clients)
.add_observer(despawn_clients)
.add_observer(apply_movement)
.add_systems(Startup, (read_cli.map(Result::unwrap), spawn_camera))
.add_systems(Update, (read_input, draw_boxes));
}
}
fn read_cli(
mut commands: Commands,
cli: Res<Cli>,
channels: Res<RepliconChannels>,
) -> Result<(), Box<dyn Error>> {
const PROTOCOL_ID: u64 = 0;
match *cli {
Cli::SinglePlayer => {
info!("starting single-player game");
commands.spawn((
PlayerBox {
color: GREEN.into(),
},
BoxOwner(SERVER),
));
}
Cli::Server { port } => {
info!("starting server at port {port}");
let server_channels_config = channels.server_configs();
let client_channels_config = channels.client_configs();
let server = RenetServer::new(ConnectionConfig {
server_channels_config,
client_channels_config,
..Default::default()
});
let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, port))?;
let server_config = ServerConfig {
current_time,
max_clients: 10,
protocol_id: PROTOCOL_ID,
authentication: ServerAuthentication::Unsecure,
public_addresses: Default::default(),
};
let transport = NetcodeServerTransport::new(server_config, socket)?;
commands.insert_resource(server);
commands.insert_resource(transport);
commands.spawn((
Text::new("Server"),
TextFont {
font_size: 30.0,
..Default::default()
},
TextColor::WHITE,
));
commands.spawn((
PlayerBox {
color: GREEN.into(),
},
BoxOwner(SERVER),
));
}
Cli::Client { port, ip } => {
info!("connecting to {ip}:{port}");
let server_channels_config = channels.server_configs();
let client_channels_config = channels.client_configs();
let client = RenetClient::new(ConnectionConfig {
server_channels_config,
client_channels_config,
..Default::default()
});
let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let client_id = current_time.as_millis() as u64;
let server_addr = SocketAddr::new(ip, port);
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))?;
let authentication = ClientAuthentication::Unsecure {
client_id,
protocol_id: PROTOCOL_ID,
server_addr,
user_data: None,
};
let transport = NetcodeClientTransport::new(current_time, authentication, socket)?;
commands.insert_resource(client);
commands.insert_resource(transport);
commands.spawn((
Text(format!("Client: {client_id}")),
TextFont {
font_size: 30.0,
..default()
},
TextColor::WHITE,
));
}
}
Ok(())
}
fn spawn_camera(mut commands: Commands) {
commands.spawn(Camera2d);
}
/// Spawns a new box whenever a client connects.
fn spawn_clients(trigger: Trigger<OnAdd, ConnectedClient>, mut commands: Commands) {
// Hash index to generate visually distinctive color.
let mut hasher = DefaultHasher::new();
trigger.entity().index().hash(&mut hasher);
let hash = hasher.finish();
// Use the lower 24 bits.
// Divide by 255 to convert bytes into 0..1 floats.
let r = ((hash >> 16) & 0xFF) as f32 / 255.0;
let g = ((hash >> 8) & 0xFF) as f32 / 255.0;
let b = (hash & 0xFF) as f32 / 255.0;
// Generate pseudo random color from client entity.
info!("spawning box for `{}`", trigger.entity());
commands.spawn((
PlayerBox {
color: Color::srgb(r, g, b),
},
BoxOwner(trigger.entity()),
));
}
/// Despawns a box whenever a client disconnects.
fn despawn_clients(
trigger: Trigger<OnRemove, ConnectedClient>,
mut commands: Commands,
boxes: Query<(Entity, &BoxOwner)>,
) {
let (entity, _) = boxes
.iter()
.find(|&(_, owner)| **owner == trigger.entity())
.expect("all clients should have entities");
commands.entity(entity).despawn();
}
/// Reads player inputs and sends [`MoveDirection`] events.
fn read_input(mut commands: Commands, input: Res<ButtonInput<KeyCode>>) {
let mut direction = Vec2::ZERO;
if input.pressed(KeyCode::KeyW) {
direction.y += 1.0;
}
if input.pressed(KeyCode::KeyA) {
direction.x -= 1.0;
}
if input.pressed(KeyCode::KeyS) {
direction.y -= 1.0;
}
if input.pressed(KeyCode::KeyD) {
direction.x += 1.0;
}
if direction != Vec2::ZERO {
commands.client_trigger(MoveBox(direction.normalize_or_zero()));
}
}
/// Mutates [`BoxPosition`] based on [`MoveBox`] events.
///
/// Fast-paced games usually you don't want to wait until server send a position back because of the latency.
/// But this example just demonstrates simple replication concept.
fn apply_movement(
trigger: Trigger<FromClient<MoveBox>>,
time: Res<Time>,
mut boxes: Query<(&BoxOwner, &mut BoxPosition)>,
) {
const MOVE_SPEED: f32 = 300.0;
info!("received movement from `{}`", trigger.client_entity);
// Find the sender entity. We don't include the entity as a trigger target to save traffic, since the server knows
// which entity to apply the input to. We could have a resource that maps connected clients to controlled entities,
// but we didn't implement it for the sake of simplicity.
let (_, mut position) = boxes
.iter_mut()
.find(|&(owner, _)| **owner == trigger.client_entity)
.unwrap_or_else(|| panic!("`{}` should be connected", trigger.client_entity));
**position += *trigger.event * time.delta_secs() * MOVE_SPEED;
}
fn draw_boxes(mut gizmos: Gizmos, boxes: Query<(&BoxPosition, &PlayerBox)>) {
for (position, player) in &boxes {
gizmos.rect(
Vec3::new(position.x, position.y, 0.0),
Vec2::ONE * 50.0,
player.color,
);
}
}
const PORT: u16 = 5000;
/// A simple game with moving boxes.
#[derive(Parser, PartialEq, Resource)]
enum Cli {
/// No networking will be used, the player will control its box locally.
SinglePlayer,
/// Run game instance will act as both a player and a host.
Server {
#[arg(short, long, default_value_t = PORT)]
port: u16,
},
/// The game instance will connect to a host in order to start the game.
Client {
#[arg(short, long, default_value_t = Ipv4Addr::LOCALHOST.into())]
ip: IpAddr,
#[arg(short, long, default_value_t = PORT)]
port: u16,
},
}
impl Default for Cli {
fn default() -> Self {
Self::parse()
}
}
/// Player-controlled box.
///
/// We want to replicate all boxes, so we just set [`Replicated`] as a required component.
#[derive(Component, Deref, Deserialize, Serialize, Default)]
#[require(BoxPosition, Replicated)]
struct PlayerBox {
/// Color to visually distinguish boxes.
color: Color,
}
/// Position of a player-controlled box.
///
/// This is a separate component from [`PlayerBox`] because, when the position
/// changes, we only want to send this component (and it changes often!).
#[derive(Component, Deserialize, Serialize, Deref, DerefMut, Default)]
struct BoxPosition(Vec2);
/// Identifies which player controls the box.
///
/// Points to client entity. Used to apply movement to the correct box.
///
/// It's not replicated and present only on server or singleplayer.
#[derive(Component, Clone, Copy, Deref)]
struct BoxOwner(Entity);
/// A movement event for the controlled box.
#[derive(Deserialize, Deref, Event, Serialize)]
struct MoveBox(Vec2);