roguelike-game/src/main.rs

70 lines
1.4 KiB
Rust
Raw Normal View History

2021-10-22 10:05:06 -04:00
use rltk::{GameState, Rltk, RGB};
2021-10-21 12:54:39 -04:00
use specs::prelude::*;
2021-10-20 12:01:15 -04:00
2021-10-22 10:05:06 -04:00
mod components;
pub use components::*;
mod map;
pub use map::*;
mod player;
use player::*;
mod rect;
// pub use rect::Rect;
2021-10-21 12:54:39 -04:00
2021-10-22 10:05:06 -04:00
pub struct State {
2021-10-21 12:54:39 -04:00
ecs: World,
}
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
}
}
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
}