Skip to content

Commit

Permalink
map selection through hotkey DigitalExtinction#70
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Nov 3, 2022
1 parent a31a575 commit 5cdc6e6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
1 change: 1 addition & 0 deletions TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rotate the camera around its focus point on the terrain.
# Hotkeys

* Press `Esc` — cancel current action.
* Key `M` — Cycle through default maps

# Building Construction

Expand Down
3 changes: 3 additions & 0 deletions assets/de_map_test.tar
Git LFS file not shown
2 changes: 2 additions & 0 deletions crates/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use bevy::{app::PluginGroupBuilder, prelude::PluginGroup};
use map::MapLoaderPlugin;

mod map;
mod map_select;

pub struct LoaderPluginGroup;

impl PluginGroup for LoaderPluginGroup {
fn build(&mut self, group: &mut PluginGroupBuilder) {
group.add(MapLoaderPlugin);
group.add(map_select::MapSelectPlugin);
}
}
30 changes: 21 additions & 9 deletions crates/loader/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ impl Plugin for MapLoaderPlugin {

struct MapLoadingTask(Task<Result<Map, MapLoadingError>>);

#[derive(Component, Reflect)]
pub struct RemoveBeforeLoad;

fn load_map_system(mut commands: Commands, game_config: Res<GameConfig>) {
let map_path = if game_config.map_path().is_relative() {
asset_path(game_config.map_path())
Expand All @@ -48,6 +51,7 @@ fn spawn_map(
task: Option<ResMut<MapLoadingTask>>,
mut move_focus_events: EventWriter<MoveFocusEvent>,
game_config: Res<GameConfig>,
entities_to_remove: Query<Entity, Or<(With<Handle<Scene>>, With<RemoveBeforeLoad>)>>,
) -> Progress {
let mut task = match task {
Some(task) => task,
Expand All @@ -59,6 +63,10 @@ fn spawn_map(
None => return false.into(),
};

info!("Map loaded, removing old one");
for to_remove in entities_to_remove.iter() {
commands.entity(to_remove).despawn_recursive();
}
info!("Map loaded, spawning");
commands.remove_resource::<MapLoadingTask>();

Expand Down Expand Up @@ -91,7 +99,9 @@ fn spawn_map(
}

setup_light(&mut commands);
commands.spawn_bundle(TerrainBundle::flat(map.bounds()));
commands
.spawn_bundle(TerrainBundle::flat(map.bounds()))
.insert(RemoveBeforeLoad);

for object in map.objects() {
let mut entity_commands = commands.spawn();
Expand Down Expand Up @@ -120,13 +130,15 @@ fn setup_light(commands: &mut Commands) {

let mut transform = Transform::identity();
transform.look_at(Vec3::new(1., -1., 0.), Vec3::new(1., 1., 0.));
commands.spawn_bundle(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::WHITE,
illuminance: 30000.,
commands
.spawn_bundle(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::WHITE,
illuminance: 30000.,
..Default::default()
},
transform,
..Default::default()
},
transform,
..Default::default()
});
})
.insert(RemoveBeforeLoad);
}
39 changes: 39 additions & 0 deletions crates/loader/src/map_select.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use bevy::prelude::*;
use de_core::{gconfig::GameConfig, player::Player, state::GameState};
use iyes_loopless::{prelude::IntoConditionalSystem, state::NextState};

pub struct MapSelectPlugin;

impl Plugin for MapSelectPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<MapList>();
app.add_system(cycle_map.run_in_state(GameState::Playing));
}
}

pub struct MapList {
pub maps: Vec<String>,
pub current_id: usize,
}

impl Default for MapList {
fn default() -> Self {
Self {
maps: vec!["map.tar".into(), "de_map_test.tar".into()],
current_id: 0,
}
}
}

fn cycle_map(
mut commands: Commands,
mut config: ResMut<GameConfig>,
mut map_list: ResMut<MapList>,
kbd: Res<Input<KeyCode>>,
) {
if kbd.just_pressed(KeyCode::M) {
map_list.current_id = (map_list.current_id + 1) % map_list.maps.len();
*config = GameConfig::new(&map_list.maps[map_list.current_id], Player::Player1);
commands.insert_resource(NextState(GameState::Loading));
}
}

0 comments on commit 5cdc6e6

Please sign in to comment.