2021-11-03 09:55:17 -04:00
|
|
|
use rltk::{GameState, Point, Rltk};
|
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;
|
2021-11-03 16:04:36 -04:00
|
|
|
mod damage_system;
|
2021-11-04 10:44:52 -04:00
|
|
|
mod game_log;
|
2021-11-03 16:04:36 -04:00
|
|
|
mod gui;
|
|
|
|
mod inventory_system;
|
2021-10-22 10:05:06 -04:00
|
|
|
mod map;
|
2021-11-03 16:04:36 -04:00
|
|
|
mod map_indexing_system;
|
|
|
|
mod melee_combat_system;
|
|
|
|
mod monster_ai_system;
|
2021-10-22 10:05:06 -04:00
|
|
|
mod player;
|
|
|
|
mod rect;
|
2021-11-03 16:04:36 -04:00
|
|
|
mod spawner;
|
2021-10-25 15:26:39 -04:00
|
|
|
mod visibility_system;
|
2021-11-03 16:04:36 -04:00
|
|
|
|
|
|
|
pub use components::*;
|
2021-10-29 15:15:22 -04:00
|
|
|
use damage_system::DamageSystem;
|
2021-11-04 10:44:52 -04:00
|
|
|
pub use game_log::GameLog;
|
2021-11-04 15:06:04 -04:00
|
|
|
use inventory_system::{ItemCollectionSystem, ItemDropSystem, ItemUseSystem};
|
2021-11-03 16:04:36 -04:00
|
|
|
pub use map::*;
|
|
|
|
use map_indexing_system::MapIndexingSystem;
|
|
|
|
use melee_combat_system::MeleeCombatSystem;
|
|
|
|
use monster_ai_system::MonsterAI;
|
|
|
|
use player::*;
|
|
|
|
pub use rect::Rect;
|
|
|
|
use visibility_system::VisibilitySystem;
|
2021-10-25 15:26:39 -04:00
|
|
|
|
2021-11-04 11:27:44 -04:00
|
|
|
/// Cut down on the amount of syntax to register components
|
|
|
|
macro_rules! register {
|
|
|
|
// $gs is needed to get the scope at the usage point
|
|
|
|
// $Type is the Component type that is being registered
|
2021-11-04 11:38:13 -04:00
|
|
|
($gs: ident <- $( $Type: ty ),*,) => {
|
2021-11-04 11:27:44 -04:00
|
|
|
$(
|
|
|
|
$gs.ecs.register::<$Type>();
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-03 15:11:19 -04:00
|
|
|
ShowInventory,
|
2021-11-04 09:40:58 -04:00
|
|
|
ShowDropItem,
|
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 {
|
2021-11-04 15:06:04 -04:00
|
|
|
fn new() -> Self {
|
|
|
|
State { ecs: World::new() }
|
|
|
|
}
|
|
|
|
|
2021-10-25 14:23:19 -04:00
|
|
|
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-11-03 15:11:19 -04:00
|
|
|
let mut pickup = ItemCollectionSystem {};
|
|
|
|
pickup.run_now(&self.ecs);
|
|
|
|
|
2021-11-04 15:06:04 -04:00
|
|
|
let mut items = ItemUseSystem {};
|
|
|
|
items.run_now(&self.ecs);
|
2021-11-03 15:59:23 -04:00
|
|
|
|
2021-11-04 09:40:58 -04:00
|
|
|
let mut drop_items = ItemDropSystem {};
|
|
|
|
drop_items.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-11-04 10:00:42 -04:00
|
|
|
|
|
|
|
// Draw the UI
|
|
|
|
draw_map(&self.ecs, ctx);
|
|
|
|
{
|
|
|
|
let positions = self.ecs.read_storage::<Position>();
|
|
|
|
let renderables = self.ecs.read_storage::<Renderable>();
|
|
|
|
let map = self.ecs.fetch::<Map>();
|
|
|
|
|
|
|
|
let mut data = (&positions, &renderables).join().collect::<Vec<_>>();
|
|
|
|
data.sort_by(|&a, &b| b.1.render_order.cmp(&a.1.render_order));
|
|
|
|
for (pos, render) in data.iter() {
|
|
|
|
let idx = map.xy_idx(pos.x, pos.y);
|
|
|
|
|
|
|
|
if map.visible_tiles[idx] {
|
|
|
|
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gui::draw_ui(&self.ecs, ctx);
|
|
|
|
}
|
|
|
|
|
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();
|
2021-11-03 15:59:23 -04:00
|
|
|
self.ecs.maintain();
|
2021-10-29 15:15:22 -04:00
|
|
|
newrunstate = RunState::AwaitingInput;
|
|
|
|
}
|
|
|
|
RunState::AwaitingInput => {
|
|
|
|
newrunstate = player_input(self, ctx);
|
|
|
|
}
|
|
|
|
RunState::PlayerTurn => {
|
|
|
|
self.run_systems();
|
2021-11-03 15:59:23 -04:00
|
|
|
self.ecs.maintain();
|
2021-10-29 15:15:22 -04:00
|
|
|
newrunstate = RunState::MonsterTurn;
|
|
|
|
}
|
|
|
|
RunState::MonsterTurn => {
|
|
|
|
self.run_systems();
|
2021-11-03 15:59:23 -04:00
|
|
|
self.ecs.maintain();
|
2021-10-29 15:15:22 -04:00
|
|
|
newrunstate = RunState::AwaitingInput;
|
|
|
|
}
|
2021-11-03 15:11:19 -04:00
|
|
|
RunState::ShowInventory => {
|
2021-11-03 15:59:23 -04:00
|
|
|
let result = gui::show_inventory(self, ctx);
|
|
|
|
match result.0 {
|
|
|
|
gui::ItemMenuResult::Cancel => newrunstate = RunState::AwaitingInput,
|
|
|
|
gui::ItemMenuResult::NoResponse => {}
|
|
|
|
gui::ItemMenuResult::Selected => {
|
|
|
|
let item_entity = result.1.unwrap();
|
2021-11-04 15:06:04 -04:00
|
|
|
let mut intent = self.ecs.write_storage::<WantsToUseItem>();
|
2021-11-03 15:59:23 -04:00
|
|
|
intent
|
|
|
|
.insert(
|
|
|
|
*self.ecs.fetch::<Entity>(),
|
2021-11-04 15:06:04 -04:00
|
|
|
WantsToUseItem {
|
|
|
|
item: item_entity,
|
2021-11-03 15:59:23 -04:00
|
|
|
},
|
|
|
|
)
|
2021-11-04 15:06:04 -04:00
|
|
|
.expect("failed to add intent to use item");
|
2021-11-03 15:59:23 -04:00
|
|
|
|
|
|
|
newrunstate = RunState::PlayerTurn;
|
|
|
|
}
|
2021-11-03 15:11:19 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 09:40:58 -04:00
|
|
|
RunState::ShowDropItem => {
|
|
|
|
let result = gui::drop_item_menu(self, ctx);
|
|
|
|
|
|
|
|
match result.0 {
|
|
|
|
gui::ItemMenuResult::Cancel => newrunstate = RunState::AwaitingInput,
|
|
|
|
gui::ItemMenuResult::NoResponse => {}
|
|
|
|
gui::ItemMenuResult::Selected => {
|
|
|
|
let item_entity = result.1.unwrap();
|
|
|
|
let mut intent = self.ecs.write_storage::<WantsToDropItem>();
|
|
|
|
intent
|
|
|
|
.insert(
|
|
|
|
*self.ecs.fetch::<Entity>(),
|
|
|
|
WantsToDropItem { item: item_entity },
|
|
|
|
)
|
|
|
|
.expect("failed to add intent to drop item");
|
|
|
|
|
|
|
|
newrunstate = RunState::PlayerTurn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 15:15:22 -04:00
|
|
|
}
|
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-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-11-04 15:06:04 -04:00
|
|
|
let mut gs = State::new();
|
2021-10-21 12:54:39 -04:00
|
|
|
|
2021-11-04 11:27:44 -04:00
|
|
|
register!(
|
2021-11-04 11:38:13 -04:00
|
|
|
gs <-
|
2021-11-04 11:27:44 -04:00
|
|
|
Position,
|
|
|
|
Renderable,
|
|
|
|
Player,
|
|
|
|
Viewshed,
|
|
|
|
Monster,
|
|
|
|
Name,
|
|
|
|
BlocksTile,
|
|
|
|
CombatStats,
|
|
|
|
WantsToMelee,
|
|
|
|
SufferDamage,
|
|
|
|
Item,
|
2021-11-04 15:06:04 -04:00
|
|
|
ProvidesHealing,
|
2021-11-04 11:27:44 -04:00
|
|
|
InBackpack,
|
|
|
|
WantsToPickupItem,
|
2021-11-04 15:06:04 -04:00
|
|
|
WantsToUseItem,
|
2021-11-04 11:27:44 -04:00
|
|
|
WantsToDropItem,
|
2021-11-04 15:06:04 -04:00
|
|
|
Consumable,
|
2021-11-04 11:27:44 -04:00
|
|
|
);
|
2021-10-21 12:54:39 -04:00
|
|
|
|
2021-11-04 15:06:04 -04:00
|
|
|
let map = Map::new_map_rooms_and_corridors();
|
2021-10-25 14:23:19 -04:00
|
|
|
let (player_x, player_y) = map.rooms[0].center();
|
2021-10-22 14:50:04 -04:00
|
|
|
|
2021-11-03 09:55:17 -04:00
|
|
|
let player_entity = spawner::player(&mut gs.ecs, player_x, player_y);
|
2021-10-26 15:43:59 -04:00
|
|
|
|
2021-11-03 09:55:17 -04:00
|
|
|
gs.ecs.insert(rltk::RandomNumberGenerator::new());
|
|
|
|
for room in map.rooms.iter().skip(1) {
|
|
|
|
spawner::spawn_room(&mut gs.ecs, room);
|
2021-10-26 14:38:30 -04:00
|
|
|
}
|
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-11-04 10:44:52 -04:00
|
|
|
gs.ecs.insert(game_log::GameLog {
|
2021-11-01 14:46:45 -04:00
|
|
|
entries: vec!["Welcome to Rusty Roguelike".to_string()],
|
|
|
|
});
|
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
|
|
|
}
|