Working on the editor

This commit is contained in:
Thomas Forgione 2019-03-30 12:53:29 +01:00
parent e539fe402e
commit ae601720e3
No known key found for this signature in database
GPG Key ID: BFD17A2D71B3B5E7
2 changed files with 44 additions and 4 deletions

View File

@ -2,10 +2,11 @@ use clap::{App, crate_version};
use sfml::system::Vector2;
use sfml::window::{Event, Key};
use sfml::window::mouse::Button;
use sfml::graphics::{RectangleShape, RenderTarget, Transformable, Color, Shape};
use rusty::engine::renderer::Renderer;
use rusty::engine::map::{GraphicTile, Map};
use rusty::engine::map::{GraphicTile, CollisionTile, Map};
fn main() {
let _ = App::new("Rusty Editor")
@ -16,9 +17,12 @@ fn main() {
let mut running = true;
let map = Map::from_str(include_str!("../../assets/levels/level2.txt")).unwrap();
let mut map = Map::from_str(include_str!("../../assets/levels/level2.txt")).unwrap();
loop {
let top_panel_size = Vector2::new(renderer.window().size().x as f32, 50.0);
let left_panel_size = Vector2::new(100.0, renderer.window().size().y as f32 - top_panel_size.y);
// Manage the events
while let Some(event) = renderer.poll_event() {
match event {
@ -30,6 +34,22 @@ fn main() {
code: Key::Escape, ..
} => running = false,
// A click on the screen
Event::MouseButtonPressed {
button: Button::Left, x, y,
} => {
if x as f32 >= left_panel_size.x && y as f32 >= top_panel_size.y {
let x = ((x - left_panel_size.x as i32) / 16) as usize;
let y = ((y - top_panel_size.y as i32) / 16) as usize;
if map.tile(y, x) == CollisionTile::empty() {
map.set_tile(y, x, CollisionTile::full());
} else {
map.set_tile(y, x, CollisionTile::empty());
}
}
},
_ => (),
}
}
@ -38,13 +58,11 @@ fn main() {
renderer.clear();
// Show the top panel
let top_panel_size = Vector2::new(renderer.window().size().x as f32, 50.0);
let mut top_panel = RectangleShape::with_size(top_panel_size);
top_panel.set_fill_color(&Color::rgb(255, 0, 0));
renderer.window_mut().draw(&top_panel);
// Show the left panel
let left_panel_size = Vector2::new(100.0, renderer.window().size().y as f32 - top_panel_size.y);
let mut left_panel = RectangleShape::with_size(left_panel_size);
left_panel.set_position((0.0, top_panel_size.y));
left_panel.set_fill_color(&Color::rgb(0, 255, 0));

View File

@ -109,6 +109,18 @@ pub enum GraphicTile {
}
impl GraphicTile {
/// Returns a graphic tile from a collision tile.
///
/// This might be ugly for the moment.
pub fn from_collision_tile(collision: CollisionTile) -> GraphicTile {
if collision == CollisionTile::empty() {
GraphicTile::Hidden
} else {
GraphicTile::Center
}
}
/// Checks if a graphic tile has a top border.
pub fn is_top(self) -> bool {
match self {
@ -343,6 +355,16 @@ impl Map {
}
}
/// Returns a tile of the map.
pub fn tile(&self, i: usize, j: usize) -> CollisionTile {
self.tiles[(i, j)].0
}
/// Changes a tile of the map.
pub fn set_tile(&mut self, i: usize, j: usize, tile: CollisionTile) {
self.tiles[(i, j)] = (tile, GraphicTile::from_collision_tile(tile));
}
/// Finds a possible entrance.
pub fn find_entrance(tiles: &Matrix<(CollisionTile, GraphicTile)>) -> (usize, usize) {
(tiles.rows() - 5, 1)