Change tile type from magic number to enum
This commit is contained in:
parent
0091bf51ee
commit
8f3f8c804f
3 changed files with 36 additions and 20 deletions
13
src/main.rs
13
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<Vec<Tile>> {
|
||||
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<Vec<Tile>> {
|
|||
}
|
||||
}
|
||||
|
||||
map[0][0].type_ = TileType::Road {
|
||||
direction: tile::Direction::NorthSouth,
|
||||
};
|
||||
map[0][1].type_ = TileType::Road {
|
||||
direction: tile::Direction::EastWest,
|
||||
};
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
|
|
|
@ -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<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, "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>;
|
||||
|
||||
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]
|
||||
|
|
18
src/tile.rs
18
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 }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue