Commit 41f10519 authored by Mathieu Nivoliez's avatar Mathieu Nivoliez

Finished the "Walking map" chapter. There are walls now, and the player

will bump into them.
parent 7bc08475
......@@ -7,3 +7,4 @@ Little Rogue Like made in Rust using the [guide](http://bfnightly.bracketproduct
- [Introduction](http://bfnightly.bracketproductions.com/rustbook/chapter_0.html)
- [Section 1](http://bfnightly.bracketproductions.com/rustbook/chapter_1.html)
- [Entities and component](http://bfnightly.bracketproductions.com/rustbook/chapter_2.html)
- [Walking a Map](http://bfnightly.bracketproductions.com/rustbook/chapter_3.html)
......@@ -21,37 +21,98 @@ struct Renderable {
bg: RGB,
}
#[derive(Component)]
struct LeftMover;
#[derive(Component, Debug)]
struct Player;
// Systems
struct LeftWalker;
#[derive(PartialEq, Copy, Clone)]
enum TileType {
Wall,
Floor,
}
impl<'a> System<'a> for LeftWalker {
type SystemData = (ReadStorage<'a, LeftMover>, WriteStorage<'a, Position>);
// Functions
fn run(&mut self, (lefty, mut pos): Self::SystemData) {
for (_lefty, pos) in (&lefty, &mut pos).join() {
pos.x -= 1;
if pos.x < 0 {
pos.x = 79;
}
pub fn xy_idx(x: i32, y: i32) -> usize {
(y as usize * 80) + x as usize
}
fn new_map() -> Vec<TileType> {
let mut map = vec![TileType::Floor; 80 * 50];
// 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;
}
// Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent
// illustration
// First, obtain the thread-local RNG
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
}
// Functions
fn draw_map(map: &[TileType], ctx: &mut Rltk) {
let mut y = 0;
let mut x = 0;
for tile in map.iter() {
// Render a tile depending upon the tile TileType
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;
}
}
}
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>>();
for (_player, pos) in (&mut players, &mut positions).join() {
pos.x = min(79, max(0, pos.x + delta_x));
pos.y = min(49, max(0, pos.y + delta_y));
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));
}
}
}
......@@ -75,11 +136,7 @@ struct State {
}
impl State {
fn run_systems(&mut self) {
let mut lw = LeftWalker;
lw.run_now(&self.ecs);
self.ecs.maintain();
}
fn run_systems(&mut self) {}
}
impl GameState for State {
......@@ -92,6 +149,9 @@ impl GameState for State {
let positions = self.ecs.read_storage::<Position>();
let renderables = self.ecs.read_storage::<Renderable>();
let map = self.ecs.fetch::<Vec<TileType>>();
draw_map(&map, ctx);
for (pos, render) in (&positions, &renderables).join() {
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
}
......@@ -109,7 +169,6 @@ fn main() {
// Register components to the world
gs.ecs.register::<Position>();
gs.ecs.register::<Renderable>();
gs.ecs.register::<LeftMover>();
gs.ecs.register::<Player>();
// Create an entity
......@@ -124,18 +183,7 @@ fn main() {
.with(Player)
.build();
for i in 0..10 {
gs.ecs
.create_entity()
.with(Position { x: i * 7, y: 20 })
.with(Renderable {
glyph: rltk::to_cp437('☺'),
fg: RGB::named(rltk::RED),
bg: RGB::named(rltk::BLACK),
})
.with(LeftMover)
.build();
}
gs.ecs.insert(new_map());
// Run the game
rltk::main_loop(context, gs);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment