use std::ops::{Deref, DerefMut};
use glam::{Mat3A, Vec3A};
use crate::{CarControls, PhysState, RaycastHitInfo, consts};
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CarCore {
pub phys: PhysState,
pub controls: CarControls,
pub has_jumped: bool,
pub has_double_jumped: bool,
pub has_flipped: bool,
pub flip_rel_torque: Vec3A,
pub jump_ticks: u32,
pub flip_time: f32,
pub is_flipping: bool,
pub is_jumping: bool,
pub jump_hold_broken: bool,
pub air_time_since_jump: f32,
pub boost: f32,
pub is_auto_flipping: bool,
pub auto_flip_timer: f32,
pub auto_flip_torque_scale: f32,
pub bump_cooldown_timer: f32,
pub bump_cooldown_victim: Option<usize>,
pub last_extra_hit_tick: Option<u64>,
pub is_demoed: bool,
pub demo_respawn_timer: f32,
}
impl CarCore {
pub const DEFAULT: Self = Self {
phys: PhysState {
pos: Vec3A::new(0.0, 0.0, consts::car::spawn::REST_Z),
rot_mat: Mat3A::IDENTITY,
vel: Vec3A::ZERO,
ang_vel: Vec3A::ZERO,
},
controls: CarControls::DEFAULT,
has_jumped: false,
has_double_jumped: false,
has_flipped: false,
flip_rel_torque: Vec3A::ZERO,
jump_ticks: 0,
flip_time: 0.0,
is_flipping: false,
is_jumping: false,
jump_hold_broken: false,
air_time_since_jump: 0.0,
boost: consts::car::boost::SPAWN_AMOUNT,
is_auto_flipping: false,
auto_flip_timer: 0.0,
auto_flip_torque_scale: 0.0,
bump_cooldown_timer: 0.0,
bump_cooldown_victim: None,
last_extra_hit_tick: None,
is_demoed: false,
demo_respawn_timer: 0.0,
};
#[must_use]
pub fn grounded(phys: PhysState, boost: f32) -> Self {
Self {
phys,
boost: boost.clamp(0.0, consts::car::boost::MAX),
has_jumped: false,
has_double_jumped: false,
has_flipped: false,
is_jumping: false,
is_flipping: false,
..Self::DEFAULT
}
}
#[must_use]
pub fn aerial(phys: PhysState) -> Self {
Self {
phys,
has_jumped: false,
has_double_jumped: false,
has_flipped: false,
is_jumping: false,
is_flipping: false,
..Self::DEFAULT
}
}
#[must_use]
pub fn jumping(phys: PhysState, ticks: u32) -> Self {
Self {
phys,
has_jumped: true,
is_jumping: true,
jump_ticks: ticks,
..Self::DEFAULT
}
}
#[must_use]
pub fn flipping(phys: PhysState, rel_torque: Vec3A) -> Self {
Self {
phys,
has_jumped: true,
has_flipped: true,
is_flipping: true,
flip_rel_torque: rel_torque,
..Self::DEFAULT
}
}
#[must_use]
pub fn with_controls(mut self, controls: CarControls) -> Self {
self.controls = controls;
self
}
#[must_use]
pub fn with_boost(mut self, boost: f32) -> Self {
self.boost = boost.clamp(0.0, consts::car::boost::MAX);
self
}
#[must_use]
pub fn with_vel(mut self, vel: Vec3A) -> Self {
self.phys.vel = vel;
self
}
#[must_use]
pub fn with_pos(mut self, pos: Vec3A) -> Self {
self.phys.pos = pos;
self
}
#[must_use]
pub fn with_rot_mat(mut self, rot_mat: Mat3A) -> Self {
self.phys.rot_mat = rot_mat;
self
}
#[must_use]
pub fn with_ang_vel(mut self, ang_vel: Vec3A) -> Self {
self.phys.ang_vel = ang_vel;
self
}
pub fn debug_validate(&self) {
debug_assert!(!self.is_jumping || self.has_jumped);
debug_assert!(!self.is_flipping || self.has_flipped);
debug_assert!((0.0..=consts::car::boost::MAX).contains(&self.boost));
}
}
impl Default for CarCore {
fn default() -> Self {
Self::DEFAULT
}
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CarTuning {
pub handbrake_val: f32,
pub is_supersonic: bool,
pub supersonic_grace_timer: f32,
pub time_since_boosted: f32,
pub boosting_time: f32,
pub is_boosting: bool,
pub air_time: f32,
}
impl CarTuning {
pub const DEFAULT: Self = Self {
handbrake_val: 0.0,
is_supersonic: false,
supersonic_grace_timer: 0.0,
time_since_boosted: 0.0,
boosting_time: 0.0,
is_boosting: false,
air_time: 0.0,
};
#[must_use]
pub fn from_speed(vel: Vec3A) -> Self {
Self {
is_supersonic: vel.length_squared()
>= consts::car::supersonic::START_SPEED * consts::car::supersonic::START_SPEED,
..Self::DEFAULT
}
}
#[must_use]
pub const fn preserve(state: &CarState) -> Self {
state.tuning()
}
}
impl Default for CarTuning {
fn default() -> Self {
Self::DEFAULT
}
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub(crate) struct CarCache {
pub is_on_ground: bool,
pub wheels_with_contact: [Option<RaycastHitInfo>; 4],
pub world_contact_normal: Option<Vec3A>,
pub prev_controls: CarControls,
#[cfg_attr(feature = "serde", serde(default))]
pub world_wheels_prev: u8,
#[cfg_attr(feature = "serde", serde(default = "default_demo_tick"))]
pub demo_tick: u64,
#[cfg_attr(feature = "serde", serde(default))]
pub demoer_idx: Option<usize>,
}
#[cfg(feature = "serde")]
const fn default_demo_tick() -> u64 {
u64::MAX
}
impl Default for CarCache {
fn default() -> Self {
Self {
is_on_ground: true,
wheels_with_contact: [None; 4],
world_contact_normal: None,
prev_controls: CarControls::DEFAULT,
world_wheels_prev: 0,
demo_tick: u64::MAX,
demoer_idx: None,
}
}
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CarState {
pub phys: PhysState,
pub controls: CarControls,
pub has_jumped: bool,
pub has_double_jumped: bool,
pub has_flipped: bool,
pub flip_rel_torque: Vec3A,
pub jump_ticks: u32,
pub flip_time: f32,
pub is_flipping: bool,
pub is_jumping: bool,
pub jump_hold_broken: bool,
pub air_time: f32,
pub air_time_since_jump: f32,
pub boost: f32,
pub handbrake_val: f32,
pub is_supersonic: bool,
pub supersonic_grace_timer: f32,
pub time_since_boosted: f32,
pub boosting_time: f32,
pub is_boosting: bool,
pub is_auto_flipping: bool,
pub auto_flip_timer: f32,
pub auto_flip_torque_scale: f32,
pub bump_cooldown_timer: f32,
pub bump_cooldown_victim: Option<usize>,
pub last_extra_hit_tick: Option<u64>,
pub is_demoed: bool,
pub demo_respawn_timer: f32,
pub(crate) cache: CarCache,
}
impl Default for CarState {
fn default() -> Self {
Self::DEFAULT
}
}
impl CarState {
pub const DEFAULT: Self = Self {
phys: CarCore::DEFAULT.phys,
controls: CarCore::DEFAULT.controls,
has_jumped: CarCore::DEFAULT.has_jumped,
has_double_jumped: CarCore::DEFAULT.has_double_jumped,
has_flipped: CarCore::DEFAULT.has_flipped,
flip_rel_torque: CarCore::DEFAULT.flip_rel_torque,
jump_ticks: CarCore::DEFAULT.jump_ticks,
flip_time: CarCore::DEFAULT.flip_time,
is_flipping: CarCore::DEFAULT.is_flipping,
is_jumping: CarCore::DEFAULT.is_jumping,
jump_hold_broken: CarCore::DEFAULT.jump_hold_broken,
air_time: CarTuning::DEFAULT.air_time,
air_time_since_jump: CarCore::DEFAULT.air_time_since_jump,
boost: CarCore::DEFAULT.boost,
handbrake_val: CarTuning::DEFAULT.handbrake_val,
is_supersonic: CarTuning::DEFAULT.is_supersonic,
supersonic_grace_timer: CarTuning::DEFAULT.supersonic_grace_timer,
time_since_boosted: CarTuning::DEFAULT.time_since_boosted,
boosting_time: CarTuning::DEFAULT.boosting_time,
is_boosting: CarTuning::DEFAULT.is_boosting,
is_auto_flipping: CarCore::DEFAULT.is_auto_flipping,
auto_flip_timer: CarCore::DEFAULT.auto_flip_timer,
auto_flip_torque_scale: CarCore::DEFAULT.auto_flip_torque_scale,
bump_cooldown_timer: CarCore::DEFAULT.bump_cooldown_timer,
bump_cooldown_victim: CarCore::DEFAULT.bump_cooldown_victim,
last_extra_hit_tick: CarCore::DEFAULT.last_extra_hit_tick,
is_demoed: CarCore::DEFAULT.is_demoed,
demo_respawn_timer: CarCore::DEFAULT.demo_respawn_timer,
cache: CarCache {
is_on_ground: true,
wheels_with_contact: [None; 4],
world_contact_normal: None,
prev_controls: CarControls::DEFAULT,
world_wheels_prev: 0,
demo_tick: u64::MAX,
demoer_idx: None,
},
};
#[must_use]
pub const fn core(&self) -> CarCore {
CarCore {
phys: self.phys,
controls: self.controls,
has_jumped: self.has_jumped,
has_double_jumped: self.has_double_jumped,
has_flipped: self.has_flipped,
flip_rel_torque: self.flip_rel_torque,
jump_ticks: self.jump_ticks,
flip_time: self.flip_time,
is_flipping: self.is_flipping,
is_jumping: self.is_jumping,
jump_hold_broken: self.jump_hold_broken,
air_time_since_jump: self.air_time_since_jump,
boost: self.boost,
is_auto_flipping: self.is_auto_flipping,
auto_flip_timer: self.auto_flip_timer,
auto_flip_torque_scale: self.auto_flip_torque_scale,
bump_cooldown_timer: self.bump_cooldown_timer,
bump_cooldown_victim: self.bump_cooldown_victim,
last_extra_hit_tick: self.last_extra_hit_tick,
is_demoed: self.is_demoed,
demo_respawn_timer: self.demo_respawn_timer,
}
}
#[must_use]
pub const fn tuning(&self) -> CarTuning {
CarTuning {
handbrake_val: self.handbrake_val,
is_supersonic: self.is_supersonic,
supersonic_grace_timer: self.supersonic_grace_timer,
time_since_boosted: self.time_since_boosted,
boosting_time: self.boosting_time,
is_boosting: self.is_boosting,
air_time: self.air_time,
}
}
#[must_use]
pub fn with_tuning(mut self, tuning: CarTuning) -> Self {
self.handbrake_val = tuning.handbrake_val;
self.is_supersonic = tuning.is_supersonic;
self.supersonic_grace_timer = tuning.supersonic_grace_timer;
self.time_since_boosted = tuning.time_since_boosted;
self.boosting_time = tuning.boosting_time;
self.is_boosting = tuning.is_boosting;
self.air_time = tuning.air_time;
self
}
#[must_use]
pub const fn has_flip_or_jump(&self) -> bool {
self.cache.is_on_ground
|| (!self.has_flipped
&& !self.has_double_jumped
&& self.air_time_since_jump + consts::TICK_TIME
< consts::car::jump::DOUBLEJUMP_MAX_DELAY)
}
#[must_use]
pub const fn has_flip_reset(&self) -> bool {
!self.cache.is_on_ground && self.has_flip_or_jump() && !self.has_jumped
}
#[must_use]
pub const fn got_flip_reset(&self) -> bool {
!self.cache.is_on_ground && !self.has_jumped
}
#[must_use]
pub fn num_wheels_in_contact(&self) -> usize {
self.cache
.wheels_with_contact
.iter()
.filter(|w| w.is_some())
.count()
}
#[must_use]
pub const fn jump_time(&self) -> f32 {
self.jump_ticks as f32 * consts::TICK_TIME
}
#[must_use]
pub const fn is_on_ground(&self) -> bool {
self.cache.is_on_ground
}
#[must_use]
pub const fn wheels_with_contact(&self) -> &[Option<RaycastHitInfo>; 4] {
&self.cache.wheels_with_contact
}
#[must_use]
pub const fn world_contact_normal(&self) -> Option<Vec3A> {
self.cache.world_contact_normal
}
#[must_use]
pub const fn prev_controls(&self) -> &CarControls {
&self.cache.prev_controls
}
pub fn set_contact_flags(&mut self, is_on_ground: bool, wheel_contacts: [bool; 4]) {
self.cache.is_on_ground = is_on_ground;
self.cache.wheels_with_contact = wheel_contacts.map(|has_contact| {
has_contact.then_some(RaycastHitInfo {
hit_point: Vec3A::ZERO,
hit_normal: Vec3A::Z,
hit_fraction: 0.0,
user_info: crate::UserInfoType::None,
})
});
}
pub const fn set_prev_controls(&mut self, controls: CarControls) {
self.cache.prev_controls = controls;
}
pub fn debug_validate(&self) {
self.core().debug_validate();
debug_assert!((0.0..=1.0).contains(&self.handbrake_val));
debug_assert!(self.is_supersonic || self.supersonic_grace_timer == 0.0);
if self.cache.is_on_ground {
debug_assert!(!self.has_double_jumped && !self.has_flipped && !self.is_flipping);
debug_assert_eq!(self.air_time, 0.0);
debug_assert_eq!(self.flip_time, 0.0);
}
}
}
impl From<CarCore> for CarState {
fn from(mut core: CarCore) -> Self {
core.boost = core.boost.clamp(0.0, consts::car::boost::MAX);
if core.is_jumping {
core.has_jumped = true;
}
if core.is_flipping {
core.has_flipped = true;
}
let tuning = CarTuning::from_speed(core.phys.vel);
Self {
phys: core.phys,
controls: core.controls,
has_jumped: core.has_jumped,
has_double_jumped: core.has_double_jumped,
has_flipped: core.has_flipped,
flip_rel_torque: core.flip_rel_torque,
jump_ticks: core.jump_ticks,
flip_time: core.flip_time,
is_flipping: core.is_flipping,
is_jumping: core.is_jumping,
jump_hold_broken: core.jump_hold_broken,
air_time: tuning.air_time,
air_time_since_jump: core.air_time_since_jump,
boost: core.boost,
handbrake_val: tuning.handbrake_val,
is_supersonic: tuning.is_supersonic,
supersonic_grace_timer: tuning.supersonic_grace_timer,
time_since_boosted: tuning.time_since_boosted,
boosting_time: tuning.boosting_time,
is_boosting: tuning.is_boosting,
is_auto_flipping: core.is_auto_flipping,
auto_flip_timer: core.auto_flip_timer,
auto_flip_torque_scale: core.auto_flip_torque_scale,
bump_cooldown_timer: core.bump_cooldown_timer,
bump_cooldown_victim: core.bump_cooldown_victim,
last_extra_hit_tick: core.last_extra_hit_tick,
is_demoed: core.is_demoed,
demo_respawn_timer: core.demo_respawn_timer,
cache: CarCache {
is_on_ground: false,
wheels_with_contact: [None; 4],
world_contact_normal: None,
prev_controls: core.controls,
world_wheels_prev: 0,
demo_tick: u64::MAX,
demoer_idx: None,
},
}
}
}
impl Deref for CarCore {
type Target = PhysState;
fn deref(&self) -> &Self::Target {
&self.phys
}
}
impl DerefMut for CarCore {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.phys
}
}
impl Deref for CarState {
type Target = PhysState;
fn deref(&self) -> &Self::Target {
&self.phys
}
}
impl DerefMut for CarState {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.phys
}
}