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

Commit

Permalink
Merge pull request #16 from Mubelotix/poo_experiment2
Browse files Browse the repository at this point in the history
Implement entity framework
  • Loading branch information
Mubelotix authored Nov 18, 2023
2 parents 4bf53ad + 400d2f9 commit 48c9de9
Show file tree
Hide file tree
Showing 118 changed files with 2,877 additions and 1,544 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ members = [
"minecraft-protocol",
"minecraft-protocol-derive",
"minecraft-server",
"minecraft-entities",
"minecraft-positions",
"tags-macros"
"minecraft-entities-derive",
"minecraft-positions"
]

workspace.resolver = "2"
3 changes: 3 additions & 0 deletions minecraft-entities-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ proc-macro = true

[dependencies]
convert_case = "0.6"
proc-macro-error = "1.0.4"
quote = "1.0"
proc-macro2 = "1.0"
133 changes: 133 additions & 0 deletions minecraft-entities-derive/examples/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use minecraft_entities_derive::*;

use std::{pin::Pin, future::Future, sync::{Mutex, Arc}};
type CallBack<O> = fn(O) -> Pin<Box<dyn Future<Output = ()> + Sync + Send>>;
type CallBack1<O, I> = fn(O, I) -> Pin<Box<dyn Future<Output = ()> + Sync + Send>>;
type CallBack2<O, I, J> = fn(O, I, J) -> Pin<Box<dyn Future<Output = ()> + Sync + Send>>;
type Eid = u32;

trait TryAsEntityRef<T> {
fn try_as_entity_ref(&self) -> Option<&T>;
fn try_as_entity_mut(&mut self) -> Option<&mut T>;
}

enum AnyEntity {
Entity(Entity),
Animal(Animal),
Cow(Cow),
}

pub struct Handler<T> {
uuid: Eid,
world: Arc<Mutex<()>>,
entity: std::marker::PhantomData<T>,
}

impl<T> Handler<T> {
fn assume(uuid: Eid, world: Arc<Mutex<()>>) -> Self {
Self {
uuid,
world,
entity: std::marker::PhantomData,
}
}

fn assume_other<V>(self) -> Handler<V> {
Handler {
uuid: self.uuid,
world: self.world,
entity: std::marker::PhantomData,
}
}
}

// Entity

#[MinecraftEntity(
inheritable,
ancestors { },
descendants { Animal... },
defines {
on_moved(self, from: f32, to: f32);
on_spawned(self);
}
)]
pub struct Entity {

}

impl Handler<Entity> {
async fn on_moved(self, from: f32, to: f32) {
println!("Entity moved from {} to {}", from, to);
}

async fn on_spawned(self) {
println!("Entity spawned");
}
}

// Animal

#[MinecraftEntity(
inheritable,
ancestors { Entity },
descendants { Cow },
defines {
Entity.on_spawned(self);
on_hit(self, damage: usize);
on_jump(self);
}
)]
pub struct Animal {
entity: Entity,
}

impl Handler<Animal> {
async fn on_hit(self, damage: usize) {
println!("Animal hit with {} damage", damage);
}

async fn on_jump(self) {
println!("Animal jumped");
}

async fn on_spawned(self) {
println!("Animal spawned");
}
}

// Cow

#[MinecraftEntity(
ancestors { Animal, Entity },
defines {
Entity.on_spawned(self);
Animal.on_hit(self, damage: usize);
on_milked(self);
}
)]
pub struct Cow {
animal: Animal,
}

impl Handler<Cow> {
async fn on_milked(self) {
println!("Cow milked");
}

async fn on_hit(self, damage: usize) {
println!("Cow hit with {} damage", damage);
}

async fn on_spawned(self) {
println!("Cow spawned");
}
}

fn main() {
}

#[test]
fn test() {

}
Loading

0 comments on commit 48c9de9

Please sign in to comment.