1 module example;
2        
3 import std.stdio;
4 import artemisd.all;
5 
6 final class Position : Component
7 {
8     mixin TypeDecl;
9 
10     float x;
11     float y;
12 
13     this(float x, float y)
14     {
15         this.x = x;
16         this.y = y;
17     }
18 }
19 
20 final class Velocity : Component
21 {
22     mixin TypeDecl;
23 
24     float x;
25     float y;
26 
27     this(float x, float y)
28     {
29         this.x = x;
30         this.y = y;
31     }
32 }
33 
34 final class Renderer : Component
35 {
36     mixin TypeDecl;
37 }
38 
39 final class MovementSystem : EntityProcessingSystem
40 {
41     mixin TypeDecl;
42 
43     this()
44     {
45         super(Aspect.getAspectForAll!(Position,Velocity));
46     }
47 
48     override void process(Entity e)
49     {
50         Position pos = e.getComponent!Position;
51         Velocity vel = e.getComponent!Velocity;
52 
53         assert(pos !is null);
54         assert(vel !is null);
55 
56         pos.x += vel.x * world.getDelta();
57         pos.y += vel.y * world.getDelta();
58 
59         writeln(e, " move to (", pos.x, ",", pos.y, ")");
60     }
61 }
62 
63 final class RenderSystem : EntityProcessingSystem
64 {
65     mixin TypeDecl;
66 
67     this()
68     {
69         super(Aspect.getAspectForAll!(Position, Renderer));
70     }
71 
72     override void process(Entity e)
73     {
74         Renderer rend = e.getComponent!Renderer;
75         Position pos = e.getComponent!Position;
76         assert(pos !is null);
77         assert(rend !is null);
78 
79         writeln(e, " rendered at (", pos.x, ",", pos.y, ")");
80     }
81 }
82 
83 void main(string[] argv)
84 {
85     World world = new World();
86     world.setSystem(new MovementSystem);
87     world.setSystem(new RenderSystem);
88     world.initialize();
89 
90     Entity e = world.createEntity();
91     e.addComponent(new Position(0,0));
92     e.addComponent(new Velocity(10,0));
93     e.addToWorld();
94 
95     Entity e1 = world.createEntity();
96     e1.addComponent(new Position(0,0));
97     e1.addComponent(new Velocity(10,0));
98     e1.addComponent(new Renderer);
99     e1.addToWorld();
100 
101     import core.thread;
102     
103     while(true)
104     {
105         world.setDelta(1/60.0f);
106         world.process();
107         Thread.sleep(1000.msecs);
108     }
109 }