Do a rustfmt
This commit is contained in:
parent
46c288be8c
commit
f4a352d5cd
2 changed files with 53 additions and 28 deletions
65
src/main.rs
65
src/main.rs
|
@ -1,22 +1,22 @@
|
||||||
extern crate sdl2;
|
|
||||||
extern crate perlin_noise;
|
extern crate perlin_noise;
|
||||||
|
extern crate sdl2;
|
||||||
|
|
||||||
mod texture_manager;
|
|
||||||
mod canvas_copy_at;
|
mod canvas_copy_at;
|
||||||
|
mod texture_manager;
|
||||||
mod tile;
|
mod tile;
|
||||||
|
|
||||||
use sdl2::EventPump;
|
|
||||||
use sdl2::event::Event;
|
use sdl2::event::Event;
|
||||||
|
use sdl2::image::INIT_PNG;
|
||||||
use sdl2::keyboard::Keycode;
|
use sdl2::keyboard::Keycode;
|
||||||
|
use sdl2::mouse::MouseButton;
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
use sdl2::render::WindowCanvas;
|
use sdl2::render::WindowCanvas;
|
||||||
use sdl2::image::INIT_PNG;
|
use sdl2::EventPump;
|
||||||
use sdl2::mouse::MouseButton;
|
|
||||||
|
|
||||||
use perlin_noise::PerlinNoise;
|
use perlin_noise::PerlinNoise;
|
||||||
|
|
||||||
use texture_manager::TextureManager;
|
|
||||||
use canvas_copy_at::CopyAt;
|
use canvas_copy_at::CopyAt;
|
||||||
|
use texture_manager::TextureManager;
|
||||||
use tile::Tile;
|
use tile::Tile;
|
||||||
|
|
||||||
struct GameState {
|
struct GameState {
|
||||||
|
@ -51,41 +51,56 @@ fn update(event_pump: &mut EventPump, game_state: &mut GameState) -> bool {
|
||||||
for event in event_pump.poll_iter() {
|
for event in event_pump.poll_iter() {
|
||||||
match event {
|
match event {
|
||||||
Event::Quit { .. } => return true,
|
Event::Quit { .. } => return true,
|
||||||
Event::KeyDown { keycode: Some(keycode), .. } => {
|
Event::KeyDown {
|
||||||
|
keycode: Some(keycode),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
match keycode {
|
match keycode {
|
||||||
Keycode::Escape => return true,
|
Keycode::Escape => return true,
|
||||||
|
|
||||||
Keycode::Up => game_state.camera_y -= 8,
|
Keycode::Up => game_state.camera_y -= 8,
|
||||||
Keycode::Down => game_state.camera_y += 8,
|
Keycode::Down => game_state.camera_y += 8,
|
||||||
Keycode::Left => game_state.camera_x -= 8,
|
Keycode::Left => game_state.camera_x -= 8,
|
||||||
Keycode::Right => game_state.camera_x += 8,
|
Keycode::Right => game_state.camera_x += 8,
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
|
|
||||||
Event::MouseButtonDown { mouse_btn: MouseButton::Right, .. } => {
|
Event::MouseButtonDown {
|
||||||
|
mouse_btn: MouseButton::Right,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
game_state.camera_panning = true;
|
game_state.camera_panning = true;
|
||||||
},
|
}
|
||||||
Event::MouseButtonUp { mouse_btn: MouseButton::Right, .. } => {
|
Event::MouseButtonUp {
|
||||||
|
mouse_btn: MouseButton::Right,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
game_state.camera_panning = false;
|
game_state.camera_panning = false;
|
||||||
},
|
}
|
||||||
Event::MouseMotion { xrel: x, yrel: y, .. } => {
|
Event::MouseMotion {
|
||||||
|
xrel: x, yrel: y, ..
|
||||||
|
} => {
|
||||||
if game_state.camera_panning {
|
if game_state.camera_panning {
|
||||||
game_state.camera_x -= x;
|
game_state.camera_x -= x;
|
||||||
game_state.camera_y -= y;
|
game_state.camera_y -= y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {},
|
_ => {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(canvas: &mut WindowCanvas, texture_manager: &TextureManager, map: &[Vec<Tile>],
|
fn render(
|
||||||
game_state: &mut GameState) {
|
canvas: &mut WindowCanvas,
|
||||||
|
texture_manager: &TextureManager,
|
||||||
|
map: &[Vec<Tile>],
|
||||||
|
game_state: &mut GameState,
|
||||||
|
) {
|
||||||
canvas.clear();
|
canvas.clear();
|
||||||
|
|
||||||
for (y, row) in map.iter().enumerate() {
|
for (y, row) in map.iter().enumerate() {
|
||||||
|
@ -93,11 +108,13 @@ fn render(canvas: &mut WindowCanvas, texture_manager: &TextureManager, map: &[Ve
|
||||||
let texture = &texture_manager[tile.type_];
|
let texture = &texture_manager[tile.type_];
|
||||||
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.copy_at(
|
canvas
|
||||||
texture,
|
.copy_at(
|
||||||
screen_x - game_state.camera_x,
|
texture,
|
||||||
screen_y - game_state.camera_y
|
screen_x - game_state.camera_x,
|
||||||
).unwrap();
|
screen_y - game_state.camera_y,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,19 @@ pub struct TextureManager<'a> {
|
||||||
impl<'a> TextureManager<'a> {
|
impl<'a> TextureManager<'a> {
|
||||||
pub fn new(texture_creator: &'a TextureCreator<WindowContext>) -> TextureManager<'a> {
|
pub fn new(texture_creator: &'a TextureCreator<WindowContext>) -> TextureManager<'a> {
|
||||||
TextureManager {
|
TextureManager {
|
||||||
bridge_east: texture_creator.load_texture("data/png/bridgeEast.png").unwrap(),
|
bridge_east: texture_creator
|
||||||
bridge_north: texture_creator.load_texture("data/png/bridgeNorth.png").unwrap(),
|
.load_texture("data/png/bridgeEast.png")
|
||||||
crossroad:texture_creator.load_texture("data/png/crossroad.png").unwrap(),
|
.unwrap(),
|
||||||
|
bridge_north: texture_creator
|
||||||
|
.load_texture("data/png/bridgeNorth.png")
|
||||||
|
.unwrap(),
|
||||||
|
crossroad: texture_creator
|
||||||
|
.load_texture("data/png/crossroad.png")
|
||||||
|
.unwrap(),
|
||||||
dirt: texture_creator.load_texture("data/png/dirt.png").unwrap(),
|
dirt: texture_creator.load_texture("data/png/dirt.png").unwrap(),
|
||||||
dirt_high: texture_creator.load_texture("data/png/dirtHigh.png").unwrap(),
|
dirt_high: texture_creator
|
||||||
|
.load_texture("data/png/dirtHigh.png")
|
||||||
|
.unwrap(),
|
||||||
grass: texture_creator.load_texture("data/png/grass.png").unwrap(),
|
grass: texture_creator.load_texture("data/png/grass.png").unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue