Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Run tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Jan 4, 2024
1 parent 8f43a42 commit b7776e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions minecraft-server/src/server_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions minecraft-server/src/world/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<E>(&self, entity: AnyEntity, world: &'static World, receiver: BroadcastReceiver<ServerMessage>) -> (Eid, UUID)
where AnyEntity: TryAsEntityRef<E>, Handler<E>: EntityExt
{
Expand Down Expand Up @@ -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::<Entity>::assume(*eid, world);
task.tick(h).await;
}
}
}
5 changes: 5 additions & 0 deletions minecraft-server/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ impl World {
}
}
}

pub async fn tick(&'static self) {
self.entities.tick(self).await;
// TODO: tick world
}
}

#[cfg(test)]
Expand Down

0 comments on commit b7776e9

Please sign in to comment.