Create a skeleton for the Inventory system and Item system

Former-commit-id: 2579b70257ee9faa6d8614c33677ad0fc3c77a0d
This commit is contained in:
Piotr Korgól 2019-05-18 16:46:14 +00:00 committed by Joshua Barretto
parent 03502c1bac
commit 0e2b09a5f8
5 changed files with 104 additions and 7 deletions

View File

@ -1,3 +1,4 @@
use crate::inventory::Inventory;
use rand::prelude::*;
use specs::{Component, FlaggedStorage, VecStorage};
use vek::*;
@ -49,6 +50,11 @@ pub enum Foot {
Default,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Shoulder {
Default,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Weapon {
Daggers,
@ -60,11 +66,6 @@ pub enum Weapon {
Staff,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Shoulder {
Default,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Draw {
Default,

View File

@ -0,0 +1,46 @@
use crate::comp::actor;
use specs::{Component, VecStorage};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Armor {
//TODO: Don't make armor be a bodypart. Wearing enemy's head is funny but also creepy thing to do.
Helmet(actor::Head),
Shoulders(actor::Shoulder),
Chestplate(actor::Chest),
Belt(actor::Belt),
Gloves(actor::Hand),
Pants(actor::Pants),
Boots(actor::Foot),
Back,
Tabard,
Gem,
Necklace,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Rarity {
Common,
Uncommon,
Rare,
Legendary,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Item {
Weapon {
damage: i32,
strength: i32,
rarity: Rarity,
variant: actor::Weapon,
},
Armor {
defense: i32,
health_bonus: i32,
rarity: Rarity,
variant: Armor,
},
}
impl Component for Item {
type Storage = VecStorage<Self>;
}

View File

@ -0,0 +1,47 @@
//Library
use specs::{Component, VecStorage};
//Re-Exports
pub mod item;
use item::Item;
use std::mem::swap;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Inventory {
pub slots: Vec<Option<Item>>,
}
impl Inventory {
pub fn new() -> Inventory {
Inventory {
slots: vec![None; 24],
}
}
// Get info about an item slot
pub fn get(&self, cell: usize) -> Option<Item> {
self.slots.get(cell).cloned().flatten()
}
// Insert an item to a slot if its empty
pub fn swap(&mut self, cell: usize, item: Item) -> Option<Item> {
//TODO: Check if a slot is empty first.
self.slots.get_mut(cell).and_then(|cell| cell.replace(item))
}
// Remove an item from the slot
pub fn remove(&mut self, cell: usize, item: Item) -> Option<Item> {
let mut tmp_item = Some(item);
if let Some(old_item) = self.slots.get_mut(cell) {
swap(old_item, &mut tmp_item);
}
tmp_item
}
}
impl Component for Inventory {
type Storage = VecStorage<Self>;
}

View File

@ -2,7 +2,8 @@
euclidean_division,
duration_float,
trait_alias,
bind_by_move_pattern_guards
bind_by_move_pattern_guards,
option_flattening, // Converts Option<Option<Item>> into Option<Item> TODO: Remove this once this feature becomes stable
)]
#[macro_use]
@ -14,6 +15,7 @@ pub mod assets;
pub mod clock;
pub mod comp;
pub mod figure;
pub mod inventory;
pub mod msg;
pub mod ray;
pub mod state;

View File

@ -2,7 +2,7 @@
pub use sphynx::Uid;
use crate::{
comp,
comp, inventory,
msg::{EcsCompPacket, EcsResPacket},
sys,
terrain::{TerrainChunk, TerrainMap},
@ -111,6 +111,7 @@ impl State {
ecs.register::<comp::AnimationHistory>();
ecs.register::<comp::Agent>();
ecs.register::<comp::Control>();
ecs.register::<inventory::Inventory>();
// Register synced resources used by the ECS.
ecs.add_resource_synced(TimeOfDay(0.0));