1 module Engine.Component;
2 
3 
4 import t = Engine.Transform;
5 import std.traits;
6 import e = Engine.Entity;
7 import std.traits;
8 import std.typetuple;
9 import Engine.CStorage;
10 import Engine.util;
11 
12 class Component {
13 	package void* component;
14 	package TypeInfo type;
15 	package ComponentStorage storage;
16 
17 	this(T)(T t)  {
18 		component = cast(void*)t;
19 		static if (is(T == class)) {
20 			type = typeid(T);
21 			storage = StorageImpl!T.it;
22 		}
23 		else {
24 			alias U = baseType!T;
25 			type = typeid(U);
26 			storage = StorageImpl!U.it;
27 		}
28 	}
29 
30 	this()(void* component, ComponentStorage storage) {
31 		this.component = component;
32 		this.storage = storage;
33 		this.type = storage.Type();
34 	}
35 
36 	public Component Clone() {
37 		return new Component(storage.Clone(component), storage);
38 	}	
39 
40 	public ReturnType!T RunFunction(T,Args...)(string name, Args args) {
41 		auto func = FindFunction!T(name);
42 		if (func is null)
43 			return;
44 		static if (is(T == delegate)) {
45 			func.ptr = component;
46 			return func(args);
47 		} else {
48 			return func(args);
49 		}
50 	}
51 
52 	public void RunFunction(Args...)(string name, Args args) {
53 		RunFunction!(void delegate(Args))(name,args);
54 	}
55 
56 	public T FindFunction(T)(string name) {
57 		auto fnc = storage.FindFunction!T(name);
58 		static if (is(T == delegate)) {
59 			if (fnc is null)
60 				return null;
61 			fnc.ptr = component;
62 		}
63 		return fnc;
64 	}
65 
66 	public bool FindFunction(T)(auto ref T t, string name) {
67 		auto found = storage.FindFunction(t,name);
68 		static if (is(T == delegate)) {
69 			if (!found)
70 				return false;
71 			t.ptr = component;
72 		}
73 		return found;
74 	}
75 
76 	public auto Cast(T)() {
77 		return storage.Cast!T(component);
78 	}	
79 }
80 
81 template ComponentBase()
82 {
83 	public e.Entity _entity;
84 
85 	final @property public e.Entity entity() {
86 		return _entity;	
87 	};
88 
89 	final @property public t.Transform transform() {
90 		return entity.transform;
91 	};
92 }