Entity cleanup
This commit is contained in:
@@ -1,17 +1,11 @@
|
||||
package io.anuke.mindustry.entities;
|
||||
|
||||
import io.anuke.arc.*;
|
||||
import io.anuke.arc.collection.*;
|
||||
import io.anuke.arc.function.*;
|
||||
import io.anuke.arc.graphics.*;
|
||||
import io.anuke.arc.math.geom.*;
|
||||
import io.anuke.mindustry.entities.traits.*;
|
||||
|
||||
/** Simple container for managing entity groups.*/
|
||||
public class Entities{
|
||||
private final Array<EntityGroup<?>> groupArray = new Array<>();
|
||||
private final Rectangle viewport = new Rectangle();
|
||||
private final boolean clip = true;
|
||||
private int count = 0;
|
||||
|
||||
public void clear(){
|
||||
for(EntityGroup group : groupArray){
|
||||
@@ -19,52 +13,21 @@ public class Entities{
|
||||
}
|
||||
}
|
||||
|
||||
public EntityGroup<?> getGroup(int id){
|
||||
public EntityGroup<?> get(int id){
|
||||
return groupArray.get(id);
|
||||
}
|
||||
|
||||
public Array<EntityGroup<?>> getAllGroups(){
|
||||
public Array<EntityGroup<?>> all(){
|
||||
return groupArray;
|
||||
}
|
||||
|
||||
public <T extends Entity> EntityGroup<T> addGroup(Class<T> type){
|
||||
return addGroup(type, true);
|
||||
public <T extends Entity> EntityGroup<T> add(Class<T> type){
|
||||
return add(type, true);
|
||||
}
|
||||
|
||||
public <T extends Entity> EntityGroup<T> addGroup(Class<T> type, boolean useTree){
|
||||
public <T extends Entity> EntityGroup<T> add(Class<T> type, boolean useTree){
|
||||
EntityGroup<T> group = new EntityGroup<>(groupArray.size, type, useTree);
|
||||
groupArray.add(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
public int countInBounds(EntityGroup<?> group){
|
||||
count = 0;
|
||||
draw(group, e -> true, e -> count++);
|
||||
return count;
|
||||
}
|
||||
|
||||
public void draw(EntityGroup<?> group){
|
||||
draw(group, e -> true);
|
||||
}
|
||||
|
||||
public <T extends DrawTrait> void draw(EntityGroup<?> group, Predicate<T> toDraw){
|
||||
draw(group, toDraw, DrawTrait::draw);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends DrawTrait> void draw(EntityGroup<?> group, Predicate<T> toDraw, Consumer<T> cons){
|
||||
if(clip){
|
||||
Camera cam = Core.camera;
|
||||
viewport.set(cam.position.x - cam.width / 2, cam.position.y - cam.height / 2, cam.width, cam.height);
|
||||
}
|
||||
|
||||
for(Entity e : group.all()){
|
||||
if(!(e instanceof DrawTrait) || !toDraw.test((T)e) || !e.isAdded()) continue;
|
||||
DrawTrait draw = (DrawTrait)e;
|
||||
|
||||
if(!clip || viewport.overlaps(draw.getX() - draw.drawSize()/2f, draw.getY() - draw.drawSize()/2f, draw.drawSize(), draw.drawSize())){
|
||||
cons.accept((T)e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package io.anuke.mindustry.entities;
|
||||
|
||||
import io.anuke.arc.*;
|
||||
import io.anuke.arc.collection.*;
|
||||
import io.anuke.arc.function.*;
|
||||
import io.anuke.arc.graphics.*;
|
||||
import io.anuke.arc.math.geom.*;
|
||||
import io.anuke.mindustry.entities.traits.*;
|
||||
|
||||
import static io.anuke.mindustry.Vars.collisions;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class EntityGroup<T extends Entity>{
|
||||
private final boolean useTree;
|
||||
private final int id;
|
||||
@@ -19,6 +22,10 @@ public class EntityGroup<T extends Entity>{
|
||||
private Consumer<T> removeListener;
|
||||
private Consumer<T> addListener;
|
||||
|
||||
private final Rectangle viewport = new Rectangle();
|
||||
private final boolean clip = true;
|
||||
private int count = 0;
|
||||
|
||||
public EntityGroup(int id, Class<T> type, boolean useTree){
|
||||
this.useTree = useTree;
|
||||
this.id = id;
|
||||
@@ -41,6 +48,34 @@ public class EntityGroup<T extends Entity>{
|
||||
}
|
||||
}
|
||||
|
||||
public int countInBounds(){
|
||||
count = 0;
|
||||
draw(e -> true, e -> count++);
|
||||
return count;
|
||||
}
|
||||
|
||||
public void draw(){
|
||||
draw(e -> true);
|
||||
}
|
||||
|
||||
public void draw(Predicate<T> toDraw){
|
||||
draw(toDraw, t -> ((DrawTrait)t).draw());
|
||||
}
|
||||
|
||||
public void draw(Predicate<T> toDraw, Consumer<T> cons){
|
||||
Camera cam = Core.camera;
|
||||
viewport.set(cam.position.x - cam.width / 2, cam.position.y - cam.height / 2, cam.width, cam.height);
|
||||
|
||||
for(Entity e : all()){
|
||||
if(!(e instanceof DrawTrait) || !toDraw.test((T)e) || !e.isAdded()) continue;
|
||||
DrawTrait draw = (DrawTrait)e;
|
||||
|
||||
if(viewport.overlaps(draw.getX() - draw.drawSize()/2f, draw.getY() - draw.drawSize()/2f, draw.drawSize(), draw.drawSize())){
|
||||
cons.accept((T)e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean useTree(){
|
||||
return useTree;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user