1 module Engine.Systems.BatchSystem;
2 
3 import Engine.System;
4 import Engine.CStorage;
5 import Engine.Entity;
6 import Engine.Batch;
7 
8 
9 class BatchSystem : System {
10 
11 	package static Batch[] batches;
12 
13 	override void start() {
14 
15 	}
16 
17 	override void onEntityEnter(Entity e) {
18 		foreach(c ; e.Components) {
19 			auto b = c.Cast!(Batchable)();
20 			if (b !is null) {
21 				AddBatch(e,b);
22 			}
23         }
24 	}
25 
26 	override void onEntityLeave(Entity e) {
27 		foreach(c ; e.Components) {
28 			auto b = c.Cast!(BatchData)();
29 			if (b !is null) {
30 				b.batch.Remove(e, b.batchable);
31 			}
32         }
33 	}
34 
35 	public static void AddBatch(Entity entity, Batchable batch) {
36 		auto mat = batch.material;
37 		foreach(ref b; batches) {
38 			if (b.material == mat) {
39 				b.Add(entity,batch);
40 				return;
41 			}
42 		}	
43 		auto b = new Batch(4, mat);
44 		batches ~= b;
45 		b.Add(entity, batch);
46 	}
47 
48 	public override void process() {
49 		foreach (ref b; batches) {
50 			b.Update();
51 			b.Draw();
52 		}
53 	}	
54 
55 	@property override Timing timing() { 
56 		return Timing.Draw;
57 	}
58 }