Skip to content

Commit

Permalink
feat: add minimap
Browse files Browse the repository at this point in the history
  • Loading branch information
buxx committed Aug 9, 2023
1 parent f19f158 commit 8519fbd
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions battle_gui/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ impl EventHandler<ggez::GameError> for Engine {
.draw_ui(ctx, &mut canvas, ui_draw_param, mesh_builder)?;

HudPainter::new(&self.hud, &self.gui_state).draw(ctx, &mut canvas)?;
self.graphics.draw_minimap(ctx, &mut canvas)?;

self.draw_egui(ctx, &mut canvas);

Expand Down
63 changes: 63 additions & 0 deletions battle_gui/src/graphics/minimap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use battle_core::map::Map;
use ggez::{
graphics::{DrawParam, Image, InstanceArray},
Context, GameError, GameResult,
};
use image::imageops::{resize, FilterType};
use oc_core::resources::Resources;

use crate::ui::hud::{
builder::{MARGIN, RIGHT_BOX_WIDTH},
morale::MORALE_INDICATOR_HEIGHT,
HUD_HEIGHT,
};

pub struct MinimapBuilder<'a> {
ctx: &'a mut Context,
map: &'a Map,
}

impl<'a> MinimapBuilder<'a> {
pub fn new(ctx: &'a mut Context, map: &'a Map) -> Self {
Self { ctx, map }
}

pub fn build(&self) -> GameResult<InstanceArray> {
let resources = match Resources::new() {
Ok(resources) => resources,
Err(error) => return Err(GameError::ResourceLoadError(error.to_string())),
};
let bg_image_path_abs = resources.lib().join(
self.map
.background_image_path()
.strip_prefix("/")
.expect("Must start with /"),
);
let minimap_path_cache_abs = resources
.cache_abs()
.join(format!("{}__minimap.png", self.map.name()));

let bg_dark_image_path_rel = resources
.cache_ggez()
.join(format!("{}__minimap.png", self.map.name()));

if !minimap_path_cache_abs.exists() {
let bg_image = image::open(&bg_image_path_abs)?.into_rgba8();
let minimap = resize(
&bg_image,
(RIGHT_BOX_WIDTH - MARGIN * 2.0) as u32,
(HUD_HEIGHT - MORALE_INDICATOR_HEIGHT - MARGIN * 3.0) as u32,
FilterType::Gaussian,
);
minimap.save(minimap_path_cache_abs)?;
}

let minimap = Image::from_path(self.ctx, &bg_dark_image_path_rel)?;
let mut instance_array = InstanceArray::new(self.ctx, minimap);
instance_array.push(
DrawParam::new(), // .src(Rect::new(1.0, 1.0, 1.0, 1.0))
// .dest(Vec2::new(0., 0.)),
);
Ok(instance_array)
}
}
27 changes: 26 additions & 1 deletion battle_gui/src/graphics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ use battle_core::{
};
use oc_core::resources::RESOURCE_PATH;

use crate::{debug::DebugTerrain, ui::menu::squad_menu_sprite_info};
use crate::{
debug::DebugTerrain,
ui::{
hud::{
builder::{MARGIN, RIGHT_BOX_WIDTH},
morale::MORALE_INDICATOR_HEIGHT,
HUD_HEIGHT,
},
menu::squad_menu_sprite_info,
},
};

use self::{
background::{Background, BackgroundBuilder},
Expand All @@ -35,6 +45,7 @@ use self::{
explosions::{Explosions, ExplosionsBuilder},
interiors::{Interiors, InteriorsBuilder},
message::GraphicsMessage,
minimap::MinimapBuilder,
qualified::Zoom,
soldier::{Soldiers, SoldiersBuilder},
vehicles::{Vehicles, VehiclesBuilder},
Expand All @@ -49,6 +60,7 @@ pub mod flag;
pub mod interiors;
pub mod map;
pub mod message;
pub mod minimap;
pub mod order;
pub mod qualified;
pub mod soldier;
Expand Down Expand Up @@ -97,6 +109,7 @@ pub struct Graphics {
background: Background,
dark_background: Background,
dark_background_first: bool,
minimap: InstanceArray,
flags: InstanceArray,
// Map interiors sprite batch
interiors: Interiors,
Expand Down Expand Up @@ -142,6 +155,7 @@ impl Graphics {
let background = BackgroundBuilder::new(ctx, map).build()?;
let dark_background = BackgroundBuilder::new(ctx, map).dark(true).build()?;
let map_interiors_batch = InteriorsBuilder::new(ctx, map).build()?;
let minimap = MinimapBuilder::new(ctx, map).build()?;
let decor = DecorsBuilder::new(ctx, map)
.rule(DrawOnly(a_control.clone()))
.build()?;
Expand Down Expand Up @@ -169,6 +183,7 @@ impl Graphics {
background,
dark_background,
dark_background_first: false,
minimap,
flags,
interiors: map_interiors_batch,
decor,
Expand Down Expand Up @@ -592,6 +607,16 @@ impl Graphics {
self.decor = DecorsBuilder::new(ctx, map).rule(DrawAll).build()?;
Ok(())
}

pub fn draw_minimap(&self, ctx: &mut Context, canvas: &mut Canvas) -> GameResult {
let window = ctx.gfx.window().inner_size();
let dest = WindowPoint::new(
window.width as f32 - RIGHT_BOX_WIDTH + MARGIN,
window.height as f32 - HUD_HEIGHT + MORALE_INDICATOR_HEIGHT + MARGIN * 2.0,
);
canvas.draw(&self.minimap, DrawParam::new().dest(dest.to_vec2()));
Ok(())
}
}

pub fn create_batch(file_path: &str, ctx: &mut Context) -> GameResult<InstanceArray> {
Expand Down

0 comments on commit 8519fbd

Please sign in to comment.