-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added map loader * Added map loader * Fixed Map Loader issues * Changes to formatting and to failing builds * Fixed cargo check --------- Co-authored-by: HikariIT <alice.it.mail@gmail.com>
- Loading branch information
1 parent
2344062
commit 7388f7b
Showing
16 changed files
with
204 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
target | ||
Cargo.lock | ||
|
||
.idea/ | ||
.idea | ||
logs/ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod asset_manager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,41 @@ | ||
use simplelog::{ColorChoice, Config, TermLogger, TerminalMode}; | ||
use log::LevelFilter; | ||
use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode, WriteLogger}; | ||
|
||
pub fn init_logging() { | ||
TermLogger::init( | ||
log::LevelFilter::Info, | ||
Config::default(), | ||
TerminalMode::Stderr, | ||
ColorChoice::Auto, | ||
) | ||
.or_else(|err| { | ||
eprintln!("Failed to initialize logging system: {}", err); | ||
Ok::<(), ()>(()) | ||
}) | ||
.unwrap(); | ||
pub(crate) fn init_logging() { | ||
// Create log directory if it doesn't exist | ||
match std::fs::create_dir("logs") { | ||
Ok(_) => (), | ||
Err(e) => { | ||
if e.kind() != std::io::ErrorKind::AlreadyExists { | ||
eprintln!("Error while creating log directory: {}", e); | ||
std::process::exit(1); | ||
} | ||
} | ||
} | ||
|
||
let current_time_string = chrono::Local::now().format("%Y-%m-%d_%H-%M-%S").to_string(); | ||
let log_file_name = format!("logs/{}.log", current_time_string); | ||
let log_file = match std::fs::File::create(log_file_name.clone()) { | ||
Ok(file) => file, | ||
Err(e) => { | ||
eprintln!("Error while creating log file: {}", e); | ||
std::process::exit(1); | ||
} | ||
}; | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub mod algorithms; | ||
mod asset_manager; | ||
pub mod display; | ||
pub mod game; | ||
mod input; | ||
mod simulation; | ||
pub mod terrain_manager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
use crate::simulation::grid::Grid; | ||
use crate::simulation::grid::{Grid, GridPoint}; | ||
use crate::terrain_manager::sampler::Sampler2D; | ||
use image::GrayImage; | ||
|
||
struct WorldCellData {} | ||
#[derive(Default, Clone)] | ||
pub(crate) struct WorldCellData { | ||
elevation: i32, | ||
// ... | ||
} | ||
|
||
type WorldGrid = Grid<WorldCellData>; | ||
pub(crate) type WorldGrid = Grid<WorldCellData>; | ||
|
||
/* | ||
impl WorldGrid { | ||
pub fn from_height_map(height_map: GrayImage) -> WorldGrid { | ||
let mut grid = WorldGrid::new(height_map.width() as usize); | ||
let mut offset: i32 = 0; | ||
let map_sampler = Sampler2D::new(height_map.clone(), height_map.width() as usize); | ||
for r in 0..height_map.height() { | ||
for q in -offset..height_map.width() as i32 - offset { | ||
let elevation = map_sampler.sample_hexagonal_axial(q, r as i32); | ||
let point = GridPoint::new(q, r as i32); | ||
grid.set_cell_data(&point, WorldCellData { elevation }); | ||
} | ||
if r % 2 == 0 { | ||
offset += 1; | ||
} | ||
} | ||
grid | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use image::GrayImage; | ||
|
||
pub struct Map { | ||
pub side_len: usize, | ||
pub height_map: GrayImage, | ||
pub height_modifiers: GrayImage, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod map; | ||
pub mod map_loader; | ||
pub mod sampler; | ||
mod world_object; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|