2021-10-26 15:43:59 -04:00
|
|
|
use rltk::{GameState, Point, 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;
|
2021-10-29 11:11:17 -04:00
|
|
|
pub use rect::Rect;
|
2021-10-25 15:26:39 -04:00
|
|
|
mod visibility_system;
|
|
|
|
use visibility_system::VisibilitySystem;
|
2021-10-26 15:43:59 -04:00
|
|
|
mod monster_ai_system;
|
2021-10-29 15:15:22 -04:00
|
|
|
use monster_ai_system::MonsterAI;
|
2021-10-29 11:11:17 -04:00
|
|
|
mod map_indexing_system;
|
2021-10-29 15:15:22 -04:00
|
|
|
use map_indexing_system::MapIndexingSystem;
|
|
|
|
mod melee_combat_system;
|
|
|
|
use melee_combat_system::MeleeCombatSystem;
|
|
|
|
mod damage_system;
|
|
|
|
use damage_system::DamageSystem;
|
2021-10-25 15:26:39 -04:00
|
|
|
|
2021-10-29 11:11:17 -04:00
|
|
|
pub const MAP_SIZE: usize = 80 * 50;
|
2021-10-21 12:54:39 -04:00
|
|
|
|
2021-10-26 15:43:59 -04:00
|
|
|
#[derive(PartialEq, Copy, Clone)]
|
|
|
|
pub enum RunState {
|
2021-10-29 15:15:22 -04:00
|
|
|
AwaitingInput,
|
|
|
|
PreRun,
|
|
|
|
PlayerTurn,
|
|
|
|
MonsterTurn,
|
2021-10-26 15:43:59 -04:00
|
|
|
}
|
|
|
|
|
2021-10-22 10:05:06 -04:00
|
|
|
pub struct State {
|
2021-10-26 15:43:59 -04:00
|
|
|
pub ecs: World,
|
2021-10-21 12:54:39 -04:00
|
|
|
}
|
2021-10-20 12:01:15 -04:00
|
|
|
|
2021-10-25 14:23:19 -04:00
|
|
|
impl State {
|
|
|
|
fn run_systems(&mut self) {
|
2021-10-25 15:26:39 -04:00
|
|
|
let mut vis = VisibilitySystem {};
|
|
|
|
vis.run_now(&self.ecs);
|
|
|
|
|
2021-10-26 15:43:59 -04:00
|
|
|
let mut mob = MonsterAI {};
|
|
|
|
mob.run_now(&self.ecs);
|
|
|
|
|
2021-10-29 11:11:17 -04:00
|
|
|
let mut mapindex = MapIndexingSystem {};
|
|
|
|
mapindex.run_now(&self.ecs);
|
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
let mut melee = MeleeCombatSystem {};
|
|
|
|
melee.run_now(&self.ecs);
|
|
|
|
|
|
|
|
let mut damage = DamageSystem {};
|
|
|
|
damage.run_now(&self.ecs);
|
|
|
|
|
2021-10-25 14:23:19 -04:00
|
|
|
self.ecs.maintain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:01:15 -04:00
|
|
|
impl GameState for State {
|
|
|
|
fn tick(&mut self, ctx: &mut Rltk) {
|
|
|
|
ctx.cls();
|
2021-10-29 15:15:22 -04:00
|
|
|
let mut newrunstate;
|
|
|
|
{
|
|
|
|
let runstate = self.ecs.fetch::<RunState>();
|
|
|
|
newrunstate = *runstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
match newrunstate {
|
|
|
|
RunState::PreRun => {
|
|
|
|
self.run_systems();
|
|
|
|
newrunstate = RunState::AwaitingInput;
|
|
|
|
}
|
|
|
|
RunState::AwaitingInput => {
|
|
|
|
newrunstate = player_input(self, ctx);
|
|
|
|
}
|
|
|
|
RunState::PlayerTurn => {
|
|
|
|
self.run_systems();
|
|
|
|
newrunstate = RunState::MonsterTurn;
|
|
|
|
}
|
|
|
|
RunState::MonsterTurn => {
|
|
|
|
self.run_systems();
|
|
|
|
newrunstate = RunState::AwaitingInput;
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 14:24:40 -04:00
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
{
|
|
|
|
let mut runwriter = self.ecs.write_resource::<RunState>();
|
|
|
|
*runwriter = newrunstate;
|
2021-10-26 15:43:59 -04:00
|
|
|
}
|
2021-10-21 14:24:40 -04:00
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
damage_system::delete_the_dead(&mut self.ecs);
|
|
|
|
|
2021-10-25 14:23:19 -04:00
|
|
|
draw_map(&self.ecs, ctx);
|
2021-10-21 14:52:03 -04:00
|
|
|
|
2021-10-21 14:24:40 -04:00
|
|
|
let positions = self.ecs.read_storage::<Position>();
|
|
|
|
let renderables = self.ecs.read_storage::<Renderable>();
|
2021-10-26 14:38:30 -04:00
|
|
|
let map = self.ecs.fetch::<Map>();
|
2021-10-21 14:24:40 -04:00
|
|
|
|
|
|
|
for (pos, render) in (&positions, &renderables).join() {
|
2021-10-26 14:38:30 -04:00
|
|
|
let idx = map.xy_idx(pos.x, pos.y);
|
2021-10-29 15:15:22 -04:00
|
|
|
|
2021-10-26 14:38:30 -04:00
|
|
|
if map.visible_tiles[idx] {
|
2021-10-29 15:15:22 -04:00
|
|
|
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph)
|
2021-10-26 14:38:30 -04:00
|
|
|
}
|
2021-10-21 14:24:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-29 15:15:22 -04:00
|
|
|
let mut gs = State { ecs: World::new() };
|
2021-10-21 12:54:39 -04:00
|
|
|
|
|
|
|
gs.ecs.register::<Position>();
|
|
|
|
gs.ecs.register::<Renderable>();
|
2021-10-21 14:24:40 -04:00
|
|
|
gs.ecs.register::<Player>();
|
2021-10-25 15:26:39 -04:00
|
|
|
gs.ecs.register::<Viewshed>();
|
2021-10-26 15:43:59 -04:00
|
|
|
gs.ecs.register::<Monster>();
|
|
|
|
gs.ecs.register::<Name>();
|
2021-10-29 11:11:17 -04:00
|
|
|
gs.ecs.register::<BlocksTile>();
|
2021-10-29 15:15:22 -04:00
|
|
|
gs.ecs.register::<CombatStats>();
|
|
|
|
gs.ecs.register::<WantsToMelee>();
|
|
|
|
gs.ecs.register::<SufferDamage>();
|
2021-10-21 12:54:39 -04:00
|
|
|
|
2021-10-25 14:23:19 -04:00
|
|
|
let map: Map = Map::new_map_rooms_and_corridors();
|
|
|
|
let (player_x, player_y) = map.rooms[0].center();
|
2021-10-22 14:50:04 -04:00
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
let player_entity = gs
|
|
|
|
.ecs
|
|
|
|
.create_entity()
|
|
|
|
.with(Position {
|
|
|
|
x: player_x,
|
|
|
|
y: player_y,
|
|
|
|
})
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437('@'),
|
|
|
|
fg: RGB::named(rltk::YELLOW),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
})
|
|
|
|
.with(Player {})
|
|
|
|
.with(Viewshed {
|
|
|
|
visible_tiles: Vec::new(),
|
|
|
|
range: 8,
|
|
|
|
dirty: true,
|
|
|
|
})
|
|
|
|
.with(Name {
|
|
|
|
name: "Player".to_string(),
|
|
|
|
})
|
|
|
|
.with(CombatStats {
|
|
|
|
max_hp: 30,
|
|
|
|
hp: 30,
|
|
|
|
defense: 2,
|
|
|
|
power: 5,
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
|
2021-10-26 15:43:59 -04:00
|
|
|
let mut rng = rltk::RandomNumberGenerator::new();
|
|
|
|
for (i, room) in map.rooms.iter().skip(1).enumerate() {
|
2021-10-26 14:38:30 -04:00
|
|
|
let (x, y) = room.center();
|
2021-10-26 15:43:59 -04:00
|
|
|
|
|
|
|
let glyph: rltk::FontCharType;
|
|
|
|
let name: String;
|
|
|
|
let roll = rng.roll_dice(1, 2);
|
|
|
|
match roll {
|
|
|
|
1 => {
|
|
|
|
glyph = rltk::to_cp437('g');
|
|
|
|
name = "Goblin".to_string();
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
glyph = rltk::to_cp437('o');
|
|
|
|
name = "Orc".to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 14:38:30 -04:00
|
|
|
gs.ecs
|
|
|
|
.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
2021-10-26 15:43:59 -04:00
|
|
|
glyph,
|
2021-10-26 14:38:30 -04:00
|
|
|
fg: RGB::named(rltk::RED),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
})
|
|
|
|
.with(Viewshed {
|
|
|
|
visible_tiles: Vec::new(),
|
|
|
|
range: 8,
|
|
|
|
dirty: true,
|
|
|
|
})
|
2021-10-26 15:43:59 -04:00
|
|
|
.with(Monster {})
|
|
|
|
.with(Name {
|
|
|
|
name: format!("{} #{}", &name, i),
|
|
|
|
})
|
2021-10-29 11:11:17 -04:00
|
|
|
.with(BlocksTile {})
|
2021-10-29 15:15:22 -04:00
|
|
|
.with(CombatStats {
|
|
|
|
max_hp: 16,
|
|
|
|
hp: 16,
|
|
|
|
defense: 1,
|
|
|
|
power: 4,
|
|
|
|
})
|
2021-10-26 14:38:30 -04:00
|
|
|
.build();
|
|
|
|
}
|
2021-10-22 14:50:04 -04:00
|
|
|
|
2021-10-26 14:38:30 -04:00
|
|
|
gs.ecs.insert(map);
|
2021-10-26 15:43:59 -04:00
|
|
|
gs.ecs.insert(Point::new(player_x, player_y));
|
2021-10-29 15:15:22 -04:00
|
|
|
gs.ecs.insert(player_entity);
|
|
|
|
gs.ecs.insert(RunState::PreRun);
|
2021-10-26 14:38:30 -04:00
|
|
|
|
2021-10-20 12:01:15 -04:00
|
|
|
rltk::main_loop(context, gs)
|
2021-10-21 12:54:39 -04:00
|
|
|
}
|