blademaster/components/
gamecell.rs1use tui::style::Color;
2
3use crate::{CellAccess, CellKind, CellVisibility};
4
5#[derive(Clone, Debug, PartialEq)]
6pub struct GameCell {
7 x: i16,
8 y: i16,
9 kind: CellKind,
10 name: String,
11 color: Color,
12 access: CellAccess,
13 visible: CellVisibility,
14}
15
16impl GameCell {
17 pub fn new(
18 x: i16,
19 y: i16,
20 kind: CellKind,
21 name: &str,
22 color: Color,
23 access: CellAccess,
24 ) -> Self {
25 Self {
26 x,
27 y,
28 kind,
29 name: name.to_string(),
30 color,
31 access,
32 visible: CellVisibility::Unvisited,
33 }
34 }
35
36 pub fn move_up(&mut self) {
37 self.y -= 1;
38 }
39 pub fn move_down(&mut self) {
40 self.y += 1;
41 }
42 pub fn move_left(&mut self) {
43 self.x -= 1;
44 }
45 pub fn move_right(&mut self) {
46 self.x += 1;
47 }
48
49 pub fn inside(&self, x1: u16, y1: u16, x2: u16, y2: u16) -> bool {
50 self.x >= x1 as i16 && self.y >= y1 as i16 && self.x <= x2 as i16 && self.y <= y2 as i16
51 }
52
53 pub fn x(&self) -> u16 {
54 if self.x > 0 {
55 self.x as u16
56 } else {
57 1
58 }
59 }
60 pub fn y(&self) -> u16 {
61 if self.y > 0 {
62 self.y as u16
63 } else {
64 1
65 }
66 }
67 pub fn kind(&self) -> CellKind {
68 self.kind
69 }
70 pub fn name(&self) -> String {
71 self.name.clone()
72 }
73 pub fn color(&self) -> Color {
74 self.color
75 }
76 pub fn access(&self) -> CellAccess {
77 self.access
78 }
79 pub fn visible(&self) -> CellVisibility {
80 self.visible
81 }
82 pub fn set_visible(&mut self, visible: CellVisibility) {
83 self.visible = visible;
84 }
85}