From b7776e9b8e498cba69e3c19b011cc7d096f9b7f6 Mon Sep 17 00:00:00 2001 From: Mubelotix Date: Thu, 4 Jan 2024 15:46:10 +0100 Subject: [PATCH] Run tasks --- minecraft-server/src/server_behavior.rs | 2 ++ minecraft-server/src/world/ecs.rs | 10 ++++++++++ minecraft-server/src/world/mod.rs | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/minecraft-server/src/server_behavior.rs b/minecraft-server/src/server_behavior.rs index c7672e51..d034ac13 100644 --- a/minecraft-server/src/server_behavior.rs +++ b/minecraft-server/src/server_behavior.rs @@ -21,11 +21,13 @@ impl ServerBehavior { let world = Box::leak(Box::new(World::new(receiver.resubscribe()))); // Send ticks to player handlers + let world2: &World = world; tokio::spawn(async move { let mut tick_id = 0; let mut tick = tokio::time::interval(Duration::from_millis(50)); loop { tick.tick().await; + world2.tick().await; let _ = sender.send(ServerMessage::Tick(tick_id)); tick_id += 1; } diff --git a/minecraft-server/src/world/ecs.rs b/minecraft-server/src/world/ecs.rs index 8e134614..58b4d8fd 100644 --- a/minecraft-server/src/world/ecs.rs +++ b/minecraft-server/src/world/ecs.rs @@ -69,6 +69,8 @@ impl Entities { } } + // TODO: Since we lock tasks it makes it impossible for an entity task to itself call this function + // It would be resolved if we had a temporary task buffer that would be added only on Ecs::tick pub(super) async fn spawn_entity(&self, entity: AnyEntity, world: &'static World, receiver: BroadcastReceiver) -> (Eid, UUID) where AnyEntity: TryAsEntityRef, Handler: EntityExt { @@ -104,4 +106,12 @@ impl Entities { self.uuids.write().await.retain(|_,v| *v != eid); entity } + + pub(super) async fn tick(&self, world: &'static World) { + let mut tasks = self.tasks.write().await; + for (eid, task) in tasks.iter_mut() { + let h = Handler::::assume(*eid, world); + task.tick(h).await; + } + } } diff --git a/minecraft-server/src/world/mod.rs b/minecraft-server/src/world/mod.rs index 3a2ed2dd..e6cc9968 100644 --- a/minecraft-server/src/world/mod.rs +++ b/minecraft-server/src/world/mod.rs @@ -158,6 +158,11 @@ impl World { } } } + + pub async fn tick(&'static self) { + self.entities.tick(self).await; + // TODO: tick world + } } #[cfg(test)]