Skip to content

Commit

Permalink
refactor grid related stuff and sets
Browse files Browse the repository at this point in the history
  • Loading branch information
inodentry committed Apr 16, 2024
1 parent 74305b5 commit 8bf6475
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 122 deletions.
4 changes: 2 additions & 2 deletions lib/mw_app/src/appstate.rs → lib/mw_app/src/apporg.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::*;

pub struct AppStatesPlugin;
pub struct AppOrganizationPlugin;

impl Plugin for AppStatesPlugin {
impl Plugin for AppOrganizationPlugin {
fn build(&self, app: &mut App) {
app.init_state::<GameMode>();
app.init_state::<AppState>();
Expand Down
38 changes: 21 additions & 17 deletions lib/mw_app/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ impl Plugin for MwCameraPlugin {
app.add_event::<CameraJumpTo>();
app.add_event::<ScreenShakeEvent>();
app.init_resource::<GridCursor>();
app.configure_sets(Update, (
CameraControlSet
.in_set(InGameSet(None)),
GridCursorSet
.in_set(InGameSet(None)),
GridCursorChangedSet
.after(GridCursorSet)
.run_if(resource_changed::<GridCursor>)
));
app.configure_stage_set(
Update,
CameraControlSS,
rc_camera_changed,
);
app.configure_stage_set(
Update,
GridCursorSS,
resource_changed::<GridCursor>,
);
}
}

Expand All @@ -44,14 +45,17 @@ pub enum ScreenShakeEvent {
Strong,
}

#[derive(SystemSet, Debug, PartialEq, Eq, Clone, Copy, Hash, Default)]
pub struct CameraControlSet;
#[derive(Resource, Default)]
pub struct GridCursor(pub Pos);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemSet)]
pub struct GridCursorSet;
#[derive(SystemSet, Debug, PartialEq, Eq, Clone, Copy, Hash, Default)]
pub struct CameraControlSS;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemSet)]
pub struct GridCursorChangedSet;
#[derive(SystemSet, Debug, PartialEq, Eq, Clone, Copy, Hash, Default)]
pub struct GridCursorSS;

#[derive(Resource, Default)]
pub struct GridCursor(pub Pos);
fn rc_camera_changed(
q_camera: Query<(), (Changed<Transform>, With<GameCamera>)>,
) -> bool {
!q_camera.is_empty()
}
34 changes: 22 additions & 12 deletions lib/mw_app/src/gfx2d/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ use bevy::{input::mouse::{MouseWheel, MouseScrollUnit}, window::PrimaryWindow};
use bevy_tweening::*;
use mw_common::{game::MapDescriptor, grid::*};

use crate::{prelude::*, ui::UiCamera, input::GameInputSet};
use crate::{input::GameInputSet, map::{GridCursorTileEntity, MapTileIndex, MapTopologySet}, prelude::*, ui::UiCamera};
use crate::{camera::*, input::InputAction};

use super::Gfx2dSet;
use super::Gfx2dModeSet;

pub struct Gfx2dCameraPlugin;

impl Plugin for Gfx2dCameraPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(iyes_bevy_extras::d2::WorldCursorPlugin);
app.add_systems(OnEnter(AppState::InGame), setup_game_camera_2d.in_set(Gfx2dSet::Any));
app.add_systems(OnEnter(AppState::InGame), setup_game_camera_2d.in_set(Gfx2dModeSet::Any));
app.add_systems(Update, (
camera_control_zoom_mousewheel,
inputaction_zoom
.in_set(GameInputSet::ProcessEvents),
)
.in_set(CameraControlSet)
.in_set(Gfx2dSet::Any)
.in_set(Gfx2dModeSet::Any)
.in_set(SetStage::Provide(CameraControlSS))
);
app.add_systems(Update, (
grid_cursor
.run_if(resource_changed::<WorldCursor>),
grid_cursor::<Hex>.in_set(MapTopologySet(Topology::Hex)),
grid_cursor::<Sq>.in_set(MapTopologySet(Topology::Sq)),
)
.in_set(GridCursorSet)
.in_set(Gfx2dSet::Any)
.after(iyes_bevy_extras::d2::WorldCursorSet)
.in_set(Gfx2dModeSet::Any)
.in_set(SetStage::Provide(GridCursorSS))
.in_set(SetStage::WantChanged(WorldCursorSS))
);
app.add_systems(Update, component_animator_system::<OrthographicProjection>);
}
Expand All @@ -41,12 +41,14 @@ fn setup_game_camera_2d(
world.spawn((StateDespawnMarker, GameCamera, UiCamera, WorldCursorCamera, camera));
}

