veloren_common/figure/
cell.rs1use std::num::NonZeroU8;
2
3use crate::vol::FilledVox;
4use vek::*;
5
6const GLOWY: u8 = 1 << 1;
7const SHINY: u8 = 1 << 2;
8const HOLLOW: u8 = 1 << 3;
9
10#[derive(Copy, Clone, Debug, PartialEq, Eq)]
11pub struct CellData {
12 pub col: Rgb<u8>,
13 pub attr: NonZeroU8, }
15
16impl CellData {
17 pub(super) fn new(col: Rgb<u8>, glowy: bool, shiny: bool, hollow: bool) -> Self {
18 CellData {
19 col,
20 attr: NonZeroU8::new(
21 1 + glowy as u8 * GLOWY + shiny as u8 * SHINY + hollow as u8 * HOLLOW,
22 )
23 .unwrap(),
24 }
25 }
26
27 pub fn is_hollow(&self) -> bool { self.attr.get() & HOLLOW != 0 }
28}
29
30impl Default for CellData {
31 fn default() -> Self { Self::new(Rgb::broadcast(255), false, false, false) }
32}
33
34#[derive(Copy, Clone, Debug, PartialEq, Eq)]
36pub enum Cell {
37 Filled(CellData),
38 Empty,
39}
40
41impl Cell {
42 pub fn new(col: Rgb<u8>, glowy: bool, shiny: bool, hollow: bool) -> Self {
43 Cell::Filled(CellData::new(col, glowy, shiny, hollow))
44 }
45
46 pub fn get_color(&self) -> Option<Rgb<u8>> {
47 match self {
48 Cell::Filled(data) => Some(data.col),
49 Cell::Empty => None,
50 }
51 }
52
53 pub fn is_glowy(&self) -> bool {
54 match self {
55 Cell::Filled(data) => data.attr.get() & GLOWY != 0,
56 Cell::Empty => false,
57 }
58 }
59
60 pub fn is_shiny(&self) -> bool {
61 match self {
62 Cell::Filled(data) => data.attr.get() & SHINY != 0,
63 Cell::Empty => false,
64 }
65 }
66
67 pub fn is_hollow(&self) -> bool {
68 match self {
69 Cell::Filled(data) => data.is_hollow(),
70 Cell::Empty => false,
71 }
72 }
73}
74
75impl FilledVox for Cell {
76 fn default_non_filled() -> Self { Cell::Empty }
77
78 fn is_filled(&self) -> bool { matches!(self, Cell::Filled(_)) }
79}
80
81#[cfg(test)]
82mod test {
83 use super::*;
84
85 #[test]
86 fn cell_size() {
87 assert_eq!(4, std::mem::size_of::<Cell>());
88 assert_eq!(1, std::mem::align_of::<Cell>());
89 }
90}