Created
March 2, 2017 04:13
-
-
Save Root3287/d2ef12cfca9c3c8f9010c2a337bd0ecb to your computer and use it in GitHub Desktop.
ECS/ Componet System
This file contains 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
package components; | |
public class Components implements ComponentsInterface{ | |
} |
This file contains 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
package components; | |
public interface ComponentsInterface { | |
} |
This file contains 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
package entities; | |
import java.util.HashMap; | |
import java.util.UUID; | |
import components.Components; | |
public abstract class Entites { | |
public UUID id; | |
//List of all entities. | |
protected static HashMap<Class, HashMap<UUID, ? extends Components>> components = new HashMap<>(); | |
protected Entites(){ | |
this.id = UUID.randomUUID(); | |
// Add to list of all entities | |
} | |
public static <T extends Components> void addComponents(UUID entity, T component){ | |
synchronized(components){ | |
HashMap<UUID, ? extends Components> store = components.get(component); | |
if(store == null){ | |
store = new HashMap<UUID, T>(); | |
components.put(component.getClass(), store); | |
} | |
((HashMap<UUID, T>) store).put(entity, component); | |
} | |
} | |
public <T extends Components> void addComponents(T component){ | |
synchronized(components){ | |
HashMap<UUID, ? extends Components> store = components.get(component); | |
if(store == null){ | |
store = new HashMap<UUID, T>(); | |
components.put(component.getClass(), store); | |
} | |
((HashMap<UUID, T>) store).put(this.id, component); | |
} | |
} | |
public static <T> T getComponents(UUID entity, Class<T> component){ | |
HashMap<UUID, ? extends Components> store = components.get(component); | |
T results = (T) store.get(entity); | |
if(results == null) | |
throw new IllegalArgumentException("Get Fail: "+entity.toString()+" does not posses Component of Class \n missing: "+ component); | |
return results; | |
} | |
public <T> T getComponents( Class<T> component) { | |
HashMap<UUID, ? extends Components> store = components.get(component); | |
T results = (T) store.get(this.id); | |
if(results == null) | |
throw new IllegalArgumentException("Get Fail: "+this.id.toString()+" does not posses Component of Class \n missing: "+ component); | |
return results; | |
} | |
public static <T> boolean hasComponents(UUID entity, Class<T> component){ | |
HashMap<UUID, ? extends Components> store = components.get(component); | |
T results = (T) store.get(entity); | |
if(results == null) | |
return false; | |
return true; | |
} | |
public <T> boolean hasComponents( Class<T> component) { | |
HashMap<UUID, ? extends Components> store = components.get(component); | |
if((T) store.get(id) == null) | |
return false; | |
return true; | |
} | |
} |
This file contains 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
package main; | |
import components.NameComponent; | |
import entities.Entites; | |
import entities.Player; | |
public class Main { | |
public static void main(String[] args) { | |
Player jack = new Player(); | |
NameComponent jackName = new NameComponent(); | |
jackName.name = "Jackdsafasdfsa"; | |
Entites.addComponents(jack.id, jackName); | |
System.out.println(jack.getComponents(NameComponent.class).name); | |
} | |
} |
This file contains 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
package components; | |
public class NameComponent extends Components{ | |
public String name; | |
} |
This file contains 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
package entities; | |
import components.PositionComponent; | |
public class Player extends Entites{ | |
public Player() { | |
super(); | |
PositionComponent position = new PositionComponent(); | |
position.x = 10f; | |
position.y = 20f; | |
addComponents(position); | |
} | |
} |
This file contains 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
package components; | |
public class PositionComponent extends Components{ | |
public float x, y; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment