Autogenerated render layer groups

This commit is contained in:
Anuken
2020-02-06 13:20:03 -05:00
parent 6a99a3922e
commit 6fe5663f79
11 changed files with 91 additions and 56 deletions

View File

@@ -3,4 +3,3 @@ apply plugin: "java"
sourceCompatibility = 1.8 sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = ["src/main/java/"] sourceSets.main.java.srcDirs = ["src/main/java/"]
sourceSets.main.resources.srcDirs = ["src/main/resources/"] sourceSets.main.resources.srcDirs = ["src/main/resources/"]

View File

@@ -5,14 +5,14 @@ import java.lang.annotation.*;
public class Annotations{ public class Annotations{
//region entity interfaces //region entity interfaces
/** Indicates that a component field is read-only. */ /** Indicates that a component field is read-only.
@Target({ElementType.METHOD}) @Target({ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface Render{ public @interface Render{
RenderLayer value(); RenderLayer value();
} }*/
public enum RenderLayer{ public enum DrawLayer{
floor, floor,
groundShadows, groundShadows,
ground, ground,

View File

@@ -3,7 +3,6 @@ package mindustry.annotations;
import arc.files.*; import arc.files.*;
import arc.struct.Array; import arc.struct.Array;
import arc.util.*; import arc.util.*;
import arc.util.Log.*;
import com.squareup.javapoet.*; import com.squareup.javapoet.*;
import com.sun.source.util.*; import com.sun.source.util.*;
import mindustry.annotations.util.*; import mindustry.annotations.util.*;
@@ -154,7 +153,7 @@ public abstract class BaseProcessor extends AbstractProcessor{
messager = env.getMessager(); messager = env.getMessager();
if(System.getProperty("debug") == null){ if(System.getProperty("debug") == null){
Log.setLogLevel(LogLevel.err); //Log.setLogLevel(LogLevel.err);
} }
} }

View File

@@ -60,7 +60,8 @@ public class EntityProcess extends BaseProcessor{
//parse groups //parse groups
for(Smethod group : allGroups){ for(Smethod group : allGroups){
GroupDef an = group.annotation(GroupDef.class); GroupDef an = group.annotation(GroupDef.class);
groupDefs.add(new GroupDefinition(group.name(), types(an, GroupDef::value), an)); Array<Stype> types = types(an, GroupDef::value);
groupDefs.add(new GroupDefinition(group.name(), ClassName.bestGuess(packageName + "." + interfaceName(types.first())), types, an.spatial(), an.mapping()));
} }
//create component interfaces //create component interfaces
@@ -70,7 +71,8 @@ public class EntityProcess extends BaseProcessor{
//implement extra interfaces these components may have, e.g. position //implement extra interfaces these components may have, e.g. position
for(Stype extraInterface : component.interfaces().select(i -> !isCompInterface(i))){ for(Stype extraInterface : component.interfaces().select(i -> !isCompInterface(i))){
inter.addSuperinterface(extraInterface.mirror()); //javapoet completely chokes on this if I add `addSuperInterface` or create the type name with TypeName.get
inter.superinterfaces.add(tname(extraInterface.fullName()));
} }
//implement super interfaces //implement super interfaces
@@ -135,18 +137,36 @@ public class EntityProcess extends BaseProcessor{
Log.info(""); Log.info("");
} }
//generate special render layer interfaces
for(DrawLayer layer : DrawLayer.values()){
//create the DrawLayer interface that entities need to implement
String name = "DrawLayer" + Strings.capitalize(layer.name()) + "c";
TypeSpec.Builder inter = TypeSpec.interfaceBuilder(name)
.addSuperinterface(tname(packageName + ".Entityc"))
.addModifiers(Modifier.PUBLIC).addAnnotation(EntityInterface.class);
inter.addMethod(MethodSpec.methodBuilder("draw" + Strings.capitalize(layer.name())).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).build());
write(inter);
//create group definition with no components directly
GroupDefinition def = new GroupDefinition(layer.name(), ClassName.bestGuess(packageName + "." + name), Array.with(), false, false);
//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)))));
groupDefs.add(def);
}
//look at each definition //look at each definition
for(Stype type : allDefs){ for(Stype type : allDefs){
boolean isFinal = type.annotation(EntityDef.class).isFinal(); boolean isFinal = type.annotation(EntityDef.class).isFinal();
if(!type.name().endsWith("Def")){ if(!type.name().endsWith("Def")){
err("All entity def names must end with 'Def'", type.e); err("All entity def names must end with 'Def'", type.e);
} }
String name = type.name().replace("Def", "Entity"); //TODO remove extra underscore String name = type.name().replace("Def", "Entity");
TypeSpec.Builder builder = TypeSpec.classBuilder(name).addModifiers(Modifier.PUBLIC); TypeSpec.Builder builder = TypeSpec.classBuilder(name).addModifiers(Modifier.PUBLIC);
if(isFinal) builder.addModifiers(Modifier.FINAL); if(isFinal) builder.addModifiers(Modifier.FINAL);
Array<Stype> components = allComponents(type); Array<Stype> components = allComponents(type);
Array<GroupDefinition> groups = groupDefs.select(g -> !g.components.contains(s -> !components.contains(s))); Array<GroupDefinition> groups = groupDefs.select(g -> (!g.components.isEmpty() && !g.components.contains(s -> !components.contains(s))) || g.manualInclusions.contains(type));
ObjectMap<String, Array<Smethod>> methods = new ObjectMap<>(); ObjectMap<String, Array<Smethod>> methods = new ObjectMap<>();
//add all components //add all components
@@ -268,31 +288,40 @@ public class EntityProcess extends BaseProcessor{
TypeSpec.Builder groupsBuilder = TypeSpec.classBuilder("Groups").addModifiers(Modifier.PUBLIC); TypeSpec.Builder groupsBuilder = TypeSpec.classBuilder("Groups").addModifiers(Modifier.PUBLIC);
MethodSpec.Builder groupInit = MethodSpec.methodBuilder("init").addModifiers(Modifier.PUBLIC, Modifier.STATIC); MethodSpec.Builder groupInit = MethodSpec.methodBuilder("init").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
for(GroupDefinition group : groupDefs){ for(GroupDefinition group : groupDefs){
Stype ctype = group.components.first(); //Stype ctype = group.components.first();
//class names for interface/group //class names for interface/group
ClassName itype = ClassName.bestGuess("mindustry.gen." + interfaceName(ctype)); ClassName itype = group.baseType;
ClassName groupc = ClassName.bestGuess("mindustry.entities.EntityGroup"); ClassName groupc = ClassName.bestGuess("mindustry.entities.EntityGroup");
//add field... //add field...
groupsBuilder.addField(ParameterizedTypeName.get( groupsBuilder.addField(ParameterizedTypeName.get(
ClassName.bestGuess("mindustry.entities.EntityGroup"), itype), group.name, Modifier.PUBLIC, Modifier.STATIC); 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.def.spatial(), group.def.mapping()); groupInit.addStatement("$L = new $T<>($L.class, $L, $L)", group.name, groupc, itype, group.spatial, group.mapping);
} }
//write the groups //write the groups
groupsBuilder.addMethod(groupInit.build()); groupsBuilder.addMethod(groupInit.build());
//add method for resizing all necessary groups
MethodSpec.Builder groupResize = MethodSpec.methodBuilder("resize") MethodSpec.Builder groupResize = MethodSpec.methodBuilder("resize")
.addParameter(TypeName.FLOAT, "x").addParameter(TypeName.FLOAT, "y").addParameter(TypeName.FLOAT, "w").addParameter(TypeName.FLOAT, "h") .addParameter(TypeName.FLOAT, "x").addParameter(TypeName.FLOAT, "y").addParameter(TypeName.FLOAT, "w").addParameter(TypeName.FLOAT, "h")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC); .addModifiers(Modifier.PUBLIC, Modifier.STATIC);
//method resize
for(GroupDefinition group : groupDefs){ for(GroupDefinition group : groupDefs){
if(group.def.spatial()){ if(group.spatial){
groupResize.addStatement("$L.resize(x, y, w, h)", group.name); groupResize.addStatement("$L.resize(x, y, w, h)", group.name);
} }
} }
for(DrawLayer layer : DrawLayer.values()){
MethodSpec.Builder groupDraw = MethodSpec.methodBuilder("draw" + Strings.capitalize(layer.name()))
.addModifiers(Modifier.PUBLIC, Modifier.STATIC);
groupDraw.addStatement("$L.draw($L::$L)", layer.name(), "DrawLayer" + Strings.capitalize(layer.name()) + "c", "draw" + Strings.capitalize(layer.name()));
groupsBuilder.addMethod(groupDraw.build());
}
groupsBuilder.addMethod(groupResize.build()); groupsBuilder.addMethod(groupResize.build());
write(groupsBuilder); write(groupsBuilder);
@@ -520,13 +549,17 @@ public class EntityProcess extends BaseProcessor{
class GroupDefinition{ class GroupDefinition{
final String name; final String name;
final ClassName baseType;
final Array<Stype> components; final Array<Stype> components;
final GroupDef def; final boolean spatial, mapping;
final ObjectSet<Stype> manualInclusions = new ObjectSet<>();
public GroupDefinition(String name, Array<Stype> components, GroupDef def){ public GroupDefinition(String name, ClassName bestType, Array<Stype> components, boolean spatial, boolean mapping){
this.baseType = bestType;
this.components = components; this.components = components;
this.name = name; this.name = name;
this.def = def; this.spatial = spatial;
this.mapping = mapping;
} }
@Override @Override

View File

@@ -187,7 +187,7 @@ public class Renderer implements ApplicationListener{
blocks.floor.drawFloor(); blocks.floor.drawFloor();
render(RenderLayer.floor); Groups.drawFloor();
blocks.processBlocks(); blocks.processBlocks();
blocks.drawShadows(); blocks.drawShadows();
@@ -208,25 +208,25 @@ public class Renderer implements ApplicationListener{
blocks.drawBlocks(Layer.overlay); blocks.drawBlocks(Layer.overlay);
render(RenderLayer.groundShadows); Groups.drawGroundShadows();
render(RenderLayer.ground); Groups.drawGround();
blocks.drawBlocks(Layer.turret); blocks.drawBlocks(Layer.turret);
render(RenderLayer.flyingShadows); Groups.drawFlyingShadows();
blocks.drawBlocks(Layer.power); blocks.drawBlocks(Layer.power);
blocks.drawBlocks(Layer.lights); blocks.drawBlocks(Layer.lights);
render(RenderLayer.flying); Groups.drawFlying();
Draw.flush(); Draw.flush();
if(bloom != null && !pixelator.enabled()){ if(bloom != null && !pixelator.enabled()){
bloom.capture(); bloom.capture();
} }
render(RenderLayer.bullets); Groups.drawBullets();
render(RenderLayer.effects); Groups.drawEffects();
Draw.flush(); Draw.flush();
if(bloom != null && !pixelator.enabled()){ if(bloom != null && !pixelator.enabled()){
@@ -240,9 +240,9 @@ public class Renderer implements ApplicationListener{
overlays.drawTop(); overlays.drawTop();
render(RenderLayer.names); if(!pixelator.enabled()){
//TODO should use (draw) Groups.drawNames();
Groups.player.each(p -> !p.dead(), Playerc::drawName); }
if(state.rules.lighting){ if(state.rules.lighting){
lights.draw(); lights.draw();
@@ -254,10 +254,6 @@ public class Renderer implements ApplicationListener{
Draw.flush(); Draw.flush();
} }
private void render(RenderLayer layer){
}
private void drawLanding(){ private void drawLanding(){
if(landTime > 0 && player.closestCore() != null){ if(landTime > 0 && player.closestCore() != null){
float fract = landTime / Fx.coreLand.lifetime; float fract = landTime / Fx.coreLand.lifetime;

View File

@@ -22,9 +22,6 @@ class AllEntities{
@EntityDef({UnitComp.class}) @EntityDef({UnitComp.class})
class GenericUnitDef{} class GenericUnitDef{}
@EntityDef({BuilderComp.class})
class GenericBuilderDef{}
@GroupDef(EntityComp.class) @GroupDef(EntityComp.class)
void all(){ void all(){
@@ -45,11 +42,6 @@ class AllEntities{
} }
@GroupDef(DrawComp.class)
void drawer(){
}
@GroupDef(SyncComp.class) @GroupDef(SyncComp.class)
void sync(){ void sync(){

View File

@@ -6,13 +6,27 @@ import mindustry.annotations.Annotations.*;
import mindustry.gen.*; import mindustry.gen.*;
@Component @Component
abstract class DrawShadowComp implements Drawc, Rotc, Flyingc{ abstract class DrawShadowComp implements Drawc, Rotc, Flyingc, DrawLayerFlyingShadowsc, DrawLayerGroundShadowsc{
static final float shadowTX = -12, shadowTY = -13, shadowColor = Color.toFloatBits(0, 0, 0, 0.22f); static final float shadowTX = -12, shadowTY = -13, shadowColor = Color.toFloatBits(0, 0, 0, 0.22f);
transient float x, y, rotation; transient float x, y, rotation;
abstract TextureRegion getShadowRegion(); abstract TextureRegion getShadowRegion();
@Override
public void drawFlyingShadows(){
if(isFlying()){
drawShadow();
}
}
@Override
public void drawGroundShadows(){
if(isGrounded()){
drawShadow();
}
}
void drawShadow(){ void drawShadow(){
if(!isGrounded()){ if(!isGrounded()){
Draw.color(shadowColor); Draw.color(shadowColor);

View File

@@ -21,6 +21,10 @@ abstract class FlyingComp implements Posc, Velc, Healthc{
return elevation < 0.001f; return elevation < 0.001f;
} }
boolean isFlying(){
return elevation >= 0.001f;
}
@Override @Override
public void update(){ public void update(){
Floor floor = floorOn(); Floor floor = floorOn();

View File

@@ -19,10 +19,20 @@ import mindustry.world.blocks.*;
import static mindustry.Vars.*; import static mindustry.Vars.*;
@Component @Component
abstract class UnitComp implements Healthc, Velc, Statusc, Teamc, Itemsc, Hitboxc, Rotc, Massc, Unitc, Weaponsc{ abstract class UnitComp implements Healthc, Velc, Statusc, Teamc, Itemsc, Hitboxc, Rotc, Massc, Unitc, Weaponsc, DrawShadowc{
private UnitController controller; private UnitController controller;
private UnitDef type; private UnitDef type;
@Override
public TextureRegion getShadowRegion(){
return type.region;
}
@Override
public float clipSize(){
return type.region.getWidth() * 2f;
}
@Override @Override
public int itemCapacity(){ public int itemCapacity(){
return type.itemCapacity; return type.itemCapacity;

View File

@@ -6,6 +6,7 @@ import arc.graphics.Texture.*;
import arc.graphics.g2d.*; import arc.graphics.g2d.*;
import arc.graphics.gl.*; import arc.graphics.gl.*;
import arc.util.*; import arc.util.*;
import mindustry.gen.*;
import static arc.Core.*; import static arc.Core.*;
import static mindustry.Vars.renderer; import static mindustry.Vars.renderer;
@@ -52,8 +53,7 @@ public class Pixelator implements Disposable{
Draw.rect(Draw.wrap(buffer.getTexture()), Core.camera.position.x, Core.camera.position.y, Core.camera.width, -Core.camera.height); Draw.rect(Draw.wrap(buffer.getTexture()), Core.camera.position.x, Core.camera.position.y, Core.camera.width, -Core.camera.height);
Draw.blend(); Draw.blend();
//TODO implement drawing functions, maybe Groups.drawNames();
//Groups.player.draw(p -> !p.isDead(), Playerc::drawName);
Core.camera.position.set(px, py); Core.camera.position.set(px, py);
Core.settings.put("animatedwater", hadWater); Core.settings.put("animatedwater", hadWater);

View File

@@ -1,12 +0,0 @@
package mindustry.graphics;
public enum RenderLayer{
floor,
groundShadows,
ground,
flyingShadows,
flying,
bullets,
effects,
names,
}