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)]
|
|
|
|
struct LeftMover {}
|
|
|
|
|
|
|
|
#[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: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>();
|
|
|
|
|
|
|
|
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),
|
|
|
|
_ => {},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
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::<LeftMover>();
|
|
|
|
gs.ecs.register::<Player>();
|
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
|
|
|
|
2021-10-21 14:24:40 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:01:15 -04:00
|
|
|
rltk::main_loop(context, gs)
|
2021-10-21 12:54:39 -04:00
|
|
|
}
|