Skip to content

Commit

Permalink
Changes to formatting and to failing builds
Browse files Browse the repository at this point in the history
  • Loading branch information
HikariIT committed May 10, 2024
1 parent a939b09 commit 4fe7b80
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 128 deletions.
30 changes: 18 additions & 12 deletions bdi_game/src/asset_manager/asset_manager.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
pub struct AssetManager {}

impl AssetManager {
const ASSET_DIR: &'static str = "assets";

const MAP_SUBDIR: &'static str = "maps";
const MAP_EXTENSION: &'static str = "png";

pub fn get_map_path(map_name: &str) -> String {
format!("{}/{}/{}.{}", Self::ASSET_DIR, Self::MAP_SUBDIR, map_name, Self::MAP_EXTENSION)
}
}
pub struct AssetManager {}

impl AssetManager {
const ASSET_DIR: &'static str = "assets";

const MAP_SUBDIR: &'static str = "maps";
const MAP_EXTENSION: &'static str = "png";

pub fn get_map_path(map_name: &str) -> String {
format!(
"{}/{}/{}.{}",
Self::ASSET_DIR,
Self::MAP_SUBDIR,
map_name,
Self::MAP_EXTENSION
)
}
}
2 changes: 1 addition & 1 deletion bdi_game/src/asset_manager/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod asset_manager;
pub mod asset_manager;
4 changes: 2 additions & 2 deletions bdi_game/src/game/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::{Duration, Instant};
use crate::{
display::game_display::GameDisplay,
input::Input,
simulation::{simulation::Simulation, world_state::WorldState, world_grid::WorldGrid},
simulation::{simulation::Simulation, world_grid::WorldGrid, world_state::WorldState},
terrain_manager::map_loader::MapLoader,
};

Expand All @@ -25,7 +25,7 @@ impl<D: GameDisplay> Game<D> {
display,
simulation: Simulation {
state: WorldState {},
grid: WorldGrid { data: vec![] }
grid: WorldGrid { data: vec![] },
},
// grid: WorldGrid::from_height_map(map),
input: Input {},
Expand Down
29 changes: 11 additions & 18 deletions bdi_game/src/game/logging.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use log::LevelFilter;
use simplelog::{ColorChoice, Config, TermLogger, TerminalMode, CombinedLogger, WriteLogger};

use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode, WriteLogger};

pub(crate) fn init_logging() {
// Create log directory if it doesn't exist
Expand All @@ -24,25 +23,19 @@ pub(crate) fn init_logging() {
}
};

match CombinedLogger::init(
vec![
TermLogger::new(
LevelFilter::Warn,
Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto
),
WriteLogger::new(
LevelFilter::Debug,
Config::default(),
log_file
),
]
) {
match CombinedLogger::init(vec![
TermLogger::new(
LevelFilter::Warn,
Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto,
),
WriteLogger::new(LevelFilter::Debug, Config::default(), log_file),
]) {
Ok(_) => (),
Err(e) => {
eprintln!("Error while initializing logger: {}", e);
std::process::exit(1);
}
}
}
}
2 changes: 1 addition & 1 deletion bdi_game/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod algorithms;
pub mod terrain_manager;
mod asset_manager;
pub mod display;
pub mod game;
mod input;
mod simulation;
mod asset_manager;
2 changes: 1 addition & 1 deletion bdi_game/src/simulation/simulation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::world_state::WorldState;
use super::world_grid::WorldGrid;
use super::world_state::WorldState;

