Skip to content

Commit

Permalink
feat: squad center on click and minimap
Browse files Browse the repository at this point in the history
  • Loading branch information
buxx committed Apr 10, 2024
1 parent 3ca3b9a commit df6df17
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
9 changes: 9 additions & 0 deletions battle_gui/src/engine/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Engine {
))]
}
HudEvent::SelectSoldier(soldier_index) => self.select_soldier(&soldier_index),
HudEvent::CenterMapOnSquad(squad_id) => self.center_screen_on_squad(&squad_id),
}
}

Expand All @@ -47,6 +48,14 @@ impl Engine {
))]
}

pub fn center_screen_on_squad(&self, squad_id: &SquadUuid) -> Vec<EngineMessage> {
let squad = self.battle_state.squad(*squad_id);
let leader = self.battle_state.soldier(squad.leader());
vec![EngineMessage::GuiState(GuiStateMessage::CenterSceneOn(
leader.world_point(),
))]
}

pub fn select_soldier(&self, soldier_index: &SoldierIndex) -> Vec<EngineMessage> {
vec![EngineMessage::GuiState(GuiStateMessage::SetSelectedSquads(
Some(*soldier_index),
Expand Down
11 changes: 11 additions & 0 deletions battle_gui/src/ui/hud/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ impl<'a> HudBuilder<'a> {
})
.map(|s| s.world_point())
.collect();
let selected_squads = self
.gui_state
.selected_squads()
.1
.clone()
.iter()
.map(|i| self.battle_state.squad(*i))
.map(|s| self.battle_state.soldier(s.leader()))
.map(|s| s.world_point())
.collect();
Minimap::new(
*point,
self.battle_state.map().visual_width() as f32,
Expand All @@ -156,6 +166,7 @@ impl<'a> HudBuilder<'a> {
self.gui_state.zoom.clone(),
blue_positions,
red_positions,
selected_squads,
)
}
}
1 change: 1 addition & 0 deletions battle_gui/src/ui/hud/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub enum HudEvent {
RequestBeginBattle,
RequestEndBattle,
SelectSquad(SquadUuid),
CenterMapOnSquad(SquadUuid),
SelectSoldier(SoldierIndex),
CenterMapOn(WorldPoint),
}
24 changes: 21 additions & 3 deletions battle_gui/src/ui/hud/minimap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use battle_core::types::{Offset, WindowPoint, WorldPoint};
use battle_core::types::{Offset, SquadUuid, WindowPoint, WorldPoint};
use ggegui::egui::Vec2;
use ggez::{
graphics::{
Expand Down Expand Up @@ -28,6 +28,7 @@ pub struct Minimap {
zoom: Zoom,
blue_positions: Vec<WorldPoint>,
red_positions: Vec<WorldPoint>,
selected_squads: Vec<WorldPoint>,
}

impl Minimap {
Expand All @@ -39,6 +40,7 @@ impl Minimap {
zoom: Zoom,
blue_positions: Vec<WorldPoint>,
red_positions: Vec<WorldPoint>,
selected_squads: Vec<WorldPoint>,
) -> Self {
Self {
point,
Expand All @@ -48,6 +50,7 @@ impl Minimap {
zoom,
blue_positions,
red_positions,
selected_squads,
}
}

Expand Down Expand Up @@ -90,10 +93,11 @@ impl Minimap {

pub fn draw_squads(&self, ctx: &Context, mesh_builder: &mut MeshBuilder) -> GameResult {
for position in &self.blue_positions {
self.draw_side_squads(ctx, mesh_builder, position, Color::BLUE)?
let stroke = self.selected_squads.contains(position);
self.draw_side_squads(ctx, mesh_builder, position, Color::BLUE, stroke)?
}
for position in &self.red_positions {
self.draw_side_squads(ctx, mesh_builder, position, Color::RED)?
self.draw_side_squads(ctx, mesh_builder, position, Color::RED, false)?
}

Ok(())
Expand All @@ -105,6 +109,7 @@ impl Minimap {
mesh_builder: &mut MeshBuilder,
position: &WorldPoint,
color: Color,
stroke: bool,
) -> GameResult {
let relative_point =
Vec2::new(position.x, position.y) / Vec2::new(self.map_width, self.map_height);
Expand All @@ -125,6 +130,19 @@ impl Minimap {
color,
)?;

if stroke {
mesh_builder.rectangle(
DrawMode::Stroke(StrokeOptions::DEFAULT),
Rect::new(
point.x - SQUAD_SQUARE_SIZE / 2.,
point.y - SQUAD_SQUARE_SIZE / 2.,
SQUAD_SQUARE_SIZE,
SQUAD_SQUARE_SIZE,
),
Color::GREEN,
)?;
}

Ok(())
}
}
Expand Down
7 changes: 6 additions & 1 deletion battle_gui/src/ui/hud/squad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,12 @@ impl Component<HudEvent> for SquadStatuses {
&& mouse_position.y >= draw_card.dest.y
&& mouse_position.y <= draw_card.dest.y + SQUAD_CARD_HEIGHT
{
return Some(HudEvent::SelectSquad(*draw_card.squad_status.squad_id()));
let squad_id = draw_card.squad_status.squad_id();
if self.selected_squads.contains(squad_id) {
return Some(HudEvent::CenterMapOnSquad(*squad_id));
} else {
return Some(HudEvent::SelectSquad(*squad_id));
}
}
}

Expand Down

0 comments on commit df6df17

Please sign in to comment.