Created
January 28, 2019 12:54
-
-
Save RChehowski/36073c60651f50eae4aa93c176a079e3 to your computer and use it in GitHub Desktop.
GameObject java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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