Adding visibility system
This commit is contained in:
parent
fa2d7b8e9e
commit
b56eaac0a2
@ -18,3 +18,9 @@ pub struct Renderable {
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct Player {}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Viewshed {
|
||||
pub visible_tiles : Vec<rltk::Point>,
|
||||
pub range : i32
|
||||
}
|
||||
19
src/main.rs
19
src/main.rs
@ -13,12 +13,17 @@ pub use player::*;
|
||||
mod rect;
|
||||
pub use rect::Rect;
|
||||
|
||||
mod visibility_system;
|
||||
pub use visibility_system::*;
|
||||
|
||||
pub struct State {
|
||||
pub ecs: World,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn run_systems(&mut self) {
|
||||
let mut vis = VisibilitySystem{};
|
||||
vis.run_now(&self.ecs);
|
||||
self.ecs.maintain();
|
||||
}
|
||||
}
|
||||
@ -27,11 +32,11 @@ impl GameState for State {
|
||||
fn tick(&mut self, ctx: &mut Rltk) {
|
||||
ctx.cls();
|
||||
|
||||
self.run_systems();
|
||||
player_input(self, ctx);
|
||||
self.run_systems();
|
||||
|
||||
let map = self.ecs.fetch::<Vec<TileType>>();
|
||||
draw_map(&map, ctx);
|
||||
// let map = self.ecs.fetch::<Map>();
|
||||
draw_map(&self.ecs, ctx);
|
||||
|
||||
let positions = self.ecs.read_storage::<Position>();
|
||||
let renderables = self.ecs.read_storage::<Renderable>();
|
||||
@ -51,10 +56,12 @@ fn main() -> rltk::BError {
|
||||
gs.ecs.register::<Position>();
|
||||
gs.ecs.register::<Renderable>();
|
||||
gs.ecs.register::<Player>();
|
||||
gs.ecs.register::<Viewshed>();
|
||||
|
||||
let (rooms, map) = new_map_rooms_and_corridors();
|
||||
let map = Map::new_map_rooms_and_corridors();
|
||||
let (player_x, player_y) = map.rooms[0].center();
|
||||
gs.ecs.insert(map);
|
||||
let (player_x, player_y) = rooms[0].center();
|
||||
|
||||
|
||||
gs.ecs
|
||||
.create_entity()
|
||||
@ -68,8 +75,10 @@ fn main() -> rltk::BError {
|
||||
bg: RGB::named(rltk::BLACK),
|
||||
})
|
||||
.with(Player {})
|
||||
.with(Viewshed{ visible_tiles : Vec::new(), range : 8})
|
||||
.build();
|
||||
|
||||
// Main loop runner
|
||||
rltk::main_loop(context, gs)
|
||||
}
|
||||
|
||||
287
src/map.rs
287
src/map.rs
@ -1,6 +1,9 @@
|
||||
use super::Rect;
|
||||
use rltk::{RandomNumberGenerator, Rltk, RGB};
|
||||
use std::cmp::{max, min};
|
||||
use crate::Viewshed;
|
||||
|
||||
use super::{Rect, Player};
|
||||
use rltk::{RandomNumberGenerator, Rltk, RGB, Algorithm2D, Point, BaseMap};
|
||||
use std::{cmp::{max, min}};
|
||||
use specs::prelude::*;
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
pub enum TileType {
|
||||
@ -8,133 +11,175 @@ pub enum TileType {
|
||||
Floor,
|
||||
}
|
||||
|
||||
pub fn xy_idx(x: i32, y: i32) -> usize {
|
||||
(y as usize * 80) + x as usize
|
||||
}
|
||||
/// Makes a map with solid boundaries and 400 randomly placed walls, this is bad
|
||||
pub fn new_map_test() -> Vec<TileType> {
|
||||
let mut map = vec![TileType::Floor; 80 * 50];
|
||||
|
||||
for x in 0..80 {
|
||||
map[xy_idx(x, 0)] = TileType::Wall;
|
||||
map[xy_idx(x, 49)] = TileType::Wall;
|
||||
}
|
||||
for y in 0..50 {
|
||||
map[xy_idx(0, y)] = TileType::Wall;
|
||||
map[xy_idx(79, y)] = TileType::Wall;
|
||||
}
|
||||
|
||||
let mut rng = rltk::RandomNumberGenerator::new();
|
||||
|
||||
for _i in 0..400 {
|
||||
let x = rng.roll_dice(1, 79);
|
||||
let y = rng.roll_dice(1, 49);
|
||||
let idx = xy_idx(x, y);
|
||||
if idx != xy_idx(40, 25) {
|
||||
map[idx] = TileType::Wall;
|
||||
}
|
||||
}
|
||||
|
||||
map
|
||||
pub struct Map {
|
||||
pub tiles : Vec::<TileType>,
|
||||
pub rooms : Vec::<Rect>,
|
||||
pub width : i32,
|
||||
pub height : i32
|
||||
}
|
||||
|
||||
pub fn new_map_rooms_and_corridors() -> (Vec<Rect>, Vec<TileType>) {
|
||||
let mut map = vec![TileType::Wall; 80 * 50];
|
||||
impl Map {
|
||||
pub fn xy_idx(&self, x: i32, y: i32) -> usize {
|
||||
(y as usize * 80) + x as usize
|
||||
}
|
||||
|
||||
let mut rooms: Vec<Rect> = Vec::new();
|
||||
const MAX_ROOMS: i32 = 30;
|
||||
const MIN_SIZE: i32 = 6;
|
||||
const MAX_SIZE: i32 = 10;
|
||||
|
||||
let mut rng = RandomNumberGenerator::new();
|
||||
|
||||
for _ in 0..MAX_ROOMS {
|
||||
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
||||
let h = rng.range(MIN_SIZE, MAX_SIZE);
|
||||
let x = rng.roll_dice(1, 80 - w - 1) - 1;
|
||||
let y = rng.roll_dice(1, 50 - h - 1) - 1;
|
||||
let new_room = Rect::new(x, y, w, h);
|
||||
let mut ok = true;
|
||||
for other_room in rooms.iter() {
|
||||
if new_room.intersect(other_room) {
|
||||
ok = false
|
||||
pub fn apply_room_to_map(&mut self, room: &Rect) {
|
||||
for y in room.y1 + 1..=room.y2 {
|
||||
for x in room.x1 + 1..=room.x2 {
|
||||
let idx = self.xy_idx(x, y);
|
||||
self.tiles[idx] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
if ok {
|
||||
apply_room_to_map(&new_room, &mut map);
|
||||
if !rooms.is_empty() {
|
||||
let (new_x, new_y) = new_room.center();
|
||||
let (prev_x, prev_y) = rooms[rooms.len() - 1].center();
|
||||
if rng.range(0, 2) == 1 {
|
||||
apply_horizontal_tunnel(&mut map, prev_x, new_x, prev_y);
|
||||
apply_vertical_tunnel(&mut map, prev_y, new_y, new_x);
|
||||
} else {
|
||||
apply_vertical_tunnel(&mut map, prev_y, new_y, prev_x);
|
||||
apply_horizontal_tunnel(&mut map, prev_x, new_x, new_y);
|
||||
}
|
||||
|
||||
fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
|
||||
for x in min(x1, x2)..=max(x1, x2) {
|
||||
let idx = self.xy_idx(x, y);
|
||||
if idx > 0 && idx < self.width as usize * self.height as usize {
|
||||
self.tiles[idx as usize] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_vertical_tunnel(&mut self, y1: i32, y2: i32, x: i32) {
|
||||
for y in min(y1, y2)..=max(y1, y2) {
|
||||
let idx = self.xy_idx(x, y);
|
||||
if idx > 0 && idx < self.width as usize * self.height as usize {
|
||||
self.tiles[idx as usize] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_map_rooms_and_corridors() -> Map {
|
||||
let mut map = Map{
|
||||
tiles : vec![TileType::Wall; 80*50],
|
||||
rooms : Vec::new(),
|
||||
width : 80,
|
||||
height : 50
|
||||
};
|
||||
|
||||
const MAX_ROOMS: i32 = 30;
|
||||
const MIN_SIZE: i32 = 6;
|
||||
const MAX_SIZE: i32 = 10;
|
||||
|
||||
let mut rng = RandomNumberGenerator::new();
|
||||
|
||||
for _ in 0..MAX_ROOMS {
|
||||
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
||||
let h = rng.range(MIN_SIZE, MAX_SIZE);
|
||||
let x = rng.roll_dice(1, map.width - w - 1) - 1;
|
||||
let y = rng.roll_dice(1, map.height - h - 1) - 1;
|
||||
let new_room = Rect::new(x, y, w, h);
|
||||
let mut ok = true;
|
||||
for other_room in map.rooms.iter() {
|
||||
if new_room.intersect(other_room) {
|
||||
ok = false
|
||||
}
|
||||
}
|
||||
rooms.push(new_room);
|
||||
}
|
||||
}
|
||||
|
||||
(rooms, map)
|
||||
}
|
||||
|
||||
pub fn apply_room_to_map(room: &Rect, map: &mut [TileType]) {
|
||||
for y in room.y1 + 1..=room.y2 {
|
||||
for x in room.x1 + 1..=room.x2 {
|
||||
map[xy_idx(x, y)] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_horizontal_tunnel(map: &mut [TileType], x1: i32, x2: i32, y: i32) {
|
||||
for x in min(x1, x2)..=max(x1, x2) {
|
||||
let idx = xy_idx(x, y);
|
||||
if idx > 0 && idx < 80 * 50 {
|
||||
map[idx as usize] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_vertical_tunnel(map: &mut [TileType], y1: i32, y2: i32, x: i32) {
|
||||
for y in min(y1, y2)..=max(y1, y2) {
|
||||
let idx = xy_idx(x, y);
|
||||
if idx > 0 && idx < 80 * 50 {
|
||||
map[idx as usize] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_map(map: &[TileType], ctx: &mut Rltk) {
|
||||
let mut y = 0;
|
||||
let mut x = 0;
|
||||
for tile in map.iter() {
|
||||
match tile {
|
||||
TileType::Floor => {
|
||||
ctx.set(
|
||||
x,
|
||||
y,
|
||||
RGB::from_f32(0.5, 0.5, 0.5),
|
||||
RGB::from_f32(0., 0., 0.),
|
||||
rltk::to_cp437('.'),
|
||||
);
|
||||
}
|
||||
TileType::Wall => {
|
||||
ctx.set(
|
||||
x,
|
||||
y,
|
||||
RGB::from_f32(0.0, 1.0, 0.0),
|
||||
RGB::from_f32(0., 0., 0.),
|
||||
rltk::to_cp437('#'),
|
||||
);
|
||||
if ok {
|
||||
map.apply_room_to_map(&new_room);
|
||||
if !map.rooms.is_empty() {
|
||||
let (new_x, new_y) = new_room.center();
|
||||
let (prev_x, prev_y) = map.rooms[map.rooms.len() - 1].center();
|
||||
if rng.range(0, 2) == 1 {
|
||||
map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
|
||||
map.apply_vertical_tunnel(prev_y, new_y, new_x);
|
||||
} else {
|
||||
map.apply_vertical_tunnel(prev_y, new_y, prev_x);
|
||||
map.apply_horizontal_tunnel(prev_x, new_x, new_y);
|
||||
}
|
||||
}
|
||||
map.rooms.push(new_room);
|
||||
}
|
||||
}
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
x += 1;
|
||||
if x > 79 {
|
||||
x = 0;
|
||||
y += 1;
|
||||
}
|
||||
}
|
||||
|
||||
impl Algorithm2D for Map {
|
||||
fn dimensions(&self) -> rltk::Point {
|
||||
Point::new(self.width, self.height)
|
||||
}
|
||||
}
|
||||
|
||||
impl BaseMap for Map {
|
||||
fn is_opaque(&self, idx: usize) -> bool {
|
||||
self.tiles[idx as usize] == TileType::Wall
|
||||
}
|
||||
}
|
||||
|
||||
// /// Makes a map with solid boundaries and 400 randomly placed walls, this is bad
|
||||
// pub fn new_map_test() -> Vec<TileType> {
|
||||
// let mut map = vec![TileType::Floor; 80 * 50];
|
||||
|
||||
// for x in 0..80 {
|
||||
// map[xy_idx(x, 0)] = TileType::Wall;
|
||||
// map[xy_idx(x, 49)] = TileType::Wall;
|
||||
// }
|
||||
// for y in 0..50 {
|
||||
// map[xy_idx(0, y)] = TileType::Wall;
|
||||
// map[xy_idx(79, y)] = TileType::Wall;
|
||||
// }
|
||||
|
||||
// let mut rng = rltk::RandomNumberGenerator::new();
|
||||
|
||||
// for _i in 0..400 {
|
||||
// let x = rng.roll_dice(1, 79);
|
||||
// let y = rng.roll_dice(1, 49);
|
||||
// let idx = xy_idx(x, y);
|
||||
// if idx != xy_idx(40, 25) {
|
||||
// map[idx] = TileType::Wall;
|
||||
// }
|
||||
// }
|
||||
|
||||
// map
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
||||
let mut viewsheds = ecs.write_storage::<Viewshed>();
|
||||
let mut players = ecs.write_storage::<Player>();
|
||||
let map = ecs.fetch::<Map>();
|
||||
|
||||
for (_player, viewshed) in (&mut players, &mut viewsheds).join() {
|
||||
let mut y = 0;
|
||||
let mut x = 0;
|
||||
for tile in map.tiles.iter() {
|
||||
let pt = Point::new(x,y);
|
||||
if viewshed.visible_tiles.contains(&pt) {
|
||||
match tile {
|
||||
TileType::Floor => {
|
||||
ctx.set(
|
||||
x,
|
||||
y,
|
||||
RGB::from_f32(0.5, 0.5, 0.5),
|
||||
RGB::from_f32(0., 0., 0.),
|
||||
rltk::to_cp437('.'),
|
||||
);
|
||||
}
|
||||
TileType::Wall => {
|
||||
ctx.set(
|
||||
x,
|
||||
y,
|
||||
RGB::from_f32(0.0, 1.0, 0.0),
|
||||
RGB::from_f32(0., 0., 0.),
|
||||
rltk::to_cp437('#'),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
x += 1;
|
||||
if x > 79 {
|
||||
x = 0;
|
||||
y += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
use rltk::{VirtualKeyCode, Rltk};
|
||||
use specs::prelude::*;
|
||||
use super::{Position, Player, TileType, xy_idx, State};
|
||||
use super::{Position, Player, TileType, Map, State};
|
||||
use std::cmp::{min, max};
|
||||
|
||||
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
||||
let mut positions = ecs.write_storage::<Position>();
|
||||
let mut players = ecs.write_storage::<Player>();
|
||||
let map = ecs.fetch::<Vec<TileType>>();
|
||||
let map = ecs.fetch::<Map>();
|
||||
|
||||
for (_player, pos) in (&mut players, &mut positions).join() {
|
||||
let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
|
||||
if map[destination_idx] != TileType::Wall {
|
||||
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
|
||||
if map.tiles[destination_idx] != TileType::Wall {
|
||||
pos.x = min(79, max(0, pos.x + delta_x));
|
||||
pos.y = min(49, max(0, pos.y + delta_y));
|
||||
}
|
||||
@ -21,10 +21,21 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||
match ctx.key {
|
||||
None => {}
|
||||
Some(key) => match key {
|
||||
VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
|
||||
VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
|
||||
VirtualKeyCode::Left |
|
||||
VirtualKeyCode::Numpad4 |
|
||||
VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
|
||||
|
||||
VirtualKeyCode::Right |
|
||||
VirtualKeyCode::Numpad6 |
|
||||
VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs),
|
||||
|
||||
VirtualKeyCode::Up |
|
||||
VirtualKeyCode::Numpad8 |
|
||||
VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
|
||||
|
||||
VirtualKeyCode::Down |
|
||||
VirtualKeyCode::Numpad2 |
|
||||
VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
|
||||
20
src/visibility_system.rs
Normal file
20
src/visibility_system.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use specs::prelude::*;
|
||||
use super::{Viewshed, Position, Map};
|
||||
use rltk::{field_of_view, Point};
|
||||
|
||||
pub struct VisibilitySystem {}
|
||||
|
||||
impl<'a> System<'a> for VisibilitySystem {
|
||||
type SystemData = ( ReadExpect<'a, Map>,
|
||||
WriteStorage<'a, Viewshed>,
|
||||
WriteStorage<'a, Position>);
|
||||
|
||||
fn run(&mut self, data : Self::SystemData) {
|
||||
let (map, mut viewshed, pos) = data;
|
||||
for (viewshed, pos) in (&mut viewshed, &pos).join() {
|
||||
viewshed.visible_tiles.clear();
|
||||
viewshed.visible_tiles = field_of_view(Point::new(pos.x, pos.y), viewshed.range, &*map);
|
||||
viewshed.visible_tiles.retain(|p| p.x >= 0 && p.x < map.width && p.y >= 0 && p.y < map.height);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user