From acc32d346112c0f6721098de8f671c6f2fd7e7a6 Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Fri, 10 Jul 2020 00:18:38 -0500 Subject: [PATCH] fucking with the structuring for cartesian vectors/points as well as getting config loading(ish) --- Cargo.toml | 5 ++-- resources/{data => }/conf.toml | 0 resources/data/tile_conf.toml | 3 +++ src/main.rs | 19 ++++++++++--- src/util.rs | 49 ++++++++++------------------------ src/world.rs | 17 ++++++------ src/world/tile.rs | 39 ++++++++++++++++++++++++--- 7 files changed, 80 insertions(+), 52 deletions(-) rename resources/{data => }/conf.toml (100%) create mode 100644 resources/data/tile_conf.toml diff --git a/Cargo.toml b/Cargo.toml index f32065d..862622a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,11 @@ [package] name = "unnamed" version = "0.1.0" -authors = ["isabelle"] +authors = ["Isabelle L. "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.dev] +opt-level = 1 [dependencies] ggez = "0.5.1" diff --git a/resources/data/conf.toml b/resources/conf.toml similarity index 100% rename from resources/data/conf.toml rename to resources/conf.toml diff --git a/resources/data/tile_conf.toml b/resources/data/tile_conf.toml new file mode 100644 index 0000000..90dbfa9 --- /dev/null +++ b/resources/data/tile_conf.toml @@ -0,0 +1,3 @@ +tile_data="/data/tiles.toml" +tile_width=32 +tile_height=32 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 87becb6..16cfc0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod world; // namespacing use ggez::{ + conf::{WindowMode, WindowSetup}, event::{self, EventHandler}, graphics, Context, ContextBuilder, GameResult, }; @@ -38,7 +39,8 @@ impl MainState { // create a new gamestate pub fn new(ctx: &mut Context) -> MainState { MainState { - world: World::new(ctx, "/data/tiles.toml".into(), 5, 5, 1).expect("failed init world"), + world: World::new(ctx, "/data/tile_conf.toml".into(), 20, 20, 1) + .expect("failed init world"), } } } @@ -60,9 +62,20 @@ impl EventHandler for MainState { } fn main() { - let cb = ContextBuilder::new("unnamed", "Isabelle L.").add_resource_path("./"); + let win_mode = WindowMode { + width: 1024.0, + height: 768.0, + ..Default::default() + }; + let win_setup = WindowSetup { + title: "unnamed game thinger".to_owned(), + ..Default::default() + }; + let cb = ContextBuilder::new("unnamed", "Isabelle L.") + .add_resource_path("./") + .window_mode(win_mode) + .window_setup(win_setup); let (mut ctx, mut event_loop) = cb.build().expect("failed to start context"); - let mut state = MainState::new(&mut ctx); if let Err(err) = event::run(&mut ctx, &mut event_loop, &mut state) { diff --git a/src/util.rs b/src/util.rs index 6358265..0781d21 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,17 +1,8 @@ -pub struct IsometricVector2(mint::Point2); -pub struct CartesianVector2(mint::Point2); +use mint::Point2; -impl IsometricVector2 { - pub fn x(&self) -> f32 { - self.0.x - } - - pub fn y(&self) -> f32 { - self.0.y - } -} +pub struct IsometricVector2(Point2); -impl CartesianVector2 { +impl IsometricVector2 { pub fn x(&self) -> f32 { self.0.x } @@ -21,31 +12,19 @@ impl CartesianVector2 { } } -impl From<(T, T)> for CartesianVector2 -where - T: std::convert::Into, -{ - fn from(tuple: (T, T)) -> CartesianVector2 { - CartesianVector2(mint::Point2 { - x: tuple.0.into(), - y: tuple.1.into(), - }) - } -} - -impl From for IsometricVector2 { - fn from(cart: CartesianVector2) -> IsometricVector2 { - let x = cart.x() - cart.y(); - let y = (cart.x() + cart.y()) / 2.0; - IsometricVector2(mint::Point2 { x, y }) +impl From> for IsometricVector2 { + fn from(cart: Point2) -> IsometricVector2 { + let x = cart.x - cart.y; + let y = (cart.x + cart.y) / 2.0; + IsometricVector2(Point2 { x, y }) } } -impl From for CartesianVector2 { - fn from(iso: IsometricVector2) -> CartesianVector2 { - let x = (2.0 * iso.y() + iso.x()) / 2.0; - let y = (2.0 * iso.y() - iso.x()) / 2.0; - CartesianVector2(mint::Point2 { x, y }) +impl Into> for IsometricVector2 { + fn into(self) -> Point2 { + let x = (2.0 * self.y() + self.x()) / 2.0; + let y = (2.0 * self.y() - self.x()) / 2.0; + Point2 { x, y } } } @@ -54,7 +33,7 @@ where T: std::convert::Into, { fn from(tuple: (T, T)) -> IsometricVector2 { - IsometricVector2(mint::Point2 { + IsometricVector2(Point2 { x: tuple.0.into(), y: tuple.1.into(), }) diff --git a/src/world.rs b/src/world.rs index d8f8590..f2c1f55 100644 --- a/src/world.rs +++ b/src/world.rs @@ -7,7 +7,7 @@ use ggez::{ graphics::{self, DrawParam}, Context, }; -use mint::Vector3; +use mint::{Point2, Vector3}; use std::{collections::HashMap, path::PathBuf}; use tile::*; @@ -17,8 +17,8 @@ pub struct World { depth: isize, height: isize, data: HashMap, Tile>, - _builder: TileBuilder, - offset: CartesianVector2, + builder: TileBuilder, + offset: Point2, } impl World { @@ -32,7 +32,7 @@ impl World { ) -> Result { let builder = TileBuilder::new(ctx, tile_config)?; let mut data: HashMap, Tile> = HashMap::new(); - let offset: CartesianVector2 = (350.0f32, 100.0f32).into(); + let offset: Point2 = [350.0f32, 100.0f32].into(); for x in 0..width { for y in 0..depth { @@ -47,7 +47,7 @@ impl World { height, depth, data, - _builder: builder, + builder: builder, offset, }) } @@ -58,11 +58,10 @@ impl World { for y in 0..self.depth { for z in 0..self.height { let tile = self.data.get(&[x, y, z].into()).unwrap(); - let iso_coord: IsometricVector2 = - CartesianVector2::from((x as f32, y as f32)).into(); + let iso_coord: IsometricVector2 = Point2::from([x as f32, y as f32]).into(); let dest = [ - (iso_coord.x() * 16.0 + self.offset.x()) as f32, - (iso_coord.y() * 16.0 + self.offset.y()) as f32, + iso_coord.x() * self.builder.tile_width() / 2.0 + self.offset.x, + iso_coord.y() * self.builder.tile_height() / 2.0 + self.offset.y, ]; let param = DrawParam::default().dest(mint::Point2::from_slice(&dest)); diff --git a/src/world/tile.rs b/src/world/tile.rs index 35a1490..e73e24b 100644 --- a/src/world/tile.rs +++ b/src/world/tile.rs @@ -1,5 +1,6 @@ use crate::{Error, Result}; use ggez::{graphics::Image, Context}; +use mint::Vector2; use serde::Deserialize; use std::io::Read; use std::{collections::HashMap, path::PathBuf, rc::Rc}; @@ -7,15 +8,27 @@ use std::{collections::HashMap, path::PathBuf, rc::Rc}; /// used to contain all of the textures and tile data pub struct TileBuilder { textures: HashMap, + tile_dimensions: Vector2, } impl TileBuilder { /// create a new tile builder - pub fn new(ctx: &mut Context, tiles_config: PathBuf) -> Result { - let mut file = ggez::filesystem::open(ctx, tiles_config)?; + pub fn new(ctx: &mut Context, tile_config: PathBuf) -> Result { + let mut file = ggez::filesystem::open(ctx, tile_config)?; let mut raw_data = String::new(); file.read_to_string(&mut raw_data)?; + let loaded_config: TileConfig = toml::from_str(&raw_data)?; + // tile_dimensions construction + let tile_dimensions = Vector2 { + x: loaded_config.tile_width, + y: loaded_config.tile_height, + }; + + // load the tiles data + let mut file = ggez::filesystem::open(ctx, loaded_config.tile_data)?; + let mut raw_data = String::new(); + file.read_to_string(&mut raw_data)?; let raw_map: HashMap = toml::from_str(&raw_data)?; let mut textures: HashMap = HashMap::new(); @@ -28,7 +41,10 @@ impl TileBuilder { textures.insert(kind.to_owned(), tile); }); - Ok(TileBuilder { textures: textures }) + Ok(TileBuilder { + textures: textures, + tile_dimensions, + }) } /// build a new tile @@ -43,6 +59,16 @@ impl TileBuilder { Err(Error::NoMasterTile { expected: kind }) } } + + /// return the tile_width + pub fn tile_width(&self) -> f32 { + self.tile_dimensions.x + } + + /// return the tile_height + pub fn tile_height(&self) -> f32 { + self.tile_dimensions.y + } } /// tile for external use in rendering and etc @@ -72,3 +98,10 @@ struct RawTile { path: PathBuf, blocking: bool, } + +#[derive(Deserialize)] +struct TileConfig { + tile_data: PathBuf, + tile_width: f32, + tile_height: f32, +}