boseiju/ability_tree/event/player_action_event/
player_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 PlayerAttacksAction {
11    pub attacked_player: Option<crate::ability_tree::player::PlayerSpecifier>,
12    pub with: Option<crate::ability_tree::object::ObjectReference>,
13    #[cfg(feature = "spanned_tree")]
14    pub span: crate::ability_tree::span::TreeSpan,
15}
16
17impl AbilityTreeNode for PlayerAttacksAction {
18    fn node_id(&self) -> usize {
19        use idris::Idris;
20        crate::ability_tree::NodeKind::PlayerAttacksAction.id()
21    }
22
23    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
24        use crate::ability_tree::dummy_terminal::TreeNodeDummyTerminal;
25
26        let mut children = arrayvec::ArrayVec::new_const();
27        match self.attacked_player.as_ref() {
28            Some(child) => children.push(child as &dyn AbilityTreeNode),
29            None => children.push(TreeNodeDummyTerminal::none_node() as &dyn AbilityTreeNode),
30        }
31        match self.with.as_ref() {
32            Some(child) => children.push(child as &dyn AbilityTreeNode),
33            None => children.push(TreeNodeDummyTerminal::none_node() as &dyn AbilityTreeNode),
34        }
35
36        children
37    }
38
39    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
40        use std::io::Write;
41        write!(out, "player attacks")?;
42
43        Ok(())
44    }
45
46    fn node_tag(&self) -> &'static str {
47        "player action: attack"
48    }
49
50    #[cfg(feature = "spanned_tree")]
51    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
52        self.span
53    }
54}
55
56#[cfg(feature = "parser")]
57impl crate::utils::DummyInit for PlayerAttacksAction {
58    fn dummy_init() -> Self {
59        Self {
60            attacked_player: None,
61            with: None,
62            #[cfg(feature = "spanned_tree")]
63            span: Default::default(),
64        }
65    }
66}