From 59a791de56d0d9d3a0f9a0b9365a2f7ece49b166 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 21 Oct 2021 14:24:40 -0400 Subject: [PATCH] Finish chapter 2.1 --- Cargo.toml | 1 + src/main.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 64ace80..5cb0d18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "roguelike_tutorial" version = "0.1.0" +rust-version = "1.56" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index dc20cc0..0b49d56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,14 +16,77 @@ struct Renderable { bg: RGB, } +#[derive(Component)] +struct LeftMover {} + +#[derive(Component, Debug)] +struct Player {} + struct State { ecs: World, } +fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { + let mut positions = ecs.write_storage::(); + let mut players = ecs.write_storage::(); + + 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)); + } +} + +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), + _ => {}, + }, + } +} + impl GameState for State { fn tick(&mut self, ctx: &mut Rltk) { ctx.cls(); - ctx.print(1, 1, "Hello Rust World"); + + player_input(self, ctx); + self.run_systems(); + + let positions = self.ecs.read_storage::(); + let renderables = self.ecs.read_storage::(); + + for (pos, render) in (&positions, &renderables).join() { + ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph); + } + } +} + +struct LeftWalker {} + +impl<'a> System<'a> for LeftWalker { + type SystemData = (ReadStorage<'a, LeftMover>, WriteStorage<'a, Position>); + + 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; + } + } + } +} + +impl State { + fn run_systems(&mut self) { + let mut lw = LeftWalker{}; + lw.run_now(&self.ecs); + self.ecs.maintain(); } } @@ -38,6 +101,8 @@ fn main() -> rltk::BError { gs.ecs.register::(); gs.ecs.register::(); + gs.ecs.register::(); + gs.ecs.register::(); gs.ecs .create_entity() @@ -47,7 +112,21 @@ fn main() -> rltk::BError { fg: RGB::named(rltk::YELLOW), bg: RGB::named(rltk::BLACK), }) + .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(); + } + rltk::main_loop(context, gs) }