fn grid_cursor(
fn grid_cursor<C: Coord>(
crs_in: Res<WorldCursor>,
mut crs_out: ResMut<GridCursor>,
mapdesc: Res<MapDescriptor>,
index: Option<Res<MapTileIndex<C>>>,
mut cursor_tile: ResMut<GridCursorTileEntity>,
) {
match mapdesc.topology {
match C::TOPOLOGY {
Topology::Hex => {
let tdim = Vec2::new(super::sprite::WIDTH6, super::sprite::HEIGHT6);
let conv = bevy::math::Mat2::from_cols_array(
Expand All @@ -58,6 +60,10 @@ fn grid_cursor(
let new_pos = Pos::from(new);
if crs_out.0 != new_pos {
crs_out.0 = new_pos;
let new_e = index.and_then(|inner| inner.0.get(new_pos.into()).cloned());
if cursor_tile.0 != new_e {
cursor_tile.0 = new_e;
}
}
}
}
Expand All @@ -69,6 +75,10 @@ fn grid_cursor(
let new_pos = Pos::from(new);
if crs_out.0 != new_pos {
crs_out.0 = new_pos;
let new_e = index.and_then(|inner| inner.0.get(new_pos.into()).cloned());
if cursor_tile.0 != new_e {
cursor_tile.0 = new_e;
}
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions lib/mw_app/src/gfx2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ impl Plugin for Gfx2dPlugin {
tilemap::Gfx2dTilemapPlugin,
));
app.configure_sets(Update, (
Gfx2dSet::Any.in_set(NeedsMapSet).run_if(rc_gfx2d_any),
Gfx2dSet::Sprites.in_set(NeedsMapSet).run_if(rc_gfx2d_sprites),
Gfx2dModeSet::Any.in_set(NeedsMapSet).run_if(rc_gfx2d_any),
Gfx2dModeSet::Sprites.in_set(NeedsMapSet).run_if(rc_gfx2d_sprites),
#[cfg(feature = "gfx2d_tilemap")]
Gfx2dSet::Tilemap.in_set(NeedsMapSet).run_if(rc_gfx2d_tilemap),
Gfx2dModeSet::Tilemap.in_set(NeedsMapSet).run_if(rc_gfx2d_tilemap),
));
app.configure_sets(OnEnter(AppState::InGame), (
Gfx2dModeSet::Any.run_if(rc_gfx2d_any),
Gfx2dModeSet::Sprites.run_if(rc_gfx2d_sprites),
#[cfg(feature = "gfx2d_tilemap")]
Gfx2dModeSet::Tilemap.run_if(rc_gfx2d_tilemap),
));
}
}

#[derive(SystemSet, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Gfx2dSet {
pub enum Gfx2dModeSet {
Any,
Sprites,
#[cfg(feature = "gfx2d_tilemap")]
Expand Down
11 changes: 5 additions & 6 deletions lib/mw_app/src/gfx2d/sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use mw_common::plid::PlayerId;

use crate::prelude::*;
use crate::assets::GameAssets;
use crate::camera::GridCursor;
use crate::camera::GridCursorChangedSet;
use crate::camera::{GridCursor, GridCursorSS};
use crate::player::PlayersIndex;
use crate::view::PlidViewing;
use crate::view::ViewMapData;
Expand Down Expand Up @@ -38,19 +37,19 @@ impl Plugin for Gfx2dSpritesPlugin {
sprites_reghighlight,
)
.run_if(resource_exists::<TilemapInitted>),
).in_set(Gfx2dSet::Sprites));
).in_set(Gfx2dModeSet::Sprites));
app.add_systems(OnEnter(AppState::InGame), (
setup_cursor,
).in_set(Gfx2dSet::Any));
).in_set(Gfx2dModeSet::Any));
app.add_systems(Update, (
(
cursor_sprite::<Hex>
.in_set(MapTopologySet(Topology::Hex)),
cursor_sprite::<Sq>
.in_set(MapTopologySet(Topology::Sq)),
)
.in_set(GridCursorChangedSet),
).in_set(Gfx2dSet::Any));
.in_set(SetStage::WantChanged(GridCursorSS)),
).in_set(Gfx2dModeSet::Any));
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/mw_app/src/gfx2d/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Plugin for Gfx2dTilemapPlugin {
tilemap_reghighlight.run_if(resource_changed::<GridCursorTileEntity>),
)
.run_if(resource_exists::<TilemapInitted>),
).in_set(Gfx2dSet::Tilemap));
).in_set(Gfx2dModeSet::Tilemap));
}
}

