Skip to content

Commit

Permalink
implement tile alerts, minimap blinking on tile alert
Browse files Browse the repository at this point in the history
  • Loading branch information
inodentry committed Sep 28, 2023
1 parent 51061cc commit 16705f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/mw_app/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ struct CitIndex {
#[derive(Component)]
pub struct MwTilePos(pub Pos);

#[derive(Component)]
pub struct TileAlert(pub Timer);

/// Map region (cit association) of a tile
#[derive(Component)]
pub struct TileRegion(pub u8);
Expand Down
25 changes: 24 additions & 1 deletion lib/mw_app/src/map/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ impl Plugin for MapUpdatePlugin {
).in_set(MapUpdateSet::TileGent),
).in_set(MapTopologySet(Topology::Sq)),
).in_set(NeedsMapSet).after(GameEventSet).after(ViewSwitchSet));
app.add_systems(Update, (
alert_timer,
).in_set(NeedsMapSet));
}
}

Expand All @@ -48,6 +51,7 @@ fn event_kind<C: Coord>(
}

fn event_owner<C: Coord>(
mut commands: Commands,
mut evr: EventReader<GameEvent>,
viewing: Res<PlidViewing>,
index: Res<MapTileIndex<C>>,
Expand All @@ -58,7 +62,13 @@ fn event_owner<C: Coord>(
continue;
}
if let MwEv::Map { pos, ev: MapEv::Owner { plid }} = ev.ev {
if let Ok(mut owner) = q_tile.get_mut(index.0[pos.into()]) {
let e_tile = index.0[pos.into()];
if let Ok(mut owner) = q_tile.get_mut(e_tile) {
if owner.0 == viewing.0 && plid != viewing.0 {
commands.entity(e_tile).insert(
TileAlert(Timer::new(Duration::from_millis(1000), TimerMode::Once))
);
}
owner.0 = plid;
}
}
Expand Down Expand Up @@ -139,3 +149,16 @@ fn event_explosion<C: Coord>(
}
}
}

fn alert_timer(
time: Res<Time>,
mut commands: Commands,
mut q_alert: Query<(Entity, &mut TileAlert)>,
) {
for (e, mut alert) in &mut q_alert {
alert.0.tick(time.delta());
if alert.0.finished() {
commands.entity(e).remove::<TileAlert>();
}
}
}
19 changes: 14 additions & 5 deletions src/minimap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use mw_app::map::{MwTilePos, TileOwner, NeedsMapSet, MapUpdateSet};
use mw_app::map::{MwTilePos, TileOwner, NeedsMapSet, MapUpdateSet, TileAlert};
use mw_common::{game::{MapDescriptor, TileKind}, grid::Topology};

use crate::{prelude::*, settings::NeedsSettingsSet};
Expand Down Expand Up @@ -41,8 +41,8 @@ fn update_minimap(
mut ass_image: ResMut<Assets<Image>>,
desc: Res<MapDescriptor>,
q_tile: Query<
(&MwTilePos, Option<&TileOwner>, &TileKind),
Or<(Changed<TileOwner>, Changed<TileKind>)>
(&MwTilePos, Option<&TileOwner>, &TileKind, Option<&TileAlert>),
Or<(Changed<TileOwner>, Changed<TileKind>, With<TileAlert>)>
>,
) {
if desc.is_changed() || settings.is_changed() {
Expand Down Expand Up @@ -77,15 +77,24 @@ fn update_minimap(
);
}

for (pos, owner, kind) in &q_tile {
for (pos, owner, kind, alert) in &q_tile {
let minimap_image = ass_image.get_mut(&minimap_handle.0)
.expect("minimap image must exist");
let w = minimap_image.texture_descriptor.size.width as i32;
let h = minimap_image.texture_descriptor.size.height as i32;

let rgba = owner
let mut rgba = owner
.map(|o| Color::from(settings.player_colors.visible[o.0.i()]).as_rgba_u8())
.unwrap_or([0, 0, 0, 0]);

if let Some(alert) = alert {
// FIXME: this can get stuck on white
// if the last frame before the TileAlert component was removed
// triggers this
if alert.0.elapsed_secs().rem_euclid(0.26) < 0.125 {
rgba = [255, 255, 255, 255];
}
}
match desc.topology {
Topology::Hex => {
let tile_size = settings.ui_hud.minimap_scale.max(2) as i32;
Expand Down

0 comments on commit 16705f3

Please sign in to comment.