pub struct Simulation {
pub state: WorldState,
Expand Down
4 changes: 2 additions & 2 deletions bdi_game/src/simulation/world_grid.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use image::GrayImage;
use crate::simulation::grid::{Grid, GridPoint};
use crate::terrain_manager::sampler::Sampler2D;
use image::GrayImage;

#[derive(Default, Clone)]
pub(crate) struct WorldCellData {
Expand Down Expand Up @@ -32,4 +32,4 @@ impl WorldGrid {
grid
}
}
*/
*/
14 changes: 7 additions & 7 deletions bdi_game/src/terrain_manager/map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use image::GrayImage;

pub struct Map {
pub side_len: usize,
pub height_map: GrayImage,
pub height_modifiers: GrayImage,
}
use image::GrayImage;

pub struct Map {
pub side_len: usize,
pub height_map: GrayImage,
pub height_modifiers: GrayImage,
}
72 changes: 36 additions & 36 deletions bdi_game/src/terrain_manager/map_loader.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
use log::{debug, info};
use image::GrayImage;
use image::io::Reader as ImageReader;
use crate::asset_manager::asset_manager::AssetManager;

pub struct MapLoader {
map_asset_path: String,
}

impl MapLoader {
pub fn map_from_image(map_name: &str) -> GrayImage {
let map_path = AssetManager::get_map_path(map_name);

let img = match ImageReader::open(map_path) {
Ok(img) => img,
Err(e) => {
eprintln!("Error while loading map from file: {}", e);
return GrayImage::new(0, 0);
}
};

let img = match img.decode() {
Ok(img) => img,
Err(e) => {
eprintln!("Error while decoding map format: {}", e);
return GrayImage::new(0, 0);
}
};

info!("Map '{}' loaded successfully!", map_name);
debug!("Map format: {:?}", img.color());
debug!("Map dimensions: {}x{}", img.width(), img.height());

return img.into_luma8();
}
}
use crate::asset_manager::asset_manager::AssetManager;
use image::io::Reader as ImageReader;
use image::GrayImage;
use log::{debug, info};

pub struct MapLoader {
map_asset_path: String,
}

impl MapLoader {
pub fn map_from_image(map_name: &str) -> GrayImage {
let map_path = AssetManager::get_map_path(map_name);

let img = match ImageReader::open(map_path) {
Ok(img) => img,
Err(e) => {
eprintln!("Error while loading map from file: {}", e);
return GrayImage::new(0, 0);
}
};

let img = match img.decode() {
Ok(img) => img,
Err(e) => {
eprintln!("Error while decoding map format: {}", e);
return GrayImage::new(0, 0);
}
};

info!("Map '{}' loaded successfully!", map_name);
debug!("Map format: {:?}", img.color());
debug!("Map dimensions: {}x{}", img.width(), img.height());

return img.into_luma8();
}
}
8 changes: 4 additions & 4 deletions bdi_game/src/terrain_manager/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod map_loader;
pub mod sampler;
mod map;
mod world_object;
mod map;
pub mod map_loader;
pub mod sampler;
mod world_object;
88 changes: 44 additions & 44 deletions bdi_game/src/terrain_manager/sampler.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
use image::GrayImage;

pub struct Sampler2D {
height_map: GrayImage,
side_len: usize,
}

impl Sampler2D {
pub fn new(height_map: GrayImage, side_len: usize) -> Sampler2D {
Sampler2D {
height_map,
side_len
}
}

pub fn sample_hexagonal_axial(&self, q: i32, r: i32) -> i32 {
let scale = self.height_map.width() as f32 / self.side_len as f32;

let offset_q_x = scale * q as f32;
let offset_q_y = 0f32;

let offset_r_x = scale * 0.5 * r as f32;
let offset_r_y = scale * 3.0f32.sqrt() / 2.0 * r as f32;

let x = offset_q_x + offset_r_x;
let y = offset_q_y + offset_r_y;

// Linear interpolation
let x0 = x.floor() as u32;
let x1 = x.ceil() as u32;
let y0 = y.floor() as u32;
let y1 = y.ceil() as u32;

let x0y0 = self.height_map.get_pixel(x0, y0)[0] as f32;
let x1y0 = self.height_map.get_pixel(x1, y0)[0] as f32;
let x0y1 = self.height_map.get_pixel(x0, y1)[0] as f32;
let x1y1 = self.height_map.get_pixel(x1, y1)[0] as f32;

let x0y = x0y0 + (x0y1 - x0y0) * (y - y0 as f32);
let x1y = x1y0 + (x1y1 - x1y0) * (y - y0 as f32);

(x0y + (x1y - x0y) * (x - x0 as f32)) as i32
}
}
use image::GrayImage;

pub struct Sampler2D {
height_map: GrayImage,
side_len: usize,
}

impl Sampler2D {
pub fn new(height_map: GrayImage, side_len: usize) -> Sampler2D {
Sampler2D {
height_map,
side_len,
}
}

pub fn sample_hexagonal_axial(&self, q: i32, r: i32) -> i32 {
let scale = self.height_map.width() as f32 / self.side_len as f32;

let offset_q_x = scale * q as f32;
let offset_q_y = 0f32;

let offset_r_x = scale * 0.5 * r as f32;
let offset_r_y = scale * 3.0f32.sqrt() / 2.0 * r as f32;

let x = offset_q_x + offset_r_x;
let y = offset_q_y + offset_r_y;

// Linear interpolation
let x0 = x.floor() as u32;
let x1 = x.ceil() as u32;
let y0 = y.floor() as u32;
let y1 = y.ceil() as u32;

let x0y0 = self.height_map.get_pixel(x0, y0)[0] as f32;
let x1y0 = self.height_map.get_pixel(x1, y0)[0] as f32;
let x0y1 = self.height_map.get_pixel(x0, y1)[0] as f32;
let x1y1 = self.height_map.get_pixel(x1, y1)[0] as f32;

let x0y = x0y0 + (x0y1 - x0y0) * (y - y0 as f32);
let x1y = x1y0 + (x1y1 - x1y0) * (y - y0 as f32);

(x0y + (x1y - x0y) * (x - x0 as f32)) as i32
}
}
1 change: 1 addition & 0 deletions bdi_game/src/terrain_manager/world_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 4fe7b80

Please sign in to comment.