Commits

Michael Ludwig committed 105aead

Implement onSet() in ComponentData

Comments (0)

Files changed (4)

src/main/java/com/lhkbob/entreri/ComponentData.java

     }
 
     /**
+     * Event hook called when this ComponentData is assigned to a valid
+     * component at the provided non-zero index.
+     * 
+     * @param index The new index
+     */
+    protected void onSet(int index) { 
+        // do nothing in base class
+    }
+
+    /**
      * A slightly faster method that requires only an index to a component, and
      * performs no validation. It also does not look up the component reference
      * since it assumes it's valid. These are lazily done when needed.
     boolean setFast(int componentIndex) {
         index = componentIndex;
         id = owner.getId(index);
+        onSet(index);
         return index != 0;
     }
 }

src/test/java/com/lhkbob/entreri/ComponentDataTest.java

 
 import org.junit.Test;
 
-import com.lhkbob.entreri.Component;
-import com.lhkbob.entreri.Entity;
-import com.lhkbob.entreri.EntitySystem;
-import com.lhkbob.entreri.TypeId;
 import com.lhkbob.entreri.component.IntComponent;
+import com.lhkbob.entreri.component.OnSetComponent;
 
 public class ComponentDataTest {
     @Test
         cd.set(e.add(TypeId.get(IntComponent.class)));
         Assert.assertTrue(cd.isValid());
     }
+    
+    @Test
+    public void testOnSetInvoked() {
+        // must test both public set() and default setFast()
+        EntitySystem system = new EntitySystem();
+        Entity e = system.addEntity();
+        Component<OnSetComponent> c = e.add(TypeId.get(OnSetComponent.class));
+        OnSetComponent cd = system.createDataInstance(TypeId.get(OnSetComponent.class));
+        
+        // sanity checks (onset gets called once during initialization)
+        Assert.assertEquals(0, cd.onsetIndex);
+        Assert.assertTrue(cd.onsetCalled);
+        
+        // reset
+        cd.onsetCalled = false;
+        cd.onsetIndex = 0;
+        
+        // trigger a regular set() call
+        cd.set(c);
+        Assert.assertEquals(c.index, cd.onsetIndex);
+        Assert.assertEquals(cd.getIndex(), cd.onsetIndex);
+        Assert.assertTrue(cd.onsetCalled);
+        
+        // reset
+        cd.onsetCalled = false;
+        cd.onsetIndex = 0;
+        
+        // trigger a setFast() call
+        e.get(cd);
+        Assert.assertEquals(c.index, cd.onsetIndex);
+        Assert.assertEquals(cd.getIndex(), cd.onsetIndex);
+        Assert.assertTrue(cd.onsetCalled);
+    }
 }

src/test/java/com/lhkbob/entreri/ReflectionComponentDataFactoryTest.java

             c.setFloat(i);
         }
         
-        int i = 0;
         UnmanagedFieldComponent c = system.createDataInstance(id);
         for (Entity e: system) {
             Assert.assertTrue(e.get(c));
             float f = c.getFloat();
             Assert.assertEquals(0, f, .0001f);
-            i++;
         }
     }
 }

src/test/java/com/lhkbob/entreri/component/OnSetComponent.java

+package com.lhkbob.entreri.component;
+
+import com.lhkbob.entreri.ComponentData;
+import com.lhkbob.entreri.annot.Unmanaged;
+
+public class OnSetComponent extends ComponentData<OnSetComponent> {
+    @Unmanaged
+    public int onsetIndex;
+    
+    @Unmanaged
+    public boolean onsetCalled;
+    
+    protected OnSetComponent() { }
+    
+    @Override
+    protected void onSet(int index) {
+        onsetIndex = index;
+        onsetCalled = true;
+    }
+}