boseiju/ability_tree/event/creature_action_event/
creature_attacks_action.rs

1use crate::ability_tree::AbilityTreeNode;
2use crate::ability_tree::MAX_CHILDREN_PER_NODE;
3
4/// Action for a creature to deal combat damage.
5///
6/// Combat damage is the special kind of damage that creature deals when
7/// they fight each other, or when they attack a player.
8#[derive(serde::Serialize, serde::Deserialize)]
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct CreatureAttacksAction {
11    pub attacked_player: Option<crate::ability_tree::player::PlayerSpecifier>,
12    #[cfg(feature = "spanned_tree")]
13    pub span: crate::ability_tree::span::TreeSpan,
14}
15
16impl AbilityTreeNode for CreatureAttacksAction {
17    fn node_id(&self) -> usize {
18        use idris::Idris;
19        crate::ability_tree::NodeKind::CreatureAttacksAction.id()
20    }
21
22    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
23        use crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal;
24
25        let mut children = arrayvec::ArrayVec::new_const();
26        match self.attacked_player.as_ref() {
27            Some(child) => children.push(child as &dyn AbilityTreeNode),
28            None => children.push(TreeNodeDummyTerminal::none_node() as &dyn AbilityTreeNode),
29        }
30
31        children
32    }
33
34    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
35        use std::io::Write;
36        write!(out, "creature attacks")?;
37
38        Ok(())
39    }
40
41    fn node_tag(&self) -> &'static str {
42        "creature action: attack"
43    }
44
45    #[cfg(feature = "spanned_tree")]
46    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
47        self.span
48    }
49}
50
51#[cfg(feature = "parser")]
52impl crate::utils::DummyInit for CreatureAttacksAction {
53    fn dummy_init() -> Self {
54        Self {
55            attacked_player: None,
56            #[cfg(feature = "spanned_tree")]
57            span: Default::default(),
58        }
59    }
60}