Skip to main content

thedes_domain/
event.rs

1use serde::{Deserialize, Serialize};
2use thedes_geometry::orientation::Direction;
3use thiserror::Error;
4
5use crate::{
6    game::{
7        Game,
8        MonsterAttackError,
9        MonsterFollowError,
10        MonsterGrowlError,
11        MoveMonsterError,
12        SpawnMonsterError,
13        VanishMonsterError,
14    },
15    geometry::{Coord, CoordPair},
16    monster::{self, MonsterPosition},
17};
18
19#[derive(Debug, Error)]
20pub enum ApplyError {
21    #[error("Failed to spawn a monster")]
22    TrySpawnMonster(
23        #[from]
24        #[source]
25        SpawnMonsterError,
26    ),
27    #[error("Failed to vanish a monster")]
28    VanishMonster(
29        #[from]
30        #[source]
31        VanishMonsterError,
32    ),
33    #[error("Failed to move a monster")]
34    TryMoveMonster(
35        #[from]
36        #[source]
37        MoveMonsterError,
38    ),
39    #[error("Failed to make a monster attack")]
40    MonsterAttack(
41        #[from]
42        #[source]
43        MonsterAttackError,
44    ),
45    #[error("Failed to make a monster growl")]
46    MonsterGrowl(
47        #[from]
48        #[source]
49        MonsterGrowlError,
50    ),
51    #[error("Failed to make a monster follow the player")]
52    MonsterFollow(
53        #[from]
54        #[source]
55        MonsterFollowError,
56    ),
57}
58
59#[derive(
60    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
61)]
62pub enum Event {
63    TrySpawnMonster(MonsterPosition),
64    VanishMonster(monster::Id),
65    TryMoveMonster(monster::Id, Direction),
66    MonsterAttack(monster::Id),
67    FollowPlayer { id: monster::Id, period: Coord, limit: u32 },
68    MonsterGrowl(monster::Id),
69}
70
71impl Event {
72    pub(crate) fn apply(self, game: &mut Game) -> Result<(), ApplyError> {
73        match self {
74            Self::TrySpawnMonster(position) => {
75                game.try_spawn_moster(position)?
76            },
77            Self::VanishMonster(id) => game.vanish_monster(id)?,
78            Self::TryMoveMonster(id, direction) => {
79                game.try_move_monster(id, direction)?
80            },
81            Self::MonsterAttack(id) => game.monster_attack(id)?,
82            Self::MonsterGrowl(id) => game.monster_growl(id)?,
83            Self::FollowPlayer { id, period: speed, limit } => {
84                game.monster_follow_player(id, speed, limit)?
85            },
86        }
87        Ok(())
88    }
89}
90
91#[derive(
92    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
93)]
94pub enum MetaEvent {
95    MonsterHit(CoordPair),
96    MonsterGrowl(CoordPair),
97}