veloren/voxygen/src/hud/slots.rs

194 lines
6.2 KiB
Rust
Raw Normal View History

2020-04-11 06:33:06 +00:00
use super::{
hotbar::{self, Slot as HotbarSlot},
img_ids,
item_imgs::{ItemImgs, ItemKey},
};
2020-04-06 15:25:45 +00:00
use crate::ui::slot::{self, SlotKey, SumSlot};
use common::comp::{
item::{
tool::{AbilityMap, ToolKind},
ItemKind,
},
slot::InvSlotId,
Energy, Inventory,
};
2020-04-11 06:33:06 +00:00
use conrod_core::{image, Color};
pub use common::comp::slot::{ArmorSlot, EquipSlot};
#[derive(Clone, Copy, Debug, PartialEq)]
2020-04-06 15:25:45 +00:00
pub enum SlotKind {
Inventory(InventorySlot),
Equip(EquipSlot),
2020-04-11 06:33:06 +00:00
Hotbar(HotbarSlot),
Trade(TradeSlot),
2020-04-11 06:33:06 +00:00
/* Spellbook(SpellbookSlot), TODO */
}
2020-04-06 15:25:45 +00:00
pub type SlotManager = slot::SlotManager<SlotKind>;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct InventorySlot(pub InvSlotId);
2020-04-06 15:25:45 +00:00
impl SlotKey<Inventory, ItemImgs> for InventorySlot {
type ImageKey = ItemKey;
2020-04-11 06:33:06 +00:00
fn image_key(&self, source: &Inventory) -> Option<(Self::ImageKey, Option<Color>)> {
source.get(self.0).map(|i| (i.into(), None))
}
2020-04-06 15:25:45 +00:00
fn amount(&self, source: &Inventory) -> Option<u32> {
source
.get(self.0)
.map(|item| item.amount())
.filter(|amount| *amount > 1)
}
2020-04-06 15:25:45 +00:00
fn image_id(key: &Self::ImageKey, source: &ItemImgs) -> image::Id {
source.img_id_or_not_found_img(key.clone())
}
2020-04-04 17:51:41 +00:00
}
impl SlotKey<Inventory, ItemImgs> for EquipSlot {
2020-04-04 17:51:41 +00:00
type ImageKey = ItemKey;
fn image_key(&self, source: &Inventory) -> Option<(Self::ImageKey, Option<Color>)> {
let item = source.equipped(*self);
item.map(|i| (i.into(), None))
2020-04-04 17:51:41 +00:00
}
fn amount(&self, _: &Inventory) -> Option<u32> { None }
2020-04-04 17:51:41 +00:00
2020-04-06 15:25:45 +00:00
fn image_id(key: &Self::ImageKey, source: &ItemImgs) -> image::Id {
2020-04-04 17:51:41 +00:00
source.img_id_or_not_found_img(key.clone())
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TradeSlot {
pub index: usize,
pub quantity: u32,
pub invslot: Option<InvSlotId>,
}
impl SlotKey<Inventory, ItemImgs> for TradeSlot {
type ImageKey = ItemKey;
fn image_key(&self, source: &Inventory) -> Option<(Self::ImageKey, Option<Color>)> {
self.invslot
.and_then(|inv_id| InventorySlot(inv_id).image_key(source))
}
fn amount(&self, source: &Inventory) -> Option<u32> {
self.invslot
.and_then(|inv_id| InventorySlot(inv_id).amount(source))
.map(|x| x.min(self.quantity))
}
fn image_id(key: &Self::ImageKey, source: &ItemImgs) -> image::Id {
source.img_id_or_not_found_img(key.clone())
}
}
2020-04-11 06:33:06 +00:00
#[derive(Clone, PartialEq)]
pub enum HotbarImage {
Item(ItemKey),
2020-10-08 20:54:12 +00:00
FireAoe,
SnakeArrow,
SwordWhirlwind,
2020-09-21 20:45:50 +00:00
HammerLeap,
AxeLeapSlash,
BowJumpBurst,
2020-04-11 06:33:06 +00:00
}
2020-04-06 15:25:45 +00:00
type HotbarSource<'a> = (&'a hotbar::State, &'a Inventory, &'a Energy, &'a AbilityMap);
2020-04-11 06:33:06 +00:00
type HotbarImageSource<'a> = (&'a ItemImgs, &'a img_ids::Imgs);
impl<'a> SlotKey<HotbarSource<'a>, HotbarImageSource<'a>> for HotbarSlot {
type ImageKey = HotbarImage;
fn image_key(
&self,
(hotbar, inventory, energy, ability_map): &HotbarSource<'a>,
2020-04-11 06:33:06 +00:00
) -> Option<(Self::ImageKey, Option<Color>)> {
hotbar.get(*self).and_then(|contents| match contents {
hotbar::SlotContents::Inventory(idx) => inventory
.get(idx)
.map(|item| HotbarImage::Item(item.into()))
2020-04-11 06:33:06 +00:00
.map(|i| (i, None)),
hotbar::SlotContents::Ability3 => {
let tool = match inventory.equipped(EquipSlot::Mainhand).map(|i| i.kind()) {
Some(ItemKind::Tool(tool)) => Some(tool),
_ => None,
};
tool.and_then(|tool| {
match tool.kind {
ToolKind::Staff => Some(HotbarImage::FireAoe),
ToolKind::Hammer => Some(HotbarImage::HammerLeap),
ToolKind::Axe => Some(HotbarImage::AxeLeapSlash),
ToolKind::Bow => Some(HotbarImage::BowJumpBurst),
ToolKind::Debug => Some(HotbarImage::SnakeArrow),
ToolKind::Sword => Some(HotbarImage::SwordWhirlwind),
_ => None,
}
.map(|i| {
(
i,
if let Some(skill) = tool.get_abilities(ability_map).skills.get(0) {
if energy.current() >= skill.get_energy_cost() {
Some(Color::Rgba(1.0, 1.0, 1.0, 1.0))
} else {
Some(Color::Rgba(0.3, 0.3, 0.3, 0.8))
}
} else {
Some(Color::Rgba(1.0, 1.0, 1.0, 1.0))
},
)
})
})
},
2020-04-11 06:33:06 +00:00
})
2020-04-06 15:25:45 +00:00
}
fn amount(&self, (hotbar, inventory, _, _): &HotbarSource<'a>) -> Option<u32> {
2020-04-11 06:33:06 +00:00
hotbar
.get(*self)
.and_then(|content| match content {
hotbar::SlotContents::Inventory(idx) => inventory.get(idx),
hotbar::SlotContents::Ability3 => None,
})
.map(|item| item.amount())
2020-04-06 15:25:45 +00:00
.filter(|amount| *amount > 1)
}
2020-04-11 06:33:06 +00:00
fn image_id(key: &Self::ImageKey, (item_imgs, imgs): &HotbarImageSource<'a>) -> image::Id {
match key {
HotbarImage::Item(key) => item_imgs.img_id_or_not_found_img(key.clone()),
HotbarImage::SnakeArrow => imgs.snake_arrow_0,
2020-10-08 20:54:12 +00:00
HotbarImage::FireAoe => imgs.fire_aoe,
2020-09-21 22:38:01 +00:00
HotbarImage::SwordWhirlwind => imgs.sword_whirlwind,
2020-09-21 20:45:50 +00:00
HotbarImage::HammerLeap => imgs.hammerleap,
HotbarImage::AxeLeapSlash => imgs.skill_axe_leap_slash,
HotbarImage::BowJumpBurst => imgs.skill_bow_jump_burst,
2020-04-11 06:33:06 +00:00
}
2020-04-06 15:25:45 +00:00
}
2020-04-11 06:33:06 +00:00
}
2020-04-06 15:25:45 +00:00
impl From<InventorySlot> for SlotKind {
fn from(inventory: InventorySlot) -> Self { Self::Inventory(inventory) }
}
impl From<EquipSlot> for SlotKind {
fn from(equip: EquipSlot) -> Self { Self::Equip(equip) }
}
2020-04-11 06:33:06 +00:00
impl From<HotbarSlot> for SlotKind {
fn from(hotbar: HotbarSlot) -> Self { Self::Hotbar(hotbar) }
}
impl From<TradeSlot> for SlotKind {
fn from(trade: TradeSlot) -> Self { Self::Trade(trade) }
}
2020-04-06 15:25:45 +00:00
impl SumSlot for SlotKind {}