Skip to content

Instantly share code, notes, and snippets.

@RChehowski
Created January 28, 2019 12:54
Show Gist options
  • Save RChehowski/36073c60651f50eae4aa93c176a079e3 to your computer and use it in GitHub Desktop.
Save RChehowski/36073c60651f50eae4aa93c176a079e3 to your computer and use it in GitHub Desktop.
GameObject java
static class GameObject
{
private Map<Class<?>, List<Number>> componentsByClass = new HashMap<>();
public void AddComponent(Class<? extends Number> componentClass) throws IllegalAccessException, InstantiationException
{
if (componentClass != null)
{
List<Number> components;
if ((components = componentsByClass.get(componentClass)) == null)
{
components = new ArrayList<>();
componentsByClass.put(componentClass, components);
}
components.add(componentClass.newInstance());
}
}
public <T extends Number> T GetComponent(Class<T> clazz)
{
final List<Number> components = componentsByClass.get(clazz);
return (T)components.get(0);
}
public <T extends Number> List<T> GetComponents(Class<T> clazz)
{
final List<Number> components = componentsByClass.get(clazz);
return (List<T>)Collections.unmodifiableList(components);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment