Refactor potion, and its associated components to be more generic

This commit is contained in:
Timothy Warren 2021-11-04 15:06:04 -04:00
parent 923e0dc42b
commit 4e9869b09f
4 changed files with 54 additions and 34 deletions

View File

@ -72,7 +72,7 @@ impl SufferDamage {
pub struct Item {} pub struct Item {}
#[derive(Component, Debug)] #[derive(Component, Debug)]
pub struct Potion { pub struct ProvidesHealing {
pub heal_amount: i32, pub heal_amount: i32,
} }
@ -88,11 +88,14 @@ pub struct WantsToPickupItem {
} }
#[derive(Component, Debug)] #[derive(Component, Debug)]
pub struct WantsToDrinkPotion { pub struct WantsToUseItem {
pub potion: Entity, pub item: Entity,
} }
#[derive(Component, Debug, Clone)] #[derive(Component, Debug, Clone)]
pub struct WantsToDropItem { pub struct WantsToDropItem {
pub item: Entity, pub item: Entity,
} }
#[derive(Component, Debug)]
pub struct Consumable {}

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
game_log::GameLog, CombatStats, InBackpack, Name, Position, Potion, WantsToDrinkPotion, game_log::GameLog, CombatStats, Consumable, InBackpack, Map, Name, Position, ProvidesHealing,
WantsToDropItem, WantsToPickupItem, WantsToDropItem, WantsToPickupItem, WantsToUseItem,
}; };
use specs::prelude::*; use specs::prelude::*;
@ -44,17 +44,19 @@ impl<'a> System<'a> for ItemCollectionSystem {
} }
} }
pub struct PotionUseSystem {} pub struct ItemUseSystem {}
impl<'a> System<'a> for PotionUseSystem { impl<'a> System<'a> for ItemUseSystem {
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
type SystemData = ( type SystemData = (
ReadExpect<'a, Entity>, ReadExpect<'a, Entity>,
WriteExpect<'a, GameLog>, WriteExpect<'a, GameLog>,
ReadExpect<'a, Map>,
Entities<'a>, Entities<'a>,
WriteStorage<'a, WantsToDrinkPotion>, WriteStorage<'a, WantsToUseItem>,
ReadStorage<'a, Name>, ReadStorage<'a, Name>,
ReadStorage<'a, Potion>, ReadStorage<'a, Consumable>,
ReadStorage<'a, ProvidesHealing>,
WriteStorage<'a, CombatStats>, WriteStorage<'a, CombatStats>,
); );
@ -62,33 +64,42 @@ impl<'a> System<'a> for PotionUseSystem {
let ( let (
player_entity, player_entity,
mut gamelog, mut gamelog,
map,
entities, entities,
mut wants_drink, mut wants_use,
names, names,
potions, consumables,
healing,
mut combat_stats, mut combat_stats,
) = data; ) = data;
for (entity, drink, stats) in (&entities, &wants_drink, &mut combat_stats).join() { for (entity, useitem, stats) in (&entities, &wants_use, &mut combat_stats).join() {
match potions.get(drink.potion) { match healing.get(useitem.item) {
None => {} None => {}
Some(potion) => { Some(healer) => {
stats.hp = i32::min(stats.max_hp, stats.hp + potion.heal_amount); stats.hp = i32::min(stats.max_hp, stats.hp + healer.heal_amount);
if entity == *player_entity { if entity == *player_entity {
gamelog.entries.push(format!( gamelog.entries.push(format!(
"You drink the {}, healing {} hp.", "You drink the {}, healing {} hp.",
names.get(drink.potion).unwrap().name, names.get(useitem.item).unwrap().name,
potion.heal_amount healer.heal_amount
)); ));
} }
}
}
let consumable = consumables.get(useitem.item);
match consumable {
None => {}
Some(_) => {
entities entities
.delete(drink.potion) .delete(useitem.item)
.expect("Failed to delete potion"); .expect("Failed to consume item");
} }
} }
} }
wants_drink.clear(); wants_use.clear();
} }
} }

View File

