boseiju/ability_tree/event/
player_action_event.rs

1mod player_attacks_action;
2mod player_casts_spell_action;
3
4pub use player_attacks_action::PlayerAttacksAction;
5pub use player_casts_spell_action::PlayerCastsSpellEvent;
6
7use crate::ability_tree::AbilityTreeNode;
8use crate::ability_tree::MAX_CHILDREN_PER_NODE;
9
10/// Events related to Player foing actions.
11///
12/// Player events includes attacking, drawing cards, etc.
13#[derive(serde::Serialize, serde::Deserialize)]
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct PlayerActionEvent {
16    pub player: crate::ability_tree::player::PlayerSpecifier,
17    pub action: PlayerAction,
18    #[cfg(feature = "spanned_tree")]
19    pub span: crate::ability_tree::span::TreeSpan,
20}
21
22impl AbilityTreeNode for PlayerActionEvent {
23    fn node_id(&self) -> usize {
24        use idris::Idris;
25        crate::ability_tree::NodeKind::PlayerActionEvent.id()
26    }
27
28    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
29        let mut children = arrayvec::ArrayVec::new_const();
30        children.push(&self.player as &dyn AbilityTreeNode);
31        children.push(&self.action as &dyn AbilityTreeNode);
32        children
33    }
34
35    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
36        use std::io::Write;
37        write!(out, "player action")?;
38        out.push_inter_branch()?;
39        write!(out, "player:")?;
40        out.push_final_branch()?;
41        self.player.display(out)?;
42        out.pop_branch();
43        out.next_final_branch()?;
44        write!(out, "action:")?;
45        out.push_final_branch()?;
46        self.action.display(out)?;
47        out.pop_branch();
48        out.pop_branch();
49        Ok(())
50    }
51
52    fn node_tag(&self) -> &'static str {
53        "player action event"
54    }
55
56    #[cfg(feature = "spanned_tree")]
57    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
58        self.span
59    }
60}
61
62#[cfg(feature = "parser")]
63impl crate::utils::DummyInit for PlayerActionEvent {
64    fn dummy_init() -> Self {
65        Self {
66            player: crate::utils::dummy(),
67            action: crate::utils::dummy(),
68            #[cfg(feature = "spanned_tree")]
69            span: Default::default(),
70        }
71    }
72}
73
74#[derive(serde::Serialize, serde::Deserialize)]
75#[derive(Debug, Clone, PartialEq, Eq)]
76pub enum PlayerAction {
77    Attacks(PlayerAttacksAction),
78    CastsSpell(PlayerCastsSpellEvent),
79}
80
81#[cfg(feature = "spanned_tree")]
82impl PlayerAction {
83    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
84        match self {
85            Self::Attacks(child) => child.span,
86            Self::CastsSpell(child) => child.span,
87        }
88    }
89}
90
91impl AbilityTreeNode for PlayerAction {
92    fn node_id(&self) -> usize {
93        use idris::Idris;
94        crate::ability_tree::NodeKind::PlayerAction.id()
95    }
96
97    fn children(&self) -> arrayvec::ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE> {
98        let mut children = arrayvec::ArrayVec::new_const();
99        match self {
100            Self::Attacks(child) => children.push(child as &dyn AbilityTreeNode),
101            Self::CastsSpell(child) => children.push(child as &dyn AbilityTreeNode),
102        }
103        children
104    }
105
106    fn display(&self, out: &mut crate::utils::TreeFormatter<'_>) -> std::io::Result<()> {
107        use std::io::Write;
108        write!(out, "player action")?;
109        out.push_final_branch()?;
110        match self {
111            Self::Attacks(action) => action.display(out)?,
112            Self::CastsSpell(action) => action.display(out)?,
113        }
114        out.pop_branch();
115        Ok(())
116    }
117
118    fn node_tag(&self) -> &'static str {
119        "player action"
120    }
121
122    #[cfg(feature = "spanned_tree")]
123    fn node_span(&self) -> crate::ability_tree::span::TreeSpan {
124        match self {
125            Self::Attacks(child) => child.node_span(),
126            Self::CastsSpell(child) => child.node_span(),
127        }
128    }
129}
130
131#[cfg(feature = "parser")]
132impl crate::utils::DummyInit for PlayerAction {
133    fn dummy_init() -> Self {
134        Self::Attacks(crate::utils::dummy())
135    }
136}