lyrix/src/timesys.rs

94 lines
2.6 KiB
Rust

use bevy::{
app::{AppBuilder, Plugin},
core::{Time, Timer},
ecs::system::{Res, ResMut},
prelude::IntoSystem,
};
struct TickTimer(Timer);
pub struct TimeDate {
pub tick: u64, // when hits 40 inc second
pub second: u64, // when hits 60 inc minute
pub minute: u64, // when hits 120 inc day
pub day: u8, // when hits 255 inc season
pub season: u8, // when hits 4 inc year and restart all previous counters
pub year: u64, // ahhhhhhhhh
}
pub struct TickManager;
impl Plugin for TickManager {
fn build(&self, app: &mut AppBuilder) {
app.insert_resource(TimeDate {
tick: 0, // when hits 40 inc second
second: 0, // when hits 60 inc minute
minute: 0, // when hits 120 inc day
day: 0, // when hits 255 inc season
season: 0, // when hits 4 inc year and restart all previous counters
year: 0, // ahhhhhhhhh
})
// the reason we call from_seconds with the true flag is to make the timer repeat itself
.insert_resource(TickTimer(Timer::from_seconds(0.0025, true)))
.add_system(game_tick.system())
.add_system(inc_sec.system())
.add_system(inc_min.system())
.add_system(inc_day.system())
.add_system(inc_season.system())
.add_system(inc_year.system());
}
}
fn game_tick(time: Res<Time>, mut timer: ResMut<TickTimer>, mut timesys: ResMut<TimeDate>) {
if timer.0.tick(time.delta()).just_finished() {
timesys.tick += 1;
}
// Physics runs every tick
// Mob AI acts on its goal every tick
}
fn inc_sec(mut timesys: ResMut<TimeDate>) {
if timesys.tick == 39 {
timesys.second += 1;
timesys.tick = 0;
//println!("Second {}", timesys.second)
}
// Mob AI updates goal every second
}
fn inc_min(mut timesys: ResMut<TimeDate>) {
if timesys.second == 59 {
timesys.second = 0;
timesys.minute += 1;
//println!("Minute {}", timesys.minute)
}
}
fn inc_day(mut timesys: ResMut<TimeDate>) {
if timesys.minute == 119 {
timesys.minute = 0;
timesys.day += 1;
//println!("Day {}", timesys.day)
}
// Run the calender check every day
}
fn inc_season(mut timesys: ResMut<TimeDate>) {
/*
Jan 1 to Mar 14 Season one ()
Mar 14 to mar 26
Mar 26 to Aug 7
Aug 7 to Oct 19
oct 19 to Dec 31
*/
if timesys.day == 72 {
timesys.day = 0;
timesys.season += 1;
}
}
fn inc_year(mut timesys: ResMut<TimeDate>) {
if timesys.season == 4 {
timesys.season = 0;
timesys.year += 1;
}
}