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 Renderer rend = e.getComponent!Renderer; 53 assert(pos !is null); 54 assert(vel !is null); 55 assert(rend is null); 56 57 pos.x += vel.x * world.getDelta(); 58 pos.y += vel.y * world.getDelta(); 59 60 writeln(e, " move to (", pos.x, ",", pos.y, ")"); 61 } 62 } 63 64 final class RenderSystem : EntityProcessingSystem 65 { 66 mixin TypeDecl; 67 68 this() 69 { 70 super(Aspect.getAspectForAll!(Position, Renderer)); 71 } 72 73 override void process(Entity e) 74 { 75 Renderer rend = e.getComponent!Renderer; 76 Position pos = e.getComponent!Position; 77 assert(pos !is null); 78 assert(rend !is null); 79 80 writeln(e, " rendered at (", pos.x, ",", pos.y, ")"); 81 } 82 } 83 84 void main(string[] argv) 85 { 86 World world = new World(); 87 world.setSystem(new MovementSystem); 88 world.setSystem(new RenderSystem); 89 world.initialize(); 90 91 Entity e = world.createEntity(); 92 e.addComponent(new Position(0,0)); 93 e.addComponent(new Velocity(10,0)); 94 e.addToWorld(); 95 96 Entity e1 = world.createEntity(); 97 e1.addComponent(new Position(0,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(dur!("msecs")(1000)); 108 } 109 }