Skip to content

Commit

Permalink
convert game usize/isize types to explicitly sized ones
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksa2808 committed Dec 14, 2023
1 parent 267c5dd commit d155adb
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 159 deletions.
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,5 @@ parking_lot = "0.12"
wasm-bindgen = "0.2"

[patch.crates-io]
# bevy_ggrs = { path = "../bevy_ggrs" }
bevy_ggrs = { git = "https://github.com/aleksa2808/bevy_ggrs" }
bevy_ggrs = { git = "https://github.com/aleksa2808/bevy_ggrs", branch = "desync_fixes" }
ggrs = { git = "https://github.com/aleksa2808/ggrs" }
32 changes: 16 additions & 16 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ pub struct Player {

#[derive(Component, Clone, Copy)]
pub struct Dead {
pub cleanup_frame: usize,
pub cleanup_frame: u32,
}

#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Position {
pub y: isize,
pub x: isize,
pub y: i8,
pub x: i8,
}

impl Position {
pub fn offset(&self, direction: Direction, distance: usize) -> Self {
let distance = distance as isize;
pub fn offset(&self, direction: Direction, distance: u8) -> Self {
let distance = distance as i8;

let (y_offset, x_offset) = match direction {
Direction::Right => (0, distance),
Expand All @@ -73,33 +73,33 @@ impl Position {

#[derive(Component, Clone, Copy, Hash)]
pub struct BombSatchel {
pub bombs_available: usize,
pub bomb_range: usize,
pub bombs_available: u8,
pub bomb_range: u8,
}

#[derive(Component, Clone, Copy)]
pub struct Bomb {
pub owner: Option<PlayerID>,
pub range: usize,
pub expiration_frame: usize,
pub range: u8,
pub expiration_frame: u32,
}

#[derive(Component, Clone, Copy)]
pub struct Moving {
pub direction: Direction,
pub next_move_frame: usize,
pub frame_interval: usize,
pub next_move_frame: u32,
pub frame_interval: u32,
}

#[derive(Component, Clone, Copy)]
pub struct Fuse {
pub color: Color,
pub start_frame: usize,
pub start_frame: u32,
}

#[derive(Component, Clone, Copy)]
pub struct Fire {
pub expiration_frame: usize,
pub expiration_frame: u32,
}

#[derive(Component, Clone, Copy)]
Expand All @@ -113,10 +113,10 @@ pub struct Destructible;

#[derive(Component, Clone, Copy)]
pub struct Crumbling {
pub expiration_frame: usize,
pub expiration_frame: u32,
}

#[derive(Component, Debug, Clone, Copy, Hash)]
#[derive(Component, Debug, Clone, Copy)]
pub enum Item {
BombsUp,
RangeUp,
Expand All @@ -125,5 +125,5 @@ pub enum Item {

#[derive(Component, Clone, Copy)]
pub struct BurningItem {
pub expiration_frame: usize,
pub expiration_frame: u32,
}
26 changes: 13 additions & 13 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ pub const COLORS: [RGBColor; 16] = [
RGBColor(242, 242, 242),
];

pub const PIXEL_SCALE: usize = 8;
pub const PIXEL_SCALE: u32 = 8;

pub const HUD_HEIGHT: usize = 14 * PIXEL_SCALE;
pub const HUD_HEIGHT: u32 = 14 * PIXEL_SCALE;

pub const TILE_HEIGHT: usize = 8 * PIXEL_SCALE;
pub const TILE_WIDTH: usize = 6 * PIXEL_SCALE;
pub const TILE_HEIGHT: u32 = 8 * PIXEL_SCALE;
pub const TILE_WIDTH: u32 = 6 * PIXEL_SCALE;

pub const WALL_Z_LAYER: f32 = 60.0;
pub const PLAYER_Z_LAYER: f32 = 50.0;
Expand All @@ -39,21 +39,21 @@ pub const INPUT_LEFT: u8 = 1 << 2;
pub const INPUT_RIGHT: u8 = 1 << 3;
pub const INPUT_ACTION: u8 = 1 << 4;

pub const ROUND_DURATION_SECS: usize = 60;
pub const ROUND_DURATION_SECS: u32 = 60;

pub const FPS: usize = 30;
pub const MAX_PREDICTED_FRAMES: usize = 8;
pub const FPS: u32 = 30;
pub const MAX_PREDICTED_FRAMES: u32 = 8;

// these must not be lower than MAX_PREDICTED_FRAMES
// TODO can some static asserts be made?
pub const GET_READY_DISPLAY_FRAME_COUNT: usize = 3 * FPS;
pub const GAME_START_FREEZE_FRAME_COUNT: usize = FPS / 2;
pub const LEADERBOARD_DISPLAY_FRAME_COUNT: usize = 2 * FPS;
pub const TOURNAMENT_WINNER_DISPLAY_FRAME_COUNT: usize = 5 * FPS;
pub const GET_READY_DISPLAY_FRAME_COUNT: u32 = 3 * FPS;
pub const GAME_START_FREEZE_FRAME_COUNT: u32 = FPS / 2;
pub const LEADERBOARD_DISPLAY_FRAME_COUNT: u32 = 2 * FPS;
pub const TOURNAMENT_WINNER_DISPLAY_FRAME_COUNT: u32 = 5 * FPS;

pub const BOMB_SHORTENED_FUSE_FRAME_COUNT: usize = 2;
pub const BOMB_SHORTENED_FUSE_FRAME_COUNT: u32 = 2;

pub const MOVING_OBJECT_FRAME_INTERVAL: usize = 1;
pub const MOVING_OBJECT_FRAME_INTERVAL: u32 = 1;

// TODO figure out if floats can be used deterministically
pub const ITEM_SPAWN_CHANCE_PERCENTAGE: u64 = 33;
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn run() {
let input_fn = native_input;

app.add_plugins(GgrsPlugin::<GgrsConfig>::default())
.set_rollback_schedule_fps(FPS)
.set_rollback_schedule_fps(FPS as usize)
.add_systems(ReadInputs, input_fn)
// Bevy components
.rollback_component_with_clone::<Sprite>()
Expand Down Expand Up @@ -145,8 +145,12 @@ pub fn run() {
.checksum_component_with_hash::<Player>()
.checksum_component_with_hash::<Position>()
.checksum_component_with_hash::<BombSatchel>()
.checksum_component_with_hash::<Item>()
// .checksum_resource_with_hash::<SessionRng>()
// enums seem to hash from an isize so the derived implementation isn't portable
.checksum_component::<Item>(|item| match item {
Item::BombsUp => 0,
Item::RangeUp => 1,
Item::BombPush => 2,
})
.add_systems(
GgrsSchedule,
// list too long for one chain
Expand Down
2 changes: 1 addition & 1 deletion src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Args {
pub room_id: String,

#[clap(long, short, default_value = "2")]
pub number_of_players: usize,
pub number_of_players: u8,
}

impl Default for Args {
Expand Down
27 changes: 13 additions & 14 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
#[derive(Resource)]
pub struct NetworkStatsCooldown {
pub cooldown: Cooldown,
pub print_cooldown: usize,
pub print_cooldown: u32,
}

#[derive(Resource)]
Expand Down Expand Up @@ -93,7 +93,7 @@ impl GameTextures {
self.penguin_variants
.iter()
.cycle()
.nth(player_id.0)
.nth(player_id.0 as usize)
.unwrap()
}
}
Expand Down Expand Up @@ -149,11 +149,10 @@ impl FromWorld for GameTextures {

#[derive(Resource, Clone, Copy)]
pub struct MapSize {
pub rows: usize,
pub columns: usize,
pub rows: u8,
pub columns: u8,
}

// Not to be confused with Bevy ECS `World`!
#[derive(Resource, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(clippy::enum_variant_names)]
pub enum WorldType {
Expand Down Expand Up @@ -185,7 +184,7 @@ impl WorldType {

#[derive(Resource)]
pub struct MatchboxConfig {
pub number_of_players: usize,
pub number_of_players: u8,
pub room_id: String,
pub matchbox_server_url: Option<String>,
pub ice_server_config: Option<ICEServerConfig>,
Expand Down Expand Up @@ -213,37 +212,37 @@ impl SessionRng {
}

#[derive(Resource)]
pub struct LocalPlayerID(pub usize);
pub struct LocalPlayerID(pub u8);

#[derive(Resource)]
pub struct Leaderboard {
pub scores: HashMap<PlayerID, usize>,
pub winning_score: usize,
pub scores: HashMap<PlayerID, u8>,
pub winning_score: u8,
}

#[derive(Resource, Clone, Copy)]
pub struct FrameCount {
pub frame: usize,
pub frame: u32,
}

#[derive(Resource, Clone, Copy)]
pub enum WallOfDeath {
Dormant {
activation_frame: usize,
activation_frame: u32,
},
Active {
position: Position,
direction: Direction,
next_step_frame: usize,
next_step_frame: u32,
},
Done,
}

#[derive(Resource)]
pub struct GameEndFrame(pub usize);
pub struct GameEndFrame(pub u32);

#[derive(Resource, Clone, Copy)]
pub struct GameFreeze {
pub end_frame: usize,
pub end_frame: u32,
pub post_freeze_action: Option<PostFreezeAction>,
}
Loading

0 comments on commit d155adb

Please sign in to comment.