Group collisions

This commit is contained in:
Anuken
2020-02-10 09:22:19 -05:00
parent e9b066b866
commit fbadb9f4aa
4 changed files with 31 additions and 13 deletions

View File

@@ -56,10 +56,10 @@ public class Annotations{
} }
/** Creates a group that only examines entities that have all the components listed. */ /** Creates a group that only examines entities that have all the components listed. */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface GroupDef{ public @interface GroupDef{
Class[] value(); Class[] value();
Class[] collide() default {};
boolean spatial() default false; boolean spatial() default false;
boolean mapping() default false; boolean mapping() default false;
} }

View File

@@ -35,7 +35,7 @@ public class EntityProcess extends BaseProcessor{
ObjectMap<Svar, String> varInitializers = new ObjectMap<>(); ObjectMap<Svar, String> varInitializers = new ObjectMap<>();
ObjectMap<Smethod, String> methodBlocks = new ObjectMap<>(); ObjectMap<Smethod, String> methodBlocks = new ObjectMap<>();
ObjectSet<String> imports = new ObjectSet<>(); ObjectSet<String> imports = new ObjectSet<>();
Array<Smethod> allGroups = new Array<>(); Array<Selement> allGroups = new Array<>();
Array<Selement> allDefs = new Array<>(); Array<Selement> allDefs = new Array<>();
Array<Stype> allInterfaces = new Array<>(); Array<Stype> allInterfaces = new Array<>();
@@ -45,7 +45,7 @@ public class EntityProcess extends BaseProcessor{
@Override @Override
public void process(RoundEnvironment env) throws Exception{ public void process(RoundEnvironment env) throws Exception{
allGroups.addAll(methods(GroupDef.class)); allGroups.addAll(elements(GroupDef.class));
allDefs.addAll(elements(EntityDef.class)); allDefs.addAll(elements(EntityDef.class));
allInterfaces.addAll(types(EntityInterface.class)); allInterfaces.addAll(types(EntityInterface.class));
@@ -169,17 +169,18 @@ public class EntityProcess extends BaseProcessor{
}else if(round == 2){ //round 2: get component classes and generate interfaces for them }else if(round == 2){ //round 2: get component classes and generate interfaces for them
//parse groups //parse groups
for(Smethod group : allGroups){ for(Selement<?> group : allGroups){
GroupDef an = group.annotation(GroupDef.class); GroupDef an = group.annotation(GroupDef.class);
Array<Stype> types = types(an, GroupDef::value).map(this::interfaceToComp);; Array<Stype> types = types(an, GroupDef::value).map(this::interfaceToComp);
groupDefs.add(new GroupDefinition(group.name(), ClassName.bestGuess(packageName + "." + interfaceName(types.first())), types, an.spatial(), an.mapping())); Array<Stype> collides = types(an, GroupDef::collide);
groupDefs.add(new GroupDefinition(group.name(), ClassName.bestGuess(packageName + "." + interfaceName(types.first())), types, an.spatial(), an.mapping(), collides));
} }
//add special generated groups //add special generated groups
for(DrawLayer layer : DrawLayer.values()){ for(DrawLayer layer : DrawLayer.values()){
String name = "DrawLayer" + Strings.capitalize(layer.name()) + "c"; String name = "DrawLayer" + Strings.capitalize(layer.name()) + "c";
//create group definition with no components directly //create group definition with no components directly
GroupDefinition def = new GroupDefinition(layer.name(), ClassName.bestGuess(packageName + "." + name), Array.with(), false, false); GroupDefinition def = new GroupDefinition(layer.name(), ClassName.bestGuess(packageName + "." + name), Array.with(), false, false, new Array<>(0));
//add manual inclusions of entities to be added to this group //add manual inclusions of entities to be added to this group
def.manualInclusions.addAll(allDefs.select(s -> allComponents(s).contains(comp -> comp.interfaces().contains(in -> in.name().equals(name))))); def.manualInclusions.addAll(allDefs.select(s -> allComponents(s).contains(comp -> comp.interfaces().contains(in -> in.name().equals(name)))));
groupDefs.add(def); groupDefs.add(def);
@@ -420,6 +421,12 @@ public class EntityProcess extends BaseProcessor{
groupUpdate.addStatement("all.update()"); groupUpdate.addStatement("all.update()");
for(GroupDefinition group : groupDefs){
for(Stype collide : group.collides){
groupUpdate.addStatement("$L.collide($L)", group.name, collide.name());
}
}
for(DrawLayer layer : DrawLayer.values()){ for(DrawLayer layer : DrawLayer.values()){
MethodSpec.Builder groupDraw = MethodSpec.methodBuilder("draw" + Strings.capitalize(layer.name())) MethodSpec.Builder groupDraw = MethodSpec.methodBuilder("draw" + Strings.capitalize(layer.name()))
.addModifiers(Modifier.PUBLIC, Modifier.STATIC); .addModifiers(Modifier.PUBLIC, Modifier.STATIC);
@@ -673,15 +680,17 @@ public class EntityProcess extends BaseProcessor{
final String name; final String name;
final ClassName baseType; final ClassName baseType;
final Array<Stype> components; final Array<Stype> components;
final Array<Stype> collides;
final boolean spatial, mapping; final boolean spatial, mapping;
final ObjectSet<Selement> manualInclusions = new ObjectSet<>(); final ObjectSet<Selement> manualInclusions = new ObjectSet<>();
public GroupDefinition(String name, ClassName bestType, Array<Stype> components, boolean spatial, boolean mapping){ public GroupDefinition(String name, ClassName bestType, Array<Stype> components, boolean spatial, boolean mapping, Array<Stype> collides){
this.baseType = bestType; this.baseType = bestType;
this.components = components; this.components = components;
this.name = name; this.name = name;
this.spatial = spatial; this.spatial = spatial;
this.mapping = mapping; this.mapping = mapping;
this.collides = collides;
} }
@Override @Override

View File

@@ -6,27 +6,32 @@ import mindustry.gen.*;
class AllDefs{ class AllDefs{
@GroupDef(Entityc.class) @GroupDef(Entityc.class)
void all(){ class all{
} }
@GroupDef(Playerc.class) @GroupDef(Playerc.class)
void player(){ class player{
}
@GroupDef(value = Bulletc.class, spatial = true, collide = {unit.class})
class bullet{
} }
@GroupDef(value = Unitc.class, spatial = true) @GroupDef(value = Unitc.class, spatial = true)
void unit(){ class unit{
} }
@GroupDef(Tilec.class) @GroupDef(Tilec.class)
void tile(){ class tile{
} }
@GroupDef(Syncc.class) @GroupDef(Syncc.class)
void sync(){ class sync{
} }
} }

View File

@@ -45,6 +45,10 @@ public class EntityGroup<T extends Entityc> implements Iterable<T>{
array.sort(comp); array.sort(comp);
} }
public void collide(EntityGroup<? extends Hitboxc> other){
collisions.collideGroups((EntityGroup<? extends Hitboxc>)this, other);
}
public void updatePhysics(){ public void updatePhysics(){
collisions.updatePhysics((EntityGroup<? extends Hitboxc>)this); collisions.updatePhysics((EntityGroup<? extends Hitboxc>)this);
} }