veloren_common/comp/body/
item_drop.rs

1use crate::{
2    comp::{
3        Density, Mass, Ori,
4        item::{
5            Item, ItemKind, Utility,
6            armor::ArmorKind,
7            tool::{Tool, ToolKind},
8        },
9    },
10    consts::WATER_DENSITY,
11    make_case_elim,
12    util::Dir,
13};
14use rand::prelude::*;
15use serde::{Deserialize, Serialize};
16use std::f32::consts::PI;
17use vek::Vec3;
18
19make_case_elim!(
20    armor,
21    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
22    #[repr(u32)]
23    pub enum ItemDropArmorKind {
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
39make_case_elim!(
40    body,
41    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
42    #[repr(u32)]
43    pub enum Body {
44        Tool(tool: ToolKind) = 0,
45        ModularComponent = 1,
46        Lantern = 2,
47        Glider = 3,
48        Armor(armor: ItemDropArmorKind) = 4,
49        Utility = 5,
50        Consumable = 6,
51        Throwable = 7,
52        Ingredient = 8,
53        Coins = 9,
54        CoinPouch = 10,
55        Empty = 11,
56    }
57);
58
59impl From<Body> for super::Body {
60    fn from(body: Body) -> Self { super::Body::ItemDrop(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(ItemDropArmorKind::Shoulder),
72                ArmorKind::Chest => Body::Armor(ItemDropArmorKind::Chest),
73                ArmorKind::Belt => Body::Armor(ItemDropArmorKind::Belt),
74                ArmorKind::Hand => Body::Armor(ItemDropArmorKind::Hand),
75                ArmorKind::Pants => Body::Armor(ItemDropArmorKind::Pants),
76                ArmorKind::Foot => Body::Armor(ItemDropArmorKind::Foot),
77                ArmorKind::Back => Body::Armor(ItemDropArmorKind::Back),
78                ArmorKind::Backpack => Body::Armor(ItemDropArmorKind::Back),
79                ArmorKind::Ring => Body::Armor(ItemDropArmorKind::Ring),
80                ArmorKind::Neck => Body::Armor(ItemDropArmorKind::Neck),
81                ArmorKind::Head => Body::Armor(ItemDropArmorKind::Head),
82                ArmorKind::Tabard => Body::Armor(ItemDropArmorKind::Tabard),
83                ArmorKind::Bag => Body::Armor(ItemDropArmorKind::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::Throwable { .. } => Body::Throwable,
97            ItemKind::Ingredient { .. } => Body::Ingredient,
98            _ => Body::Empty,
99        }
100    }
101}
102
103impl Body {
104    pub fn to_string(self) -> &'static str {
105        match self {
106            Body::Tool(_) => "tool",
107            Body::ModularComponent => "modular_component",
108            Body::Lantern => "lantern",
109            Body::Glider => "glider",
110            Body::Armor(_) => "armor",
111            Body::Utility => "utility",
112            Body::Consumable => "consumable",
113            Body::Throwable => "throwable",
114            Body::Ingredient => "ingredient",
115            Body::Coins => "coins",
116            Body::CoinPouch => "coin_pouch",
117            Body::Empty => "empty",
118        }
119    }
120
121    pub fn density(&self) -> Density { Density(1.1 * WATER_DENSITY) }
122
123    pub fn mass(&self) -> Mass { Mass(2.0) }
124
125    pub fn dimensions(&self) -> Vec3<f32> { Vec3::new(0.0, 0.1, 0.0) }
126
127    pub fn orientation(&self, rng: &mut impl Rng) -> Ori {
128        let random = rng.gen_range(-1.0..1.0f32);
129        let default = Ori::default();
130        match self {
131            Body::Tool(_) => default
132                .pitched_down(PI / 2.0)
133                .yawed_left(PI / 2.0)
134                .pitched_towards(
135                    Dir::from_unnormalized(Vec3::new(
136                        random,
137                        rng.gen_range(-1.0..1.0f32),
138                        rng.gen_range(-1.0..1.0f32),
139                    ))
140                    .unwrap_or_default(),
141                ),
142
143            Body::Armor(
144                ItemDropArmorKind::Neck | ItemDropArmorKind::Back | ItemDropArmorKind::Tabard,
145            ) => default.yawed_left(random).pitched_down(PI / 2.0),
146            _ => default.yawed_left(random),
147        }
148    }
149}