| @@ -1,10 +1,11 @@ | |||||
| [package] | [package] | ||||
| name = "unnamed" | name = "unnamed" | ||||
| version = "0.1.0" | version = "0.1.0" | ||||
| authors = ["isabelle"] | |||||
| authors = ["Isabelle L. <me@izzabelle.dev>"] | |||||
| edition = "2018" | edition = "2018" | ||||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |||||
| [profile.dev] | |||||
| opt-level = 1 | |||||
| [dependencies] | [dependencies] | ||||
| ggez = "0.5.1" | ggez = "0.5.1" | ||||
| @@ -0,0 +1,3 @@ | |||||
| tile_data="/data/tiles.toml" | |||||
| tile_width=32 | |||||
| tile_height=32 | |||||
| @@ -6,6 +6,7 @@ mod world; | |||||
| // namespacing | // namespacing | ||||
| use ggez::{ | use ggez::{ | ||||
| conf::{WindowMode, WindowSetup}, | |||||
| event::{self, EventHandler}, | event::{self, EventHandler}, | ||||
| graphics, Context, ContextBuilder, GameResult, | graphics, Context, ContextBuilder, GameResult, | ||||
| }; | }; | ||||
| @@ -38,7 +39,8 @@ impl MainState { | |||||
| // create a new gamestate | // create a new gamestate | ||||
| pub fn new(ctx: &mut Context) -> MainState { | pub fn new(ctx: &mut Context) -> MainState { | ||||
| 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() { | 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 ctx, mut event_loop) = cb.build().expect("failed to start context"); | ||||
| let mut state = MainState::new(&mut ctx); | let mut state = MainState::new(&mut ctx); | ||||
| if let Err(err) = event::run(&mut ctx, &mut event_loop, &mut state) { | if let Err(err) = event::run(&mut ctx, &mut event_loop, &mut state) { | ||||
| @@ -1,17 +1,8 @@ | |||||
| pub struct IsometricVector2(mint::Point2<f32>); | |||||
| pub struct CartesianVector2(mint::Point2<f32>); | |||||
| 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<f32>); | |||||
| impl CartesianVector2 { | |||||
| impl IsometricVector2 { | |||||
| pub fn x(&self) -> f32 { | pub fn x(&self) -> f32 { | ||||
| self.0.x | self.0.x | ||||
| } | } | ||||
| @@ -21,31 +12,19 @@ impl CartesianVector2 { | |||||
| } | } | ||||
| } | } | ||||
| impl<T> From<(T, T)> for CartesianVector2 | |||||
| where | |||||
| T: std::convert::Into<f32>, | |||||
| { | |||||
| fn from(tuple: (T, T)) -> CartesianVector2 { | |||||
| CartesianVector2(mint::Point2 { | |||||
| x: tuple.0.into(), | |||||
| y: tuple.1.into(), | |||||
| }) | |||||
| } | |||||
| } | |||||
| impl From<CartesianVector2> 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<Point2<f32>> for IsometricVector2 { | |||||
| fn from(cart: Point2<f32>) -> IsometricVector2 { | |||||
| let x = cart.x - cart.y; | |||||
| let y = (cart.x + cart.y) / 2.0; | |||||
| IsometricVector2(Point2 { x, y }) | |||||
| } | } | ||||
| } | } | ||||
| impl From<IsometricVector2> 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<Point2<f32>> for IsometricVector2 { | |||||
| fn into(self) -> Point2<f32> { | |||||
| 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<f32>, | T: std::convert::Into<f32>, | ||||
| { | { | ||||
| fn from(tuple: (T, T)) -> IsometricVector2 { | fn from(tuple: (T, T)) -> IsometricVector2 { | ||||
| IsometricVector2(mint::Point2 { | |||||
| IsometricVector2(Point2 { | |||||
| x: tuple.0.into(), | x: tuple.0.into(), | ||||
| y: tuple.1.into(), | y: tuple.1.into(), | ||||
| }) | }) | ||||
| @@ -7,7 +7,7 @@ use ggez::{ | |||||
| graphics::{self, DrawParam}, | graphics::{self, DrawParam}, | ||||
| Context, | Context, | ||||
| }; | }; | ||||
| use mint::Vector3; | |||||
| use mint::{Point2, Vector3}; | |||||
| use std::{collections::HashMap, path::PathBuf}; | use std::{collections::HashMap, path::PathBuf}; | ||||
| use tile::*; | use tile::*; | ||||
| @@ -17,8 +17,8 @@ pub struct World { | |||||
| depth: isize, | depth: isize, | ||||
| height: isize, | height: isize, | ||||
| data: HashMap<Vector3<isize>, Tile>, | data: HashMap<Vector3<isize>, Tile>, | ||||
| _builder: TileBuilder, | |||||
| offset: CartesianVector2, | |||||
| builder: TileBuilder, | |||||
| offset: Point2<f32>, | |||||
| } | } | ||||
| impl World { | impl World { | ||||
| @@ -32,7 +32,7 @@ impl World { | |||||
| ) -> Result<World> { | ) -> Result<World> { | ||||
| let builder = TileBuilder::new(ctx, tile_config)?; | let builder = TileBuilder::new(ctx, tile_config)?; | ||||
| let mut data: HashMap<Vector3<isize>, Tile> = HashMap::new(); | let mut data: HashMap<Vector3<isize>, Tile> = HashMap::new(); | ||||
| let offset: CartesianVector2 = (350.0f32, 100.0f32).into(); | |||||
| let offset: Point2<f32> = [350.0f32, 100.0f32].into(); | |||||
| for x in 0..width { | for x in 0..width { | ||||
| for y in 0..depth { | for y in 0..depth { | ||||
| @@ -47,7 +47,7 @@ impl World { | |||||
| height, | height, | ||||
| depth, | depth, | ||||
| data, | data, | ||||
| _builder: builder, | |||||
| builder: builder, | |||||
| offset, | offset, | ||||
| }) | }) | ||||
| } | } | ||||
| @@ -58,11 +58,10 @@ impl World { | |||||
| for y in 0..self.depth { | for y in 0..self.depth { | ||||
| for z in 0..self.height { | for z in 0..self.height { | ||||
| let tile = self.data.get(&[x, y, z].into()).unwrap(); | 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 = [ | 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)); | let param = DrawParam::default().dest(mint::Point2::from_slice(&dest)); | ||||
| @@ -1,5 +1,6 @@ | |||||
| use crate::{Error, Result}; | use crate::{Error, Result}; | ||||
| use ggez::{graphics::Image, Context}; | use ggez::{graphics::Image, Context}; | ||||
| use mint::Vector2; | |||||
| use serde::Deserialize; | use serde::Deserialize; | ||||
| use std::io::Read; | use std::io::Read; | ||||
| use std::{collections::HashMap, path::PathBuf, rc::Rc}; | 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 | /// used to contain all of the textures and tile data | ||||
| pub struct TileBuilder { | pub struct TileBuilder { | ||||
| textures: HashMap<String, MasterTile>, | textures: HashMap<String, MasterTile>, | ||||
| tile_dimensions: Vector2<f32>, | |||||
| } | } | ||||
| impl TileBuilder { | impl TileBuilder { | ||||
| /// create a new tile builder | /// create a new tile builder | ||||
| pub fn new(ctx: &mut Context, tiles_config: PathBuf) -> Result<TileBuilder> { | |||||
| let mut file = ggez::filesystem::open(ctx, tiles_config)?; | |||||
| pub fn new(ctx: &mut Context, tile_config: PathBuf) -> Result<TileBuilder> { | |||||
| let mut file = ggez::filesystem::open(ctx, tile_config)?; | |||||
| let mut raw_data = String::new(); | let mut raw_data = String::new(); | ||||
| file.read_to_string(&mut raw_data)?; | 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<String, RawTile> = toml::from_str(&raw_data)?; | let raw_map: HashMap<String, RawTile> = toml::from_str(&raw_data)?; | ||||
| let mut textures: HashMap<String, MasterTile> = HashMap::new(); | let mut textures: HashMap<String, MasterTile> = HashMap::new(); | ||||
| @@ -28,7 +41,10 @@ impl TileBuilder { | |||||
| textures.insert(kind.to_owned(), tile); | textures.insert(kind.to_owned(), tile); | ||||
| }); | }); | ||||
| Ok(TileBuilder { textures: textures }) | |||||
| Ok(TileBuilder { | |||||
| textures: textures, | |||||
| tile_dimensions, | |||||
| }) | |||||
| } | } | ||||
| /// build a new tile | /// build a new tile | ||||
| @@ -43,6 +59,16 @@ impl TileBuilder { | |||||
| Err(Error::NoMasterTile { expected: kind }) | 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 | /// tile for external use in rendering and etc | ||||
| @@ -72,3 +98,10 @@ struct RawTile { | |||||
| path: PathBuf, | path: PathBuf, | ||||
| blocking: bool, | blocking: bool, | ||||
| } | } | ||||
| #[derive(Deserialize)] | |||||
| struct TileConfig { | |||||
| tile_data: PathBuf, | |||||
| tile_width: f32, | |||||
| tile_height: f32, | |||||
| } | |||||