Start of inventory system
This commit is contained in:
parent
07c703f2a4
commit
0ac2226947
@ -25,18 +25,18 @@ pub struct Viewshed {
|
|||||||
pub dirty: bool,
|
pub dirty: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Monster {}
|
pub struct Monster {}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Name {
|
pub struct Name {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component, Debug)]
|
||||||
pub struct BlocksTile {}
|
pub struct BlocksTile {}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component, Debug)]
|
||||||
pub struct CombatStats {
|
pub struct CombatStats {
|
||||||
pub max_hp: i32,
|
pub max_hp: i32,
|
||||||
pub hp: i32,
|
pub hp: i32,
|
||||||
@ -66,3 +66,22 @@ impl SufferDamage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Item {}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Potion {
|
||||||
|
pub heal_amount: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone)]
|
||||||
|
pub struct InBackpack {
|
||||||
|
pub owner: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone)]
|
||||||
|
pub struct WantsToPickupItem {
|
||||||
|
pub collected_by: Entity,
|
||||||
|
pub item: Entity,
|
||||||
|
}
|
||||||
|
86
src/gui.rs
86
src/gui.rs
@ -1,5 +1,5 @@
|
|||||||
use crate::{gamelog::GameLog, CombatStats, Map, Name, Player, Position};
|
use crate::{gamelog::GameLog, CombatStats, InBackpack, Map, Name, Player, Position, State};
|
||||||
use rltk::{Point, Rltk, RGB};
|
use rltk::{Point, Rltk, VirtualKeyCode, RGB};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
|
||||||
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
||||||
@ -153,3 +153,85 @@ 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 {
|
||||||
|
let player_entity = gs.ecs.fetch::<Entity>();
|
||||||
|
let names = gs.ecs.read_storage::<Name>();
|
||||||
|
let backpack = gs.ecs.read_storage::<InBackpack>();
|
||||||
|
|
||||||
|
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,
|
||||||
|
32,
|
||||||
|
(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 j = 0;
|
||||||
|
for (_pack, name) in (&backpack, &names)
|
||||||
|
.join()
|
||||||
|
.filter(|item| item.0.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());
|
||||||
|
y += 1;
|
||||||
|
j += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
match ctx.key {
|
||||||
|
None => ItemMenuResult::NoResponse,
|
||||||
|
Some(key) => match key {
|
||||||
|
VirtualKeyCode::Escape => ItemMenuResult::Cancel,
|
||||||
|
_ => ItemMenuResult::NoResponse,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
42
src/inventory_system.rs
Normal file
42
src/inventory_system.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
use crate::{gamelog::GameLog, InBackpack, Name, Position, WantsToPickupItem};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub struct ItemCollectionSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for ItemCollectionSystem {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
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("Failed to add item to backpack");
|
||||||
|
|
||||||
|
if pickup.collected_by == *player_entity {
|
||||||
|
gamelog.entries.push(format!(
|
||||||
|
"You pick up the {}.",
|
||||||
|
names.get(pickup.item).unwrap().name
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wants_pickup.clear();
|
||||||
|
}
|
||||||
|
}
|
15
src/main.rs
15
src/main.rs
@ -22,7 +22,9 @@ use damage_system::DamageSystem;
|
|||||||
mod gamelog;
|
mod gamelog;
|
||||||
mod gui;
|
mod gui;
|
||||||
pub use gamelog::GameLog;
|
pub use gamelog::GameLog;
|
||||||
|
mod inventory_system;
|
||||||
mod spawner;
|
mod spawner;
|
||||||
|
use inventory_system::ItemCollectionSystem;
|
||||||
|
|
||||||
pub const MAP_SIZE: usize = 80 * 50;
|
pub const MAP_SIZE: usize = 80 * 50;
|
||||||
|
|
||||||
@ -32,6 +34,7 @@ pub enum RunState {
|
|||||||
PreRun,
|
PreRun,
|
||||||
PlayerTurn,
|
PlayerTurn,
|
||||||
MonsterTurn,
|
MonsterTurn,
|
||||||
|
ShowInventory,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
@ -55,6 +58,9 @@ impl State {
|
|||||||
let mut damage = DamageSystem {};
|
let mut damage = DamageSystem {};
|
||||||
damage.run_now(&self.ecs);
|
damage.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut pickup = ItemCollectionSystem {};
|
||||||
|
pickup.run_now(&self.ecs);
|
||||||
|
|
||||||
self.ecs.maintain();
|
self.ecs.maintain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,6 +90,11 @@ impl GameState for State {
|
|||||||
self.run_systems();
|
self.run_systems();
|
||||||
newrunstate = RunState::AwaitingInput;
|
newrunstate = RunState::AwaitingInput;
|
||||||
}
|
}
|
||||||
|
RunState::ShowInventory => {
|
||||||
|
if gui::show_inventory(self, ctx) == gui::ItemMenuResult::Cancel {
|
||||||
|
newrunstate = RunState::AwaitingInput;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -130,6 +141,10 @@ fn main() -> rltk::BError {
|
|||||||
gs.ecs.register::<CombatStats>();
|
gs.ecs.register::<CombatStats>();
|
||||||
gs.ecs.register::<WantsToMelee>();
|
gs.ecs.register::<WantsToMelee>();
|
||||||
gs.ecs.register::<SufferDamage>();
|
gs.ecs.register::<SufferDamage>();
|
||||||
|
gs.ecs.register::<Item>();
|
||||||
|
gs.ecs.register::<Potion>();
|
||||||
|
gs.ecs.register::<InBackpack>();
|
||||||
|
gs.ecs.register::<WantsToPickupItem>();
|
||||||
|
|
||||||
let map: Map = Map::new_map_rooms_and_corridors();
|
let map: 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();
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
use crate::{CombatStats, Map, Player, Position, RunState, State, Viewshed, WantsToMelee};
|
use crate::{
|
||||||
|
gamelog::GameLog, CombatStats, Item, Map, Player, Position, RunState, State, Viewshed,
|
||||||
|
WantsToMelee, WantsToPickupItem,
|
||||||
|
};
|
||||||
use rltk::{Point, Rltk, VirtualKeyCode};
|
use rltk::{Point, Rltk, VirtualKeyCode};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
@ -56,6 +59,7 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
|||||||
match ctx.key {
|
match ctx.key {
|
||||||
None => return RunState::AwaitingInput, // Nothing happened
|
None => return RunState::AwaitingInput, // Nothing happened
|
||||||
Some(key) => match key {
|
Some(key) => match key {
|
||||||
|
// Cardinal directions
|
||||||
VirtualKeyCode::Left
|
VirtualKeyCode::Left
|
||||||
| VirtualKeyCode::Numpad4
|
| VirtualKeyCode::Numpad4
|
||||||
| VirtualKeyCode::H
|
| VirtualKeyCode::H
|
||||||
@ -91,9 +95,49 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
|||||||
try_move_player(-1, 1, &mut gs.ecs)
|
try_move_player(-1, 1, &mut gs.ecs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pick up item
|
||||||
|
VirtualKeyCode::G => get_item(&mut gs.ecs),
|
||||||
|
|
||||||
|
// Show inventory
|
||||||
|
VirtualKeyCode::I => return RunState::ShowInventory,
|
||||||
|
|
||||||
_ => return RunState::AwaitingInput,
|
_ => return RunState::AwaitingInput,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
RunState::PlayerTurn
|
RunState::PlayerTurn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_item(ecs: &mut World) {
|
||||||
|
let player_pos = ecs.fetch::<Point>();
|
||||||
|
let player_entity = ecs.fetch::<Entity>();
|
||||||
|
let entities = ecs.entities();
|
||||||
|
let items = ecs.read_storage::<Item>();
|
||||||
|
let positions = ecs.read_storage::<Position>();
|
||||||
|
let mut gamelog = ecs.fetch_mut::<GameLog>();
|
||||||
|
|
||||||
|
let mut target_item: Option<Entity> = 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::<WantsToPickupItem>();
|
||||||
|
pickup
|
||||||
|
.insert(
|
||||||
|
*player_entity,
|
||||||
|
WantsToPickupItem {
|
||||||
|
collected_by: *player_entity,
|
||||||
|
item,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.expect("Unable to pick up item?!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
BlocksTile, CombatStats, Monster, Name, Player, Position, Rect, Renderable, Viewshed, MAP_WIDTH,
|
BlocksTile, CombatStats, Item, Monster, Name, Player, Position, Potion, Rect, Renderable,
|
||||||
|
Viewshed, MAP_WIDTH,
|
||||||
};
|
};
|
||||||
use rltk::{RandomNumberGenerator, RGB};
|
use rltk::{RandomNumberGenerator, RGB};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
@ -90,11 +91,13 @@ fn monster<S: ToString>(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharTy
|
|||||||
/// fills a room with stuff!
|
/// fills a room with stuff!
|
||||||
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();
|
||||||
|
|
||||||
// Scope to keep the borrow checker happy
|
// Scope to keep the borrow checker happy
|
||||||
{
|
{
|
||||||
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||||
let num_monsters = rng.roll_dice(1, MAX_MONSTERS + 2) - 3;
|
let num_monsters = rng.roll_dice(1, MAX_MONSTERS + 2) - 3;
|
||||||
|
let num_items = rng.roll_dice(1, MAX_ITEMS + 2) - 3;
|
||||||
|
|
||||||
for _i in 0..num_monsters {
|
for _i in 0..num_monsters {
|
||||||
let mut added = false;
|
let mut added = false;
|
||||||
@ -109,6 +112,20 @@ pub fn spawn_room(ecs: &mut World, room: &Rect) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actually spawn the monsters
|
// Actually spawn the monsters
|
||||||
@ -118,4 +135,28 @@ pub fn spawn_room(ecs: &mut World, room: &Rect) {
|
|||||||
|
|
||||||
random_monster(ecs, x as i32, y as i32);
|
random_monster(ecs, x as i32, y as i32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actually spawn the potions
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn health_potion(ecs: &mut World, x: i32, y: i32) {
|
||||||
|
ecs.create_entity()
|
||||||
|
.with(Position { x, y })
|
||||||
|
.with(Renderable {
|
||||||
|
glyph: rltk::to_cp437('¡'),
|
||||||
|
fg: RGB::named(rltk::MAGENTA),
|
||||||
|
bg: RGB::named(rltk::BLACK),
|
||||||
|
})
|
||||||
|
.with(Name {
|
||||||
|
name: "Health Potion".to_string(),
|
||||||
|
})
|
||||||
|
.with(Item {})
|
||||||
|
.with(Potion { heal_amount: 8 })
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user