use rltk::{GameState, Point, Rltk}; use specs::prelude::*; mod components; mod damage_system; mod game_log; mod gui; mod inventory_system; mod map; mod map_indexing_system; mod melee_combat_system; mod monster_ai_system; mod player; mod rect; mod spawner; mod visibility_system; use components::*; use damage_system::DamageSystem; pub use game_log::GameLog; use inventory_system::{ItemCollectionSystem, ItemDropSystem, ItemUseSystem}; 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; /// 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 ($gs: ident <- $( $Type: ty ),*,) => { $( $gs.ecs.register::<$Type>(); )* } } pub const MAP_SIZE: usize = 80 * 50; #[derive(PartialEq, Copy, Clone)] pub enum RunState { AwaitingInput, PreRun, PlayerTurn, MonsterTurn, ShowInventory, ShowDropItem, ShowTargeting { range: i32, item: Entity }, } pub struct State { pub ecs: World, } impl State { fn new() -> Self { State { ecs: World::new() } } fn run_systems(&mut self) { let mut vis = VisibilitySystem {}; vis.run_now(&self.ecs); let mut mob = MonsterAI {}; mob.run_now(&self.ecs); let mut mapindex = MapIndexingSystem {}; mapindex.run_now(&self.ecs); let mut melee = MeleeCombatSystem {}; melee.run_now(&self.ecs); let mut damage = DamageSystem {}; damage.run_now(&self.ecs); let mut pickup = ItemCollectionSystem {}; pickup.run_now(&self.ecs); let mut items = ItemUseSystem {}; items.run_now(&self.ecs); let mut drop_items = ItemDropSystem {}; drop_items.run_now(&self.ecs); self.ecs.maintain(); } } impl GameState for State { fn tick(&mut self, ctx: &mut Rltk) { ctx.cls(); // Draw the UI draw_map(&self.ecs, ctx); { let positions = self.ecs.read_storage::(); let renderables = self.ecs.read_storage::(); let map = self.ecs.fetch::(); let mut data: Vec<_> = (&positions, &renderables).join().collect(); 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); } let mut newrunstate; { let runstate = self.ecs.fetch::(); newrunstate = *runstate; } match newrunstate { RunState::PreRun => { self.run_systems(); self.ecs.maintain(); newrunstate = RunState::AwaitingInput; } RunState::AwaitingInput => { newrunstate = player_input(self, ctx); } RunState::PlayerTurn => { self.run_systems(); self.ecs.maintain(); newrunstate = RunState::MonsterTurn; } RunState::MonsterTurn => { self.run_systems(); self.ecs.maintain(); newrunstate = RunState::AwaitingInput; } RunState::ShowInventory => { 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(); let is_ranged = self.ecs.read_storage::(); let is_item_ranged = is_ranged.get(item_entity); if let Some(is_item_ranged) = is_item_ranged { newrunstate = RunState::ShowTargeting { range: is_item_ranged.range, item: item_entity, }; } else { let mut intent = self.ecs.write_storage::(); intent .insert( *self.ecs.fetch::(), WantsToUseItem { item: item_entity, target: None, }, ) .expect("failed to add intent to use item"); newrunstate = RunState::PlayerTurn; } } } } 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::(); intent .insert( *self.ecs.fetch::(), WantsToDropItem { item: item_entity }, ) .expect("failed to add intent to drop item"); newrunstate = RunState::PlayerTurn; } } } RunState::ShowTargeting { range, item } => { let result = gui::ranged_target(self, ctx, range); match result.0 { gui::ItemMenuResult::Cancel => newrunstate = RunState::AwaitingInput, gui::ItemMenuResult::NoResponse => {} gui::ItemMenuResult::Selected => { let mut intent = self.ecs.write_storage::(); intent .insert( *self.ecs.fetch::(), WantsToUseItem { item, target: result.1, }, ) .expect("failed to add intent to use item"); newrunstate = RunState::PlayerTurn; } } } } { let mut runwriter = self.ecs.write_resource::(); *runwriter = newrunstate; } damage_system::delete_the_dead(&mut self.ecs); } } fn main() -> rltk::BError { use rltk::RltkBuilder; let context = RltkBuilder::simple80x50() .with_title("Roguelike Tutorial") .build()?; let mut gs = State::new(); register!( gs <- Position, Renderable, Player, Viewshed, Monster, Name, BlocksTile, CombatStats, WantsToMelee, SufferDamage, Item, ProvidesHealing, InBackpack, WantsToPickupItem, WantsToUseItem, WantsToDropItem, Consumable, Ranged, InflictsDamage, AreaOfEffect, Confusion, ); let map = Map::new_map_rooms_and_corridors(); let (player_x, player_y) = map.rooms[0].center(); let player_entity = spawner::player(&mut gs.ecs, player_x, player_y); gs.ecs.insert(rltk::RandomNumberGenerator::new()); for room in map.rooms.iter().skip(1) { spawner::spawn_room(&mut gs.ecs, room); } gs.ecs.insert(map); gs.ecs.insert(Point::new(player_x, player_y)); gs.ecs.insert(player_entity); gs.ecs.insert(RunState::PreRun); gs.ecs.insert(GameLog::new("Welcome to Rusty Roguelike")); rltk::main_loop(context, gs) }