mindus/block/
walls.rs

1//! walls
2use crate::block::simple::*;
3use crate::block::*;
4use crate::data::dynamic::DynType;
5use crate::data::renderer::load;
6
7make_simple!(WallBlock, |_, _, _, _, _, s| {
8    let mut base = load!("thruster", s);
9    unsafe { base.overlay(&load!("thruster-top", s)) };
10    base
11});
12
13pub struct DoorBlock {
14    size: u8,
15    symmetric: bool,
16    build_cost: BuildCost,
17}
18
19impl DoorBlock {
20    #[must_use]
21    pub const fn new(size: u8, symmetric: bool, build_cost: BuildCost) -> Self {
22        assert!(size != 0, "invalid size");
23        Self {
24            size,
25            symmetric,
26            build_cost,
27        }
28    }
29
30    state_impl!(pub bool);
31}
32
33impl BlockLogic for DoorBlock {
34    impl_block!();
35
36    fn draw(
37        &self,
38        name: &str,
39        state: Option<&State>,
40        _: Option<&RenderingContext>,
41        _: Rotation,
42        s: Scale,
43    ) -> ImageHolder<4> {
44        if let Some(state) = state {
45            if *Self::get_state(state) {
46                return load!(s -> match name {
47                    "door" => "door-open",
48                    "blast-door" => "blast-door-open",
49                    _ => "door-large-open",
50                });
51            };
52        }
53        load!(from name which is ["door" | "blast-door" | "door-large"], s)
54    }
55
56    fn data_from_i32(&self, _: i32, _: GridPos) -> Result<DynData, DataConvertError> {
57        Ok(DynData::Boolean(false))
58    }
59
60    fn deserialize_state(&self, data: DynData) -> Result<Option<State>, DeserializeError> {
61        match data {
62            DynData::Boolean(opened) => Ok(Some(Self::create_state(opened))),
63            _ => Err(DeserializeError::InvalidType {
64                have: data.get_type(),
65                expect: DynType::Boolean,
66            }),
67        }
68    }
69
70    fn serialize_state(&self, state: &State) -> Result<DynData, SerializeError> {
71        let state = Self::get_state(state);
72        Ok(DynData::Boolean(*state))
73    }
74
75    fn read(&self, build: &mut Build, buff: &mut DataRead) -> Result<(), DataReadError> {
76        build.state = Some(Self::create_state(buff.read_bool()?));
77        Ok(())
78    }
79}