Expand Down
40 changes: 28 additions & 12 deletions lib/mw_app/src/gfx3d/camera.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
use bevy::{input::mouse::MouseScrollUnit, render::camera::ScalingMode, window::PrimaryWindow};
use mw_common::{game::MapDescriptor, grid::*};

use crate::map::{GridCursorTileEntity, MapTileIndex, MapTopologySet};
use crate::{prelude::*, ui::UiCamera};
use crate::camera::{GameCamera, GridCursor, GridCursorSet};
use crate::camera::{CameraControlSS, GameCamera, GridCursor, GridCursorSS};

use super::*;

pub struct Gfx3dCameraPlugin;

impl Plugin for Gfx3dCameraPlugin {
fn build(&self, app: &mut App) {
// app.add_systems(OnEnter(AppState::InGame), setup_game_camera_3d.in_set(Gfx3dSet::Any));
app.add_systems(OnEnter(AppState::InGame), setup_game_camera_3d.in_set(Gfx3dModeSet::Any));
app.add_systems(Update, (
cursor_to_ground_plane
.in_set(InGameSet(None))
.in_set(Gfx3dSet::Any)
.in_set(WorldCursorSet),
grid_cursor
.in_set(GridCursorSet)
.in_set(Gfx3dSet::Any)
.after(WorldCursorSet),
.in_set(Gfx3dModeSet::Any)
.in_set(SetStage::Provide(WorldCursorSS)),
(
grid_cursor::<Hex>.in_set(MapTopologySet(Topology::Hex)),
grid_cursor::<Sq>.in_set(MapTopologySet(Topology::Sq)),
)
.in_set(Gfx3dModeSet::Any)
.in_set(SetStage::Provide(GridCursorSS))
.in_set(SetStage::WantChanged(WorldCursorSS)),
pan_orbit_camera
.before(WorldCursorSet),
.in_set(Gfx3dModeSet::Any)
.in_set(SetStage::Provide(CameraControlSS))
.in_set(SetStage::Prepare(WorldCursorSS)),
));
}
}
Expand Down Expand Up @@ -165,6 +171,10 @@ fn cursor_to_ground_plane(
return;
};

if newpos == wc.pos && newpos == wc.pos_prev {
return;
}

wc.pos_prev = wc.pos;
wc.pos = newpos;
}
Expand All @@ -180,16 +190,18 @@ fn compute_cursor_to_ground_plane(
let ray = camera.viewport_to_world(camera_transform, cursor_position)?;
let distance = ray.intersect_plane(plane_origin, plane)?;
let global_cursor = ray.get_point(distance);

Some(Vec2::new(global_cursor.x, -global_cursor.z))
}


