-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_tick.rs
45 lines (37 loc) · 1.29 KB
/
game_tick.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use azalea::{
app::{App, Plugin},
ecs::prelude::*,
entity::{metadata::Player, LocalEntity},
prelude::*,
};
use bounded_counter::BoundedCounter;
/// Tracks game ticks for counting intervals
pub struct GameTickPlugin;
impl Plugin for GameTickPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(GameTicks::default())
.add_systems(GameTick, Self::handle_game_ticks);
}
}
#[derive(Component, Debug, Default, Deref, DerefMut, Resource)]
pub struct GameTicks(BoundedCounter<u128>);
type InitQueryData = Entity;
type InitQueryFilter = (With<LocalEntity>, With<Player>, Without<GameTicks>);
type RunQueryData<'a> = &'a mut GameTicks;
type RunQueryFilter = (With<LocalEntity>, With<Player>, With<GameTicks>);
impl GameTickPlugin {
pub fn handle_game_ticks(
mut init_query: Query<InitQueryData, InitQueryFilter>,
mut commands: Commands,
mut run_query: Query<RunQueryData, RunQueryFilter>,
mut game_ticks: ResMut<GameTicks>,
) {
for entity in &mut init_query {
commands.entity(entity).insert(GameTicks::default());
}
game_ticks.next(); /* Global game ticks counter */
for mut game_ticks in &mut run_query {
game_ticks.next(); /* Local game ticks counter */
}
}
}