forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_inventory.rs
More file actions
112 lines (100 loc) · 3.14 KB
/
Copy pathp_inventory.rs
File metadata and controls
112 lines (100 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use feather_core::inventory::{
Inventory, InventoryType, SlotIndex, SLOT_ARMOR_CHEST, SLOT_ARMOR_FEET, SLOT_ARMOR_HEAD,
SLOT_ARMOR_LEGS, SLOT_HOTBAR_OFFSET, SLOT_OFFHAND,
};
use feather_core::ItemStack;
use fecs::Entity;
use num_derive::{FromPrimitive, ToPrimitive};
use smallvec::SmallVec;
use std::ops::{Deref, DerefMut};
/// Component for storing a player's inventory.
#[derive(Clone, Debug)]
pub struct EntityInventory {
pub inventory: Inventory,
/// The player's held item.
/// This is stored as an index in the range 0..9.
pub held_item: SlotIndex,
}
impl EntityInventory {
pub fn new() -> Self {
Self {
inventory: Inventory::new(InventoryType::Player, 46),
held_item: 0,
}
}
/// Returns the item in this inventory's
/// main hand.
pub fn item_in_main_hand(&self) -> Option<&ItemStack> {
self.inventory.item_at(SLOT_HOTBAR_OFFSET + self.held_item)
}
/// Sets the item in this inventory's main hand.
pub fn set_item_in_main_hand(&mut self, item: ItemStack) {
self.inventory
.set_item_at(SLOT_HOTBAR_OFFSET + self.held_item, item);
}
}
impl Default for EntityInventory {
fn default() -> Self {
Self::new()
}
}
impl Deref for EntityInventory {
type Target = Inventory;
fn deref(&self) -> &Self::Target {
&self.inventory
}
}
impl DerefMut for EntityInventory {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inventory
}
}
/// An equipment slot, with variants
/// listed in the order of the Entity Equipment
/// IDs to allow for easy conversion using `ToPrimitive`/`FromPrimitive`.
#[derive(Debug, Clone, Copy, ToPrimitive, FromPrimitive, PartialEq, Eq, Hash)]
pub enum Equipment {
MainHand,
OffHand,
Boots,
Leggings,
Chestplate,
Helmet,
}
impl Equipment {
pub fn from_slot_index(index: SlotIndex) -> Option<Self> {
match index {
SLOT_OFFHAND => Some(Equipment::OffHand),
SLOT_ARMOR_FEET => Some(Equipment::Boots),
SLOT_ARMOR_LEGS => Some(Equipment::Leggings),
SLOT_ARMOR_CHEST => Some(Equipment::Chestplate),
SLOT_ARMOR_HEAD => Some(Equipment::Helmet),
_ => None,
}
}
pub fn slot_index(self, held_item: SlotIndex) -> SlotIndex {
match self {
Equipment::MainHand => held_item + SLOT_HOTBAR_OFFSET,
Equipment::OffHand => SLOT_OFFHAND,
Equipment::Boots => SLOT_ARMOR_FEET,
Equipment::Leggings => SLOT_ARMOR_LEGS,
Equipment::Chestplate => SLOT_ARMOR_CHEST,
Equipment::Helmet => SLOT_ARMOR_HEAD,
}
}
}
/// Event which is triggered when a player
/// updates their inventory.
///
/// This event could also be triggered when the player
/// changes their held item.
#[derive(Debug, Clone)]
pub struct InventoryUpdateEvent {
/// The slot(s) affected by the update.
///
/// Multiple slots could be affected when, for
/// example, a player uses the "drag" inventory interaction.
pub slots: SmallVec<[SlotIndex; 2]>,
/// The player owning the updated inventory.
pub player: Entity,
}