underworld_core/components/spells/
spell.rs

1#[cfg(feature = "bevy_components")]
2use bevy_ecs::prelude::Component;
3#[cfg(feature = "openapi")]
4use poem_openapi::Object;
5#[cfg(feature = "serialization")]
6use serde::{Deserialize, Serialize};
7
8use crate::components::{Attack, Defense};
9
10use super::{SpellName, SpellType};
11
12#[derive(Clone, Debug)]
13#[cfg_attr(feature = "bevy_components", derive(Component))]
14#[cfg_attr(
15    feature = "serialization",
16    derive(Deserialize, Serialize),
17    serde(rename_all = "snake_case")
18)]
19pub struct Spell {
20    pub name: SpellName,
21    #[cfg_attr(feature = "serialization", serde(default))]
22    pub attack: Option<Attack>,
23    #[cfg_attr(feature = "serialization", serde(default))]
24    pub defense: Option<Defense>,
25    pub uses: i32,
26}
27
28impl Spell {
29    pub fn damage(&self) -> i32 {
30        let mut rng = rand::thread_rng();
31
32        match &self.attack {
33            Some(attack) => attack.attack_roll(&mut rng),
34            None => 0,
35        }
36    }
37
38    pub fn spell_type(&self) -> SpellType {
39        self.name.spell_type()
40    }
41}
42
43#[derive(Clone, Debug)]
44#[cfg_attr(feature = "bevy_components", derive(Component))]
45#[cfg_attr(
46    feature = "serialization",
47    derive(Deserialize, Serialize),
48    serde(rename_all = "snake_case")
49)]
50#[cfg_attr(feature = "openapi", derive(Object), oai(rename = "Spell"))]
51pub struct SpellView {
52    pub name: SpellName,
53    pub attack: Option<Attack>,
54    pub knows_attack: bool,
55    pub defense: Option<Defense>,
56    pub knows_defense: bool,
57    pub uses: i32,
58    pub knows_uses: bool,
59    pub spell_type: SpellType,
60}