mindustry logic execution, map- and schematic- parsing and rendering
Add a team content representation
| -rw-r--r-- | src/team.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/team.rs b/src/team.rs index 5d2acd7..457eac9 100644 --- a/src/team.rs +++ b/src/team.rs @@ -1,5 +1,7 @@ use std::fmt; +use crate::content::{Content, Type}; + #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] pub struct Team(u8); @@ -43,6 +45,20 @@ impl From<u8> for Team } } +impl TryFrom<u16> for Team +{ + type Error = TryFromU16Error; + + fn try_from(value: u16) -> Result<Self, Self::Error> + { + if value <= u8::MAX as u16 {Ok(Team(value as u8))} + else {Err(TryFromU16Error(value))} + } +} + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub struct TryFromU16Error(pub u16); + impl From<Team> for u8 { fn from(value: Team) -> Self @@ -51,6 +67,14 @@ impl From<Team> for u8 } } +impl From<Team> for u16 +{ + fn from(value: Team) -> Self + { + value.0 as u16 + } +} + impl fmt::Display for Team { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result @@ -68,6 +92,33 @@ impl fmt::Display for Team } } +impl Content for Team +{ + fn get_type(&self) -> Type + { + Type::Team + } + + fn get_id(&self) -> u16 + { + self.0 as u16 + } + + fn get_name(&self) -> &str + { + match self.0 + { + 0 => "derelict", + 1 => "sharded", + 2 => "crux", + 3 => "malis", + 4 => "green", + 5 => "blue", + _ => "<custom>", // TODO + } + } +} + pub const DERELICT: Team = Team(0); pub const SHARDED: Team = Team(1); pub const CRUX: Team = Team(2); |