Game state cleanup
This commit is contained in:
@@ -18,6 +18,7 @@ import mindustry.game.EventType.*;
|
||||
import mindustry.gen.*;
|
||||
import mindustry.input.*;
|
||||
import mindustry.maps.*;
|
||||
import mindustry.maps.Map;
|
||||
import mindustry.mod.*;
|
||||
import mindustry.net.Net;
|
||||
import mindustry.net.*;
|
||||
@@ -142,6 +143,8 @@ public class Vars implements Loadable{
|
||||
public static Fi schematicDirectory;
|
||||
/** data subdirectory used for bleeding edge build versions */
|
||||
public static Fi bebuildDirectory;
|
||||
/** empty map, indicates no current map */
|
||||
public static Map emptyMap;
|
||||
/** map file extension */
|
||||
public static final String mapExtension = "msav";
|
||||
/** save file extension */
|
||||
@@ -219,6 +222,7 @@ public class Vars implements Loadable{
|
||||
modDirectory = dataDirectory.child("mods/");
|
||||
schematicDirectory = dataDirectory.child("schematics/");
|
||||
bebuildDirectory = dataDirectory.child("be_builds/");
|
||||
emptyMap = new Map(new StringMap());
|
||||
|
||||
if(tree == null) tree = new FileTree();
|
||||
if(mods == null) mods = new Mods();
|
||||
|
||||
@@ -88,9 +88,9 @@ public class Control implements ApplicationListener, Loadable{
|
||||
});
|
||||
|
||||
Events.on(WaveEvent.class, event -> {
|
||||
if(world.getMap().getHightScore() < state.wave){
|
||||
if(state.map.getHightScore() < state.wave){
|
||||
hiscore = true;
|
||||
world.getMap().setHighScore(state.wave);
|
||||
state.map.setHighScore(state.wave);
|
||||
}
|
||||
|
||||
Sounds.wave.play();
|
||||
|
||||
@@ -5,9 +5,10 @@ import arc.util.ArcAnnotate.*;
|
||||
import mindustry.game.EventType.*;
|
||||
import mindustry.game.*;
|
||||
import mindustry.gen.*;
|
||||
import mindustry.maps.*;
|
||||
import mindustry.type.*;
|
||||
|
||||
import static mindustry.Vars.net;
|
||||
import static mindustry.Vars.*;
|
||||
|
||||
public class GameState{
|
||||
/** Current wave number, can be anything in non-wave modes. */
|
||||
@@ -16,6 +17,8 @@ public class GameState{
|
||||
public float wavetime;
|
||||
/** Whether the game is in game over state. */
|
||||
public boolean gameOver = false, launched = false;
|
||||
/** Map that is currently being played on. */
|
||||
public @NonNull Map map = emptyMap;
|
||||
/** The current game rules. */
|
||||
public Rules rules = new Rules();
|
||||
/** Statistics for this save/game. Displayed after game over. */
|
||||
|
||||
@@ -120,12 +120,11 @@ public class Logic implements ApplicationListener{
|
||||
}
|
||||
|
||||
public void reset(){
|
||||
state.wave = 1;
|
||||
state.wavetime = state.rules.waveSpacing;
|
||||
state.gameOver = state.launched = false;
|
||||
state.teams = new Teams();
|
||||
state.rules = new Rules();
|
||||
state.stats = new Stats();
|
||||
State prev = state.getState();
|
||||
//recreate gamestate - sets state to menu
|
||||
state = new GameState();
|
||||
//fire change event, since it was technically changed
|
||||
Events.fire(new StateChangeEvent(prev, State.menu));
|
||||
|
||||
Groups.all.clear();
|
||||
Time.clear();
|
||||
|
||||
@@ -92,7 +92,6 @@ public class NetClient implements ApplicationListener{
|
||||
if(quietReset) return;
|
||||
|
||||
connecting = false;
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
platform.updateRPC();
|
||||
player.name(Core.settings.getString("name"));
|
||||
@@ -207,7 +206,6 @@ public class NetClient implements ApplicationListener{
|
||||
@Remote(called = Loc.client, variants = Variant.one)
|
||||
public static void onConnect(String ip, int port){
|
||||
netClient.disconnectQuietly();
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
|
||||
ui.join.connect(ip, port);
|
||||
@@ -233,7 +231,6 @@ public class NetClient implements ApplicationListener{
|
||||
@Remote(variants = Variant.one, priority = PacketPriority.high)
|
||||
public static void onKick(KickReason reason){
|
||||
netClient.disconnectQuietly();
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
|
||||
if(!reason.quiet){
|
||||
@@ -249,7 +246,6 @@ public class NetClient implements ApplicationListener{
|
||||
@Remote(variants = Variant.one, priority = PacketPriority.high)
|
||||
public static void onKick(String reason){
|
||||
netClient.disconnectQuietly();
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
ui.showText("$disconnect", reason, Align.left);
|
||||
ui.loadfrag.hide();
|
||||
|
||||
@@ -26,7 +26,6 @@ import static mindustry.Vars.*;
|
||||
public class World{
|
||||
public final Context context = new Context();
|
||||
|
||||
private Map currentMap = new Map(new StringMap());
|
||||
public @NonNull Tiles tiles = new Tiles(0, 0);
|
||||
|
||||
private boolean generating, invalidMap;
|
||||
@@ -60,14 +59,6 @@ public class World{
|
||||
return !wallSolid(x, y - 1) || !wallSolid(x, y + 1) || !wallSolid(x - 1, y) || !wallSolid(x + 1, y);
|
||||
}
|
||||
|
||||
public Map getMap(){
|
||||
return currentMap;
|
||||
}
|
||||
|
||||
public void setMap(Map map){
|
||||
this.currentMap = map;
|
||||
}
|
||||
|
||||
public int width(){
|
||||
return tiles.width;
|
||||
}
|
||||
@@ -214,7 +205,7 @@ public class World{
|
||||
}
|
||||
|
||||
public void loadSector(Sector sector){
|
||||
currentMap = new Map(StringMap.of("name", sector.planet.localizedName + "; Sector " + sector.id));
|
||||
state.map = new Map(StringMap.of("name", sector.planet.localizedName + "; Sector " + sector.id));
|
||||
state.rules.sector = sector;
|
||||
int size = sector.getSize();
|
||||
loadGenerator(size, size, tiles -> sector.planet.generator.generate(tiles, sector));
|
||||
@@ -238,7 +229,7 @@ public class World{
|
||||
return;
|
||||
}
|
||||
|
||||
this.currentMap = map;
|
||||
state.map = map;
|
||||
|
||||
invalidMap = false;
|
||||
|
||||
|
||||
@@ -247,11 +247,11 @@ public class MapEditorDialog extends Dialog implements Disposable{
|
||||
player.reset();
|
||||
state.rules = Gamemode.editor.apply(lastSavedRules.copy());
|
||||
state.rules.sector = null;
|
||||
world.setMap(new Map(StringMap.of(
|
||||
"name", "Editor Playtesting",
|
||||
"width", editor.width(),
|
||||
"height", editor.height()
|
||||
)));
|
||||
state.map = new Map(StringMap.of(
|
||||
"name", "Editor Playtesting",
|
||||
"width", editor.width(),
|
||||
"height", editor.height()
|
||||
));
|
||||
world.endMapLoad();
|
||||
//add entities so they update. is this really needed?
|
||||
for(Tile tile : world.tiles){
|
||||
|
||||
@@ -62,7 +62,7 @@ public abstract class SaveVersion extends SaveFileReader{
|
||||
"saved", Time.millis(),
|
||||
"playtime", headless ? 0 : control.saves.getTotalPlaytime(),
|
||||
"build", Version.build,
|
||||
"mapname", world.getMap() == null ? "unknown" : world.getMap().name(),
|
||||
"mapname", state.map.name(),
|
||||
"wave", state.wave,
|
||||
"wavetime", state.wavetime,
|
||||
"stats", JsonIO.write(state.stats),
|
||||
@@ -84,11 +84,12 @@ public abstract class SaveVersion extends SaveFileReader{
|
||||
lastReadBuild = map.getInt("build", -1);
|
||||
|
||||
Map worldmap = maps.byName(map.get("mapname", "\\\\\\"));
|
||||
world.setMap(worldmap == null ? new Map(StringMap.of(
|
||||
Map map1 = worldmap == null ? new Map(StringMap.of(
|
||||
"name", map.get("mapname", "Unknown"),
|
||||
"width", 1,
|
||||
"height", 1
|
||||
)) : worldmap);
|
||||
)) : worldmap;
|
||||
state.map = map1;
|
||||
}
|
||||
|
||||
public void writeMap(DataOutput stream) throws IOException{
|
||||
|
||||
@@ -22,6 +22,7 @@ public class FileMapGenerator implements WorldGenerator{
|
||||
|
||||
@Override
|
||||
public void generate(Tiles tiles){
|
||||
if(true) throw new IllegalArgumentException("no!");
|
||||
tiles.fill();
|
||||
|
||||
SaveIO.load(map.file);
|
||||
@@ -60,6 +61,6 @@ public class FileMapGenerator implements WorldGenerator{
|
||||
}
|
||||
|
||||
world.prepareTiles(tiles);
|
||||
world.setMap(map);
|
||||
state.map = map;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class ArcNetProvider implements NetProvider{
|
||||
Core.app.post(() -> {
|
||||
try{
|
||||
net.handleClientReceived(object);
|
||||
}catch(Exception e){
|
||||
}catch(Throwable e){
|
||||
handleException(e);
|
||||
}
|
||||
});
|
||||
@@ -113,9 +113,7 @@ public class ArcNetProvider implements NetProvider{
|
||||
Core.app.post(() -> {
|
||||
try{
|
||||
net.handleServerReceived(k, object);
|
||||
}catch(RuntimeException e){
|
||||
e.printStackTrace();
|
||||
}catch(Exception e){
|
||||
}catch(Throwable e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
@@ -267,7 +265,7 @@ public class ArcNetProvider implements NetProvider{
|
||||
return null;
|
||||
}
|
||||
|
||||
private void handleException(Exception e){
|
||||
private void handleException(Throwable e){
|
||||
if(e instanceof ArcNetException){
|
||||
Core.app.post(() -> net.showError(new IOException("mismatch")));
|
||||
}else if(e instanceof ClosedChannelException){
|
||||
|
||||
@@ -21,7 +21,7 @@ public class NetworkIO{
|
||||
|
||||
try(DataOutputStream stream = new DataOutputStream(os)){
|
||||
stream.writeUTF(JsonIO.write(state.rules));
|
||||
SaveIO.getSaveWriter().writeStringMap(stream, world.getMap().tags);
|
||||
SaveIO.getSaveWriter().writeStringMap(stream, state.map.tags);
|
||||
|
||||
stream.writeInt(state.wave);
|
||||
stream.writeFloat(state.wavetime);
|
||||
@@ -41,7 +41,7 @@ public class NetworkIO{
|
||||
try(DataInputStream stream = new DataInputStream(is)){
|
||||
Time.clear();
|
||||
state.rules = JsonIO.read(Rules.class, stream.readUTF());
|
||||
world.setMap(new Map(SaveIO.getSaveWriter().readStringMap(stream)));
|
||||
state.map = new Map(SaveIO.getSaveWriter().readStringMap(stream));
|
||||
|
||||
state.wave = stream.readInt();
|
||||
state.wavetime = stream.readFloat();
|
||||
@@ -65,7 +65,7 @@ public class NetworkIO{
|
||||
public static ByteBuffer writeServerData(){
|
||||
String name = (headless ? Config.name.string() : player.name());
|
||||
String description = headless && !Config.desc.string().equals("off") ? Config.desc.string() : "";
|
||||
String map = world.getMap() == null ? "None" : world.getMap().name();
|
||||
String map = state.map.name();
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(512);
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package mindustry.ui.dialogs;
|
||||
|
||||
import arc.*;
|
||||
import mindustry.core.GameState.*;
|
||||
import mindustry.game.*;
|
||||
import mindustry.game.EventType.*;
|
||||
import mindustry.game.Stats.*;
|
||||
import mindustry.game.*;
|
||||
import mindustry.type.*;
|
||||
import mindustry.ui.Cicon;
|
||||
import mindustry.ui.*;
|
||||
|
||||
import static mindustry.Vars.*;
|
||||
|
||||
@@ -40,7 +39,6 @@ public class GameOverDialog extends FloatingDialog{
|
||||
cont.add(Core.bundle.format("gameover.pvp", winner.localized())).pad(6);
|
||||
buttons.addButton("$menu", () -> {
|
||||
hide();
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
}).size(130f, 60f);
|
||||
}else{
|
||||
@@ -90,14 +88,12 @@ public class GameOverDialog extends FloatingDialog{
|
||||
if(state.isCampaign()){
|
||||
buttons.addButton("$continue", () -> {
|
||||
hide();
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
ui.planet.show();
|
||||
}).size(130f, 60f);
|
||||
}else{
|
||||
buttons.addButton("$menu", () -> {
|
||||
hide();
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
}).size(130f, 60f);
|
||||
}
|
||||
|
||||
@@ -178,7 +178,6 @@ public class LoadDialog extends FloatingDialog{
|
||||
state.set(State.playing);
|
||||
}catch(SaveException e){
|
||||
Log.err(e);
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
ui.showErrorMessage("$save.corrupted");
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package mindustry.ui.dialogs;
|
||||
|
||||
import arc.*;
|
||||
import arc.input.*;
|
||||
import mindustry.core.GameState.*;
|
||||
import mindustry.gen.*;
|
||||
|
||||
import static mindustry.Vars.*;
|
||||
@@ -118,7 +117,6 @@ public class PausedDialog extends FloatingDialog{
|
||||
}
|
||||
|
||||
if(control.saves.getCurrent() == null || !control.saves.getCurrent().isAutosave() || state.rules.tutorial || wasClient){
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
return;
|
||||
}
|
||||
@@ -130,7 +128,6 @@ public class PausedDialog extends FloatingDialog{
|
||||
e.printStackTrace();
|
||||
ui.showException("[accent]" + Core.bundle.get("savefail"), e);
|
||||
}
|
||||
state.set(State.menu);
|
||||
logic.reset();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user