use serde::{Deserialize, Serialize};
use std::fmt;
use std::time::Duration;
#[derive(
Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct EntityId(pub u32);
impl EntityId {
pub fn raw(self) -> u32 {
self.0
}
}
impl fmt::Display for EntityId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<u32> for EntityId {
fn from(v: u32) -> Self {
EntityId(v)
}
}
impl From<i32> for EntityId {
fn from(v: i32) -> Self {
EntityId(v as u32)
}
}
impl From<i64> for EntityId {
fn from(v: i64) -> Self {
EntityId(v as u32)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct AccountId(pub u64);
impl AccountId {
pub fn raw(self) -> u64 {
self.0
}
}
impl fmt::Display for AccountId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<u32> for AccountId {
fn from(v: u32) -> Self {
AccountId(v as u64)
}
}
impl From<i32> for AccountId {
fn from(v: i32) -> Self {
AccountId(v as u64)
}
}
impl From<i64> for AccountId {
fn from(v: i64) -> Self {
AccountId(v as u64)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct GameParamId(pub u32);
impl GameParamId {
pub fn raw(self) -> u32 {
self.0
}
}
impl fmt::Display for GameParamId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<u32> for GameParamId {
fn from(v: u32) -> Self {
GameParamId(v)
}
}
impl From<u64> for GameParamId {
fn from(v: u64) -> Self {
GameParamId(v as u32)
}
}
impl From<i64> for GameParamId {
fn from(v: i64) -> Self {
GameParamId(v as u32)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Relation(u32);
impl Relation {
pub fn new(value: u32) -> Self {
Self(value)
}
pub fn is_self(&self) -> bool {
self.0 == 0
}
pub fn is_ally(&self) -> bool {
self.0 == 1
}
pub fn is_enemy(&self) -> bool {
self.0 >= 2
}
pub fn value(&self) -> u32 {
self.0
}
}
impl From<u32> for Relation {
fn from(value: u32) -> Self {
Self(value)
}
}
#[derive(
Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct PlaneId(pub u64);
impl PlaneId {
pub fn owner_id(self) -> EntityId {
EntityId((self.0 & 0xFFFF_FFFF) as u32)
}
pub fn raw(self) -> u64 {
self.0
}
}
impl fmt::Display for PlaneId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<u64> for PlaneId {
fn from(v: u64) -> Self {
PlaneId(v)
}
}
impl From<i64> for PlaneId {
fn from(v: i64) -> Self {
PlaneId(v as u64)
}
}
#[derive(Debug, Clone, Copy, Default, Serialize)]
pub struct WorldPos {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl WorldPos {
pub fn lerp(self, other: WorldPos, t: f32) -> WorldPos {
self + (other - self) * t
}
}
impl std::ops::Add for WorldPos {
type Output = WorldPos;
fn add(self, rhs: WorldPos) -> WorldPos {
WorldPos {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl std::ops::Sub for WorldPos {
type Output = WorldPos;
fn sub(self, rhs: WorldPos) -> WorldPos {
WorldPos {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}
impl std::ops::Mul<f32> for WorldPos {
type Output = WorldPos;
fn mul(self, rhs: f32) -> WorldPos {
WorldPos {
x: self.x * rhs,
y: self.y * rhs,
z: self.z * rhs,
}
}
}
#[derive(Debug, Clone, Copy, Serialize)]
pub struct NormalizedPos {
pub x: f32,
pub y: f32,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct GameClock(pub f32);
impl GameClock {
pub fn seconds(self) -> f32 {
self.0
}
pub fn to_duration(self) -> Duration {
Duration::from_secs_f32(self.0)
}
pub fn game_time(self) -> f32 {
(self.0 - 30.0).max(0.0)
}
}
impl fmt::Display for GameClock {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:.1}s", self.0)
}
}
impl std::ops::Add<f32> for GameClock {
type Output = GameClock;
fn add(self, rhs: f32) -> GameClock {
GameClock(self.0 + rhs)
}
}
impl std::ops::Add<Duration> for GameClock {
type Output = GameClock;
fn add(self, rhs: Duration) -> GameClock {
GameClock(self.0 + rhs.as_secs_f32())
}
}
impl std::ops::Sub for GameClock {
type Output = f32;
fn sub(self, rhs: GameClock) -> f32 {
self.0 - rhs.0
}
}
impl std::ops::Sub<Duration> for GameClock {
type Output = GameClock;
fn sub(self, rhs: Duration) -> GameClock {
GameClock(self.0 - rhs.as_secs_f32())
}
}