Michael Ludwig  committed 7359a64

Add an add() method to Entity that takes a template component, and fix documentation in EntitySystem.

  • Participants
  • Parent commits b221243
  • Branches default

Comments (0)

Files changed (2)

File src/main/java/com/googlecode/entreri/

      * @param componentId The TypedId of the component type
      * @param initParams The initialization parameters required for the
      *            component type
-     * @return A new component of type T, or an existing T if already attached
+     * @return A new component of type T
      * @throws NullPointerException if componentId is null
      * @throws IllegalArgumentException if any of the init params are invalid
+     * Add a Component of type T to this Entity, but clone its state from the
+     * existing component of type T. The existing component must still be
+     * attached to an Entity other than this entity, but it could be from a
+     * different EntitySystem. If there already exists a component of type T
+     * added to this entity, it is removed first, and a new one is instantiated.
+     * 
+     * @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
+     * @throws NullPointerException if toClone is null
+     */
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    public <T extends Component> T add(T toClone) {
+        if (toClone == null)
+            throw new NullPointerException("Component template, toClone, cannot be null");
+        ComponentIndex ci = system.getIndex(toClone.getTypedId());
+        return (T) ci.addComponent(index, toClone);
+    }
+    /**
      * Remove any attached Component of the given type, T, from this Entity.
      * True is returned if a component was removed, and false otherwise. If a
      * component is removed, the component should no longer be used and it will

File src/main/java/com/googlecode/entreri/

      * <p>
      * The iterator returns the canonical Component instance for each component
      * of the type in the system. This is the same instance that was returned by
-     * {@link Entity#add(TypedId)} and is safe to access and store after
+     * {@link Entity#add(TypedId, Object...)} and is safe to access and store after
      * iteration has completed.
      * </p>