Functional loading
This commit is contained in:
@@ -9,7 +9,7 @@ import io.anuke.mindustry.game.*;
|
||||
import io.anuke.mindustry.io.*;
|
||||
import io.anuke.mindustry.maps.filters.*;
|
||||
|
||||
import static io.anuke.mindustry.Min.maps;
|
||||
import static io.anuke.mindustry.Vars.maps;
|
||||
|
||||
public class Map implements Comparable<Map>{
|
||||
/** Whether this is a custom map. */
|
||||
@@ -50,7 +50,7 @@ public class Map implements Comparable<Map>{
|
||||
}
|
||||
|
||||
public Map(StringMap tags){
|
||||
this(Min.customMapDirectory.child(tags.get("name", "unknown")), 0, 0, tags, true);
|
||||
this(Vars.customMapDirectory.child(tags.get("name", "unknown")), 0, 0, tags, true);
|
||||
}
|
||||
|
||||
public int getHightScore(){
|
||||
@@ -58,12 +58,12 @@ public class Map implements Comparable<Map>{
|
||||
}
|
||||
|
||||
public FileHandle previewFile(){
|
||||
return Min.mapPreviewDirectory.child(file.nameWithoutExtension() + ".png");
|
||||
return Vars.mapPreviewDirectory.child(file.nameWithoutExtension() + ".png");
|
||||
}
|
||||
|
||||
public void setHighScore(int score){
|
||||
Core.settings.put("hiscore" + file.nameWithoutExtension(), score);
|
||||
Min.data.modified();
|
||||
Vars.data.modified();
|
||||
}
|
||||
|
||||
/** Returns the result of applying this map's rules to the specified gamemode.*/
|
||||
@@ -84,7 +84,7 @@ public class Map implements Comparable<Map>{
|
||||
public Rules rules(Rules base){
|
||||
try{
|
||||
Rules result = JsonIO.read(Rules.class, base, tags.get("rules", "{}"));
|
||||
if(result.spawns.isEmpty()) result.spawns = Min.defaultWaves.get();
|
||||
if(result.spawns.isEmpty()) result.spawns = Vars.defaultWaves.get();
|
||||
return result;
|
||||
}catch(Exception e){
|
||||
//error reading rules. ignore?
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package io.anuke.mindustry.maps;
|
||||
|
||||
import io.anuke.arc.assets.*;
|
||||
import io.anuke.arc.assets.loaders.*;
|
||||
import io.anuke.arc.assets.loaders.resolvers.*;
|
||||
import io.anuke.arc.files.*;
|
||||
import io.anuke.mindustry.*;
|
||||
|
||||
public class MapPreviewLoader extends TextureLoader{
|
||||
|
||||
public MapPreviewLoader(){
|
||||
super(new AbsoluteFileHandleResolver());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadAsync(AssetManager manager, String fileName, FileHandle file, TextureParameter parameter){
|
||||
try{
|
||||
super.loadAsync(manager, fileName, file.sibling(file.nameWithoutExtension()), parameter);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
MapPreviewParameter param = (MapPreviewParameter)parameter;
|
||||
Vars.maps.createNewPreview(param.map);
|
||||
}
|
||||
}
|
||||
|
||||
public static class MapPreviewParameter extends TextureParameter{
|
||||
public Map map;
|
||||
|
||||
public MapPreviewParameter(Map map){
|
||||
this.map = map;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,13 +12,14 @@ import io.anuke.arc.util.serialization.*;
|
||||
import io.anuke.mindustry.content.*;
|
||||
import io.anuke.mindustry.game.*;
|
||||
import io.anuke.mindustry.io.*;
|
||||
import io.anuke.mindustry.maps.MapPreviewLoader.*;
|
||||
import io.anuke.mindustry.maps.filters.*;
|
||||
import io.anuke.mindustry.world.*;
|
||||
import io.anuke.mindustry.world.blocks.storage.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static io.anuke.mindustry.Min.*;
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class Maps{
|
||||
/** List of all built-in maps. Filenames only. */
|
||||
@@ -298,30 +299,32 @@ public class Maps{
|
||||
}
|
||||
|
||||
public void loadPreviews(){
|
||||
Core.assets.setLoader(Texture.class, "." + mapExtension, new MapPreviewLoader());
|
||||
for(Map map : maps){
|
||||
try{
|
||||
//try to load preview
|
||||
if(map.previewFile().exists()){
|
||||
try{
|
||||
Core.assets.load(new AssetDescriptor<>(map.previewFile(), Texture.class)).loaded = t -> map.texture = (Texture)t;
|
||||
//if it works, keep going
|
||||
continue;
|
||||
}catch(Exception e){
|
||||
Log.err("Found cached preview, but failed to load it!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//if it's here, then the preview failed to load or doesn't exist, make it
|
||||
//this has to be done synchronously!
|
||||
Pixmap pix = MapIO.generatePreview(map);
|
||||
Core.app.post(() -> map.texture = new Texture(pix));
|
||||
executor.submit(() -> map.previewFile().writePNG(pix));
|
||||
}catch(IOException e){
|
||||
e.printStackTrace();
|
||||
//try to load preview
|
||||
if(map.previewFile().exists()){
|
||||
//this may fail, but calls createNewPreview
|
||||
Core.assets.load(new AssetDescriptor<>(map.previewFile().path() + "." + mapExtension, Texture.class, new MapPreviewParameter(map))).loaded = t -> map.texture = (Texture)t;
|
||||
}else{
|
||||
createNewPreview(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void createNewPreview(Map map){
|
||||
try{
|
||||
//if it's here, then the preview failed to load or doesn't exist, make it
|
||||
//this has to be done synchronously!
|
||||
Pixmap pix = MapIO.generatePreview(map);
|
||||
Core.app.post(() -> map.texture = new Texture(pix));
|
||||
executor.submit(() -> map.previewFile().writePNG(pix));
|
||||
}catch(IOException e){
|
||||
Log.err("Failed to generate preview!", e);
|
||||
//TODO create error texture instead?
|
||||
map.texture = new Texture("zones/nomap.png");
|
||||
}
|
||||
}
|
||||
|
||||
/** Find a new filename to put a map to. */
|
||||
private FileHandle findFile(){
|
||||
//find a map name that isn't used.
|
||||
|
||||
@@ -13,7 +13,7 @@ import io.anuke.mindustry.world.*;
|
||||
import io.anuke.mindustry.world.Block.*;
|
||||
import io.anuke.mindustry.world.blocks.*;
|
||||
|
||||
import static io.anuke.mindustry.Min.updateEditorOnChange;
|
||||
import static io.anuke.mindustry.Vars.updateEditorOnChange;
|
||||
|
||||
public abstract class FilterOption{
|
||||
public static final Predicate<Block> floorsOnly = b -> (b instanceof Floor && !(b instanceof OverlayFloor)) && Core.atlas.isFound(b.icon(Icon.full));
|
||||
@@ -81,7 +81,7 @@ public abstract class FilterOption{
|
||||
FloatingDialog dialog = new FloatingDialog("");
|
||||
dialog.setFillParent(false);
|
||||
int i = 0;
|
||||
for(Block block : Min.content.blocks()){
|
||||
for(Block block : Vars.content.blocks()){
|
||||
if(!filter.test(block)) continue;
|
||||
|
||||
dialog.cont.addImage(block == Blocks.air ? Core.atlas.find("icon-none-small") : block.icon(Icon.medium)).size(8 * 4).pad(3).get().clicked(() -> {
|
||||
|
||||
@@ -5,7 +5,7 @@ import io.anuke.arc.math.*;
|
||||
import io.anuke.mindustry.maps.filters.FilterOption.*;
|
||||
import io.anuke.mindustry.world.*;
|
||||
|
||||
import static io.anuke.mindustry.Min.content;
|
||||
import static io.anuke.mindustry.Vars.content;
|
||||
|
||||
public class MedianFilter extends GenerateFilter{
|
||||
float radius = 2;
|
||||
|
||||
@@ -52,6 +52,6 @@ public class OreMedianFilter extends GenerateFilter{
|
||||
int index = Math.min((int)(blocks.size * percentile), blocks.size - 1);
|
||||
int overlay = blocks.get(index);
|
||||
|
||||
in.ore = Min.content.block(overlay);
|
||||
in.ore = Vars.content.block(overlay);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import io.anuke.mindustry.world.*;
|
||||
import io.anuke.mindustry.world.blocks.*;
|
||||
import io.anuke.mindustry.world.blocks.storage.*;
|
||||
|
||||
import static io.anuke.mindustry.Min.*;
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class MapGenerator extends Generator{
|
||||
private Map map;
|
||||
|
||||
@@ -6,7 +6,7 @@ import io.anuke.mindustry.maps.Map;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
|
||||
import static io.anuke.mindustry.Min.world;
|
||||
import static io.anuke.mindustry.Vars.world;
|
||||
|
||||
public abstract class RandomGenerator extends Generator{
|
||||
protected Block floor;
|
||||
|
||||
Reference in New Issue
Block a user