generalised item use system
This commit is contained in:
parent
f26adf352e
commit
65d728b75a
4 changed files with 96 additions and 33 deletions
|
|
@ -70,7 +70,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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,6 +91,9 @@ pub struct WantsToDropItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct WantsToDrinkPotion {
|
pub struct WantsToUseItem {
|
||||||
pub potion: Entity,
|
pub item: Entity,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Consumable {}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{
|
use super::{
|
||||||
gamelog::GameLog, CombatStats, InBackpack, Name, Position, Potion, WantsToDrinkPotion, WantsToDropItem,
|
gamelog::GameLog, CombatStats, Consumable, InBackpack, Name, Position, ProvidesHealing, WantsToDropItem,
|
||||||
WantsToPickupItem,
|
WantsToPickupItem, WantsToUseItem,
|
||||||
};
|
};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
|
@ -33,40 +33,47 @@ 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>,
|
||||||
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>,
|
||||||
);
|
);
|
||||||
|
|
||||||
fn run(&mut self, data: Self::SystemData) {
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
let (player_entity, mut gamelog, entities, mut wants_drink, names, potions, mut combat_stats) = data;
|
let (player_entity, mut gamelog, entities, mut wants_use, names, consumables, healing, mut combat_stats) = data;
|
||||||
|
|
||||||
for (entity, drink, stats) in (&entities, &wants_drink, &mut combat_stats).join() {
|
for (entity, use_item, stats) in (&entities, &wants_use, &mut combat_stats).join() {
|
||||||
let potion = potions.get(drink.potion);
|
let item_heals = healing.get(use_item.item);
|
||||||
match potion {
|
match item_heals {
|
||||||
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 quaff the {}, and heal {} hp.",
|
"You quaff the {}, and heal {} hp.",
|
||||||
names.get(drink.potion).unwrap().name,
|
names.get(use_item.item).unwrap().name,
|
||||||
potion.heal_amount
|
healer.heal_amount
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
entities.delete(drink.potion).expect("Delete failed");
|
let consumable = consumables.get(use_item.item);
|
||||||
|
match consumable {
|
||||||
|
None => {}
|
||||||
|
Some(_) => {
|
||||||
|
entities.delete(use_item.item).expect("Delete failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wants_drink.clear();
|
}
|
||||||
|
}
|
||||||
|
wants_use.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
15
src/main.rs
15
src/main.rs
|
|
@ -58,8 +58,8 @@ impl State {
|
||||||
damage_system.run_now(&self.ecs);
|
damage_system.run_now(&self.ecs);
|
||||||
let mut inventory_system = ItemCollectionSystem {};
|
let mut inventory_system = ItemCollectionSystem {};
|
||||||
inventory_system.run_now(&self.ecs);
|
inventory_system.run_now(&self.ecs);
|
||||||
let mut potion_system = PotionUseSystem {};
|
let mut item_use_system = ItemUseSystem {};
|
||||||
potion_system.run_now(&self.ecs);
|
item_use_system.run_now(&self.ecs);
|
||||||
let mut drop_system = ItemDropSystem {};
|
let mut drop_system = ItemDropSystem {};
|
||||||
drop_system.run_now(&self.ecs);
|
drop_system.run_now(&self.ecs);
|
||||||
self.ecs.maintain();
|
self.ecs.maintain();
|
||||||
|
|
@ -121,9 +121,9 @@ 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(*self.ecs.fetch::<Entity>(), WantsToDrinkPotion { potion: item_entity })
|
.insert(*self.ecs.fetch::<Entity>(), WantsToUseItem { item: item_entity })
|
||||||
.expect("Unable to insert intent.");
|
.expect("Unable to insert intent.");
|
||||||
new_runstate = RunState::PlayerTurn;
|
new_runstate = RunState::PlayerTurn;
|
||||||
}
|
}
|
||||||
|
|
@ -160,7 +160,7 @@ fn main() -> rltk::BError {
|
||||||
let mut context = RltkBuilder::simple80x50()
|
let mut context = RltkBuilder::simple80x50()
|
||||||
.with_tile_dimensions(16, 16)
|
.with_tile_dimensions(16, 16)
|
||||||
//.with_fitscreen(true)
|
//.with_fitscreen(true)
|
||||||
.with_title("rust-rltk-llywelwyn.github.io")
|
.with_title("rust-rl")
|
||||||
.build()?;
|
.build()?;
|
||||||
context.with_post_scanlines(true);
|
context.with_post_scanlines(true);
|
||||||
let mut gs = State { ecs: World::new() };
|
let mut gs = State { ecs: World::new() };
|
||||||
|
|
@ -176,11 +176,12 @@ fn main() -> rltk::BError {
|
||||||
gs.ecs.register::<WantsToMelee>();
|
gs.ecs.register::<WantsToMelee>();
|
||||||
gs.ecs.register::<SufferDamage>();
|
gs.ecs.register::<SufferDamage>();
|
||||||
gs.ecs.register::<Item>();
|
gs.ecs.register::<Item>();
|
||||||
gs.ecs.register::<Potion>();
|
gs.ecs.register::<ProvidesHealing>();
|
||||||
gs.ecs.register::<InBackpack>();
|
gs.ecs.register::<InBackpack>();
|
||||||
gs.ecs.register::<WantsToPickupItem>();
|
gs.ecs.register::<WantsToPickupItem>();
|
||||||
gs.ecs.register::<WantsToDropItem>();
|
gs.ecs.register::<WantsToDropItem>();
|
||||||
gs.ecs.register::<WantsToDrinkPotion>();
|
gs.ecs.register::<WantsToUseItem>();
|
||||||
|
gs.ecs.register::<Consumable>();
|
||||||
|
|
||||||
let map = Map::new_map_rooms_and_corridors();
|
let map = Map::new_map_rooms_and_corridors();
|
||||||
let (player_x, player_y) = map.rooms[0].centre();
|
let (player_x, player_y) = map.rooms[0].centre();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use super::{
|
use super::{
|
||||||
BlocksTile, CombatStats, Item, Monster, Name, Player, Position, Potion, Rect, Renderable, Viewshed, MAPWIDTH,
|
BlocksTile, CombatStats, Consumable, Item, Monster, Name, Player, Position, ProvidesHealing, Rect, Renderable,
|
||||||
|
Viewshed, MAPWIDTH,
|
||||||
};
|
};
|
||||||
use rltk::{RandomNumberGenerator, RGB};
|
use rltk::{RandomNumberGenerator, RGB};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
|
@ -15,7 +16,7 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
|
||||||
render_order: 0,
|
render_order: 0,
|
||||||
})
|
})
|
||||||
.with(Player {})
|
.with(Player {})
|
||||||
.with(Viewshed { visible_tiles: Vec::new(), range: 8, dirty: true })
|
.with(Viewshed { visible_tiles: Vec::new(), range: 12, dirty: true })
|
||||||
.with(Name { name: "hero (you)".to_string() })
|
.with(Name { name: "hero (you)".to_string() })
|
||||||
.with(CombatStats { max_hp: 30, hp: 30, defence: 2, power: 5 })
|
.with(CombatStats { max_hp: 30, hp: 30, defence: 2, power: 5 })
|
||||||
.build()
|
.build()
|
||||||
|
|
@ -26,13 +27,27 @@ pub fn random_monster(ecs: &mut World, x: i32, y: i32) {
|
||||||
let roll: i32;
|
let roll: i32;
|
||||||
{
|
{
|
||||||
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||||
roll = rng.roll_dice(1, 2);
|
roll = rng.roll_dice(1, 3);
|
||||||
}
|
}
|
||||||
match roll {
|
match roll {
|
||||||
1 => orc(ecs, x, y),
|
1 => orc(ecs, x, y),
|
||||||
|
2 => goblin_chieftain(ecs, x, y),
|
||||||
_ => goblin(ecs, x, y),
|
_ => goblin(ecs, x, y),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Spawns a random item at a given loc
|
||||||
|
pub fn random_item(ecs: &mut World, x: i32, y: i32) {
|
||||||
|
let roll: i32;
|
||||||
|
{
|
||||||
|
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||||
|
roll = rng.roll_dice(1, 5);
|
||||||
|
}
|
||||||
|
match roll {
|
||||||
|
1 => health_potion(ecs, x, y),
|
||||||
|
2 => poison_potion(ecs, x, y),
|
||||||
|
_ => weak_health_potion(ecs, x, y),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const MAX_MONSTERS: i32 = 4;
|
const MAX_MONSTERS: i32 = 4;
|
||||||
const MAX_ITEMS: i32 = 2;
|
const MAX_ITEMS: i32 = 2;
|
||||||
|
|
@ -41,7 +56,7 @@ fn monster<S: ToString>(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharTy
|
||||||
ecs.create_entity()
|
ecs.create_entity()
|
||||||
.with(Position { x, y })
|
.with(Position { x, y })
|
||||||
.with(Renderable { glyph: glyph, fg: RGB::named(rltk::RED), bg: RGB::named(rltk::BLACK), render_order: 1 })
|
.with(Renderable { glyph: 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(Viewshed { visible_tiles: Vec::new(), range: 12, dirty: true })
|
||||||
.with(Monster {})
|
.with(Monster {})
|
||||||
.with(Name { name: name.to_string() })
|
.with(Name { name: name.to_string() })
|
||||||
.with(BlocksTile {})
|
.with(BlocksTile {})
|
||||||
|
|
@ -57,6 +72,10 @@ fn goblin(ecs: &mut World, x: i32, y: i32) {
|
||||||
monster(ecs, x, y, rltk::to_cp437('g'), "goblin");
|
monster(ecs, x, y, rltk::to_cp437('g'), "goblin");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn goblin_chieftain(ecs: &mut World, x: i32, y: i32) {
|
||||||
|
monster(ecs, x, y, rltk::to_cp437('G'), "goblin chieftain");
|
||||||
|
}
|
||||||
|
|
||||||
pub fn spawn_room(ecs: &mut World, room: &Rect) {
|
pub fn spawn_room(ecs: &mut World, room: &Rect) {
|
||||||
let mut monster_spawn_points: Vec<usize> = Vec::new();
|
let mut monster_spawn_points: Vec<usize> = Vec::new();
|
||||||
let mut item_spawn_points: Vec<usize> = Vec::new();
|
let mut item_spawn_points: Vec<usize> = Vec::new();
|
||||||
|
|
@ -103,11 +122,27 @@ pub fn spawn_room(ecs: &mut World, room: &Rect) {
|
||||||
for idx in item_spawn_points.iter() {
|
for idx in item_spawn_points.iter() {
|
||||||
let x = *idx % MAPWIDTH;
|
let x = *idx % MAPWIDTH;
|
||||||
let y = *idx / MAPWIDTH;
|
let y = *idx / MAPWIDTH;
|
||||||
health_potion(ecs, x as i32, y as i32);
|
random_item(ecs, x as i32, y as i32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn health_potion(ecs: &mut World, x: i32, y: i32) {
|
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: "potion of health".to_string() })
|
||||||
|
.with(Item {})
|
||||||
|
.with(Consumable {})
|
||||||
|
.with(ProvidesHealing { heal_amount: 12 })
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn weak_health_potion(ecs: &mut World, x: i32, y: i32) {
|
||||||
ecs.create_entity()
|
ecs.create_entity()
|
||||||
.with(Position { x, y })
|
.with(Position { x, y })
|
||||||
.with(Renderable {
|
.with(Renderable {
|
||||||
|
|
@ -116,8 +151,25 @@ fn health_potion(ecs: &mut World, x: i32, y: i32) {
|
||||||
bg: RGB::named(rltk::BLACK),
|
bg: RGB::named(rltk::BLACK),
|
||||||
render_order: 2,
|
render_order: 2,
|
||||||
})
|
})
|
||||||
.with(Name { name: "health potion".to_string() })
|
.with(Name { name: "potion of lesser health".to_string() })
|
||||||
.with(Item {})
|
.with(Item {})
|
||||||
.with(Potion { heal_amount: 8 })
|
.with(Consumable {})
|
||||||
|
.with(ProvidesHealing { heal_amount: 6 })
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poison_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::SEAGREEN),
|
||||||
|
bg: RGB::named(rltk::BLACK),
|
||||||
|
render_order: 2,
|
||||||
|
})
|
||||||
|
.with(Name { name: "potion of ... health?".to_string() })
|
||||||
|
.with(Item {})
|
||||||
|
.with(Consumable {})
|
||||||
|
.with(ProvidesHealing { heal_amount: -12 })
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue