Skip to content

Commit

Permalink
Prefabs - refactoring, inheritance, serialization and unit tests for …
Browse files Browse the repository at this point in the history
…all that.
  • Loading branch information
t3hk0d3 committed Mar 4, 2012
1 parent 4cb4b03 commit 6068ec9
Show file tree
Hide file tree
Showing 18 changed files with 744 additions and 222 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ repositories {
dependencies{
groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.2'
compile group: 'com.google.guava', name: 'guava', version: '11.0.1'
compile group: 'com.google.code.gson', name: 'gson', version: '2.1'
compile fileTree(dir: 'libs', include: '*.jar', exclude: 'mockito-all-1.9.0.jar')
testCompile 'junit:junit:4.10'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.0'
Expand Down
Binary file added libs/gson-2.1.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions src/org/terasology/components/CharacterMovementComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public final class CharacterMovementComponent extends AbstractComponent {

public void store(StorageWriter writer) {
//To change body of implemented methods use File | Settings | File Templates.


}

public void retrieve(StorageReader reader) {
Expand Down
24 changes: 24 additions & 0 deletions src/org/terasology/components/LocationComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,28 @@ public void retrieve(StorageReader reader) {
scale = reader.readFloat("scale", 1.0f);
parent = reader.read("parent", EntityRef.class, parent);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

LocationComponent component = (LocationComponent) o;

if (Float.compare(component.scale, scale) != 0) return false;
if (parent != null ? !parent.equals(component.parent) : component.parent != null) return false;
if (position != null ? !position.equals(component.position) : component.position != null) return false;
if (rotation != null ? !rotation.equals(component.rotation) : component.rotation != null) return false;

return true;
}

@Override
public int hashCode() {
int result = position != null ? position.hashCode() : 0;
result = 31 * result + (rotation != null ? rotation.hashCode() : 0);
result = 31 * result + (scale != +0.0f ? Float.floatToIntBits(scale) : 0);
result = 31 * result + (parent != null ? parent.hashCode() : 0);
return result;
}
}
24 changes: 19 additions & 5 deletions src/org/terasology/entitySystem/AbstractComponent.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package org.terasology.entitySystem;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
* @author Immortius <[email protected]>
*/
public abstract class AbstractComponent implements Component {

public String getName() {
int index = getClass().getSimpleName().lastIndexOf("Component");
if (index != -1) {
return getClass().getSimpleName().substring(0, index).toLowerCase();
String className = getClass().getSimpleName().toLowerCase();

if (className.endsWith("component")) {
return className.substring(0, className.lastIndexOf("component"));
}

return className;
}

public Component clone() {
try {
return (Component) super.clone();
} catch (CloneNotSupportedException e) {
// this shouldn't happen
throw new InternalError();
}
return getClass().getSimpleName().toLowerCase();
}

}
48 changes: 48 additions & 0 deletions src/org/terasology/entitySystem/AbstractPrefab.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.terasology.entitySystem;

import com.google.common.base.Objects;

/**
* @todo javadoc
*/
public abstract class AbstractPrefab implements Prefab {

private String name;

protected AbstractPrefab(String name) {
this.name = name;
}

public String getName() {
return name;
}

public Prefab clone() {
try {
return (Prefab) super.clone();
} catch (CloneNotSupportedException e) {
// this shouldn't happen
throw new InternalError();
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof Prefab) {
return Objects.equal(name, ((Prefab) o).getName());
}

return false;
}

@Override
public int hashCode() {
return Objects.hashCode(name);
}

@Override
public String toString() {
return "Prefab(" + name + "){ components: " + this.listOwnComponents() + ", parents: " + this.getParents() + " }";
}
}
4 changes: 3 additions & 1 deletion src/org/terasology/entitySystem/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*
* @author Immortius <[email protected]>
*/
public interface Component extends Persistable {
public interface Component extends Persistable, Cloneable {
public String getName();

public Component clone();
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
package org.terasology.entitySystem;

/**
* An entity prefab describes the recipe for creating an entity.
* Like an entity it groups a collection of components.
* @author Immortius <[email protected]>
*/
public interface PrefabRef {
/**
* @return The identifier for this prefab
*/
String getName();

/**
*
* @param newName
* @throws IllegalArgumentException If the new name is already in use
*/
void rename(String newName);

/**
*
* @param componentClass
* @param <T>
* @return The requested component, or null if the entity doesn't have a component of this class
*/
<T extends Component> T getComponent(Class<T> componentClass);

/**
* Adds a component to this entity. If the entity already has a component of the same class it is replaced.
* @param component
*/
<T extends Component> T addComponent(T component);

/**
* @param componentClass
*/
void removeComponent(Class<? extends Component> componentClass);

/**
* @param component
*/
void saveComponent(Component component);

/**
* Iterates over all the components
* @return
*/
Iterable<Component> iterateComponents();

/**
* Removes this prefab from the prefab manager
*/
void destroy();

}
package org.terasology.entitySystem;

/**
* An entity prefab describes the recipe for creating an entity.
* Like an entity it groups a collection of components.
*
* @author Immortius <[email protected]>
*/
public interface Prefab extends Cloneable{

/**
* @return The identifier for this prefab
*/
public String getName();

/**
*
* @param componentClass
* @param <T>
* @return The requested component, or null if the entity doesn't have a component of this class
*/
public <T extends Component> T getComponent(Class<T> componentClass);

/**
* Adds a component to this entity. If the entity already has a component of the same class it is replaced.
* @param component
*/
public <T extends Component> T setComponent(T component);

/**
* @param componentClass
*/
public void removeComponent(Class<? extends Component> componentClass);

/**
* Iterates over all the components
* @return
*/
public Iterable<Component> listComponents();

/**
* Iterate only over OWN components, excluding inheritance.
* Required for proper serializing
*
* @return
*/
public Iterable<Component> listOwnComponents();

/**
* Return parents prefabs
*
* @return
*/
public Iterable<Prefab> getParents();

public void addParent(Prefab parent);

public void removeParent(Prefab parent);

public Prefab clone();

}
42 changes: 19 additions & 23 deletions src/org/terasology/entitySystem/PrefabManager.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
package org.terasology.entitySystem;

/**
*
* @todo Write javadoc
* @author Immortius <[email protected]>
*/
public interface PrefabManager {

/**
* @param name
* @return A new PrefabRef
* @throws IllegalArgumentException when name is already in use
*/
PrefabRef create(String name);
public Prefab createPrefab(String name);

PrefabRef get(String name);
public Prefab getPrefab(String name);

boolean exists(String name);

/**
* @param oldName
* @param name
* @return A new PrefabRef with the new name
* @throws IllegalArgumentException if either the oldName isn't in use or the new name is already in use
*/
PrefabRef rename(String oldName, String name);

void destroy(String name);

<T extends Component> T getComponent(String name, Class<T> componentClass);
void addComponent(String name, Component component);
<T extends Component> void removeComponent(String name, Class<T> componentClass);
void saveComponent(String name, Component component);
public boolean exists(String name);

public Prefab registerPrefab(Prefab prefab);

public Iterable<Prefab> listPrefabs();

public void removePrefab(String name);

public <T extends Component> T getComponent(String name, Class<T> componentClass);

public <T extends Component> T setComponent(String name, T component);


public <T extends Component> void removeComponent(String name, Class<T> componentClass);

}
Loading

0 comments on commit 6068ec9

Please sign in to comment.