@ -18,7 +18,7 @@ mod visibility_system;
pub use components::*; pub use components::*;
use damage_system::DamageSystem; use damage_system::DamageSystem;
pub use game_log::GameLog; pub use game_log::GameLog;
use inventory_system::{ItemCollectionSystem, ItemDropSystem, PotionUseSystem}; use inventory_system::{ItemCollectionSystem, ItemDropSystem, ItemUseSystem};
pub use map::*; pub use map::*;
use map_indexing_system::MapIndexingSystem; use map_indexing_system::MapIndexingSystem;
use melee_combat_system::MeleeCombatSystem; use melee_combat_system::MeleeCombatSystem;
@ -55,6 +55,10 @@ pub struct State {
} }
impl State { impl State {
fn new() -> Self {
State { ecs: World::new() }
}
fn run_systems(&mut self) { fn run_systems(&mut self) {
let mut vis = VisibilitySystem {}; let mut vis = VisibilitySystem {};
vis.run_now(&self.ecs); vis.run_now(&self.ecs);
@ -74,8 +78,8 @@ impl State {
let mut pickup = ItemCollectionSystem {}; let mut pickup = ItemCollectionSystem {};
pickup.run_now(&self.ecs); pickup.run_now(&self.ecs);
let mut potions = PotionUseSystem {}; let mut items = ItemUseSystem {};
potions.run_now(&self.ecs); items.run_now(&self.ecs);
let mut drop_items = ItemDropSystem {}; let mut drop_items = ItemDropSystem {};
drop_items.run_now(&self.ecs); drop_items.run_now(&self.ecs);
@ -140,15 +144,15 @@ impl GameState for State {
gui::ItemMenuResult::NoResponse => {} gui::ItemMenuResult::NoResponse => {}
gui::ItemMenuResult::Selected => { gui::ItemMenuResult::Selected => {
let item_entity = result.1.unwrap(); let item_entity = result.1.unwrap();
let mut intent = self.ecs.write_storage::<WantsToDrinkPotion>(); let mut intent = self.ecs.write_storage::<WantsToUseItem>();
intent intent
.insert( .insert(
*self.ecs.fetch::<Entity>(), *self.ecs.fetch::<Entity>(),
WantsToDrinkPotion { WantsToUseItem {
potion: item_entity, item: item_entity,
}, },
) )
.expect("failed to add intent to drink potion"); .expect("failed to add intent to use item");
newrunstate = RunState::PlayerTurn; newrunstate = RunState::PlayerTurn;
} }
@ -192,7 +196,7 @@ fn main() -> rltk::BError {
.with_title("Roguelike Tutorial") .with_title("Roguelike Tutorial")
.build()?; .build()?;
let mut gs = State { ecs: World::new() }; let mut gs = State::new();
register!( register!(
gs <- gs <-
@ -207,14 +211,15 @@ fn main() -> rltk::BError {
WantsToMelee, WantsToMelee,
SufferDamage, SufferDamage,
Item, Item,
Potion, ProvidesHealing,
InBackpack, InBackpack,
WantsToPickupItem, WantsToPickupItem,
WantsToDrinkPotion, WantsToUseItem,
WantsToDropItem, WantsToDropItem,
Consumable,
); );
let map: Map = Map::new_map_rooms_and_corridors(); let map = Map::new_map_rooms_and_corridors();
let (player_x, player_y) = map.rooms[0].center(); let (player_x, player_y) = map.rooms[0].center();
let player_entity = spawner::player(&mut gs.ecs, player_x, player_y); let player_entity = spawner::player(&mut gs.ecs, player_x, player_y);

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
BlocksTile, CombatStats, Item, Monster, Name, Player, Position, Potion, Rect, Renderable, BlocksTile, CombatStats, Consumable, Item, Monster, Name, Player, Position, ProvidesHealing,
Viewshed, MAP_WIDTH, Rect, Renderable, Viewshed, MAP_WIDTH,
}; };
use rltk::{RandomNumberGenerator, RGB}; use rltk::{RandomNumberGenerator, RGB};
use specs::prelude::*; use specs::prelude::*;
@ -160,6 +160,7 @@ fn health_potion(ecs: &mut World, x: i32, y: i32) {
name: "Health Potion".to_string(), name: "Health Potion".to_string(),
}) })
.with(Item {}) .with(Item {})
.with(Potion { heal_amount: 8 }) .with(Consumable {})
.with(ProvidesHealing { heal_amount: 8 })
.build(); .build();
} }