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

Commit

Permalink
Finish observe and mutate
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Nov 11, 2023
1 parent 1dcc179 commit 154dbbe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
18 changes: 9 additions & 9 deletions minecraft-entities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ pub trait TryAsEntityRef<T> {


pub trait EntityWorldInterface {
fn observe_entity(&self, eid: Eid, observer: &dyn FnOnce(&AnyEntity)) -> Pin<Box<dyn std::future::Future<Output = ()>>>;
fn mutate_entity(&self, eid: Eid, mutator: &dyn FnOnce(&mut AnyEntity)) -> Pin<Box<dyn std::future::Future<Output = ()>>>;
fn observe_entity(&'static self, eid: Eid, observer: Box<dyn FnOnce(&AnyEntity)>) -> Pin<Box<dyn std::future::Future<Output = ()>>>;
fn mutate_entity(&'static self, eid: Eid, mutator: Box<dyn FnOnce(&mut AnyEntity)>) -> Pin<Box<dyn std::future::Future<Output = ()>>>;
}

pub struct Handler<T> where AnyEntity: TryAsEntityRef<T> {
Expand All @@ -76,7 +76,7 @@ pub struct Handler<T> where AnyEntity: TryAsEntityRef<T> {
entity: std::marker::PhantomData<T>,
}

impl<T> Handler<T> where AnyEntity: TryAsEntityRef<T> {
impl<T: 'static> Handler<T> where AnyEntity: TryAsEntityRef<T> {
pub fn assume(id: Eid, world: &'static dyn EntityWorldInterface) -> Self {
Self {
eid: id,
Expand All @@ -93,16 +93,16 @@ impl<T> Handler<T> where AnyEntity: TryAsEntityRef<T> {
}
}

pub async fn observe(&self, observer: fn(&T)) {
self.world.observe_entity(self.eid, &|entity| {
pub async fn observe(&self, observer: impl FnOnce(&T) + 'static) {
self.world.observe_entity(self.eid, Box::new(move |entity| {
observer(entity.try_as_entity_ref().unwrap())
}).await;
})).await;
}

pub async fn mutate(&self, mutator: fn(&mut T)) {
self.world.mutate_entity(self.eid, &|entity| {
pub async fn mutate(&self, mutator: impl FnOnce(&mut T) + 'static) {
self.world.mutate_entity(self.eid, Box::new(move |entity| {
mutator(entity.try_as_entity_mut().unwrap())
}).await;
})).await;
}
}

Expand Down
10 changes: 0 additions & 10 deletions minecraft-server/src/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,3 @@ impl Entities {
self.entities.write().await.remove(&eid)
}
}

impl EntityWorldInterface for World {
fn observe_entity(&self, eid: Eid, observer: &dyn FnOnce(&AnyEntity)) -> Pin<Box<dyn std::future::Future<Output = ()>>> {
todo!()
}

fn mutate_entity(&self, eid: Eid, mutator: &dyn FnOnce(&mut AnyEntity)) -> Pin<Box<dyn std::future::Future<Output = ()>>> {
todo!()
}
}
14 changes: 14 additions & 0 deletions minecraft-server/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ impl World {
}
}

impl EntityWorldInterface for World {
fn observe_entity(&'static self, eid: Eid, observer: Box<dyn FnOnce(&AnyEntity)>) -> Pin<Box<dyn std::future::Future<Output = ()>>> {
Box::pin(async move {
self.entities.observe_entity(eid, observer).await;
})
}

fn mutate_entity(&'static self, eid: Eid, mutator: Box<dyn FnOnce(&mut AnyEntity)>) -> Pin<Box<dyn std::future::Future<Output = ()>>> {
Box::pin(async move {
self.entities.mutate_entity(eid, mutator).await;
})
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 154dbbe

Please sign in to comment.