Compare commits

..

No commits in common. "648040095d08ab4683647b17bdeeb5571b38713e" and "6132fa522b47e808e292a91ffbe6a1f99a800025" have entirely different histories.

2 changed files with 167 additions and 166 deletions

View File

@ -1,4 +1,4 @@
use rltk::{Console, GameState, Rltk, VirtualKeyCode, RGB}; use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
use specs::prelude::*; use specs::prelude::*;
use std::cmp::{max, min}; use std::cmp::{max, min};
mod rect; mod rect;
@ -10,60 +10,52 @@ pub use map::*;
extern crate specs_derive; extern crate specs_derive;
#[derive(Component)] #[derive(Component)]
pub struct Position { struct Position {
x: i32, x: i32,
y: i32, y: i32,
} }
#[derive(Component)] #[derive(Component)]
pub struct Renderable { struct Renderable {
glyph: u8, glyph: u8,
fg: RGB, fg: RGB,
bg: RGB, bg: RGB,
} }
pub struct State { struct State {
ecs: World, ecs: World
} }
#[derive(Component)] #[derive(Component)]
pub struct LeftMover {} struct LeftMover {}
#[derive(Component, Debug)] #[derive(Component, Debug)]
pub struct Player {} struct Player {}
fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
let mut positions = ecs.write_storage::<Position>(); let mut positions = ecs.write_storage::<Position>();
let mut players = ecs.write_storage::<Player>(); let mut players = ecs.write_storage::<Player>();
let map = ecs.fetch::<Map>(); let map = ecs.fetch::<Vec<TileType>>();
for (_player, pos) in (&mut players, &mut positions).join() { for (_player, pos) in (&mut players, &mut positions).join() {
let x = min(79, max(0, pos.x + delta_x)); let x = min(79 , max(0, pos.x + delta_x));
let y = min(49, max(0, pos.y + delta_y)); let y = min(49, max(0, pos.y + delta_y));
if map.tile_at(x,y) != TileType::Wall { if map[xy_idx(x,y)] != TileType::Wall {
pos.x = x; pos.x = x;
pos.y = y; pos.y = y;
} }
} }
} }
pub fn player_input(gs: &mut State, ctx: &mut Rltk) { fn player_input(gs: &mut State, ctx: &mut Rltk) {
// Player movement // Player movement
match ctx.key { match ctx.key {
None => {} // Nothing happened None => {} // Nothing happened
Some(key) => match key { Some(key) => match key {
VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs), VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
VirtualKeyCode::Numpad4 => try_move_player(-1, 0, &mut gs.ecs),
VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs), VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
VirtualKeyCode::Numpad6 => try_move_player(1, 0, &mut gs.ecs),
VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs),
VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs), VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
VirtualKeyCode::Numpad8 => try_move_player(0, -1, &mut gs.ecs),
VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs), VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
VirtualKeyCode::Numpad2 => try_move_player(0, 1, &mut gs.ecs),
VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
_ => {} _ => {}
}, },
} }
@ -72,33 +64,33 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) {
struct LeftWalker {} struct LeftWalker {}
impl<'a> System<'a> for LeftWalker { impl<'a> System<'a> for LeftWalker {
type SystemData = (ReadStorage<'a, LeftMover>, WriteStorage<'a, Position>); type SystemData = (ReadStorage<'a, LeftMover>,
WriteStorage<'a, Position>);
fn run(&mut self, (lefty, mut pos): Self::SystemData) { fn run(&mut self, (lefty, mut pos) : Self::SystemData) {
for (_lefty, pos) in (&lefty, &mut pos).join() { for (_lefty,pos) in (&lefty, &mut pos).join() {
pos.x -= 1; pos.x -= 1;
if pos.x < 0 { if pos.x < 0 { pos.x = 79; }
pos.x = 79;
}
} }
} }
} }
impl State { impl State {
fn run_systems(&mut self) { fn run_systems(&mut self) {
let mut lw = LeftWalker {}; let mut lw = LeftWalker{};
lw.run_now(&self.ecs); lw.run_now(&self.ecs);
self.ecs.maintain(); self.ecs.maintain();
} }
} }
impl GameState for State { impl GameState for State {
fn tick(&mut self, ctx: &mut Rltk) { fn tick(&mut self, ctx : &mut Rltk) {
ctx.cls(); ctx.cls();
self.run_systems(); self.run_systems();
player_input(self, ctx); player_input(self, ctx);
let map = self.ecs.fetch::<Map>(); let map = self.ecs.fetch::<Vec<TileType>>();
map.draw_map(ctx); draw_map(&map, ctx);
let positions = self.ecs.read_storage::<Position>(); let positions = self.ecs.read_storage::<Position>();
let renderables = self.ecs.read_storage::<Renderable>(); let renderables = self.ecs.read_storage::<Renderable>();
@ -113,10 +105,9 @@ fn main() {
let context = RltkBuilder::simple80x50() let context = RltkBuilder::simple80x50()
.with_title("Roguelike Tutorial") .with_title("Roguelike Tutorial")
.build(); .build();
let mut gs = State { ecs: World::new() }; let mut gs = State {
let map = Map::new_map_rooms_and_corridors(); ecs: World::new()
let (player_x, player_y) = map.rooms[0].center(); };
gs.ecs.register::<Position>(); gs.ecs.register::<Position>();
gs.ecs.register::<Renderable>(); gs.ecs.register::<Renderable>();
gs.ecs.register::<LeftMover>(); gs.ecs.register::<LeftMover>();
@ -124,16 +115,13 @@ fn main() {
gs.ecs gs.ecs
.create_entity() .create_entity()
.with(Position { .with(Position { x: 40, y: 25 })
x: player_x,
y: player_y,
})
.with(Renderable { .with(Renderable {
glyph: rltk::to_cp437('@'), glyph: rltk::to_cp437('@'),
fg: RGB::named(rltk::YELLOW), fg: RGB::named(rltk::YELLOW),
bg: RGB::named(rltk::BLACK), bg: RGB::named(rltk::BLACK),
}) })
.with(Player {}) .with(Player{})
.build(); .build();
for i in 0..10 { for i in 0..10 {
@ -141,14 +129,16 @@ fn main() {
.create_entity() .create_entity()
.with(Position { x: i * 7, y: 20 }) .with(Position { x: i * 7, y: 20 })
.with(Renderable { .with(Renderable {
glyph: rltk::to_cp437('☺'), glyph: rltk::to_cp437('☺'),
fg: RGB::named(rltk::RED), fg: RGB::named(rltk::RED),
bg: RGB::named(rltk::BLACK), bg: RGB::named(rltk::BLACK),
}) })
.with(LeftMover {}) .with(LeftMover {})
.build(); .build();
} }
gs.ecs.insert(map);
gs.ecs.insert(new_map_rooms_and_corridors());
rltk::main_loop(context, gs); rltk::main_loop(context, gs);
} }

View File

@ -8,131 +8,142 @@ pub enum TileType {
Floor, Floor,
} }
pub struct Map { pub fn xy_idx(x: i32, y: i32) -> usize {
pub tiles: Vec<TileType>, (y as usize * 80) + x as usize
pub rooms: Vec<Rect>,
pub width: i32,
pub height: i32,
} }
impl Map { fn new_map_test() -> Vec<TileType> {
pub fn xy_idx(&self, x: i32, y: i32) -> usize { let mut map = vec![TileType::Floor; 80 * 50];
((y * self.width) + x) as usize
// Make the boundaries walls
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;
} }
pub fn tile_at(&self, x: i32, y:i32) -> TileType { // Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent illustration.
self.tiles[self.xy_idx(x, y)] // First, obtain the thread-local RNG:
} let mut rng = rltk::RandomNumberGenerator::new();
fn apply_room_to_map(&mut self, room: &Rect) { for _i in 0..400 {
for x in room.x1..room.x2 { let x = rng.roll_dice(1, 79);
for y in room.y1..room.y2 { let y = rng.roll_dice(1, 49);
let idx = self.xy_idx(x,y); let idx = xy_idx(x, y);
self.tiles[idx] = TileType::Floor if idx != xy_idx(40, 25) {
} map[idx] = TileType::Wall;
} }
} }
fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) { map
for x in min(x1, x2)..=max(x1, x2) { }
let idx=self.xy_idx(x, y);
self.tiles[idx] = TileType::Floor
}
}
fn apply_vertical_tunnel(&mut self, x: i32, y1: i32, y2: i32) { fn apply_room_to_map(room: &Rect, map: &mut Vec<TileType>) {
for y in min(y1, y2)..=max(y1, y2) { for x in room.x1..room.x2 {
let idx=self.xy_idx(x, y); for y in room.y1..room.y2 {
self.tiles[idx] = TileType::Floor map[xy_idx(x, y)] = TileType::Floor
} }
} }
}
pub fn new_map_rooms_and_corridors() -> Map {
let mut map = Map { fn apply_horizontal_tunnel(map: &mut Vec<TileType>, x1: i32, x2: i32, y: i32) {
tiles: vec![TileType::Wall; 80 * 50], for x in min(x1, x2)..=max(x1, x2) {
rooms: Vec::new(), map[xy_idx(x, y)] = TileType::Floor
width: 80, }
height: 50, }
};
fn apply_vertical_tunnel(map: &mut Vec<TileType>, x: i32, y1: i32, y2: i32) {
const MAX_ROOMS: i32 = 30; for y in min(y1, y2)..=max(y1, y2) {
const MIN_SIZE: i32 = 6; map[xy_idx(x, y)] = TileType::Floor
const MAX_SIZE: i32 = 10; }
}
let mut rng = RandomNumberGenerator::new();
pub fn new_map_rooms_and_corridors() -> Vec<TileType> {
for _ in 0..MAX_ROOMS { let mut map = vec![TileType::Wall; 80 * 50];
let w = rng.range(MIN_SIZE, MAX_SIZE);
let h = rng.range(MIN_SIZE, MAX_SIZE); let mut rooms: Vec<Rect> = Vec::new();
let x = rng.roll_dice(1, 80 - w - 1) - 1; const MAX_ROOMS: i32 = 30;
let y = rng.roll_dice(1, 50 - h - 1) - 1; const MIN_SIZE: i32 = 6;
let new_room = Rect::new(x, y, w, h); const MAX_SIZE: i32 = 10;
let mut ok = true;
for other_room in map.rooms.iter() { let mut rng = RandomNumberGenerator::new();
if new_room.intersect(other_room) {
ok = false for _ in 0..MAX_ROOMS {
} let w = rng.range(MIN_SIZE, MAX_SIZE);
} let h = rng.range(MIN_SIZE, MAX_SIZE);
if ok { let x = rng.roll_dice(1, 80 - w - 1) - 1;
map.apply_room_to_map(&new_room); let y = rng.roll_dice(1, 50 - h - 1) - 1;
if map.rooms.len() > 0 { let new_room = Rect::new(x, y, w, h);
let r1_center = new_room.center(); let mut ok = true;
let r2_center = map.rooms[map.rooms.len() - 1].center(); for other_room in rooms.iter() {
if rng.range(0, 2) == 1 { if new_room.intersect(other_room) {
map.apply_horizontal_tunnel(r1_center.0, r2_center.0, r1_center.1); ok = false
map.apply_vertical_tunnel( }
max(r1_center.0, r2_center.0), }
r1_center.1, if ok {
r2_center.1, apply_room_to_map(&new_room, &mut map);
); if rooms.len() > 0 {
} else { let r1_center = new_room.center();
map.apply_vertical_tunnel(r1_center.0, r1_center.1, r2_center.1); let r2_center = rooms[rooms.len() - 1].center();
map.apply_horizontal_tunnel( if rng.range(0, 2) == 1 {
r1_center.0, apply_horizontal_tunnel(&mut map, r1_center.0, r2_center.0, r1_center.1);
r2_center.0, apply_vertical_tunnel(
max(r1_center.1, r2_center.1), &mut map,
); max(r1_center.0, r2_center.0),
} r1_center.1,
} r2_center.1,
map.rooms.push(new_room); );
} } else {
} apply_vertical_tunnel(&mut map, r1_center.0, r1_center.1, r2_center.1);
apply_horizontal_tunnel(
map &mut map,
} r1_center.0,
r2_center.0,
pub fn draw_map(&self, ctx: &mut Rltk) { max(r1_center.1, r2_center.1),
let mut y = 0; );
let mut x = 0; }
for tile in self.tiles.iter() { }
// Render a tile depending upon the tile type rooms.push(new_room);
match tile { }
TileType::Floor => { }
ctx.set(
x, map
y, }
RGB::from_f32(0.5, 0.5, 0.5),
RGB::from_f32(0., 0., 0.), pub fn draw_map(map: &[TileType], ctx: &mut Rltk) {
rltk::to_cp437('.'), let mut y = 0;
); let mut x = 0;
} for tile in map.iter() {
TileType::Wall => { // Render a tile depending upon the tile type
ctx.set( match tile {
x, TileType::Floor => {
y, ctx.set(
RGB::from_f32(0.0, 1.0, 0.0), x,
RGB::from_f32(0., 0., 0.), y,
rltk::to_cp437('#'), RGB::from_f32(0.5, 0.5, 0.5),
); RGB::from_f32(0., 0., 0.),
} rltk::to_cp437('.'),
} );
}
// Move the coordinates TileType::Wall => {
x += 1; ctx.set(
if x > 79 { x,
x = 0; y,
y += 1; RGB::from_f32(0.0, 1.0, 0.0),
} RGB::from_f32(0., 0., 0.),
rltk::to_cp437('#'),
);
}
}
// Move the coordinates
x += 1;
if x > 79 {
x = 0;
y += 1;
} }
} }
} }