Skip to content

Commit

Permalink
DRY the minesweeper setup code
Browse files Browse the repository at this point in the history
  • Loading branch information
inodentry committed Sep 27, 2023
1 parent bdb2616 commit be0c8b0
Showing 1 changed file with 4 additions and 63 deletions.
67 changes: 4 additions & 63 deletions src/game/minesweeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ impl Plugin for MinesweeperGameplayPlugin {
));
}
}
fn cli_minesweeper_singleplayer(world: &mut World) {
world.resource_mut::<AllSettings>().game_minesweeper.n_plids = 1;
cli_minesweeper_playground(world);
}

fn cli_minesweeper_playground(world: &mut World) {
let minesweeper_settings = world.resource::<AllSettings>().game_minesweeper.clone();
Expand All @@ -62,30 +66,6 @@ fn cli_minesweeper_playground(world: &mut World) {
}
}

fn cli_minesweeper_singleplayer(world: &mut World) {
let mut minesweeper_settings = world.resource::<AllSettings>().game_minesweeper.clone();
minesweeper_settings.n_plids = 1;
let mapgen_settings = world.resource::<AllSettings>().mapgen.clone();
match (PROPRIETARY, mapgen_settings.style) {
(false, _) | (_, MapGenStyle::Flat) => {
match mapgen_settings.topology {
Topology::Hex => {
setup_minesweeper_singleplayer_flatmap::<Hex>(world, minesweeper_settings, mapgen_settings.size);
}
Topology::Sq => {
setup_minesweeper_singleplayer_flatmap::<Sq>(world, minesweeper_settings, mapgen_settings.size);
}
}
}
(true, MapGenStyle::MineWars) => {
#[cfg(feature = "proprietary")]
mw_proprietary_client::setup_minesweeper_singleplayer_mwmap(
world, minesweeper_settings, mapgen_settings.size, mapgen_settings.seed,
);
}
}
}

fn setup_minesweeper_playground_flatmap<C: Coord>(
world: &mut World,
minesweeper_settings: MinesweeperSettings,
Expand Down Expand Up @@ -129,45 +109,6 @@ fn setup_minesweeper_playground_flatmap<C: Coord>(
world.resource_mut::<NextState<SessionKind>>().set(SessionKind::BevyHost);
}

fn setup_minesweeper_singleplayer_flatmap<C: Coord>(
world: &mut World,
mut minesweeper_settings: MinesweeperSettings,
map_size: u8
) {
minesweeper_settings.n_plids = 1;

let dummy_map = MapData::<C, ()>::new(map_size, ());
mw_app::map::setup_map(world, &dummy_map, &[], |_| TileKind::Regular, |_| 0);
let game = GameMinesweeper::<C>::new(minesweeper_settings, &dummy_map, |_| TileKind::Regular);
world.insert_resource(BevyHost::new(game, ()));

let mut viewtile: ViewTileData = ViewTileData::default();
viewtile.set_owner(0);
viewtile.set_digit(0);
viewtile.set_kind(TileKind::Regular);
viewtile.set_item(ItemKind::Safe);
viewtile.set_has_structure(false);

let e_plid0 = world.spawn(SpectatorPlidBundle::default()).id();
let e_plid1 = world.spawn((
PlayerBundle {
plid: PlayerPlid(1.into()),
state: PlayerState::Alive,
},
ViewBundle {
mapdata: ViewMapData(MapData::<C, _>::new(map_size, viewtile)),
},
PlidPlayable,
)).id();
world.insert_resource(PlayersIndex(vec![e_plid0, e_plid1]));
world.insert_resource(PlidPlayingAs(1.into()));
world.insert_resource(PlidViewing(1.into()));

world.resource_mut::<NextState<GameMode>>().set(GameMode::Minesweeper);
world.resource_mut::<NextState<AppState>>().set(AppState::InGame);
world.resource_mut::<NextState<SessionKind>>().set(SessionKind::BevyHost);
}

// TODO: replace this with something more elaborate?
fn minesweeper_input(
crs: Res<GridCursor>,
Expand Down

0 comments on commit be0c8b0

Please sign in to comment.