Change tile type from magic number to enum

This commit is contained in:
Sijmen 2018-07-30 00:17:20 +02:00
parent 0091bf51ee
commit 8f3f8c804f
3 changed files with 36 additions and 20 deletions

View file

@ -17,7 +17,7 @@ use perlin_noise::PerlinNoise;
use canvas_copy_at::CopyAt; use canvas_copy_at::CopyAt;
use texture_manager::TextureManager; use texture_manager::TextureManager;
use tile::Tile; use tile::{Tile, TileType};
struct GameState { struct GameState {
camera_x: i32, camera_x: i32,
@ -99,7 +99,7 @@ fn render(
for (y, row) in map.iter().enumerate() { for (y, row) in map.iter().enumerate() {
for (x, tile) in row.iter().enumerate().rev() { 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); let (screen_x, screen_y) = world_to_screen(x as i32, y as i32, tile.height);
canvas canvas
@ -117,7 +117,7 @@ fn render(
fn generate_map() -> Vec<Vec<Tile>> { fn generate_map() -> Vec<Vec<Tile>> {
let perlin = PerlinNoise::new(); 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 (y, row) in map.iter_mut().enumerate() {
for (x, tile) in row.iter_mut().enumerate() { for (x, tile) in row.iter_mut().enumerate() {
@ -126,6 +126,13 @@ fn generate_map() -> Vec<Vec<Tile>> {
} }
} }
map[0][0].type_ = TileType::Road {
direction: tile::Direction::NorthSouth,
};
map[0][1].type_ = TileType::Road {
direction: tile::Direction::EastWest,
};
map map
} }

View file

@ -4,6 +4,8 @@ use sdl2::video::WindowContext;
use std::collections::HashMap; use std::collections::HashMap;
use std::ops::Index; use std::ops::Index;
use tile::{Direction, TileType};
pub struct TextureManager<'a> { pub struct TextureManager<'a> {
textures: HashMap<&'a str, Texture<'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<WindowContext>) { pub fn load_textures(&mut self, texture_creator: &'a TextureCreator<WindowContext>) {
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, "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<usize> for TextureManager<'a> { impl<'a> Index<TileType> for TextureManager<'a> {
type Output = Texture<'a>; type Output = Texture<'a>;
fn index(&self, index: usize) -> &Texture<'a> { fn index(&self, index: TileType) -> &Texture<'a> {
let key = match index { let key = match index {
1 => "bridge_east", TileType::Grass => "grass",
2 => "bridge_north", TileType::Road { direction } => match direction {
3 => "crossroad", Direction::NorthSouth => "road_north",
4 => "dirt", Direction::EastWest => "road_east",
5 => "dirt_high", },
6 => "grass",
_ => panic!("invalid texture ID"),
}; };
&self.textures[key] &self.textures[key]

View file

@ -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 struct Tile {
pub type_: usize, pub type_: TileType,
pub height: i32, pub height: i32,
} }
impl Tile { impl Tile {
pub fn new(type_: usize, height: i32) -> Tile { pub fn new(type_: TileType, height: i32) -> Tile {
Tile { type_, height } Tile { type_, height }
} }
} }