fn grid_cursor(
fn grid_cursor<C: Coord>(
crs_in: Res<WorldCursor>,
mut crs_out: ResMut<GridCursor>,
mapdesc: Res<MapDescriptor>,
index: Option<Res<MapTileIndex<C>>>,
mut cursor_tile: ResMut<GridCursorTileEntity>,
) {
match mapdesc.topology {
match C::TOPOLOGY {
Topology::Hex => {
let tdim = Vec2::new((3f64.sqrt() * TILE_SCALE as f64 / 2.0) as f32, TILE_SCALE);
let conv = bevy::math::Mat2::from_cols_array(
Expand All @@ -201,6 +213,10 @@ fn grid_cursor(
let new_pos = Pos::from(new);
if crs_out.0 != new_pos {
crs_out.0 = new_pos;
let new_e = index.and_then(|inner| inner.0.get(new_pos.into()).cloned());
if cursor_tile.0 != new_e {
cursor_tile.0 = new_e;
}
}
}
}
Expand Down
34 changes: 6 additions & 28 deletions lib/mw_app/src/gfx3d/map.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use mw_common::{game::{StructureKind, TileKind}, grid::{Coord, Hex}};

use crate::prelude::*;
use crate::{camera::{GridCursor, GridCursorSet}, map::{MapTileIndex, MwTilePos, TileGent}};
use crate::map::{MapTileIndex, MwTilePos, TileGent};

use super::*;

pub struct Gfx3dMapPlugin;

impl Plugin for Gfx3dMapPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (
// compute_tile_ass3d
// .in_set(InGameSet(None))
// .in_set(Gfx3dSet::Any),
debug_tile
.in_set(Gfx3dSet::Any)
.after(GridCursorSet),
));
// app.add_systems(Update, (
// // compute_tile_ass3d
// // .in_set(InGameSet(None))
// // .in_set(Gfx3dSet::Any),
// ));
}
}

Expand Down Expand Up @@ -81,25 +78,6 @@ pub enum Ass3dTileVariant {
V6,
}

fn debug_tile(
crs: Res<GridCursor>,
q_tile: Query<&TileAss3d>,
index: Res<MapTileIndex<Hex>>,
) {
if crs.is_changed() {
if let Some(e) = index.0.get(crs.0.into()) {
if let Ok(ass3d) = q_tile.get(*e) {
debug!(
"Tile @{},{}: {:?}/{:?}/{}, {:06b}",
crs.0.y(), crs.0.x(),
ass3d.kind, ass3d.variant, ass3d.rotation,
ass3d.neighmask,
);
}
}
}
}

pub fn compute_tile_ass3d(
tile_index: Res<MapTileIndex<Hex>>,
mut q_tile: Query<
Expand Down
10 changes: 7 additions & 3 deletions lib/mw_app/src/gfx3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ impl Plugin for Gfx3dPlugin {
simple3d::Gfx3dSimple3dPlugin,
));
app.configure_sets(Update, (
Gfx3dSet::Any.in_set(NeedsMapSet).run_if(rc_gfx3d_any),
Gfx3dSet::Simple3D.in_set(NeedsMapSet).run_if(rc_gfx3d_simple3d),
Gfx3dModeSet::Any.in_set(NeedsMapSet).run_if(rc_gfx3d_any),
Gfx3dModeSet::Simple3D.in_set(NeedsMapSet).run_if(rc_gfx3d_simple3d),
));
app.configure_sets(OnEnter(AppState::InGame), (
Gfx3dModeSet::Any.run_if(rc_gfx3d_any),
Gfx3dModeSet::Simple3D.run_if(rc_gfx3d_simple3d),
));
}
}

#[derive(SystemSet, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Gfx3dSet {
pub enum Gfx3dModeSet {
Any,
Simple3D,
}
Expand Down
Loading

0 comments on commit 8bf6475

Please sign in to comment.