mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Create a skeleton for the Inventory system and Item system
Former-commit-id: 2579b70257ee9faa6d8614c33677ad0fc3c77a0d
This commit is contained in:
committed by
Joshua Barretto
parent
a6d823a1d1
commit
1f5819d7a0
@ -1,3 +1,4 @@
|
|||||||
|
use crate::inventory::Inventory;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use specs::{Component, FlaggedStorage, VecStorage};
|
use specs::{Component, FlaggedStorage, VecStorage};
|
||||||
use vek::*;
|
use vek::*;
|
||||||
@ -49,6 +50,11 @@ pub enum Foot {
|
|||||||
Default,
|
Default,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
|
pub enum Shoulder {
|
||||||
|
Default,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
pub enum Weapon {
|
pub enum Weapon {
|
||||||
Daggers,
|
Daggers,
|
||||||
@ -60,11 +66,6 @@ pub enum Weapon {
|
|||||||
Staff,
|
Staff,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
|
||||||
pub enum Shoulder {
|
|
||||||
Default,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
pub enum Draw {
|
pub enum Draw {
|
||||||
Default,
|
Default,
|
||||||
|
46
common/src/inventory/item.rs
Normal file
46
common/src/inventory/item.rs
Normal 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>;
|
||||||
|
}
|
47
common/src/inventory/mod.rs
Normal file
47
common/src/inventory/mod.rs
Normal 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>;
|
||||||
|
}
|
@ -2,7 +2,8 @@
|
|||||||
euclidean_division,
|
euclidean_division,
|
||||||
duration_float,
|
duration_float,
|
||||||
trait_alias,
|
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]
|
#[macro_use]
|
||||||
@ -14,6 +15,7 @@ pub mod assets;
|
|||||||
pub mod clock;
|
pub mod clock;
|
||||||
pub mod comp;
|
pub mod comp;
|
||||||
pub mod figure;
|
pub mod figure;
|
||||||
|
pub mod inventory;
|
||||||
pub mod msg;
|
pub mod msg;
|
||||||
pub mod ray;
|
pub mod ray;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
pub use sphynx::Uid;
|
pub use sphynx::Uid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
comp,
|
comp, inventory,
|
||||||
msg::{EcsCompPacket, EcsResPacket},
|
msg::{EcsCompPacket, EcsResPacket},
|
||||||
sys,
|
sys,
|
||||||
terrain::{TerrainChunk, TerrainMap},
|
terrain::{TerrainChunk, TerrainMap},
|
||||||
@ -111,6 +111,7 @@ impl State {
|
|||||||
ecs.register::<comp::AnimationHistory>();
|
ecs.register::<comp::AnimationHistory>();
|
||||||
ecs.register::<comp::Agent>();
|
ecs.register::<comp::Agent>();
|
||||||
ecs.register::<comp::Control>();
|
ecs.register::<comp::Control>();
|
||||||
|
ecs.register::<inventory::Inventory>();
|
||||||
|
|
||||||
// Register synced resources used by the ECS.
|
// Register synced resources used by the ECS.
|
||||||
ecs.add_resource_synced(TimeOfDay(0.0));
|
ecs.add_resource_synced(TimeOfDay(0.0));
|
||||||
|
Reference in New Issue
Block a user