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

237 lines
7.6 KiB
Rust
Raw Normal View History

use crate::{
assets::{self, Asset},
comp::{
body::{humanoid, object},
projectile, Body, CharacterAbility, HealthChange, HealthSource, Projectile,
},
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;
2020-03-12 14:47:27 +00:00
use std::{fs::File, io::BufReader, time::Duration, vec::Vec};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
2019-12-29 23:47:42 +00:00
pub enum SwordKind {
Scimitar,
Rapier,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ToolKind {
Sword(SwordKind),
2019-06-28 23:42:51 +00:00
Axe,
Hammer,
Bow,
Dagger,
2019-06-28 23:42:51 +00:00
Staff,
Shield,
2020-03-14 15:40:29 +00:00
Debug(DebugKind),
2020-01-05 23:17:22 +00:00
}
2019-12-29 23:47:42 +00:00
impl ToolData {
pub fn equip_time(&self) -> Duration { Duration::from_millis(self.equip_time_millis) }
2020-03-14 15:40:29 +00:00
pub fn get_abilities(&self) -> Vec<CharacterAbility> {
use CharacterAbility::*;
use DebugKind::*;
2020-03-12 14:47:27 +00:00
use ToolKind::*;
match self.kind {
Sword(_) => vec![BasicMelee {
buildup_duration: Duration::from_millis(100),
recover_duration: Duration::from_millis(500),
base_damage: 60,
}],
Axe => vec![BasicMelee {
buildup_duration: Duration::from_millis(700),
recover_duration: Duration::from_millis(100),
base_damage: 80,
2020-03-14 15:40:29 +00:00
}],
Hammer => vec![BasicMelee {
buildup_duration: Duration::from_millis(700),
recover_duration: Duration::from_millis(300),
base_damage: 100,
2020-03-14 15:40:29 +00:00
}],
Bow => vec![BasicRanged {
projectile: Projectile {
hit_ground: vec![projectile::Effect::Stick],
hit_wall: vec![projectile::Effect::Stick],
hit_entity: vec![
projectile::Effect::Damage(HealthChange {
// TODO: This should not be fixed (?)
amount: -30,
cause: HealthSource::Item,
}),
projectile::Effect::Vanish,
],
time_left: Duration::from_secs(15),
owner: None,
},
projectile_body: Body::Object(object::Body::Arrow),
recover_duration: Duration::from_millis(300),
}],
Dagger => vec![BasicMelee {
buildup_duration: Duration::from_millis(100),
recover_duration: Duration::from_millis(400),
base_damage: 50,
2020-03-14 15:40:29 +00:00
}],
Staff => vec![BasicMelee {
buildup_duration: Duration::from_millis(400),
recover_duration: Duration::from_millis(300),
base_damage: 70,
2020-03-14 15:40:29 +00:00
}],
2020-03-16 12:40:14 +00:00
Shield => vec![BasicBlock],
2020-03-14 15:40:29 +00:00
Debug(kind) => match kind {
DebugKind::Boost => vec![
CharacterAbility::Boost {
duration: Duration::from_millis(100),
only_up: false,
},
CharacterAbility::Boost {
duration: Duration::from_millis(100),
only_up: true,
},
],
2020-03-14 15:40:29 +00:00
Possess => vec![],
2020-03-12 14:47:27 +00:00
},
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
2020-03-14 15:40:29 +00:00
pub enum DebugKind {
Boost,
Possess,
2019-06-28 23:42:51 +00:00
}
2019-07-29 13:44:16 +00:00
2020-01-05 23:21:37 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Armor {
Shoulder(humanoid::Shoulder),
Chest(humanoid::Chest),
Belt(humanoid::Belt),
Hand(humanoid::Hand),
Pants(humanoid::Pants),
Foot(humanoid::Foot),
}
pub type ArmorStats = u32;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
2019-09-25 14:56:57 +00:00
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
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
2020-01-29 12:01:28 +00:00
pub enum Utility {
Collar,
}
2020-01-05 23:21:37 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
2019-09-25 22:04:18 +00:00
pub enum Ingredient {
Flower,
Grass,
}
2020-03-14 15:40:29 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
2019-12-29 23:47:42 +00:00
pub struct ToolData {
pub kind: ToolKind,
equip_time_millis: u64,
// TODO: item specific abilities
2019-12-29 23:47:42 +00:00
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ItemKind {
/// Something wieldable
2019-12-29 23:47:42 +00:00
Tool(ToolData),
Armor {
kind: Armor,
stats: ArmorStats,
},
Consumable {
kind: Consumable,
effect: Effect,
},
Utility {
kind: Utility,
},
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 }
pub fn description(&self) -> &str { &self.description }
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",
2020-01-29 12:01:28 +00:00
"common.items.collar",
"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>>;
}