Beispiel #1
0
 protected override Component BuildComponent(Entity entity)
 {
     StatsComponent result = new StatsComponent();
       result.Might = might;
       result.Craft = craft;
       result.Speed = speed;
       return result;
 }
Beispiel #2
0
        public void TestEntitySetup()
        {
            // Create some components.
              Component stats = new StatsComponent();
              Component pos = new PosComponent();

              Assert.AreEqual(null, stats.Entity);
              Assert.AreEqual(null, pos.Entity);

              // Create an entity, it starts with nothing but an id.
              Entity ent = new Entity("1")
            // Give it some components (using a handly fluent interface idiom).
            .Set(stats)
            .Set(pos);

              // Read the components back. Thanks to generic magic, we don't need to
              // refer to component families explicitly at all.
              StatsComponent stats2;
              Assert.IsTrue(ent.TryGet(out stats2));
              Assert.AreEqual(stats, stats2);

              PosComponent pos2;
              Assert.IsTrue(ent.TryGet(out pos2));
              Assert.AreEqual(pos, pos2);

              Assert.AreEqual(ent, stats.Entity);
              Assert.AreEqual(ent, pos.Entity);

              // Now for a somewhat subtle pitfall from the generic magic:
              try
              {
            // We declare something as just a Component and try to get it.
            Component comp;
            ent.TryGet(out comp);
            Assert.Fail("Exception didn't happen.");
              }
              catch(ApplicationException)
              {
            // The base component class doesn't have a valid family. Trying to get
            // a variable without a subcomponent type that has a specified family
            // causes a runtime exception.
              }

              // Another bad thing to do is to add a Component without a proper
              // GetFamily method:
              try
              {
            ent.Set(new BadComponent());
            Assert.Fail("Exception didn't happen.");
              }
              catch(ApplicationException)
              {
            // This also crashes, since the component type fails to describe
            // itself properly.
              }

              // Checking for null components.
              try
              {
            ent.Set(null);
            Assert.Fail("Exception didn't happen.");
              }
              catch(ArgumentNullException)
              {
              }

              // Removing a component.
              ent.Clear(pos.Family);

              Assert.IsTrue(ent.TryGet(out stats2));

              Assert.IsFalse(ent.TryGet(out pos2));

              Assert.AreEqual(ent, stats.Entity);
              Assert.AreEqual(null, pos.Entity);
        }