Faster entity removal

This commit is contained in:
Anuken
2022-12-14 16:58:58 -05:00
parent 7f292b14a7
commit 243c50be02
4 changed files with 91 additions and 5 deletions

View File

@ -232,9 +232,15 @@ public class EntityProcess extends BaseProcessor{
Stype repr = types.first();
String groupType = repr.annotation(Component.class).base() ? baseName(repr) : interfaceName(repr);
String name = group.name().startsWith("g") ? group.name().substring(1) : group.name();
boolean collides = an.collide();
groupDefs.add(new GroupDefinition(group.name().startsWith("g") ? group.name().substring(1) : group.name(),
groupDefs.add(new GroupDefinition(name,
ClassName.bestGuess(packageName + "." + groupType), types, an.spatial(), an.mapping(), collides));
TypeSpec.Builder accessor = TypeSpec.interfaceBuilder("IndexableEntity__" + name);
accessor.addMethod(MethodSpec.methodBuilder("setIndex__" + name).addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC).addParameter(int.class, "index").returns(void.class).build());
write(accessor);
}
ObjectMap<String, Selement> usedNames = new ObjectMap<>();
@ -394,6 +400,13 @@ public class EntityProcess extends BaseProcessor{
//entities with no sync comp and no serialization gen no code
boolean hasIO = ann.genio() && (components.contains(s -> s.name().contains("Sync")) || ann.serialize());
//implement indexable interfaces.
for(GroupDefinition def : groups){
builder.addSuperinterface(tname(packageName + ".IndexableEntity__" + def.name));
builder.addMethod(MethodSpec.methodBuilder("setIndex__" + def.name).addParameter(int.class, "index").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class)
.addCode("index__$L = index;", def.name).build());
}
//add all methods from components
for(ObjectMap.Entry<String, Seq<Smethod>> entry : methods){
if(entry.value.contains(m -> m.has(Replace.class))){
@ -446,8 +459,15 @@ public class EntityProcess extends BaseProcessor{
mbuilder.addStatement("if(added == $L) return", first.name().equals("add"));
for(GroupDefinition def : groups){
//remove/add from each group, assume imported
mbuilder.addStatement("Groups.$L.$L(this)", def.name, first.name());
if(first.name().equals("add")){
//remove/add from each group, assume imported
mbuilder.addStatement("index__$L = Groups.$L.addIndex(this)", def.name, def.name);
}else{
//remove/add from each group, assume imported
mbuilder.addStatement("Groups.$L.removeIndex(this, index__$L);", def.name, def.name);
mbuilder.addStatement("index__$L = -1", def.name);
}
}
}
@ -577,6 +597,13 @@ public class EntityProcess extends BaseProcessor{
skipDeprecated(builder);
if(!legacy){
//add group index int variables
for(GroupDefinition def : groups){
builder.addField(FieldSpec.builder(int.class, "index__" + def.name, Modifier.PROTECTED).initializer("-1").build());
}
}
definitions.add(new EntityDefinition(packageName + "." + name, builder, type, typeIsBase ? null : baseClass, components, groups, allFieldSpecs, legacy));
}
@ -592,7 +619,7 @@ public class EntityProcess extends BaseProcessor{
groupsBuilder.addField(ParameterizedTypeName.get(
ClassName.bestGuess("mindustry.entities.EntityGroup"), itype), group.name, Modifier.PUBLIC, Modifier.STATIC);
groupInit.addStatement("$L = new $T<>($L.class, $L, $L)", group.name, groupc, itype, group.spatial, group.mapping);
groupInit.addStatement("$L = new $T<>($L.class, $L, $L, (e, pos) -> (($L.IndexableEntity__$L)e).setIndex__$L(pos))", group.name, groupc, itype, group.spatial, group.mapping, packageName, group.name, group.name);
}
//write the groups