Skip to content

Commit

Permalink
3600 seconds per hour
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Aug 3, 2023
1 parent 5569288 commit 05b396a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
7 changes: 5 additions & 2 deletions egregoria/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub use world::*;
use crate::engine_interaction::WorldCommand::Init;
use crate::init::{GSYSTEMS, INIT_FUNCS, SAVELOAD_FUNCS};
use crate::utils::scheduler::RunnableSystem;
use crate::utils::time::Tick;
use crate::utils::time::{Tick, SECONDS_PER_REALTIME_SECOND};
use common::FastMap;
pub use utils::config::*;
pub use utils::par_command_buffer::ParCommandBuffer;
Expand Down Expand Up @@ -240,7 +240,10 @@ impl Egregoria {
const WORLD_TICK_DT: f32 = 0.05;
{
let mut time = self.write::<GameTime>();
*time = GameTime::new(WORLD_TICK_DT, time.timestamp + WORLD_TICK_DT as f64);
*time = GameTime::new(
WORLD_TICK_DT,
time.timestamp + SECONDS_PER_REALTIME_SECOND as f64 * WORLD_TICK_DT as f64,
);
}

game_schedule.execute(self);
Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/map_dynamic/itinerary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ pub fn itinerary_update(world: &mut World, resources: &mut Resources) {
world.query_it_trans_speed().for_each(
|(it, trans, speed): (&mut Itinerary, &mut Transform, f32)| {
trans.position =
it.update_rail(trans.position, speed * time.delta, tick, time.seconds, map);
it.update_rail(trans.position, speed * time.realdelta, tick, time.seconds, map);
},
);

Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/souls/goods_company.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pub fn company_soul(goria: &mut Egregoria, company: GoodsCompany) -> Option<Soul

#[profiling::function]
pub fn company_system(world: &mut World, res: &mut Resources) {
let delta = res.get::<GameTime>().unwrap().delta;
let delta = res.get::<GameTime>().unwrap().realdelta;
let cbuf: &ParCommandBuffer<CompanyEnt> = &res.get().unwrap();
let cbuf_human: &ParCommandBuffer<HumanEnt> = &res.get().unwrap();
let binfos: &BuildingInfos = &res.get().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions egregoria/src/transportation/pedestrian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn pedestrian_decision(
) {
let (desired_v, desired_dir) = calc_decision(pedestrian, trans, it);

pedestrian.walk_anim += 7.0 * kin.0 * time.delta / pedestrian.walking_speed;
pedestrian.walk_anim += 7.0 * kin.0 * time.realdelta / pedestrian.walking_speed;
pedestrian.walk_anim %= 2.0 * std::f32::consts::PI;
physics(kin, trans, time, desired_v, desired_dir);
}
Expand All @@ -93,12 +93,12 @@ pub fn physics(
desired_dir: Vec3,
) {
let diff = desired_velocity - kin.0;
let mag = diff.min(time.delta * PEDESTRIAN_ACC);
let mag = diff.min(time.realdelta * PEDESTRIAN_ACC);
if mag > 0.0 {
kin.0 += mag;
}
const ANG_VEL: f32 = 1.0;
trans.dir = angle_lerpxy(trans.dir, desired_dir, ANG_VEL * time.delta);
trans.dir = angle_lerpxy(trans.dir, desired_dir, ANG_VEL * time.realdelta);
}

pub fn calc_decision(
Expand Down
12 changes: 6 additions & 6 deletions egregoria/src/transportation/road.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn vehicle_state_update(
match vehicle.state {
VehicleState::RoadToPark(_, ref mut t, _) => {
// Vehicle is on rails when parking.
*t += time.delta / TIME_TO_PARK;
*t += time.realdelta / TIME_TO_PARK;

if *t >= 1.0 {
let v = coll.take();
Expand Down Expand Up @@ -168,21 +168,21 @@ fn physics(

let speed = speed
+ (desired_speed - speed).clamp(
-time.delta * kind.deceleration(),
time.delta * kind.acceleration(),
-time.realdelta * kind.deceleration(),
time.realdelta * kind.acceleration(),
);

let max_ang_vel = (speed.abs() / kind.min_turning_radius()).clamp(0.0, 3.0);

let approx_angle = trans.dir.distance(desired_dir);

vehicle.ang_velocity += time.delta * kind.ang_acc();
vehicle.ang_velocity += time.realdelta * kind.ang_acc();
vehicle.ang_velocity = vehicle
.ang_velocity
.min(4.0 * approx_angle)
.min(max_ang_vel);

trans.dir = angle_lerpxy(trans.dir, desired_dir, vehicle.ang_velocity * time.delta);
trans.dir = angle_lerpxy(trans.dir, desired_dir, vehicle.ang_velocity * time.realdelta);

kin.0 = speed;
}
Expand All @@ -200,7 +200,7 @@ pub fn calc_decision<'a>(
) -> (f32, Vec3) {
let default_return = (0.0, trans.dir);
if vehicle.wait_time > 0.0 {
vehicle.wait_time -= time.delta;
vehicle.wait_time -= time.realdelta;
return default_return;
}
let objective: Vec3 = unwrap_or!(it.get_point(), return default_return);
Expand Down
12 changes: 6 additions & 6 deletions egregoria/src/transportation/train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,21 +336,21 @@ pub fn locomotive_system(world: &mut World, resources: &mut Resources) {
t.trans.dir = desired_dir;

t.speed.0 += (desired_speed - t.speed.0).clamp(
-time.delta * t.locomotive.dec_force,
time.delta * t.locomotive.acc_force,
-time.realdelta * t.locomotive.dec_force,
time.realdelta * t.locomotive.acc_force,
);
if t.speed.0 <= 0.001 {
t.res.waited_for += time.delta;
t.res.waited_for += time.realdelta;
} else {
t.res.waited_for = 0.0;
}
for v in t.res.past_travers.values_mut() {
*v += t.speed.0 * time.delta;
*v += t.speed.0 * time.realdelta;
if t.res.waited_for > 60.0 {
*v += 0.1 * time.delta;
*v += 0.1 * time.realdelta;
}
}
t.res.cur_travers_dist += t.speed.0 * time.delta;
t.res.cur_travers_dist += t.speed.0 * time.realdelta;
}
}

Expand Down
17 changes: 10 additions & 7 deletions egregoria/src/utils/time.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use egui_inspect::Inspect;
use serde::{Deserialize, Serialize};

pub const SECONDS_PER_HOUR: i32 = 200;
pub const SECONDS_PER_REALTIME_SECOND: i32 = 15;
pub const SECONDS_PER_HOUR: i32 = 3600;
pub const HOURS_PER_DAY: i32 = 24;
pub const SECONDS_PER_DAY: i32 = SECONDS_PER_HOUR * HOURS_PER_DAY;
pub const TICKS_PER_SECOND: u32 = 50;
Expand All @@ -22,13 +23,13 @@ pub struct GameInstant {
/// `GameTime` is subject to timewarp
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct GameTime {
/// Monotonic time in seconds elapsed since the start of the game.
/// Monotonic time in (game) seconds elapsed since the start of the game.
pub timestamp: f64,

/// Time elapsed since the last frame
pub delta: f32,
/// Real time elapsed since the last frame, useful for animations
pub realdelta: f32,

/// Time in seconds elapsed since the start of the game
/// Game time in seconds elapsed since the start of the game
pub seconds: u32,

/// Information about the time of the current day
Expand Down Expand Up @@ -162,7 +163,7 @@ impl GameTime {
let seconds = timestamp as u32;
GameTime {
timestamp,
delta,
realdelta: delta,
seconds,
daytime: DayTime::new(seconds as i32),
}
Expand All @@ -177,7 +178,9 @@ impl GameTime {
/// Returns true every freq seconds
pub fn tick(&self, freq: u32) -> bool {
let time_near = (self.seconds / freq * freq) as f64;
self.timestamp > time_near && (self.timestamp - self.delta as f64) <= time_near
self.timestamp > time_near
&& (self.timestamp - SECONDS_PER_REALTIME_SECOND as f64 * self.realdelta as f64)
<= time_near
}

pub fn daysec(&self) -> f64 {
Expand Down

0 comments on commit 05b396a

Please sign in to comment.