Small refactor

This commit is contained in:
Sijmen 2018-07-29 22:58:26 +02:00
parent c433a25a05
commit 5dc1faad63

View file

@ -21,6 +21,14 @@ use tile::Tile;
struct GameState { struct GameState {
camera_x: i32, camera_x: i32,
camera_y: i32, camera_y: i32,
impl GameState {
fn new() -> Self {
Self {
camera_x: 0,
camera_y: 0,
}
}
} }
fn world_to_screen(x: i32, y: i32, z: i32) -> (i32, i32) { fn world_to_screen(x: i32, y: i32, z: i32) -> (i32, i32) {
@ -54,10 +62,10 @@ fn update(event_pump: &mut EventPump, game_state: &mut GameState) -> bool {
}; };
} }
return false; false
} }
fn render(canvas: &mut WindowCanvas, texture_manager: &TextureManager, map: &Vec<Vec<Tile>>, fn render(canvas: &mut WindowCanvas, texture_manager: &TextureManager, map: &[Vec<Tile>],
game_state: &mut GameState) { game_state: &mut GameState) {
canvas.clear(); canvas.clear();
@ -81,11 +89,11 @@ 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(6, 0); 128]; 128];
for y in 0..128 { for (y, row) in map.iter_mut().enumerate() {
for x in 0..128 { for (x, tile) in row.iter_mut().enumerate() {
let height = perlin.get2d([x as f64 / 16.0, y as f64 / 16.0]) * 320.0; let height = perlin.get2d([x as f64 / 16.0, y as f64 / 16.0]) * 320.0;
println!("({}, {}) = {}", x, y, height); println!("({}, {}) = {}", x, y, height);
map[y][x].height = height as i32; tile.height = height as i32;
} }
} }
@ -116,7 +124,7 @@ pub fn main() {
let texture_manager = TextureManager::new(&texture_creator); let texture_manager = TextureManager::new(&texture_creator);
let map = generate_map(); let map = generate_map();
let mut game_state = GameState { camera_x: 0, camera_y: 0 }; let mut game_state = GameState::new();
canvas.set_draw_color(Color::RGB(48, 48, 48)); canvas.set_draw_color(Color::RGB(48, 48, 48));