This commit is contained in:
Anuken 2019-08-30 15:52:55 -04:00
parent f063f7b2d3
commit f8f140090f
12 changed files with 83 additions and 89 deletions

View File

@ -153,6 +153,7 @@ public class Vars implements Loadable{
public static NetServer netServer;
public static NetClient netClient;
public static Entities entities;
public static EntityGroup<Player> playerGroup;
public static EntityGroup<TileEntity> tileGroup;
public static EntityGroup<Bullet> bulletGroup;
@ -204,21 +205,22 @@ public class Vars implements Loadable{
indexer = new BlockIndexer();
pathfinder = new Pathfinder();
playerGroup = Entities.addGroup(Player.class).enableMapping();
tileGroup = Entities.addGroup(TileEntity.class, false);
bulletGroup = Entities.addGroup(Bullet.class).enableMapping();
effectGroup = Entities.addGroup(EffectEntity.class, false);
groundEffectGroup = Entities.addGroup(DrawTrait.class, false);
puddleGroup = Entities.addGroup(Puddle.class).enableMapping();
shieldGroup = Entities.addGroup(ShieldEntity.class, false);
fireGroup = Entities.addGroup(Fire.class).enableMapping();
entities = new Entities();
playerGroup = entities.addGroup(Player.class).enableMapping();
tileGroup = entities.addGroup(TileEntity.class, false);
bulletGroup = entities.addGroup(Bullet.class).enableMapping();
effectGroup = entities.addGroup(EffectEntity.class, false);
groundEffectGroup = entities.addGroup(DrawTrait.class, false);
puddleGroup = entities.addGroup(Puddle.class).enableMapping();
shieldGroup = entities.addGroup(ShieldEntity.class, false);
fireGroup = entities.addGroup(Fire.class).enableMapping();
unitGroups = new EntityGroup[Team.all.length];
for(Team team : Team.all){
unitGroups[team.ordinal()] = Entities.addGroup(BaseUnit.class).enableMapping();
unitGroups[team.ordinal()] = entities.addGroup(BaseUnit.class).enableMapping();
}
for(EntityGroup<?> group : Entities.getAllGroups()){
for(EntityGroup<?> group : entities.getAllGroups()){
group.setRemoveListener(entity -> {
if(entity instanceof SyncTrait && Net.client()){
netClient.addRemovedEntity((entity).getID());

View File

@ -101,8 +101,8 @@ public class Logic implements ApplicationListener{
state.rules = new Rules();
state.stats = new Stats();
entities.clear();
Time.clear();
Entities.clear();
TileEntity.sleepingEntities = 0;
Events.fire(new ResetEvent());
@ -195,20 +195,20 @@ public class Logic implements ApplicationListener{
}
if(!headless){
Entities.update(effectGroup);
Entities.update(groundEffectGroup);
effectGroup.update();
groundEffectGroup.update();
}
if(!state.isEditor()){
for(EntityGroup group : unitGroups){
Entities.update(group);
group.update();
}
Entities.update(puddleGroup);
Entities.update(shieldGroup);
Entities.update(bulletGroup);
Entities.update(tileGroup);
Entities.update(fireGroup);
puddleGroup.update();
shieldGroup.update();
bulletGroup.update();
tileGroup.update();
fireGroup.update();
}else{
for(EntityGroup<?> group : unitGroups){
group.updateEvents();
@ -217,11 +217,11 @@ public class Logic implements ApplicationListener{
}
Entities.update(playerGroup);
playerGroup.update();
//effect group only contains item transfers in the headless version, update it!
if(headless){
Entities.update(effectGroup);
effectGroup.update();
}
if(!state.isEditor()){

View File

@ -219,7 +219,7 @@ public class NetClient implements ApplicationListener{
@Remote(variants = Variant.both)
public static void onWorldDataBegin(){
Entities.clear();
entities.clear();
netClient.removed.clear();
logic.reset();
@ -253,7 +253,7 @@ public class NetClient implements ApplicationListener{
netClient.byteStream.setBytes(Net.decompressSnapshot(data, dataLen));
DataInputStream input = netClient.dataStream;
EntityGroup group = Entities.getGroup(groupID);
EntityGroup group = entities.getGroup(groupID);
//go through each entity
for(int j = 0; j < amount; j++){
@ -370,7 +370,7 @@ public class NetClient implements ApplicationListener{
quiet = false;
lastSent = 0;
Entities.clear();
entities.clear();
ui.chatfrag.clearMessages();
}

View File

@ -589,7 +589,7 @@ public class NetServer implements ApplicationListener{
viewport.setSize(player.con.viewWidth, player.con.viewHeight).setCenter(player.con.viewX, player.con.viewY);
//check for syncable groups
for(EntityGroup<?> group : Entities.getAllGroups()){
for(EntityGroup<?> group : entities.getAllGroups()){
if(group.isEmpty() || !(group.all().get(0) instanceof SyncTrait)) continue;
//make sure mapping is enabled for this group

View File

@ -259,13 +259,13 @@ public class Renderer implements ApplicationListener{
overlays.drawBottom();
draw(playerGroup, p -> true, Player::drawBuildRequests);
if(Entities.countInBounds(shieldGroup) > 0){
if(entities.countInBounds(shieldGroup) > 0){
if(settings.getBool("animatedshields") && Shaders.shield != null){
Draw.flush();
shieldBuffer.begin();
graphics.clear(Color.CLEAR);
Entities.draw(shieldGroup);
Entities.draw(shieldGroup, shield -> true, shield -> ((ShieldEntity)shield).drawOver());
entities.draw(shieldGroup);
entities.draw(shieldGroup, shield -> true, shield -> ((ShieldEntity)shield).drawOver());
Draw.flush();
shieldBuffer.end();
Draw.shader(Shaders.shield);
@ -274,7 +274,7 @@ public class Renderer implements ApplicationListener{
Draw.color();
Draw.shader();
}else{
Entities.draw(shieldGroup, shield -> true, shield -> ((ShieldEntity)shield).drawSimple());
entities.draw(shieldGroup, shield -> true, shield -> ((ShieldEntity)shield).drawSimple());
}
}
@ -351,7 +351,7 @@ public class Renderer implements ApplicationListener{
}
public <T extends DrawTrait> void draw(EntityGroup<T> group, Predicate<T> toDraw, Consumer<T> drawer){
Entities.draw(group, toDraw, drawer);
entities.draw(group, toDraw, drawer);
}
public void scaleCamera(float amount){

View File

@ -8,7 +8,6 @@ import io.anuke.arc.math.geom.*;
import io.anuke.arc.util.*;
import io.anuke.mindustry.content.*;
import io.anuke.mindustry.core.GameState.*;
import io.anuke.mindustry.entities.*;
import io.anuke.mindustry.game.EventType.*;
import io.anuke.mindustry.game.*;
import io.anuke.mindustry.io.*;
@ -179,7 +178,7 @@ public class World{
addDarkness(tiles);
}
Entities.getAllGroups().each(group -> group.resize(-finalWorldBounds, -finalWorldBounds, tiles.length * tilesize + finalWorldBounds * 2, tiles[0].length * tilesize + finalWorldBounds * 2));
entities.getAllGroups().each(group -> group.resize(-finalWorldBounds, -finalWorldBounds, tiles.length * tilesize + finalWorldBounds * 2, tiles[0].length * tilesize + finalWorldBounds * 2));
generating = false;
Events.fire(new WorldLoadEvent());

View File

@ -1,77 +1,58 @@
package io.anuke.mindustry.entities;
import io.anuke.arc.Core;
import io.anuke.arc.collection.Array;
import io.anuke.arc.collection.IntMap;
import io.anuke.arc.function.Consumer;
import io.anuke.arc.function.Predicate;
import io.anuke.arc.graphics.Camera;
import io.anuke.arc.math.geom.Rectangle;
import io.anuke.mindustry.entities.traits.DrawTrait;
import io.anuke.mindustry.entities.traits.Entity;
import static io.anuke.mindustry.Vars.collisions;
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.*;
public class Entities{
private static final Array<EntityGroup<?>> groupArray = new Array<>();
private static final IntMap<EntityGroup<?>> groups = new IntMap<>();
private static final Rectangle viewport = new Rectangle();
private static final boolean clip = true;
private static int count = 0;
private final Array<EntityGroup<?>> groupArray = new Array<>();
private final Rectangle viewport = new Rectangle();
private final boolean clip = true;
private int count = 0;
public static void clear(){
public void clear(){
for(EntityGroup group : groupArray){
group.clear();
}
}
public static EntityGroup<?> getGroup(int id){
return groups.get(id);
public EntityGroup<?> getGroup(int id){
return groupArray.get(id);
}
public static Array<EntityGroup<?>> getAllGroups(){
public Array<EntityGroup<?>> getAllGroups(){
return groupArray;
}
public static <T extends Entity> EntityGroup<T> addGroup(Class<T> type){
public <T extends Entity> EntityGroup<T> addGroup(Class<T> type){
return addGroup(type, true);
}
public static <T extends Entity> EntityGroup<T> addGroup(Class<T> type, boolean useTree){
EntityGroup<T> group = new EntityGroup<>(type, useTree);
groups.put(group.getID(), group);
public <T extends Entity> EntityGroup<T> addGroup(Class<T> type, boolean useTree){
EntityGroup<T> group = new EntityGroup<>(groupArray.size, type, useTree);
groupArray.add(group);
return group;
}
public static void update(EntityGroup<?> group){
group.updateEvents();
if(group.useTree()){
collisions.updatePhysics(group);
}
for(Entity e : group.all()){
e.update();
}
}
public static int countInBounds(EntityGroup<?> group){
public int countInBounds(EntityGroup<?> group){
count = 0;
draw(group, e -> true, e -> count++);
return count;
}
public static void draw(EntityGroup<?> group){
public void draw(EntityGroup<?> group){
draw(group, e -> true);
}
public static <T extends DrawTrait> void draw(EntityGroup<?> group, Predicate<T> toDraw){
public <T extends DrawTrait> void draw(EntityGroup<?> group, Predicate<T> toDraw){
draw(group, toDraw, DrawTrait::draw);
}
@SuppressWarnings("unchecked")
public static <T extends DrawTrait> void draw(EntityGroup<?> group, Predicate<T> toDraw, Consumer<T> cons){
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);

View File

@ -1,16 +1,13 @@
package io.anuke.mindustry.entities;
import io.anuke.arc.collection.Array;
import io.anuke.arc.collection.IntMap;
import io.anuke.arc.function.Consumer;
import io.anuke.arc.function.Predicate;
import io.anuke.arc.math.geom.QuadTree;
import io.anuke.arc.math.geom.Rectangle;
import io.anuke.arc.util.*;
import io.anuke.mindustry.entities.traits.Entity;
import io.anuke.arc.collection.*;
import io.anuke.arc.function.*;
import io.anuke.arc.math.geom.*;
import io.anuke.mindustry.entities.traits.*;
import static io.anuke.mindustry.Vars.collisions;
public class EntityGroup<T extends Entity>{
private static int lastid;
private final boolean useTree;
private final int id;
private final Class<T> type;
@ -22,9 +19,9 @@ public class EntityGroup<T extends Entity>{
private Consumer<T> removeListener;
private Consumer<T> addListener;
public EntityGroup(Class<T> type, boolean useTree){
public EntityGroup(int id, Class<T> type, boolean useTree){
this.useTree = useTree;
this.id = lastid++;
this.id = id;
this.type = type;
if(useTree){
@ -32,6 +29,18 @@ public class EntityGroup<T extends Entity>{
}
}
public void update(){
updateEvents();
if(useTree()){
collisions.updatePhysics(this);
}
for(Entity e : all()){
e.update();
}
}
public boolean useTree(){
return useTree;
}

View File

@ -33,7 +33,7 @@ public class MobileInput extends InputHandler implements GestureListener{
//gesture data
private Vector2 vector = new Vector2();
private float lastDistance = -1f, lastZoom;
private float lastZoom = -1;
/** Position where the player started dragging a line. */
private int lineStartX, lineStartY;
@ -729,6 +729,9 @@ public class MobileInput extends InputHandler implements GestureListener{
@Override
public boolean zoom(float initialDistance, float distance){
if(Core.settings.getBool("keyboard")) return false;
if(lastZoom < 0){
lastZoom = renderer.getScale();
}
renderer.setScale(distance / initialDistance * lastZoom);
return true;

View File

@ -209,7 +209,7 @@ public abstract class SaveVersion extends SaveFileReader{
//write entity chunk
int groups = 0;
for(EntityGroup<?> group : Entities.getAllGroups()){
for(EntityGroup<?> group : entities.getAllGroups()){
if(!group.isEmpty() && group.all().get(0) instanceof SaveTrait){
groups++;
}
@ -217,7 +217,7 @@ public abstract class SaveVersion extends SaveFileReader{
stream.writeByte(groups);
for(EntityGroup<?> group : Entities.getAllGroups()){
for(EntityGroup<?> group : entities.getAllGroups()){
if(!group.isEmpty() && group.all().get(0) instanceof SaveTrait){
stream.writeInt(group.size());
for(Entity entity : group.all()){

View File

@ -58,7 +58,7 @@ public class TypeIO{
byte gid = buffer.get();
if(gid == -1) return null;
int id = buffer.getInt();
return (Unit)Entities.getGroup(gid).getByID(id);
return (Unit)entities.getGroup(gid).getByID(id);
}
@WriteClass(ShooterTrait.class)
@ -71,7 +71,7 @@ public class TypeIO{
public static ShooterTrait readShooter(ByteBuffer buffer){
byte gid = buffer.get();
int id = buffer.getInt();
return (ShooterTrait)Entities.getGroup(gid).getByID(id);
return (ShooterTrait)entities.getGroup(gid).getByID(id);
}
@WriteClass(Bullet.class)

View File

@ -45,7 +45,7 @@ public class NetworkIO{
state.wave = stream.readInt();
state.wavetime = stream.readFloat();
Entities.clear();
entities.clear();
int id = stream.readInt();
player.resetNoAdd();
player.read(stream);