Skip to content

Commit

Permalink
Added map view and battle view game state.
Browse files Browse the repository at this point in the history
  • Loading branch information
StarArawn authored and StarArawn committed Apr 16, 2021
1 parent 10a74fa commit 37d1f82
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 9 deletions.
14 changes: 13 additions & 1 deletion src/game/camera/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use bevy::{
render::camera::Camera,
};
use bevy::render::camera::CameraProjection;
use crate::game::GameState;

use super::CustomOrthographicProjection;

pub struct KeyboardConf {
Expand All @@ -27,9 +29,10 @@ impl Default for KeyboardConf {
}

pub fn camera_movement(
mut game_state: ResMut<State<GameState>>,
time: Res<Time>,
windows: Res<Windows>,
keyboard_input: Res<Input<KeyCode>>,
mut keyboard_input: ResMut<Input<KeyCode>>,
mut query: Query<(&mut Camera, &mut Transform, &mut CustomOrthographicProjection)>,
) {
for (mut camera, mut transform, mut projection) in query.iter_mut() {
Expand Down Expand Up @@ -62,6 +65,15 @@ pub fn camera_movement(
projection.scale = scale;
}

if keyboard_input.just_pressed(KeyCode::P) {
if *game_state.current() == GameState::MapView {
game_state.set(GameState::BattleView).unwrap();
} else if *game_state.current() == GameState::BattleView {
game_state.set(GameState::MapView).unwrap();
}
keyboard_input.update();
}

projection.update(windows.get_primary().unwrap().width(), windows.get_primary().unwrap().height());
camera.projection_matrix = projection.get_projection_matrix();
camera.depth_calculation = projection.depth_calculation();
Expand Down
18 changes: 17 additions & 1 deletion src/game/game_state.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
use bevy::prelude::*;

#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub enum GameState {
Loading,
Generating,
Playing,
MapView,
BattleView,
}

impl Default for GameState {
fn default() -> Self {
GameState::Loading
}
}

pub fn update_visibility_for_state(
game_state: Res<State<GameState>>,
mut query: Query<(&mut Visible, &mut Transform, &GameState)>,
) {
for (mut visible, mut transform, target_game_state) in query.iter_mut() {
if game_state.current() == target_game_state {
visible.is_visible = true;
} else {
visible.is_visible = false;
}
}
}
3 changes: 3 additions & 0 deletions src/game/gameplay/enemy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bevy::prelude::*;

use crate::game::GameState;

pub mod spawner;

#[derive(Default)]
Expand All @@ -20,5 +22,6 @@ pub fn spawn_map_enemy(
transform: Transform::from_xyz(position.x, position.y, 12.0),
..Default::default()
})
.insert(GameState::MapView)
.insert(Enemy::default());
}
5 changes: 4 additions & 1 deletion src/game/gameplay/enemy/spawner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bevy::prelude::*;
use rand::{Rng, prelude::ThreadRng, thread_rng};
use rand::{Rng, thread_rng};

use crate::game::GameState;

use super::spawn_map_enemy;

Expand All @@ -26,6 +28,7 @@ pub fn spawn(
transform: Transform::from_xyz(position.x, position.y, 10.0),
..Default::default()
})
.insert(GameState::MapView)
.insert(Spawner {
wait_time: 10.0,
last_time: 0.0,
Expand Down
3 changes: 3 additions & 0 deletions src/game/gameplay/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub use player::Player;

pub use movement::movement;

use crate::game::GameState;

pub fn spawn_player(
mut commands: Commands,
asset_server: Res<AssetServer>,
Expand All @@ -22,5 +24,6 @@ pub fn spawn_player(
transform: Transform::from_xyz(0.0, 0.0, 10.0),
..Default::default()
})
.insert(GameState::MapView)
.insert(Player::default());
}
3 changes: 1 addition & 2 deletions src/game/map/generate_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ pub fn generate_map(
mut tilemap_query: Query<&mut Tilemap>
) {
for mut tilemap in tilemap_query.iter_mut() {

// Generate a seed for the map
let mut random = thread_rng();
let seed: u32 = random.gen();
Expand Down Expand Up @@ -244,7 +243,7 @@ pub fn generate_map(
}
}

game_state.set(GameState::Playing).unwrap();
game_state.set(GameState::MapView).unwrap();
}

}
3 changes: 2 additions & 1 deletion src/game/map/spawn_map_entity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{prelude::*};
use bevy_tilemap::prelude::*;

use crate::game::camera::CustomOrthographicCameraBundle;
use crate::game::{GameState, camera::CustomOrthographicCameraBundle};

#[derive(Default, Clone)]
pub struct TilemapAtlasHandles {
Expand Down Expand Up @@ -64,5 +64,6 @@ pub fn spawn_map_entity(
.insert_bundle(CustomOrthographicCameraBundle::new_2d());
commands
.spawn()
.insert(GameState::MapView)
.insert_bundle(tilemap_components);
}
19 changes: 16 additions & 3 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,31 @@ impl Plugin for GamePlugin {
)
.add_system_set(
// Gameplay update
SystemSet::on_update(GameState::Playing)
SystemSet::on_update(GameState::MapView)
.label("gameplay_update")
.with_system(gameplay::player::movement.system())
)
.add_system_set(
// Realtime update
// Used for non-gameplay items that should update every frame.
SystemSet::on_update(GameState::Playing)
SystemSet::on_update(GameState::MapView)
.label("realtime_update")
.with_system(camera::camera_movement.system())
.with_system(gameplay::enemy::spawner::tick.system())
)
.add_system_set(
SystemSet::on_update(GameState::BattleView)
.with_system(camera::camera_movement.system())
)
// Update visibilty between states.
.add_system_set(
SystemSet::on_enter(GameState::BattleView)
.with_system(game_state::update_visibility_for_state.system())
)
.add_system_set(
SystemSet::on_enter(GameState::MapView)
.with_system(game_state::update_visibility_for_state.system())
)
.add_system_to_stage(
CoreStage::PostUpdate,
bevy::render::camera::camera_system::<CustomOrthographicProjection>
Expand All @@ -57,4 +70,4 @@ impl Plugin for GamePlugin {
)
.add_plugin(map::MapPlugin);
}
}
}

0 comments on commit 37d1f82

Please sign in to comment.