Skip to main content

fantasy_craft/utils/
components.rs

1use crate::scene::scene_loader::ComponentLoader;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum Direction {
5    Down,
6    Up,
7    Right,
8    Left
9}
10
11impl Direction {
12    pub fn to_str(&self) -> &'static str {
13        match self {
14            Direction::Up => "up",
15            Direction::Down => "down",
16            Direction::Left => "left",
17            Direction::Right => "right",
18        }
19    }
20
21    pub fn from_str(value: &str) -> Direction {
22        match value {
23            "up" => Direction::Up,
24            "down" => Direction::Down,
25            "left" => Direction::Left,
26            "right" => Direction::Right,
27            _ => Direction::Down
28        }
29    }
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum State {
34    Idle,
35    Walk
36}
37
38impl State {
39    pub fn to_str(&self) -> &'static str {
40        match self {
41            State::Idle => "idle",
42            State::Walk => "walk",
43        }
44    }
45
46    pub fn from_str(value: &str) -> State {
47        match value {
48            "idle" => State::Idle,
49            "walk" => State::Walk,
50            _ => Self::Idle
51        }
52    }
53}
54
55#[derive(Debug)]
56pub struct DirectionComponent(pub Direction);
57
58pub struct DirectionComponentLoader;
59
60impl ComponentLoader for DirectionComponentLoader {
61    fn load(&self, ctx: &mut crate::prelude::Context, entity: hecs::Entity, data: &serde_json::Value) {
62        let loader_data: String = serde_json::from_value(data.clone())
63            .unwrap_or_default();
64
65        let component = DirectionComponent(Direction::from_str(&loader_data));
66
67        ctx.world.insert_one(entity, component).expect("Failed to insert DirectionComponent");
68    }
69}
70
71#[derive(Debug)]
72pub struct StateComponent(pub State);
73
74pub struct StateComponentLoader;
75
76impl ComponentLoader for StateComponentLoader {
77    fn load(&self, ctx: &mut crate::prelude::Context, entity: hecs::Entity, data: &serde_json::Value) {
78        let loader_data: String = serde_json::from_value(data.clone())
79            .unwrap_or_default();
80
81        let component = StateComponent(State::from_str(&loader_data));
82
83        ctx.world.insert_one(entity, component).expect("Failed to insert StateComponent");
84    }
85}
86
87#[derive(Debug)]
88pub struct Visible(pub bool);
89
90pub struct VisibleLoader;
91
92impl ComponentLoader for VisibleLoader {
93    fn load(&self, ctx: &mut crate::prelude::Context, entity: hecs::Entity, data: &serde_json::Value) {
94        let loader_data: bool = serde_json::from_value(data.clone())
95            .unwrap_or_default();
96
97        let component = Visible(loader_data);
98
99        ctx.world.insert_one(entity, component).expect("Failed to insert Visible");
100    }
101}
102
103#[derive(Debug)]
104pub struct LocalVisible(pub bool);
105
106pub struct LocalVisibleLoader;
107
108impl ComponentLoader for LocalVisibleLoader {
109    fn load(&self, ctx: &mut crate::prelude::Context, entity: hecs::Entity, data: &serde_json::Value) {
110        let loader_data: bool = serde_json::from_value(data.clone())
111            .unwrap_or_default();
112
113        let component = LocalVisible(loader_data);
114
115        ctx.world.insert_one(entity, component).expect("Failed to insert LocalVisible");
116    }
117}