Advertisement
Guest User

eca-d example

a guest
Aug 23rd, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.92 KB | None | 0 0
  1. import eca.component;
  2. import eca.entity;
  3. import std.stdio : writefln;
  4. import std.conv : to;
  5. import core.time;
  6.  
  7. void main()
  8. {
  9.     //Create an updater, which will update 30 times per second
  10.     auto updater = Updater();
  11.  
  12.     //Create list of entities
  13.     auto entities = [
  14.         new Entity(
  15.             new PositionComponent(0,10),
  16.             new MovementComponent(),
  17.             new PhysicsComponent()
  18.         ),
  19.         new Entity(
  20.             new PositionComponent(10, 41),
  21.             new GraphicsComponent(),
  22.         ),
  23.     ];
  24.  
  25.     //Loop for 5 seconds
  26.     MonoTime time = MonoTime.currTime();
  27.     while(MonoTime.currTime() < time + 5.seconds)
  28.     {
  29.         //Loop through all entities as fast as possible
  30.         foreach(entity; entities)
  31.         {
  32.             //TODO: Is there a better way to handle rendering?
  33.  
  34.             //Check if a graphics component is present, then render
  35.             if(entity.hasComponent!GraphicsComponent)
  36.             {
  37.                 (entity.getComponent!GraphicsComponent).update(entity);
  38.             }
  39.  
  40.             //Update according to how much time has passed
  41.             foreach(update; 0 .. updater.getUpdates())
  42.             {
  43.                 entity.update(); //This is where the magic happens
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49.  
  50. class PositionComponent : Component
  51. {
  52.     this(float x, float y)
  53.     {
  54.         this.x = x;
  55.         this.y = y;
  56.     }
  57.  
  58.     float x, y;
  59. }
  60.  
  61. //Custom components
  62. class MovementComponent : Component
  63. {
  64.     override void update(Entity entity)
  65.     {
  66.         if(entity.hasComponent!PositionComponent)
  67.         {
  68.             auto pos = entity.getComponent!PositionComponent;
  69.  
  70.             pos.x += xvel;
  71.             pos.y += yvel;
  72.         }
  73.     }
  74.  
  75.     float xvel, yvel;
  76. }
  77.  
  78. class PhysicsComponent : Component
  79. {
  80.     immutable gravity = 5; //Arbitrary number
  81.  
  82.     override void update(Entity entity)
  83.     {
  84.         if(entity.hasComponent!(PositionComponent, MovementComponent))
  85.         {
  86.             auto mov = entity.getComponent!MovementComponent;
  87.  
  88.             mov.yvel -= gravity;
  89.         }
  90.     }
  91. }
  92.  
  93. class GraphicsComponent : Component
  94. {
  95.     override void update(Entity entity)
  96.     {
  97.         /+ code for rendering to screen +/
  98.     }
  99. }
  100.  
  101.  
  102. ///Misc, used for ticking 30 times per second.
  103. struct Updater
  104. {
  105.     double getUpdates()
  106.     {
  107.         /* Kudos to Yepoleb who helped me with this */
  108.         MonoTime newtime = MonoTime.currTime();
  109.         Duration duration = newtime - _lasttime;
  110.         double durationmsec = duration.total!"nsecs" / (10.0 ^^ 6);
  111.  
  112.         _lasttime = newtime;
  113.         _msecs += durationmsec;
  114.         int updates = cast(int)(_msecs / _interval);
  115.         _msecs -= updates * _interval;
  116.  
  117.         return updates;
  118.     }
  119.  
  120.     void resetUpdates()
  121.     {
  122.         _lasttime = MonoTime.currTime();
  123.     }
  124.  
  125.     private MonoTime _lasttime;
  126.     private double _msecs = 0, _interval = 1000/30;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement