diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..76a72c6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/roguelike.iml b/.idea/roguelike.iml new file mode 100644 index 0000000..c254557 --- /dev/null +++ b/.idea/roguelike.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/components.rs b/src/components.rs index 88cc0d5..eafc560 100644 --- a/src/components.rs +++ b/src/components.rs @@ -13,6 +13,7 @@ pub struct Renderable { pub glyph: rltk::FontCharType, pub fg: RGB, pub bg: RGB, + pub render_order: i32, } #[derive(Component, Debug)] @@ -66,3 +67,34 @@ impl SufferDamage { } } } + +#[derive(Component, Debug)] +pub struct Item {} + +#[derive(Component, Debug)] +pub struct Potion { + pub heal_amount: i32, +} + +#[derive(Component, Debug)] +pub struct InBackpack { + pub owner: Entity, +} + +#[derive(Component, Debug, Clone)] +pub struct WantsToPickupItem { + pub collected_by: Entity, + pub item: Entity, +} + +#[derive(Component, Debug, Clone)] +pub struct WantsToDrinkPotion { + pub potion: Entity, +} + +#[derive(Component, Debug, Clone)] +pub struct WantsToDropItem { + pub item: Entity, +} + + diff --git a/src/gui.rs b/src/gui.rs index 8e0134d..f301289 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,13 +1,14 @@ +use rltk::{BTerm, Point, VirtualKeyCode}; +use rltk::RGB; +use rltk::Rltk; +use specs::prelude::*; + +use crate::{CombatStats, InBackpack, State}; use crate::gamelog::GameLog; -use crate::CombatStats; use crate::Map; use crate::Name; use crate::Player; use crate::Position; -use rltk::Point; -use rltk::Rltk; -use rltk::RGB; -use specs::prelude::*; pub fn draw_ui(ecs: &World, ctx: &mut Rltk) { ctx.draw_box( @@ -150,3 +151,140 @@ fn draw_tooltips(ecs: &World, ctx: &mut Rltk) { } } } + +#[derive(PartialEq, Copy, Clone)] +pub enum ItemMenuResult { + Cancel, + NoResponse, + Selected, +} + +pub fn show_inventory(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option) { + let player_entity = gs.ecs.fetch::(); + let names = gs.ecs.read_storage::(); + let backpack = gs.ecs.read_storage::(); + let entities = gs.ecs.entities(); + + let inventory = (&backpack, &names) + .join() + .filter(|item| item.0.owner == *player_entity); + let count = inventory.count(); + + let mut y = (25 - (count / 2)) as i32; + ctx.draw_box( + 15, + y - 2, + 31, + (count + 3) as i32, + RGB::named(rltk::WHITE), + RGB::named(rltk::BLACK), + ); + ctx.print_color( + 18, + y - 2, + RGB::named(rltk::YELLOW), + RGB::named(rltk::BLACK), + "Inventory", + ); + ctx.print_color( + 18, + y + count as i32 + 1, + RGB::named(rltk::YELLOW), + RGB::named(rltk::BLACK), + "ESCAPE to cancel", + ); + + let mut equippable: Vec = Vec::new(); + let mut j = 0; + for (entity, _pack, name) in (&entities, &backpack, &names) + .join() + .filter(|item| item.1.owner == *player_entity) + { + ctx.set( + 17, + y, + RGB::named(rltk::WHITE), + RGB::named(rltk::BLACK), + rltk::to_cp437('('), + ); + ctx.set( + 18, + y, + RGB::named(rltk::YELLOW), + RGB::named(rltk::BLACK), + 97 + j as rltk::FontCharType, + ); + ctx.set( + 19, + y, + RGB::named(rltk::WHITE), + RGB::named(rltk::BLACK), + rltk::to_cp437(')'), + ); + + ctx.print(21, y, &name.name.to_string()); + equippable.push(entity); + y += 1; + j += 1; + } + + match ctx.key { + None => (ItemMenuResult::NoResponse, None), + Some(key) => { + match key { + VirtualKeyCode::Escape => { (ItemMenuResult::Cancel, None) } + _ => { + let selection = rltk::letter_to_option(key); + if selection > -1 && selection < count as i32 { + return (ItemMenuResult::Selected, Some(equippable[selection as usize])); + } + (ItemMenuResult::NoResponse, None) + } + } + } + } +} + +pub fn drop_item_menu(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option) { + let player_entity = gs.ecs.fetch::(); + let names = gs.ecs.read_storage::(); + let backpack = gs.ecs.read_storage::(); + let entities = gs.ecs.entities(); + + let inventory = (&backpack, &names).join().filter(|item| item.0.owner == *player_entity); + let count = inventory.count(); + + let mut y = (25 - (count / 2)) as i32; + ctx.draw_box(15, y - 2, 31, (count + 3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK)); + ctx.print_color(18, y - 2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Drop Which Item?"); + ctx.print_color(18, y + count as i32 + 1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel"); + + let mut equippable: Vec = Vec::new(); + let mut j = 0; + for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity) { + ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('(')); + ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97 + j as rltk::FontCharType); + ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')')); + + ctx.print(21, y, &name.name.to_string()); + equippable.push(entity); + y += 1; + j += 1; + } + + match ctx.key { + None => (ItemMenuResult::NoResponse, None), + Some(key) => { + match key { + VirtualKeyCode::Escape => { (ItemMenuResult::Cancel, None) } + _ => { + let selection = rltk::letter_to_option(key); + if selection > -1 && selection < count as i32 { + return (ItemMenuResult::Selected, Some(equippable[selection as usize])); + } + (ItemMenuResult::NoResponse, None) + } + } + } + } +} \ No newline at end of file diff --git a/src/inventory_system.rs b/src/inventory_system.rs new file mode 100644 index 0000000..aa9ccee --- /dev/null +++ b/src/inventory_system.rs @@ -0,0 +1,116 @@ +use rltk::Rltk; +use specs::prelude::*; + +use crate::{CombatStats, InBackpack, Name, Position, Potion, State, WantsToDrinkPotion, WantsToDropItem, WantsToPickupItem}; +use crate::gamelog::GameLog; +use crate::gui::ItemMenuResult; + +pub struct ItemCollectionSystem {} + +impl<'a> System<'a> for ItemCollectionSystem { + type SystemData = ( + ReadExpect<'a, Entity>, + WriteExpect<'a, GameLog>, + WriteStorage<'a, WantsToPickupItem>, + WriteStorage<'a, Position>, + ReadStorage<'a, Name>, + WriteStorage<'a, InBackpack>, + ); + + fn run(&mut self, data: Self::SystemData) { + let (player_entity, mut gamelog, mut wants_pickup, mut positions, names, mut backpack) = + data; + + for pickup in wants_pickup.join() { + positions.remove(pickup.item); + backpack + .insert( + pickup.item, + InBackpack { + owner: pickup.collected_by, + }, + ) + .expect("Unable to insert backpack entry"); + + if pickup.collected_by == *player_entity { + gamelog.entries.push(format!( + "You pick up the {}.", + names.get(pickup.item).unwrap().name + )) + } + } + + wants_pickup.clear(); + } +} + +pub struct PotionUseSystem {} + +impl<'a> System<'a> for PotionUseSystem { + type SystemData = ( + ReadExpect<'a, Entity>, + WriteExpect<'a, GameLog>, + Entities<'a>, + WriteStorage<'a, WantsToDrinkPotion>, + ReadStorage<'a, Name>, + ReadStorage<'a, Potion>, + WriteStorage<'a, CombatStats> + ); + + fn run(&mut self, data: Self::SystemData) { + let (player_entity, mut gamelog, entities, mut wants_drink, names, potions, mut combat_stats) = data; + + for (entity, drink, stats) in (&entities, &wants_drink, &mut combat_stats).join() { + let potion = potions.get(drink.potion); + match potion { + None => {} + Some(potion) => { + stats.hp = i32::min(stats.max_hp, stats.hp + potion.heal_amount); + if entity == *player_entity { + gamelog.entries.push(format!("You drink the {}, healing {} hp.", names.get(drink.potion).unwrap().name, potion.heal_amount)); + } + entities.delete(drink.potion).expect("Delete failed"); + } + } + } + + wants_drink.clear(); + } +} + +pub struct ItemDropSystem {} + +impl<'a> System<'a> for ItemDropSystem { + type SystemData = ( + ReadExpect<'a, Entity>, + WriteExpect<'a, GameLog>, + Entities<'a>, + WriteStorage<'a, WantsToDropItem>, + ReadStorage<'a, Name>, + WriteStorage<'a, Position>, + WriteStorage<'a, InBackpack> + ); + + fn run(&mut self, data: Self::SystemData) { + let (player_entity, mut gamelog, entities, mut wants_drop, names, mut positions, mut backpack) = data; + + for (entity, to_drop) in (&entities, &wants_drop).join() { + let mut dropper_pos: Position = Position { x: 0, y: 0 }; + { + let dropped_pos = positions.get(entity).unwrap(); + dropper_pos.x = dropped_pos.x; + dropper_pos.y = dropped_pos.y; + } + positions.insert(to_drop.item, Position { x: dropper_pos.x, y: dropper_pos.y }).expect("Unable to insert position"); + backpack.remove(to_drop.item); + + if entity == *player_entity { + gamelog.entries.push(format!("You drop the {}.", names.get(to_drop.item).unwrap().name)); + } + } + + wants_drop.clear(); + } +} + + diff --git a/src/main.rs b/src/main.rs index b61c925..a961221 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,8 @@ +use std::thread::spawn; + +use rltk::{GameState, Point, RGB}; +use specs::prelude::*; + use components::*; use damage_system::DamageSystem; use map::*; @@ -5,21 +10,22 @@ use map_indexing_system::MapIndexingSystem; use melee_combat_system::MeleeCombatSystem; use monster_ai_system::*; use player::*; -use rltk::{GameState, Point, RGB}; use visibility_system::*; -use specs::prelude::*; +use crate::inventory_system::{ItemCollectionSystem, ItemDropSystem, PotionUseSystem}; mod components; mod damage_system; mod gamelog; 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; #[derive(PartialEq, Copy, Clone)] @@ -28,6 +34,8 @@ pub enum RunState { PreRun, PlayerTurn, MonsterTurn, + ShowInventory, + ShowDropItem, } pub struct State { @@ -51,6 +59,15 @@ impl State { let mut damagesystem = DamageSystem {}; damagesystem.run_now(&self.ecs); + let mut inventory = ItemCollectionSystem {}; + inventory.run_now(&self.ecs); + + let mut potions = PotionUseSystem {}; + potions.run_now(&self.ecs); + + let mut drop_items = ItemDropSystem {}; + drop_items.run_now(&self.ecs); + self.ecs.maintain(); } } @@ -59,6 +76,25 @@ impl GameState for State { fn tick(&mut self, ctx: &mut rltk::Rltk) { ctx.cls(); + { + 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 = (&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::(); @@ -79,6 +115,32 @@ impl GameState for State { self.run_systems(); 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 mut intent = self.ecs.write_storage::(); + intent.insert(*self.ecs.fetch::(), WantsToDrinkPotion { potion: item_entity }).expect("Unable to insert intent"); + 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("Unable to insert intent"); + newrunstate = RunState::PlayerTurn; + } + } + } } { @@ -86,21 +148,6 @@ impl GameState for State { *runwriter = newrunstate; } damage_system::delete_the_dead(&mut self.ecs); - - draw_map(&self.ecs, ctx); - - let positions = self.ecs.read_storage::(); - let renderables = self.ecs.read_storage::(); - let map = self.ecs.fetch::(); - - for (pos, render) in (&positions, &renderables).join() { - 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); } } @@ -115,6 +162,7 @@ fn main() -> rltk::BError { let mut gs = State { ecs: World::new() }; gs.ecs.insert(RunState::PreRun); + gs.ecs.insert(rltk::RandomNumberGenerator::new()); gs.ecs.insert(gamelog::GameLog { entries: vec!["Welcome to Rusty Roguelike".to_string()], }); @@ -129,83 +177,21 @@ fn main() -> rltk::BError { gs.ecs.register::(); gs.ecs.register::(); gs.ecs.register::(); + gs.ecs.register::(); + gs.ecs.register::(); + gs.ecs.register::(); + gs.ecs.register::(); + gs.ecs.register::(); + gs.ecs.register::(); let map = Map::new_map_rooms_and_corridors(); + let (player_x, player_y) = map.rooms[0].center(); - gs.ecs.insert(Point::new(player_x, player_y)); - 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(); - + let player_entity = spawner::player(&mut gs.ecs, player_x, player_y); gs.ecs.insert(player_entity); - let mut rng = rltk::RandomNumberGenerator::new(); - for (i, room) in map.rooms.iter().skip(1).enumerate() { - let (x, y) = room.center(); - let glyph: rltk::FontCharType; - let roll = rng.roll_dice(1, 2); - let name: String; - match roll { - 1 => { - glyph = rltk::to_cp437('g'); - name = "Goblin".to_string(); - } - _ => { - glyph = rltk::to_cp437('o'); - name = "Orc".to_string(); - } - } - - gs.ecs - .create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph, - fg: RGB::named(rltk::RED), - bg: RGB::named(rltk::BLACK), - }) - .with(Viewshed { - visible_tiles: Vec::new(), - range: 8, - dirty: true, - }) - .with(Monster {}) - .with(Name { - name: format!("{} #{}", &name, i), - }) - .with(BlocksTile {}) - .with(CombatStats { - max_hp: 16, - hp: 16, - defense: 1, - power: 4, - }) - .build(); + for room in map.rooms.iter().skip(1) { + spawner::spawn_room(&mut gs.ecs, room); } gs.ecs.insert(map); diff --git a/src/map.rs b/src/map.rs index 84ccec5..f8d43cd 100644 --- a/src/map.rs +++ b/src/map.rs @@ -11,9 +11,11 @@ pub enum TileType { Floor, } -const MAP_WIDTH: usize = 80; -const MAP_HEIGHT: usize = 43; -const MAP_COUNT: usize = MAP_HEIGHT * MAP_WIDTH; +pub const MAP_WIDTH: usize = 80; +pub const MAP_HEIGHT: usize = 43; +pub const MAP_COUNT: usize = MAP_HEIGHT * MAP_WIDTH; +pub const MAX_MONSTER: i32 = 4; +pub const MAX_ITEMS: i32 = 2; pub struct Map { pub tiles: Vec, diff --git a/src/player.rs b/src/player.rs index 1fa0a46..44013ec 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,11 +1,14 @@ -use crate::{ - components::{CombatStats, Player, Position, Viewshed, WantsToMelee}, - map::{Map, TileType}, - RunState, State, -}; +use std::cmp::{max, min}; + use rltk::{console, Point, Rltk, VirtualKeyCode}; use specs::prelude::*; -use std::cmp::{max, min}; + +use crate::{ + components::{CombatStats, Player, Position, Viewshed, WantsToMelee}, + Item, + map::{Map, TileType}, RunState, State, WantsToPickupItem, +}; +use crate::gamelog::GameLog; pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { let mut positions = ecs.write_storage::(); @@ -17,7 +20,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { let map = ecs.fetch::(); for (entity, _player, pos, viewshed) in - (&entities, &mut players, &mut positions, &mut viewsheds).join() + (&entities, &mut players, &mut positions, &mut viewsheds).join() { let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y); @@ -47,6 +50,40 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { } } +fn get_item(ecs: &mut World) { + let player_pos = ecs.fetch::(); + let player_entity = ecs.fetch::(); + let entities = ecs.entities(); + let items = ecs.read_storage::(); + let positions = ecs.read_storage::(); + let mut gamelog = ecs.fetch_mut::(); + + let mut target_item: Option = None; + for (item_entity, _item, position) in (&entities, &items, &positions).join() { + if position.x == player_pos.x && position.y == player_pos.y { + target_item = Some(item_entity); + } + } + + match target_item { + None => gamelog + .entries + .push("There is nothing here to pick up.".to_string()), + Some(item) => { + let mut pickup = ecs.write_storage::(); + pickup + .insert( + *player_entity, + WantsToPickupItem { + collected_by: *player_entity, + item, + }, + ) + .expect("Unable to insert want to pickup"); + } + } +} + pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState { match ctx.key { None => return RunState::AwaitingInput, @@ -59,6 +96,9 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState { VirtualKeyCode::U => try_move_player(-1, -1, &mut gs.ecs), VirtualKeyCode::N => try_move_player(1, 1, &mut gs.ecs), VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs), + VirtualKeyCode::G => get_item(&mut gs.ecs), + VirtualKeyCode::I => return RunState::ShowInventory, + VirtualKeyCode::D => return RunState::ShowDropItem, _ => return RunState::AwaitingInput, }, } diff --git a/src/spawner.rs b/src/spawner.rs new file mode 100644 index 0000000..c7c6f80 --- /dev/null +++ b/src/spawner.rs @@ -0,0 +1,152 @@ +use rltk::{FontCharType, RandomNumberGenerator, RGB}; +use specs::prelude::*; + +use crate::{ + BlocksTile, CombatStats, Item, MAP_WIDTH, MAX_ITEMS, MAX_MONSTER, Monster, Name, Player, Position, + Potion, Renderable, Viewshed, +}; +use crate::rect::Rect; + +pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity { + 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), + render_order: 0, + }) + .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() +} + +pub fn random_monster(ecs: &mut World, x: i32, y: i32) { + let roll: i32; + { + let mut rng = ecs.write_resource::(); + roll = rng.roll_dice(1, 2); + } + match roll { + 1 => orc(ecs, x, y), + _ => goblin(ecs, x, y), + } +} + +fn orc(ecs: &mut World, x: i32, y: i32) { + monster(ecs, x, y, rltk::to_cp437('o'), "Orc") +} + +fn goblin(ecs: &mut World, x: i32, y: i32) { + monster(ecs, x, y, rltk::to_cp437('g'), "Goblin") +} + +fn monster(ecs: &mut World, x: i32, y: i32, glyph: FontCharType, name: S) { + ecs.create_entity() + .with(Position { x, y }) + .with(Renderable { + glyph, + fg: RGB::named(rltk::RED), + bg: RGB::named(rltk::BLACK), + render_order: 1, + }) + .with(Viewshed { + visible_tiles: Vec::new(), + range: 8, + dirty: true, + }) + .with(Monster {}) + .with(Name { + name: name.to_string(), + }) + .with(BlocksTile {}) + .with(CombatStats { + max_hp: 16, + hp: 16, + defense: 1, + power: 4, + }) + .build(); +} + +pub fn spawn_room(ecs: &mut World, room: &Rect) { + let mut monster_spawn_points: Vec = Vec::new(); + let mut item_spawn_points: Vec = Vec::new(); + + { + let mut rng = ecs.write_resource::(); + let num_monsters = rng.roll_dice(1, MAX_MONSTER + 2) - 3; + let num_items = rng.roll_dice(1, MAX_ITEMS + 2) - 3; + + for _i in 0..num_monsters { + let mut added = false; + while !added { + let x = (room.x1 + rng.roll_dice(1, i32::abs(room.x2 - room.x1))) as usize; + let y = (room.y1 + rng.roll_dice(1, i32::abs(room.y2 - room.y1))) as usize; + let idx = (y * MAP_WIDTH) + x; + if !monster_spawn_points.contains(&idx) { + monster_spawn_points.push(idx); + added = true; + } + } + } + + for _i in 0..num_items { + let mut added = false; + while !added { + let x = (room.x1 + rng.roll_dice(1, i32::abs(room.x2 - room.x1))) as usize; + let y = (room.y1 + rng.roll_dice(1, i32::abs(room.y2 - room.y1))) as usize; + let idx = (y * MAP_WIDTH) + x; + if !item_spawn_points.contains(&idx) { + item_spawn_points.push(idx); + added = true; + } + } + } + } + + for idx in monster_spawn_points.iter() { + let x = *idx % MAP_WIDTH; + let y = *idx / MAP_WIDTH; + random_monster(ecs, x as i32, y as i32); + } + + for idx in item_spawn_points.iter() { + let x = *idx % MAP_WIDTH; + let y = *idx / MAP_WIDTH; + health_potion(ecs, x as i32, y as i32); + } +} + +pub fn health_potion(ecs: &mut World, x: i32, y: i32) { + ecs.create_entity() + .with(Position { x, y }) + .with(Renderable { + glyph: rltk::to_cp437('i'), + fg: RGB::named(rltk::MAGENTA), + bg: RGB::named(rltk::BLACK), + render_order: 2, + }) + .with(Name { + name: "Health Potion".to_string(), + }) + .with(Item {}) + .with(Potion { heal_amount: 8 }) + .build(); +}