Skip to content

Commit

Permalink
Merge pull request #62 from PraxTube/main
Browse files Browse the repository at this point in the history
Allow player to trigger desync on purpose in examples
  • Loading branch information
gschup authored Oct 23, 2023
2 parents e369a48 + b2f9d9a commit a9d3cf0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ There is no real game, just movement with ice physics. Optionally, you can speci
- S to accelerate backwards
- A to turn left
- D to turn right
- SPACE to move player 1 to (0, 0) locally (this will create a desync)

### Important Disclaimer - Determinism

Expand Down
19 changes: 18 additions & 1 deletion examples/ex_game/ex_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,16 @@ impl Game {
"Frame {}: Checksum {}",
self.periodic_checksum.0, self.periodic_checksum.1
);
let force_desync_info_str = format!("Press SPACE to trigger a desync");
draw_text(&last_checksum_str, 20.0, 20.0, 30.0, WHITE);
draw_text(&periodic_checksum_str, 20.0, 40.0, 30.0, WHITE);
draw_text(
&force_desync_info_str,
90.0,
WINDOW_HEIGHT * 9.0 / 10.0,
30.0,
WHITE,
);
}

#[allow(dead_code)]
Expand All @@ -161,7 +169,8 @@ impl Game {

#[allow(dead_code)]
// creates a compact representation of currently pressed keys and serializes it
pub fn local_input(&self, handle: PlayerHandle) -> Input {
pub fn local_input(&mut self, handle: PlayerHandle) -> Input {
self.trigger_desync();
let mut inp: u8 = 0;

if handle == self.local_handles[0] {
Expand Down Expand Up @@ -197,6 +206,14 @@ impl Game {
Input { inp }
}

// move player one to origin on local session only
// this will create a forced desync (unless player one is already at the origin)
pub fn trigger_desync(&mut self) {
if is_key_pressed(KeyCode::Space) {
self.game_state.positions[0] = (0.0, 0.0);
}
}

#[allow(dead_code)]
pub const fn current_frame(&self) -> i32 {
self.game_state.frame
Expand Down

0 comments on commit a9d3cf0

Please sign in to comment.