veloren_common/comp/body/
item.rs1use crate::{
2 comp::{
3 Density, Mass, Ori, ThrownItem,
4 item::{
5 Item, ItemKind, Utility,
6 armor::ArmorKind,
7 tool::{Tool, ToolKind},
8 },
9 },
10 consts::WATER_DENSITY,
11 util::Dir,
12};
13use common_base::enum_iter;
14use rand::prelude::*;
15use serde::{Deserialize, Serialize};
16use std::f32::consts::PI;
17use strum::IntoEnumIterator;
18use vek::Vec3;
19
20enum_iter! {
21 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
22 #[repr(u32)]
23 pub enum ItemArmorKind {
24 Shoulder = 0,
25 Chest = 1,
26 Belt = 2,
27 Hand = 3,
28 Pants = 4,
29 Foot = 5,
30 Back = 6,
31 Ring = 7,
32 Neck = 8,
33 Head = 9,
34 Tabard = 10,
35 Bag = 11,
36 }
37}
38
39enum_iter! {
40 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
41 #[repr(u32)]
42 pub enum Body {
43 Tool(ToolKind) = 0,
44 ModularComponent = 1,
45 Lantern = 2,
46 Glider = 3,
47 Armor(ItemArmorKind) = 4,
48 Utility = 5,
49 Consumable = 6,
50 Throwable = 7,
51 Ingredient = 8,
52 Coins = 9,
53 CoinPouch = 10,
54 Empty = 11,
55 Thrown(ToolKind) = 12,
56 }
57}
58
59impl From<Body> for super::Body {
60 fn from(body: Body) -> Self { super::Body::Item(body) }
61}
62
63impl From<&Item> for Body {
64 fn from(item: &Item) -> Self {
65 match &*item.kind() {
66 ItemKind::Tool(Tool { kind, .. }) => Body::Tool(*kind),
67 ItemKind::ModularComponent(_) => Body::ModularComponent,
68 ItemKind::Lantern(_) => Body::Lantern,
69 ItemKind::Glider => Body::Glider,
70 ItemKind::Armor(armor) => match armor.kind {
71 ArmorKind::Shoulder => Body::Armor(ItemArmorKind::Shoulder),
72 ArmorKind::Chest => Body::Armor(ItemArmorKind::Chest),
73 ArmorKind::Belt => Body::Armor(ItemArmorKind::Belt),
74 ArmorKind::Hand => Body::Armor(ItemArmorKind::Hand),
75 ArmorKind::Pants => Body::Armor(ItemArmorKind::Pants),
76 ArmorKind::Foot => Body::Armor(ItemArmorKind::Foot),
77 ArmorKind::Back => Body::Armor(ItemArmorKind::Back),
78 ArmorKind::Backpack => Body::Armor(ItemArmorKind::Back),
79 ArmorKind::Ring => Body::Armor(ItemArmorKind::Ring),
80 ArmorKind::Neck => Body::Armor(ItemArmorKind::Neck),
81 ArmorKind::Head => Body::Armor(ItemArmorKind::Head),
82 ArmorKind::Tabard => Body::Armor(ItemArmorKind::Tabard),
83 ArmorKind::Bag => Body::Armor(ItemArmorKind::Bag),
84 },
85 ItemKind::Utility { kind, .. } => match kind {
86 Utility::Coins => {
87 if item.amount() > 100 {
88 Body::CoinPouch
89 } else {
90 Body::Coins
91 }
92 },
93 _ => Body::Utility,
94 },
95 ItemKind::Consumable { .. } => Body::Consumable,
96 ItemKind::Ingredient { .. } => Body::Ingredient,
97 _ => Body::Empty,
98 }
99 }
100}
101
102impl From<&ThrownItem> for Body {
103 fn from(thrown_item: &ThrownItem) -> Self {
104 match &*thrown_item.0.kind() {
105 ItemKind::Tool(Tool { kind, .. }) => Body::Thrown(*kind),
106 _ => Body::Empty,
107 }
108 }
109}
110
111impl Body {
112 pub fn to_string(self) -> &'static str {
113 match self {
114 Body::Tool(_) => "tool",
115 Body::ModularComponent => "modular_component",
116 Body::Lantern => "lantern",
117 Body::Glider => "glider",
118 Body::Armor(_) => "armor",
119 Body::Utility => "utility",
120 Body::Consumable => "consumable",
121 Body::Throwable => "throwable",
122 Body::Ingredient => "ingredient",
123 Body::Coins => "coins",
124 Body::CoinPouch => "coin_pouch",
125 Body::Empty => "empty",
126 Body::Thrown(_) => "thrown",
127 }
128 }
129
130 pub fn density(&self) -> Density { Density(1.1 * WATER_DENSITY) }
131
132 pub fn mass(&self) -> Mass { Mass(2.0) }
133
134 pub fn dimensions(&self) -> Vec3<f32> { Vec3::new(0.0, 0.1, 0.0) }
135
136 pub fn orientation(&self, rng: &mut impl Rng) -> Ori {
137 let random = rng.gen_range(-1.0..1.0f32);
138 let default = Ori::default();
139 match self {
140 Body::Tool(_) | Body::Thrown(_) => default
141 .pitched_down(PI / 2.0)
142 .yawed_left(PI / 2.0)
143 .pitched_towards(
144 Dir::from_unnormalized(Vec3::new(
145 random,
146 rng.gen_range(-1.0..1.0f32),
147 rng.gen_range(-1.0..1.0f32),
148 ))
149 .unwrap_or_default(),
150 ),
151
152 Body::Armor(ItemArmorKind::Neck | ItemArmorKind::Back | ItemArmorKind::Tabard) => {
153 default.yawed_left(random).pitched_down(PI / 2.0)
154 },
155 _ => default.yawed_left(random),
156 }
157 }
158}