-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example for authority transfer (#597)
* wip * restore World access in receive * add authority handling * add authority handling * add authority * update book * merge main * add authority examples * add bit marker that indicates that the entity has been mapped already * Update replication send: - server never sends updates to the client with authority - apply the entity bit-flag mapping everywhere - on client send (despawn/update/remove) - on server send (despawn/update/remove) - the entities within components must be mapped with bit-mapping as well! * enable serializing with map-entities * separate SendEntityMap and ReceiveEntityMap * wip fix tests, it seems like too much entity-map is done! If there's a spawn, we don't to apply any mapping * add tests for replicating map-entity components after authority transfer * receiving Updates is ok even if we don't have a latest_tick on the receive side, as long as the Update as no last_action_tick either * update interpolation history to use the authority's updates as confirmed updates * clippy
- Loading branch information
1 parent
7e606c2
commit 841a7c1
Showing
38 changed files
with
2,157 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[build] | ||
# it is a good idea to specify your target here, to avoid losing incremental compilation when compiling to wasm | ||
# target = "aarch64-apple-darwin" | ||
rustflags = ["--cfg", "web_sys_unstable_apis"] | ||
|
||
[target.wasm32-unknown-unknown] | ||
runner = "wasm-server-runner" | ||
|
||
# Enable max optimizations for dependencies, but not for our code: | ||
[profile.dev.package."*"] | ||
opt-level = 3 | ||
|
||
# Enable only a small amount of optimization in debug mode | ||
[profile.dev] | ||
opt-level = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
[package] | ||
name = "distributed_authority" | ||
version = "0.1.0" | ||
authors = ["Charles Bournhonesque <charlesbour@gmail.com>"] | ||
edition = "2021" | ||
rust-version = "1.65" | ||
description = "Examples for the lightyearServer-client networking library for the Bevy game engine" | ||
readme = "README.md" | ||
repository = "https://github.com/cBournhonesque/lightyear" | ||
keywords = ["bevy", "multiplayer", "networking", "netcode", "gamedev"] | ||
categories = ["game-development", "network-programming"] | ||
license = "MIT OR Apache-2.0" | ||
publish = false | ||
|
||
[features] | ||
metrics = ["lightyear/metrics", "dep:metrics-exporter-prometheus"] | ||
|
||
[dependencies] | ||
lightyear_examples_common = { path = "../common" } | ||
lightyear = { path = "../../lightyear", features = [ | ||
"steam", | ||
"webtransport", | ||
"websocket", | ||
] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
anyhow = { version = "1.0", features = [] } | ||
tracing = "0.1" | ||
tracing-subscriber = "0.3.17" | ||
bevy = { version = "0.14", features = [ | ||
"multi_threaded", | ||
"bevy_state", | ||
"serialize", | ||
] } | ||
bevy_mod_picking = { version = "0.20", features = ["backend_bevy_ui"] } | ||
rand = "0.8" | ||
metrics-exporter-prometheus = { version = "0.15.1", optional = true } | ||
bevy-inspector-egui = "0.25" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Distributed authority | ||
|
||
This example showcases how to transfer authority over an entity to the server or to a client. | ||
This can be useful if you're going for a 'thin server' approach where clients are simulating most of the world. | ||
|
||
In this example, the ball is initially simulated on the server. | ||
When a client gets close the ball, the server transfers the authority over the ball to the client. | ||
This means that the client is now simulating the ball and sending replication updates to the server. | ||
|
||
|
||
## Running the example | ||
|
||
There are different 'modes' of operation: | ||
|
||
- as a dedicated server with `cargo run -- server` | ||
- as a listen server with `cargo run -- listen-server`. This will launch 2 independent bevy apps (client and server) in | ||
separate threads. | ||
They will communicate via channels (so with almost 0 latency) | ||
- as a listen server with `cargo run -- host-server`. This will launch a single bevy app, where the server will also act | ||
as a client. Functionally, it is similar to the "listen-server" mode, but you have a single bevy `World` instead of | ||
separate client and server `Worlds`s. | ||
|
||
Then you can launch clients with the commands: | ||
|
||
- `cargo run -- client -c 1` (`-c 1` overrides the client id, to use client id 1) | ||
- `cargo run -- client -c 2` | ||
|
||
You can modify the file `assets/settings.ron` to modify some networking settings. | ||
|
||
|
||
### Testing in wasm with webtransport | ||
|
||
NOTE: I am using [trunk](https://trunkrs.dev/) to build and serve the wasm example. | ||
|
||
To test the example in wasm, you can run the following commands: `trunk serve` | ||
|
||
You will need a valid SSL certificate to test the example in wasm using webtransport. You will need to run the following | ||
commands: | ||
|
||
- `sh examples/generate.sh` (to generate the temporary SSL certificates, they are only valid for 2 weeks) | ||
- `cargo run -- server` to start the server. The server will print out the certificate digest (something | ||
like `1fd28860bd2010067cee636a64bcbb492142295b297fd8c480e604b70ce4d644`) | ||
- You then have to replace the certificate digest in the `assets/settings.ron` file with the one that the server printed | ||
out. | ||
- then start the client wasm test with `trunk serve` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
Settings( | ||
client: ClientSettings( | ||
inspector: true, | ||
client_id: 0, | ||
client_port: 0, // the OS will assign a random open port | ||
server_addr: "127.0.0.1", | ||
conditioner: Some(Conditioner( | ||
latency_ms: 50, | ||
jitter_ms: 5, | ||
packet_loss: 0.02 | ||
)), | ||
server_port: 5000, | ||
transport: WebTransport( | ||
// this is only needed for wasm, the self-signed certificates are only valid for 2 weeks | ||
// the server will print the certificate digest on startup | ||
certificate_digest: "6e:f2:d6:57:f8:f7:c9:ab:88:ae:59:6b:e8:97:cc:1e:a7:a4:ce:71:17:e1:39:79:4d:c6:2b:79:86:9a:c5:fc", | ||
), | ||
// server_port: 5001, | ||
// transport: Udp, | ||
// server_port: 5002, | ||
// transport: WebSocket, | ||
// server_port: 5003, | ||
// transport: Steam( | ||
// app_id: 480, | ||
// ) | ||
), | ||
server: ServerSettings( | ||
headless: false, | ||
inspector: true, | ||
conditioner: Some(Conditioner( | ||
latency_ms: 50, | ||
jitter_ms: 5, | ||
packet_loss: 0.02 | ||
)), | ||
transport: [ | ||
WebTransport( | ||
local_port: 5000 | ||
), | ||
Udp( | ||
local_port: 5001 | ||
), | ||
WebSocket( | ||
local_port: 5002 | ||
), | ||
// Steam( | ||
// app_id: 480, | ||
// server_ip: "0.0.0.0", | ||
// game_port: 5003, | ||
// query_port: 27016, | ||
// ), | ||
], | ||
), | ||
shared: SharedSettings( | ||
protocol_id: 0, | ||
private_key: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), | ||
compression: None, | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> | ||
<title>Bevy game</title> | ||
<link data-trunk rel="rust"/> | ||
</head> | ||
</html> |
Oops, something went wrong.