generated from GarandPLG/rust-flake-template
Replace CellTag with CellStructure and embed BaseBuilding
The refactor removes the `Buildings` enum and the `building` module, replacing `CellTag` with a new `CellStructure` enum that directly holds a `BaseBuilding`. A `get_owner` method is added to `BaseBuilding` to support the new structure. All related imports, constructors, and rendering logic are updated to use `CellStructure` instead of the old tag and building fields.
This commit is contained in:
+21
-31
@@ -1,6 +1,5 @@
|
||||
use crate::app::{
|
||||
buildings::Buildings,
|
||||
states::{CellTag, Players, ZoomLevel},
|
||||
states::{CellStructure, Players, ZoomLevel},
|
||||
units::Units,
|
||||
};
|
||||
use ratatui::{
|
||||
@@ -17,10 +16,9 @@ pub struct CellWidget {
|
||||
col: usize,
|
||||
selected: bool,
|
||||
zoom_level: ZoomLevel,
|
||||
tag: CellTag,
|
||||
structure: CellStructure,
|
||||
marked: bool,
|
||||
unit: Option<Units>,
|
||||
building: Option<Buildings>,
|
||||
}
|
||||
|
||||
impl CellWidget {
|
||||
@@ -29,19 +27,17 @@ impl CellWidget {
|
||||
col: usize,
|
||||
zoom_level: ZoomLevel,
|
||||
selected: bool,
|
||||
tag: CellTag,
|
||||
structure: CellStructure,
|
||||
unit: Option<Units>,
|
||||
building: Option<Buildings>,
|
||||
) -> Self {
|
||||
Self {
|
||||
row,
|
||||
col,
|
||||
selected,
|
||||
zoom_level,
|
||||
tag,
|
||||
marked: false,
|
||||
structure,
|
||||
unit,
|
||||
building,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,8 +50,8 @@ impl CellWidget {
|
||||
self.zoom_level = zoom_level;
|
||||
}
|
||||
|
||||
pub fn set_tag(&mut self, tag: CellTag) {
|
||||
self.tag = tag;
|
||||
pub fn set_structure(&mut self, sctructure: CellStructure) {
|
||||
self.structure = sctructure;
|
||||
}
|
||||
|
||||
pub fn get_marked(&self) -> bool {
|
||||
@@ -80,7 +76,7 @@ impl CellWidget {
|
||||
}
|
||||
|
||||
fn display_coords(&self) -> Span<'_> {
|
||||
if self.selected || self.tag != CellTag::Stone || self.marked {
|
||||
if self.selected || self.structure != CellStructure::Stone || self.marked {
|
||||
format!("{}{}", self.col_to_letters(), self.row).green()
|
||||
} else {
|
||||
"".to_span()
|
||||
@@ -88,36 +84,30 @@ impl CellWidget {
|
||||
}
|
||||
|
||||
fn fg_color(&self) -> Color {
|
||||
match self.tag {
|
||||
match self.structure {
|
||||
_ if self.marked && self.selected => Color::Magenta,
|
||||
_ if self.marked && !self.selected => Color::LightMagenta,
|
||||
_ if self.selected && !self.marked => Color::LightYellow,
|
||||
CellTag::Base(Players::Player) if !self.selected => Color::LightBlue,
|
||||
CellTag::Base(Players::Enemy) if !self.selected => Color::LightRed,
|
||||
CellTag::Tunel if !self.selected => Color::Gray,
|
||||
CellStructure::Base(base) if !self.selected => match base.get_owner() {
|
||||
Players::Player => Color::LightBlue,
|
||||
Players::Enemy => Color::LightRed,
|
||||
},
|
||||
CellStructure::Tunel if !self.selected => Color::Gray,
|
||||
_ => Color::White,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_text_area(&self) -> Vec<Line<'_>> {
|
||||
let tag: &str = match self.building {
|
||||
Some(building) => match building {
|
||||
Buildings::Base(base) => base.get_tag(),
|
||||
// _ => " ",
|
||||
},
|
||||
None => match self.tag {
|
||||
CellTag::Tunel => "T",
|
||||
CellTag::Stone => " ",
|
||||
_ => " ",
|
||||
},
|
||||
let tag: &str = match self.structure {
|
||||
CellStructure::Base(base) => base.get_tag(),
|
||||
CellStructure::Tunel => "T",
|
||||
CellStructure::Stone => " ",
|
||||
// _ => " ",
|
||||
};
|
||||
|
||||
let b_level: &str = match self.building {
|
||||
Some(building) => match building {
|
||||
Buildings::Base(base) => base.get_level(),
|
||||
// _ => " ",
|
||||
},
|
||||
None => " ",
|
||||
let b_level: &str = match self.structure {
|
||||
CellStructure::Base(base) => base.get_level(),
|
||||
_ => " ",
|
||||
};
|
||||
|
||||
let unit: &str = match self.unit {
|
||||
|
||||
Reference in New Issue
Block a user