From 8f3f8c804f07b16c2512abc1a1baeed2996afeb8 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Mon, 30 Jul 2018 00:17:20 +0200 Subject: [PATCH] Change tile type from magic number to enum --- src/main.rs | 13 ++++++++++--- src/texture_manager.rs | 25 +++++++++++-------------- src/tile.rs | 18 +++++++++++++++--- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 266ade3..d2c0dc0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use perlin_noise::PerlinNoise; use canvas_copy_at::CopyAt; use texture_manager::TextureManager; -use tile::Tile; +use tile::{Tile, TileType}; struct GameState { camera_x: i32, @@ -99,7 +99,7 @@ fn render( for (y, row) in map.iter().enumerate() { for (x, tile) in row.iter().enumerate().rev() { - let texture = &texture_manager[tile.type_]; + let texture = &texture_manager[tile.type_.clone()]; let (screen_x, screen_y) = world_to_screen(x as i32, y as i32, tile.height); canvas @@ -117,7 +117,7 @@ fn render( fn generate_map() -> Vec> { let perlin = PerlinNoise::new(); - let mut map = vec![vec![Tile::new(6, 0); 128]; 128]; + let mut map = vec![vec![Tile::new(TileType::Grass, 0); 128]; 128]; for (y, row) in map.iter_mut().enumerate() { for (x, tile) in row.iter_mut().enumerate() { @@ -126,6 +126,13 @@ fn generate_map() -> Vec> { } } + map[0][0].type_ = TileType::Road { + direction: tile::Direction::NorthSouth, + }; + map[0][1].type_ = TileType::Road { + direction: tile::Direction::EastWest, + }; + map } diff --git a/src/texture_manager.rs b/src/texture_manager.rs index a7003ab..756f576 100644 --- a/src/texture_manager.rs +++ b/src/texture_manager.rs @@ -4,6 +4,8 @@ use sdl2::video::WindowContext; use std::collections::HashMap; use std::ops::Index; +use tile::{Direction, TileType}; + pub struct TextureManager<'a> { textures: HashMap<&'a str, Texture<'a>>, } @@ -26,27 +28,22 @@ impl<'a> TextureManager<'a> { } pub fn load_textures(&mut self, texture_creator: &'a TextureCreator) { - self.load_texture(texture_creator, "bridge_east", "data/png/bridgeEast.png"); - self.load_texture(texture_creator, "bridge_north", "data/png/bridgeNorth.png"); - self.load_texture(texture_creator, "crossroad", "data/png/crossroad.png"); - self.load_texture(texture_creator, "dirt", "data/png/dirt.png"); - self.load_texture(texture_creator, "dirt_high", "data/png/dirtHigh.png"); self.load_texture(texture_creator, "grass", "data/png/grass.png"); + self.load_texture(texture_creator, "road_north", "data/png/roadNorth.png"); + self.load_texture(texture_creator, "road_east", "data/png/roadEast.png"); } } -impl<'a> Index for TextureManager<'a> { +impl<'a> Index for TextureManager<'a> { type Output = Texture<'a>; - fn index(&self, index: usize) -> &Texture<'a> { + fn index(&self, index: TileType) -> &Texture<'a> { let key = match index { - 1 => "bridge_east", - 2 => "bridge_north", - 3 => "crossroad", - 4 => "dirt", - 5 => "dirt_high", - 6 => "grass", - _ => panic!("invalid texture ID"), + TileType::Grass => "grass", + TileType::Road { direction } => match direction { + Direction::NorthSouth => "road_north", + Direction::EastWest => "road_east", + }, }; &self.textures[key] diff --git a/src/tile.rs b/src/tile.rs index 53f3975..527d3c1 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,11 +1,23 @@ -#[derive(Clone)] +#[derive(Debug, Clone)] +pub enum Direction { + NorthSouth, + EastWest, +} + +#[derive(Debug, Clone)] +pub enum TileType { + Grass, + Road { direction: Direction }, +} + +#[derive(Debug, Clone)] pub struct Tile { - pub type_: usize, + pub type_: TileType, pub height: i32, } impl Tile { - pub fn new(type_: usize, height: i32) -> Tile { + pub fn new(type_: TileType, height: i32) -> Tile { Tile { type_, height } } }