boseiju/ability_tree/event/
creature_action_event.rs

1mod creature_attacks_action;
2mod creature_deals_combat_damage_action;
3mod creature_dies_action;
4
5pub use creature_attacks_action::CreatureAttacksAction;
6pub use creature_deals_combat_damage_action::CreatureDealsCombatDamageAction;
7pub use creature_dies_action::CreatureDiesAction;
8
9use crate::ability_tree::AbilityTreeNode;
10use crate::ability_tree::MAX_CHILDREN_PER_NODE;
11
12/// Events related to creatures.
13///
14/// These events are grouped together, but maybe they should not ?
15/// From a human perspective, it makes it easier to classify them this way.
16///
17/// Creature events includes attacking, dealing damages, blocking, etc.
18#[derive(serde::Serialize, serde::Deserialize)]
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct CreatureActionEvent {
21    pub creatures: crate::ability_tree::object::ObjectReference,
22    pub action: CreatureAction,
23    #[cfg(feature = "spanned_tree")]
24    pub span: crate::ability_tree::span::TreeSpan,
25}
26
27impl AbilityTreeNode for CreatureActionEvent {
28    fn node_id(&self) -> usize {
29        use idris::Idris;
30        crate::ability_tree::NodeKind::CreatureActionEvent.id()
31    }
32
33    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
34        let mut children = arrayvec::ArrayVec::new_const();
35        children.push(&self.creatures as &dyn AbilityTreeNode);
36        children.push(&self.action as &dyn AbilityTreeNode);
37        children
38    }
39
40    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
41        use std::io::Write;
42        write!(out, "creature action")?;
43        out.push_inter_branch()?;
44        write!(out, "creatures:")?;
45        out.push_final_branch()?;
46        self.creatures.display(out)?;
47        out.pop_branch();
48        out.next_final_branch()?;
49        write!(out, "action:")?;
50        out.push_final_branch()?;
51        self.action.display(out)?;
52        out.pop_branch();
53        out.pop_branch();
54        Ok(())
55    }
56
57    fn node_tag(&self) -> &'static str {
58        "creature action event"
59    }
60
61    #[cfg(feature = "spanned_tree")]
62    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
63        self.span
64    }
65}
66
67#[cfg(feature = "parser")]
68impl crate::utils::DummyInit for CreatureActionEvent {
69    fn dummy_init() -> Self {
70        Self {
71            creatures: crate::utils::dummy(),
72            action: crate::utils::dummy(),
73            #[cfg(feature = "spanned_tree")]
74            span: Default::default(),
75        }
76    }
77}
78
79#[derive(serde::Serialize, serde::Deserialize)]
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub enum CreatureAction {
82    Attacks(CreatureAttacksAction),
83    DealsCombatDamage(CreatureDealsCombatDamageAction),
84    Dies(CreatureDiesAction),
85}
86
87#[cfg(feature = "spanned_tree")]
88impl CreatureAction {
89    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
90        match self {
91            Self::Attacks(child) => child.span,
92            Self::DealsCombatDamage(child) => child.span,
93            Self::Dies(child) => child.span,
94        }
95    }
96}
97
98impl AbilityTreeNode for CreatureAction {
99    fn node_id(&self) -> usize {
100        use idris::Idris;
101        crate::ability_tree::NodeKind::CreatureAction.id()
102    }
103
104    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
105        let mut children = arrayvec::ArrayVec::new_const();
106        match self {
107            Self::Attacks(child) => children.push(child as &dyn AbilityTreeNode),
108            Self::DealsCombatDamage(child) => children.push(child as &dyn AbilityTreeNode),
109            Self::Dies(child) => children.push(child as &dyn AbilityTreeNode),
110        }
111        children
112    }
113
114    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
115        use std::io::Write;
116        write!(out, "creature action")?;
117        out.push_final_branch()?;
118        match self {
119            Self::Attacks(action) => action.display(out)?,
120            Self::DealsCombatDamage(action) => action.display(out)?,
121            Self::Dies(action) => action.display(out)?,
122        }
123        out.pop_branch();
124        Ok(())
125    }
126
127    fn node_tag(&self) -> &'static str {
128        "creature action"
129    }
130
131    #[cfg(feature = "spanned_tree")]
132    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
133        match self {
134            Self::Attacks(child) => child.node_span(),
135            Self::DealsCombatDamage(child) => child.node_span(),
136            Self::Dies(child) => child.node_span(),
137        }
138    }
139}
140
141#[cfg(feature = "parser")]
142impl crate::utils::DummyInit for CreatureAction {
143    fn dummy_init() -> Self {
144        Self::DealsCombatDamage(crate::utils::dummy())
145    }
146}