update for new RLTK version

This commit is contained in:
2020-04-17 20:35:31 -04:00
parent 5593c4d14e
commit bf8204d1a3
5 changed files with 166 additions and 107 deletions

View File

@@ -10,7 +10,7 @@ pub struct Position {
#[derive(Component)]
pub struct Renderable {
pub glyph: u8,
pub glyph: u16,
pub fg: RGB,
pub bg: RGB,
}

View File

@@ -1,4 +1,4 @@
use rltk::{Console, GameState, Point, Rltk, VirtualKeyCode, RGB};
use rltk::{GameState, Point, Rltk, VirtualKeyCode, RGB};
use specs::prelude::*;
use std::cmp::{max, min};
mod rect;
@@ -135,11 +135,11 @@ pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
}
}
fn main() {
fn main() -> rltk::BError {
use rltk::RltkBuilder;
let context = RltkBuilder::simple80x50()
.with_title("Roguelike Tutorial")
.build();
.build()?;
let mut gs = State {
ecs: World::new(),
runstate: RunState::Running,
@@ -180,7 +180,7 @@ fn main() {
for (idx, room) in map.rooms.iter().skip(1).enumerate() {
let (x, y) = room.center();
let (glyph, name): (u8, String) = match rng.roll_dice(1, 2) {
let (glyph, name): (u16, String) = match rng.roll_dice(1, 2) {
1 => (rltk::to_cp437('g'), "Goblin".to_string()),
_ => (rltk::to_cp437('o'), "Orc".to_string()),
};
@@ -207,5 +207,5 @@ fn main() {
gs.ecs.insert(map);
gs.ecs.insert(Point::new(player_x, player_y));
rltk::main_loop(context, gs);
rltk::main_loop(context, gs)
}

View File

@@ -96,12 +96,54 @@ impl Map {
map
}
fn is_exit_valid(&self, x: i32, y: i32) -> bool {
if x < 1 || x > self.width - 1 || y < 1 || y > self.height - 1 {
return false;
}
let idx = self.xy_idx(x, y);
self.tiles[idx as usize] != TileType::Wall
}
}
impl BaseMap for Map {
fn is_opaque(&self, idx: usize) -> bool {
self.tiles[idx as usize] == TileType::Wall
}
fn get_available_exits(&self, idx: usize) -> rltk::SmallVec<[(usize, f32); 10]> {
let mut exits = rltk::SmallVec::new();
let x = idx as i32 % self.width;
let y = idx as i32 / self.width;
let w = self.width as usize;
// Cardinal directions
if self.is_exit_valid(x - 1, y) {
exits.push((idx - 1, 1.0))
};
if self.is_exit_valid(x + 1, y) {
exits.push((idx + 1, 1.0))
};
if self.is_exit_valid(x, y - 1) {
exits.push((idx - w, 1.0))
};
if self.is_exit_valid(x, y + 1) {
exits.push((idx + w, 1.0))
};
// Diagonals
if self.is_exit_valid(x - 1, y - 1) {
exits.push(((idx - w) - 1, 1.45));
}
if self.is_exit_valid(x + 1, y - 1) {
exits.push(((idx - w) + 1, 1.45));
}
if self.is_exit_valid(x - 1, y + 1) {
exits.push(((idx + w) - 1, 1.45));
}
if self.is_exit_valid(x + 1, y + 1) {
exits.push(((idx + w) + 1, 1.45));
}
exits
}
}
impl Algorithm2D for Map {