veloren/common/src/comp/inventory/item.rs

185 lines
6.0 KiB
Rust
Raw Normal View History

use crate::{
assets::{self, Asset},
effect::Effect,
terrain::{Block, BlockKind},
};
//use rand::prelude::*;
use rand::seq::SliceRandom;
2019-07-26 21:01:41 +00:00
use specs::{Component, FlaggedStorage};
use specs_idvs::IDVStorage;
use std::fs::File;
use std::io::BufReader;
use std::time::Duration;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
2019-07-29 13:44:16 +00:00
pub enum Tool {
2019-06-28 23:42:51 +00:00
Sword,
Axe,
Hammer,
Bow,
Dagger,
2019-06-28 23:42:51 +00:00
Staff,
Shield,
Debug(Debug),
}
// TODO: Allow override in item ron?
impl Tool {
pub fn wield_duration(&self) -> Duration {
match self {
Tool::Sword => Duration::from_millis(800),
Tool::Axe => Duration::from_millis(1000),
Tool::Hammer => Duration::from_millis(1000),
Tool::Bow => Duration::from_millis(800),
Tool::Dagger => Duration::from_millis(300),
Tool::Staff => Duration::from_millis(800),
Tool::Shield => Duration::from_millis(1000),
Tool::Debug(_) => Duration::from_millis(0),
}
}
pub fn attack_buildup_duration(&self) -> Duration {
match self {
Tool::Sword => Duration::from_millis(100),
Tool::Axe => Duration::from_millis(700),
Tool::Hammer => Duration::from_millis(700),
Tool::Bow => Duration::from_millis(0),
Tool::Dagger => Duration::from_millis(100),
Tool::Staff => Duration::from_millis(400),
Tool::Shield => Duration::from_millis(100),
Tool::Debug(_) => Duration::from_millis(0),
}
}
pub fn attack_recover_duration(&self) -> Duration {
match self {
Tool::Sword => Duration::from_millis(500),
Tool::Axe => Duration::from_millis(100),
Tool::Hammer => Duration::from_millis(100),
Tool::Bow => Duration::from_millis(800),
Tool::Dagger => Duration::from_millis(400),
Tool::Staff => Duration::from_millis(300),
Tool::Shield => Duration::from_millis(1000),
Tool::Debug(_) => Duration::from_millis(0),
}
}
pub fn attack_duration(&self) -> Duration {
self.attack_buildup_duration() + self.attack_recover_duration()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Debug {
Boost,
Possess,
2019-06-28 23:42:51 +00:00
}
2019-07-29 13:44:16 +00:00
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Armor {
2019-10-09 19:28:05 +00:00
// TODO: Don't make armor be a body part. Wearing enemy's head is funny but also a creepy thing to do.
Helmet,
2019-06-28 23:42:51 +00:00
Shoulders,
Chestplate,
Belt,
Gloves,
Pants,
Boots,
Back,
Tabard,
Gem,
Necklace,
}
//TODO: Do we even need this?
2019-09-25 14:56:57 +00:00
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Consumable {
Apple,
Cheese,
2019-09-25 14:56:57 +00:00
Potion,
Mushroom,
2019-09-25 20:22:39 +00:00
Velorite,
VeloriteFrag,
PotionMinor,
2019-09-25 14:56:57 +00:00
}
2019-09-25 22:04:18 +00:00
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Ingredient {
Flower,
Grass,
}
2019-08-30 20:42:43 +00:00
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ItemKind {
Tool { kind: Tool, power: u32 },
Armor { kind: Armor, power: u32 },
Consumable { kind: Consumable, effect: Effect },
Ingredient(Ingredient),
2019-07-29 13:44:16 +00:00
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Item {
name: String,
description: String,
pub kind: ItemKind,
}
2019-10-09 19:28:05 +00:00
impl Asset for Item {
const ENDINGS: &'static [&'static str] = &["ron"];
fn parse(buf_reader: BufReader<File>) -> Result<Self, assets::Error> {
Ok(ron::de::from_reader(buf_reader).unwrap())
2019-10-09 19:28:05 +00:00
}
}
2019-10-09 19:28:05 +00:00
impl Item {
pub fn name(&self) -> &str {
&self.name
2019-07-29 13:44:16 +00:00
}
pub fn description(&self) -> &str {
&self.description
2019-07-29 13:44:16 +00:00
}
2019-09-25 14:56:57 +00:00
pub fn try_reclaim_from_block(block: Block) -> Option<Self> {
match block.kind() {
BlockKind::Apple => Some(assets::load_expect_cloned("common.items.apple")),
BlockKind::Mushroom => Some(assets::load_expect_cloned("common.items.mushroom")),
BlockKind::Velorite => Some(assets::load_expect_cloned("common.items.velorite")),
BlockKind::BlueFlower => Some(assets::load_expect_cloned("common.items.flowers.blue")),
BlockKind::PinkFlower => Some(assets::load_expect_cloned("common.items.flowers.pink")),
BlockKind::PurpleFlower => {
Some(assets::load_expect_cloned("common.items.flowers.purple"))
}
BlockKind::RedFlower => Some(assets::load_expect_cloned("common.items.flowers.red")),
BlockKind::WhiteFlower => {
Some(assets::load_expect_cloned("common.items.flowers.white"))
}
BlockKind::YellowFlower => {
Some(assets::load_expect_cloned("common.items.flowers.yellow"))
}
BlockKind::Sunflower => Some(assets::load_expect_cloned("common.items.flowers.sun")),
BlockKind::LongGrass => Some(assets::load_expect_cloned("common.items.grasses.long")),
BlockKind::MediumGrass => {
Some(assets::load_expect_cloned("common.items.grasses.medium"))
}
BlockKind::ShortGrass => Some(assets::load_expect_cloned("common.items.grasses.short")),
BlockKind::Chest => Some(assets::load_expect_cloned(
[
"common.items.apple",
"common.items.velorite",
"common.items.veloritefrag",
"common.items.cheese",
"common.items.potion_minor",
"common.items.weapons.starter_sword",
"common.items.weapons.starter_axe",
"common.items.weapons.starter_hammer",
"common.items.weapons.starter_bow",
"common.items.weapons.starter_staff",
]
.choose(&mut rand::thread_rng())
.unwrap(), // Can't fail
)),
2019-09-25 14:56:57 +00:00
_ => None,
}
}
2019-07-25 22:52:28 +00:00
}
impl Component for Item {
2019-07-26 21:01:41 +00:00
type Storage = FlaggedStorage<Self, IDVStorage<Self>>;
}