Michael Ludwig avatar Michael Ludwig committed 4932c97

Change behavior of removals for owned objects, they are now forcibly disowned first.

Comments (0)

Files changed (3)

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

         if (fromTemplate.getEntitySystem() != getEntitySystem()) {
             throw new IllegalArgumentException("Component not owned by expected EntitySystem");
         }
-        if (!fromTemplate.getClass().equals(type)) {
-            throw new IllegalArgumentException("Component not of expected type, expected: " + type + ", but was: " + type);
+        if (!fromTemplate.getType().equals(type)) {
+            throw new IllegalArgumentException("Component not of expected type, expected: " + type + ", but was: " + fromTemplate.getType());
         }
         if (!fromTemplate.isLive()) {
             throw new IllegalStateException("Template component is not live");
      */
     private Component<T> allocateComponent(int entityIndex) {
         if (entityIndexToComponentRepository[entityIndex] != 0) {
-            if (!removeComponent(entityIndex)) {
-                // could not remove it because it was owned, so we abort
-                // the addition
-                return null;
-            }
+            removeComponent(entityIndex);
         }
 
         int componentIndex = componentInsert++;
         // This code works even if componentIndex is 0
         Component<T> oldComponent = components[componentIndex];
         if (oldComponent != null) {
-            if (oldComponent.getOwner() != null) {
-                // halt removal
-                return false;
-            } else {
-                oldComponent.delegate.disownAndRemoveChildren();
-                oldComponent.index = 0;
-            }
+            oldComponent.setOwner(null);
+            oldComponent.delegate.disownAndRemoveChildren();
+            oldComponent.index = 0;
         }
 
         components[componentIndex] = null;

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

     /**
      * <p>
      * Add a new Component with a data type T to this Entity. If there already
-     * exists a component of type T and it is not owned, it is removed first,
-     * and a new one is instantiated. If there is an existing component that is
-     * owned, it is not removed and the addition fails. To designate this, null
-     * is returned.
+     * exists a component of type T, it is removed first, and a new one is
+     * instantiated.
      * </p>
      * 
      * @param <T> The parameterized type of component being added
      * @param componentType The component type
-     * @return A new component of type T, or null if there was an existing
-     *         component of type T and it is owned
+     * @return A new component of type T
      * @throws NullPointerException if componentId is null
      */
     public <T extends ComponentData<T>> Component<T> add(Class<T> componentType) {
      * Add a new Component with a data of type T to this Entity, but the new
      * component's state will be cloned from the given Component instance. The
      * <tt>toClone</tt> instance must still be live. If there already exists a
-     * component of type T and it is not owned, it is removed first, and a new
-     * one is instantiated. If there is an existing component that is owned, it
-     * is not removed and the addition fails. To designate this, null is
-     * returned.
+     * component of type T, it is removed first, and a new one is instantiated.
      * </p>
      * <p>
      * The new component is initialized by cloning the property values from
      * 
      * @param <T> The parameterized type of component to add
      * @param toClone The existing T to clone when attaching to this component
-     * @return A new component of type T, or null if there was an existing
-     *         component of type T and it is owned
+     * @return A new component of type T
      * @throws NullPointerException if toClone is null
      * @throws IllegalArgumentException if toClone is not from the same system
      *             as this entity
      * return false from {@link Component#isLive()}. This will remove the
      * component even if the component has been disabled.
      * <p>
-     * However, if the component is has a non-null owner, it is not removed and
-     * false is returned. When a component is removed, all entities and
-     * components that it owns are also removed.
+     * When a Component is removed, it will set its owner to null, and disown
+     * all of its owned objects. If any of those owned objects are entities or
+     * components, they are removed from the system as well.
      * 
      * @param <T> The parameterized type of component to remove
      * @param componentType The component type
-     * @return True if a component was removed, false if there was no component
-     *         or if the component has an owner
+     * @return True if a component was removed
      * @throws NullPointerException if componentId is null
      */
     public <T extends ComponentData<T>> boolean remove(Class<T> componentType) {

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

      * entity's components will become invalid until assigned to a new
      * component.
      * <p>
-     * If the entity has a non-null owner, the removal does not occur and false
-     * is returned. When an entity is removed, all entities and components that
-     * it owns are also removed.
+     * <p>
+     * When an entity is removed, it will set its owner to null, and disown all
+     * of its owned objects. If any of those owned objects are entities or
+     * components, they are removed from the system as well.
      * 
      * @param e The entity to remove
-     * @return True if the entity was removed, false if the entity has an owner
-     *         preventing its removal
      * @throws NullPointerException if e is null
      * @throws IllegalArgumentException if the entity was not created by this
      *             system, or already removed
      */
-    public boolean removeEntity(Entity e) {
+    public void removeEntity(Entity e) {
         if (e == null) {
             throw new NullPointerException("Cannot remove a null entity");
         }
             throw new IllegalArgumentException("Entity has already been removed");
         }
 
-        if (e.getOwner() != null) {
-            // already owned, so abort
-            return false;
-        }
-
         // Handle ownership removals
+        e.setOwner(null);
         e.delegate.disownAndRemoveChildren();
 
         // Remove all components from the entity (that weren't removed
         // clear out the entity
         entities[e.index] = null;
         e.index = 0;
-
-        return true;
     }
 
     /**
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.