Reduced reflection usage / Improved event system

This commit is contained in:
Anuken 2018-08-31 16:14:14 -04:00
parent 522e19d4bf
commit 5cb50d57ec
24 changed files with 95 additions and 85 deletions

View File

@ -26,7 +26,7 @@ allprojects {
appName = 'Mindustry'
gdxVersion = '1.9.8'
roboVMVersion = '2.3.0'
uCoreVersion = '4d8019c8f37fdc2a7a64cbd55ef6ee30fb9f1ba5'
uCoreVersion = '65a4a39a4529881463ea2d7f126583b59cdeba26'
getVersionString = {
String buildVersion = getBuildVersion()

View File

@ -8,7 +8,6 @@
<extend-configuration-property name="gdx.reflect.include" value="io.anuke.mindustry.world.Map"/>
<extend-configuration-property name="gdx.reflect.include" value="io.anuke.mindustry.game.SpawnGroup"/>
<extend-configuration-property name="gdx.reflect.include" value="io.anuke.mindustry.core.GameState"/>
<extend-configuration-property name="gdx.reflect.include" value="io.anuke.mindustry.game.EventType"/>
<extend-configuration-property name="gdx.reflect.include" value="io.anuke.mindustry.io.SaveFileVersion"/>
<extend-configuration-property name="gdx.reflect.include" value="io.anuke.ucore.entities.impl.EffectEntity"/>
<extend-configuration-property name="gdx.reflect.include" value="io.anuke.mindustry.net.Packets"/>

View File

@ -50,18 +50,18 @@ public class BlockIndexer{
private Array<Tile> returnArray = new ThreadArray<>();
public BlockIndexer(){
Events.on(TileChangeEvent.class, tile -> {
if(typeMap.get(tile.packedPosition()) != null){
TileIndex index = typeMap.get(tile.packedPosition());
Events.on(TileChangeEvent.class, event -> {
if(typeMap.get(event.tile.packedPosition()) != null){
TileIndex index = typeMap.get(event.tile.packedPosition());
for(BlockFlag flag : index.flags){
getFlagged(index.team)[flag.ordinal()].remove(tile);
getFlagged(index.team)[flag.ordinal()].remove(event.tile);
}
}
process(tile);
updateQuadrant(tile);
process(event.tile);
updateQuadrant(event.tile);
});
Events.on(WorldLoadEvent.class, () -> {
Events.on(WorldLoadEvent.class, event -> {
flagMap = new ObjectSet[Team.all.length][BlockFlag.all.length];
for(int i = 0; i < flagMap.length; i++){
for(int j = 0; j < BlockFlag.all.length; j++){

View File

@ -25,18 +25,18 @@ public class Pathfinder{
private IntArray blocked = new IntArray();
public Pathfinder(){
Events.on(WorldLoadEvent.class, this::clear);
Events.on(TileChangeEvent.class, tile -> {
Events.on(WorldLoadEvent.class, event -> clear());
Events.on(TileChangeEvent.class, event -> {
if(Net.client()) return;
for(Team team : Team.all){
TeamData data = state.teams.get(team);
if(state.teams.isActive(team) && data.team != tile.getTeam() && paths[data.team.ordinal()].weights[tile.x][tile.y] >= Float.MAX_VALUE){
update(tile, data.team);
if(state.teams.isActive(team) && data.team != event.tile.getTeam() && paths[data.team.ordinal()].weights[event.tile.x][event.tile.y] >= Float.MAX_VALUE){
update(event.tile, data.team);
}
}
update(tile, tile.getTeam());
update(event.tile, event.tile.getTeam());
});
}

View File

@ -163,7 +163,7 @@ public class WaveSpawner{
}
}
private void reset(){
private void reset(WorldLoadEvent event){
flySpawns.clear();
groundSpawns.clear();
quadrants = new GridBits(quadWidth(), quadHeight());

View File

@ -51,7 +51,6 @@ public class Control extends Module{
private Throwable error;
public Control(){
saves = new Saves();
db = new ContentDatabase();
@ -97,13 +96,13 @@ public class Control extends Module{
saves.load();
Events.on(StateChangeEvent.class, (from, to) -> {
if((from == State.playing && to == State.menu) || (from == State.menu && to != State.menu)){
Events.on(StateChangeEvent.class, event -> {
if((event.from == State.playing && event.to == State.menu) || (event.from == State.menu && event.to != State.menu)){
Timers.runTask(5f, Platform.instance::updateRPC);
}
});
Events.on(PlayEvent.class, () -> {
Events.on(PlayEvent.class, event -> {
for(Player player : players){
player.add();
}
@ -117,13 +116,13 @@ public class Control extends Module{
}
});
Events.on(WorldLoadGraphicsEvent.class, () -> {
Events.on(WorldLoadGraphicsEvent.class, event -> {
if(mobile){
Core.camera.position.set(players[0].x, players[0].y, 0);
}
});
Events.on(ResetEvent.class, () -> {
Events.on(ResetEvent.class, event -> {
for(Player player : players){
player.reset();
}
@ -133,7 +132,7 @@ public class Control extends Module{
saves.resetSave();
});
Events.on(WaveEvent.class, () -> {
Events.on(WaveEvent.class, event -> {
int last = Settings.getInt("hiscore" + world.getMap().name, 0);
@ -146,7 +145,7 @@ public class Control extends Module{
Platform.instance.updateRPC();
});
Events.on(GameOverEvent.class, () -> {
Events.on(GameOverEvent.class, event -> {
//delete saves for game-over sectors
if(world.getSector() != null && world.getSector().hasSave()){
world.getSector().getSave().delete();
@ -159,7 +158,7 @@ public class Control extends Module{
});
});
Events.on(WorldLoadEvent.class, () -> threads.runGraphics(() -> Events.fire(WorldLoadGraphicsEvent.class)));
Events.on(WorldLoadEvent.class, event -> threads.runGraphics(() -> Events.fire(new WorldLoadGraphicsEvent())));
}
public void addPlayer(int index){

View File

@ -19,7 +19,7 @@ public class GameState{
private State state = State.menu;
public void set(State astate){
Events.fire(StateChangeEvent.class, state, astate);
Events.fire(new StateChangeEvent(state, astate));
state = astate;
}

View File

@ -65,7 +65,7 @@ public class Logic extends Module{
}
}
Events.fire(PlayEvent.class);
Events.fire(new PlayEvent());
}
public void reset(){
@ -78,7 +78,7 @@ public class Logic extends Module{
Entities.clear();
TileEntity.sleepingEntities = 0;
Events.fire(ResetEvent.class);
Events.fire(new ResetEvent());
}
public void runWave(){
@ -86,14 +86,14 @@ public class Logic extends Module{
state.wave++;
state.wavetime = wavespace * state.difficulty.timeScaling;
Events.fire(WaveEvent.class);
Events.fire(new WaveEvent());
}
//this never triggers in PvP; only for checking sector game-overs
private void checkGameOver(){
if(state.teams.get(defaultTeam).cores.size == 0 && !state.gameOver){
state.gameOver = true;
Events.fire(GameOverEvent.class);
Events.fire(new GameOverEvent());
}
}

View File

@ -193,7 +193,7 @@ public class UI extends SceneModule{
public void resize(int width, int height){
super.resize(width, height);
Events.fire(ResizeEvent.class);
Events.fire(new ResizeEvent());
}
public Locale getLocale(){

View File

@ -213,7 +213,7 @@ public class World extends Module{
EntityPhysics.resizeTree(0, 0, tiles.length * tilesize, tiles[0].length * tilesize);
generating = false;
Events.fire(WorldLoadEvent.class);
Events.fire(new WorldLoadEvent());
}
/**Loads up a sector map. This does not call play(), but calls reset().*/
@ -279,7 +279,7 @@ public class World extends Module{
public void notifyChanged(Tile tile){
if(!generating){
threads.runDelay(() -> Events.fire(TileChangeEvent.class, tile));
threads.runDelay(() -> Events.fire(new TileChangeEvent(tile)));
}
}

View File

@ -268,11 +268,11 @@ public class Drone extends FlyingUnit implements BuilderTrait{
private static void initEvents(){
if(initialized) return;
Events.on(BlockBuildEvent.class, (team, tile) -> {
EntityGroup<BaseUnit> group = unitGroups[team.ordinal()];
Events.on(BlockBuildEvent.class, event -> {
EntityGroup<BaseUnit> group = unitGroups[event.team.ordinal()];
if(!(tile.entity instanceof BuildEntity)) return;
BuildEntity entity = tile.entity();
if(!(event.tile.entity instanceof BuildEntity)) return;
BuildEntity entity = event.tile.entity();
for(BaseUnit unit : group.all()){
if(unit instanceof Drone){

View File

@ -48,7 +48,7 @@ public class ContentDatabase{
//fire unlock event so other classes can use it
if(ret){
content.onUnlock();
Events.fire(UnlockEvent.class, content);
Events.fire(new UnlockEvent(content));
dirty = true;
}

View File

@ -6,64 +6,78 @@ import io.anuke.ucore.function.Event;
public class EventType{
public interface PlayEvent extends Event{
void handle();
public static class PlayEvent implements Event{
}
public interface ResetEvent extends Event{
void handle();
public static class ResetEvent implements Event{
}
public interface WaveEvent extends Event{
void handle();
public static class WaveEvent implements Event{
}
public interface GameOverEvent extends Event{
void handle();
public static class GameOverEvent implements Event{
}
/**
* This event is called from the logic thread.
* DO NOT INITIALIZE GRAPHICS HERE.
*/
public interface WorldLoadEvent extends Event{
void handle();
public static class WorldLoadEvent implements Event{
}
/**
* Called after the WorldLoadEvent is, and all logic has been loaded.
* It is safe to intialize graphics here.
*/
public interface WorldLoadGraphicsEvent extends Event{
void handle();
public static class WorldLoadGraphicsEvent implements Event{
}
/**
* Called from the logic thread. Do not access graphics here!
*/
public interface TileChangeEvent extends Event{
void handle(Tile tile);
public static class TileChangeEvent implements Event{
public final Tile tile;
public TileChangeEvent(Tile tile){
this.tile = tile;
}
}
//TODO unimplemented; remove?
public interface TileRemoveEvent extends Event{
void handle(Tile tile, Team oldTeam);
public static class StateChangeEvent implements Event{
public final State from, to;
public StateChangeEvent(State from, State to){
this.from = from;
this.to = to;
}
}
public interface StateChangeEvent extends Event{
void handle(State from, State to);
public static class UnlockEvent implements Event{
public final Content content;
public UnlockEvent(Content content){
this.content = content;
}
}
public interface UnlockEvent extends Event{
void handle(Content content);
public static class BlockBuildEvent implements Event{
public final Tile tile;
public final Team team;
public BlockBuildEvent(Tile tile, Team team){
this.tile = tile;
this.team = team;
}
}
public interface BlockBuildEvent extends Event{
void handle(Team team, Tile tile);
}
public static class ResizeEvent implements Event{
public interface ResizeEvent extends Event{
void handle();
}
}

View File

@ -31,8 +31,8 @@ public class Saves{
private long lastTimestamp;
public Saves(){
Events.on(StateChangeEvent.class, (prev, state) -> {
if(state == State.menu){
Events.on(StateChangeEvent.class, event -> {
if(event.to == State.menu){
threads.run(() -> {
totalPlaytime = 0;
lastTimestamp = 0;

View File

@ -40,18 +40,18 @@ public class BlockRenderer{
requests.set(i, new BlockRequest());
}
Events.on(WorldLoadGraphicsEvent.class, () -> {
Events.on(WorldLoadGraphicsEvent.class, event -> {
lastCamY = lastCamX = -99; //invalidate camera position so blocks get updated
});
Events.on(TileChangeEvent.class, tile -> {
Events.on(TileChangeEvent.class, event -> {
threads.runGraphics(() -> {
int avgx = Mathf.scl(camera.position.x, tilesize);
int avgy = Mathf.scl(camera.position.y, tilesize);
int rangex = (int) (camera.viewportWidth * camera.zoom / tilesize / 2) + 2;
int rangey = (int) (camera.viewportHeight * camera.zoom / tilesize / 2) + 2;
if(Math.abs(avgx - tile.x) <= rangex && Math.abs(avgy - tile.y) <= rangey){
if(Math.abs(avgx - event.tile.x) <= rangex && Math.abs(avgy - event.tile.y) <= rangey){
lastCamY = lastCamX = -99; //invalidate camera position so blocks get updated
}
});

View File

@ -42,7 +42,7 @@ public class FloorRenderer{
private IntArray drawnLayers = new IntArray();
public FloorRenderer(){
Events.on(WorldLoadGraphicsEvent.class, this::clearTiles);
Events.on(WorldLoadGraphicsEvent.class, event -> clearTiles());
gutterTile = new Tile(0, 0){
@Override

View File

@ -40,7 +40,7 @@ public class FogRenderer implements Disposable{
private boolean dirty;
public FogRenderer(){
Events.on(WorldLoadGraphicsEvent.class, () -> {
Events.on(WorldLoadGraphicsEvent.class, event -> {
dispose();
padding = world.getSector() != null ? mapPadding + extraPadding : 0;
@ -66,9 +66,9 @@ public class FogRenderer implements Disposable{
dirty = true;
});
Events.on(TileChangeEvent.class, tile -> threads.runGraphics(() -> {
if(tile.getTeam() == players[0].getTeam() && tile.block().synthetic() && tile.block().viewRange > 0){
changeQueue.add(tile);
Events.on(TileChangeEvent.class, event -> threads.runGraphics(() -> {
if(event.tile.getTeam() == players[0].getTeam() && event.tile.block().synthetic() && event.tile.block().viewRange > 0){
changeQueue.add(event.tile);
}
}));
}

View File

@ -39,13 +39,13 @@ public class MinimapRenderer implements Disposable{
private int zoom = 4;
public MinimapRenderer(){
Events.on(WorldLoadGraphicsEvent.class, () -> {
Events.on(WorldLoadGraphicsEvent.class, event -> {
reset();
updateAll();
});
//make sure to call on the graphics thread
Events.on(TileChangeEvent.class, tile -> Gdx.app.postRunnable(() -> update(tile)));
Events.on(TileChangeEvent.class, event -> Gdx.app.postRunnable(() -> update(event.tile)));
}
public Texture getTexture(){

View File

@ -34,7 +34,7 @@ public class FloatingDialog extends Dialog{
}
protected void onResize(Runnable run){
Events.on(ResizeEvent.class, () -> {
Events.on(ResizeEvent.class, event -> {
if(isShown()){
run.run();
}

View File

@ -89,14 +89,12 @@ public class BlocksFragment extends Fragment{
}).bottom().right().get();
});
Events.on(WorldLoadEvent.class, this::rebuild);
Events.on(WorldLoadEvent.class, event -> rebuild());
rebuild();
}
/**
* Rebuilds the whole placement menu, attempting to preserve previous state.
*/
/**Rebuilds the whole placement menu, attempting to preserve previous state.*/
void rebuild(){
selectTable.clear();

View File

@ -138,8 +138,8 @@ public class HudFragment extends Fragment{
parent.fill(t -> {
float notifDuration = 240f;
Events.on(StateChangeEvent.class, (from, to) -> {
if(to == State.menu || from == State.menu){
Events.on(StateChangeEvent.class, event -> {
if(event.to == State.menu || event.from == State.menu){
coreAttackTime = 0f;
lastCoreHP = Float.NaN;
}

View File

@ -27,7 +27,7 @@ public class MenuFragment extends Fragment{
buildDesktop();
}else{
buildMobile();
Events.on(ResizeEvent.class, this::buildMobile);
Events.on(ResizeEvent.class, event -> buildMobile());
}
});

View File

@ -99,7 +99,7 @@ public class Build{
}
threads.runDelay(() -> Events.fire(BlockBuildEvent.class, team, tile));
threads.runDelay(() -> Events.fire(new BlockBuildEvent(tile, team)));
}
/**Returns whether a tile can be placed at this location by this team.*/

View File

@ -94,7 +94,7 @@ public class ServerControl extends Module{
"&lrWARNING: &lyIt is highly advised to specify which version you're using by building with gradle args &lc-Pbuildversion=&lm<build>&ly so that clients know which version you are using.");
}
Events.on(GameOverEvent.class, () -> {
Events.on(GameOverEvent.class, event -> {
if(inExtraRound) return;
info("Game over!");