2021-10-21 12:54:39 -04:00
|
|
|
use rltk::{GameState, Rltk, VirtualKeyCode, RGB};
|
|
|
|
use specs::prelude::*;
|
|
|
|
use specs_derive::Component;
|
|
|
|
use std::cmp::{max, min};
|
2021-10-20 12:01:15 -04:00
|
|
|
|
2021-10-21 12:54:39 -04:00
|
|
|
#[derive(Component)]
|
|
|
|
struct Position {
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Renderable {
|
|
|
|
glyph: rltk::FontCharType,
|
|
|
|
fg: RGB,
|
|
|
|
bg: RGB,
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:24:40 -04:00
|
|
|
#[derive(Component, Debug)]
|
|
|
|
struct Player {}
|
|
|
|
|
2021-10-21 12:54:39 -04:00
|
|
|
struct State {
|
|
|
|
ecs: World,
|
|
|
|
}
|
2021-10-20 12:01:15 -04:00
|
|
|
|
2021-10-21 14:52:03 -04:00
|
|
|
#[derive(PartialEq, Copy, Clone)]
|
|
|
|
enum TileType {
|
|
|
|
Wall,
|
|
|
|
Floor,
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:24:40 -04:00
|
|
|
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>();
|
2021-10-21 14:52:03 -04:00
|
|
|
let map = ecs.fetch::<Vec<TileType>>();
|
2021-10-21 14:24:40 -04:00
|
|
|
|
|
|
|
for (_player, pos) in (&mut players, &mut positions).join() {
|
2021-10-21 14:52:03 -04:00
|
|
|
let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
|
|
|
|
if map[destination_idx] != TileType::Wall {
|
|
|
|
pos.x = min(79, max(0, pos.x + delta_x));
|
|
|
|
pos.y = min(49, max(0, pos.y + delta_y));
|
|
|
|
}
|
2021-10-21 14:24:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
|
|
|
// Player movement
|
|
|
|
match ctx.key {
|
|
|
|
None => {} // Nothing happened
|
|
|
|
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),
|
2021-10-21 14:52:03 -04:00
|
|
|
_ => {}
|
2021-10-21 14:24:40 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:52:03 -04:00
|
|
|
fn new_map() -> Vec<TileType> {
|
|
|
|
let mut map = vec![TileType::Floor; 80 * 50];
|
|
|
|
|
|
|
|
// Make the boundary 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now randomly add a bunch of walls.
|
|
|
|
// First get the random number generator
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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('#'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the next set of coordinates
|
|
|
|
x += 1;
|
|
|
|
if x > 79 {
|
|
|
|
x = 0;
|
|
|
|
y += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:01:15 -04:00
|
|
|
impl GameState for State {
|
|
|
|
fn tick(&mut self, ctx: &mut Rltk) {
|
|
|
|
ctx.cls();
|
2021-10-21 14:24:40 -04:00
|
|
|
|
|
|
|
player_input(self, ctx);
|
|
|
|
self.run_systems();
|
|
|
|
|
2021-10-21 14:52:03 -04:00
|
|
|
let map = self.ecs.fetch::<Vec<TileType>>();
|
|
|
|
draw_map(&map, ctx);
|
|
|
|
|
2021-10-21 14:24:40 -04:00
|
|
|
let positions = self.ecs.read_storage::<Position>();
|
|
|
|
let renderables = self.ecs.read_storage::<Renderable>();
|
|
|
|
|
|
|
|
for (pos, render) in (&positions, &renderables).join() {
|
|
|
|
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
fn run_systems(&mut self) {
|
|
|
|
self.ecs.maintain();
|
2021-10-20 12:01:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:52:03 -04:00
|
|
|
pub fn xy_idx(x: i32, y: i32) -> usize {
|
|
|
|
(y as usize * 80) + x as usize
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:01:15 -04:00
|
|
|
fn main() -> rltk::BError {
|
|
|
|
use rltk::RltkBuilder;
|
|
|
|
|
|
|
|
let context = RltkBuilder::simple80x50()
|
|
|
|
.with_title("Roguelike Tutorial")
|
|
|
|
.build()?;
|
|
|
|
|
2021-10-21 12:54:39 -04:00
|
|
|
let mut gs = State { ecs: World::new() };
|
|
|
|
|
|
|
|
gs.ecs.register::<Position>();
|
|
|
|
gs.ecs.register::<Renderable>();
|
2021-10-21 14:24:40 -04:00
|
|
|
gs.ecs.register::<Player>();
|
2021-10-21 12:54:39 -04:00
|
|
|
|
2021-10-21 14:52:03 -04:00
|
|
|
gs.ecs.insert(new_map());
|
|
|
|
|
2021-10-21 12:54:39 -04:00
|
|
|
gs.ecs
|
|
|
|
.create_entity()
|
|
|
|
.with(Position { x: 40, y: 25 })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('@'),
|
|
|
|
fg: RGB::named(rltk::YELLOW),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
})
|
2021-10-21 14:24:40 -04:00
|
|
|
.with(Player {})
|
2021-10-21 12:54:39 -04:00
|
|
|
.build();
|
2021-10-20 12:01:15 -04:00
|
|
|
|
|
|
|
rltk::main_loop(context, gs)
|
2021-10-21 12:54:39 -04:00
|
|
|
}
|