diff --git a/core/src/io/anuke/mindustry/Vars.java b/core/src/io/anuke/mindustry/Vars.java index 8344409142..1670d2a39e 100644 --- a/core/src/io/anuke/mindustry/Vars.java +++ b/core/src/io/anuke/mindustry/Vars.java @@ -153,6 +153,7 @@ public class Vars implements Loadable{ public static NetServer netServer; public static NetClient netClient; + public static Entities entities; public static EntityGroup playerGroup; public static EntityGroup tileGroup; public static EntityGroup 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()); diff --git a/core/src/io/anuke/mindustry/core/Logic.java b/core/src/io/anuke/mindustry/core/Logic.java index 32eee1a526..abfb994939 100644 --- a/core/src/io/anuke/mindustry/core/Logic.java +++ b/core/src/io/anuke/mindustry/core/Logic.java @@ -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()){ diff --git a/core/src/io/anuke/mindustry/core/NetClient.java b/core/src/io/anuke/mindustry/core/NetClient.java index a7d8a9d54f..9ce76df1da 100644 --- a/core/src/io/anuke/mindustry/core/NetClient.java +++ b/core/src/io/anuke/mindustry/core/NetClient.java @@ -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(); } diff --git a/core/src/io/anuke/mindustry/core/NetServer.java b/core/src/io/anuke/mindustry/core/NetServer.java index ca95cd39fd..26678d7eb2 100644 --- a/core/src/io/anuke/mindustry/core/NetServer.java +++ b/core/src/io/anuke/mindustry/core/NetServer.java @@ -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 diff --git a/core/src/io/anuke/mindustry/core/Renderer.java b/core/src/io/anuke/mindustry/core/Renderer.java index a3bf36ee39..3fe3b6b9be 100644 --- a/core/src/io/anuke/mindustry/core/Renderer.java +++ b/core/src/io/anuke/mindustry/core/Renderer.java @@ -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 void draw(EntityGroup group, Predicate toDraw, Consumer drawer){ - Entities.draw(group, toDraw, drawer); + entities.draw(group, toDraw, drawer); } public void scaleCamera(float amount){ diff --git a/core/src/io/anuke/mindustry/core/World.java b/core/src/io/anuke/mindustry/core/World.java index 67e4fd3069..e09eaf63b8 100644 --- a/core/src/io/anuke/mindustry/core/World.java +++ b/core/src/io/anuke/mindustry/core/World.java @@ -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()); diff --git a/core/src/io/anuke/mindustry/entities/Entities.java b/core/src/io/anuke/mindustry/entities/Entities.java index 2a6f388ff5..3a0ff22d6e 100755 --- a/core/src/io/anuke/mindustry/entities/Entities.java +++ b/core/src/io/anuke/mindustry/entities/Entities.java @@ -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> groupArray = new Array<>(); - private static final IntMap> groups = new IntMap<>(); - private static final Rectangle viewport = new Rectangle(); - private static final boolean clip = true; - private static int count = 0; + private final Array> 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> getAllGroups(){ + public Array> getAllGroups(){ return groupArray; } - public static EntityGroup addGroup(Class type){ + public EntityGroup addGroup(Class type){ return addGroup(type, true); } - public static EntityGroup addGroup(Class type, boolean useTree){ - EntityGroup group = new EntityGroup<>(type, useTree); - groups.put(group.getID(), group); + public EntityGroup addGroup(Class type, boolean useTree){ + EntityGroup 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 void draw(EntityGroup group, Predicate toDraw){ + public void draw(EntityGroup group, Predicate toDraw){ draw(group, toDraw, DrawTrait::draw); } @SuppressWarnings("unchecked") - public static void draw(EntityGroup group, Predicate toDraw, Consumer cons){ + public void draw(EntityGroup group, Predicate toDraw, Consumer 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); diff --git a/core/src/io/anuke/mindustry/entities/EntityGroup.java b/core/src/io/anuke/mindustry/entities/EntityGroup.java index 6b2e343ae3..4f9dfa5eb5 100644 --- a/core/src/io/anuke/mindustry/entities/EntityGroup.java +++ b/core/src/io/anuke/mindustry/entities/EntityGroup.java @@ -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{ - private static int lastid; private final boolean useTree; private final int id; private final Class type; @@ -22,9 +19,9 @@ public class EntityGroup{ private Consumer removeListener; private Consumer addListener; - public EntityGroup(Class type, boolean useTree){ + public EntityGroup(int id, Class type, boolean useTree){ this.useTree = useTree; - this.id = lastid++; + this.id = id; this.type = type; if(useTree){ @@ -32,6 +29,18 @@ public class EntityGroup{ } } + public void update(){ + updateEvents(); + + if(useTree()){ + collisions.updatePhysics(this); + } + + for(Entity e : all()){ + e.update(); + } + } + public boolean useTree(){ return useTree; } diff --git a/core/src/io/anuke/mindustry/input/MobileInput.java b/core/src/io/anuke/mindustry/input/MobileInput.java index 5ad254e216..13f1e4b46b 100644 --- a/core/src/io/anuke/mindustry/input/MobileInput.java +++ b/core/src/io/anuke/mindustry/input/MobileInput.java @@ -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; diff --git a/core/src/io/anuke/mindustry/io/SaveVersion.java b/core/src/io/anuke/mindustry/io/SaveVersion.java index eab2846b3b..e537807573 100644 --- a/core/src/io/anuke/mindustry/io/SaveVersion.java +++ b/core/src/io/anuke/mindustry/io/SaveVersion.java @@ -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()){ diff --git a/core/src/io/anuke/mindustry/io/TypeIO.java b/core/src/io/anuke/mindustry/io/TypeIO.java index ebd1ef3d45..da18df860a 100644 --- a/core/src/io/anuke/mindustry/io/TypeIO.java +++ b/core/src/io/anuke/mindustry/io/TypeIO.java @@ -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) diff --git a/core/src/io/anuke/mindustry/net/NetworkIO.java b/core/src/io/anuke/mindustry/net/NetworkIO.java index 1729f3d4e4..663cfcdf0d 100644 --- a/core/src/io/anuke/mindustry/net/NetworkIO.java +++ b/core/src/io/anuke/mindustry/net/NetworkIO.java @@ -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);