Basic non-in-game editor functionality
This commit is contained in:
@@ -25,7 +25,7 @@ public class DrawOperation{
|
||||
public void undo(MapEditor editor){
|
||||
for(int i = array.size - 1; i >= 0; i--){
|
||||
long l = array.get(i);
|
||||
array.set(i, TileOp.value(l, get(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l))));
|
||||
array.set(i, TileOp.get(TileOp.x(l), TileOp.y(l), TileOp.type(l), get(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l))));
|
||||
set(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l), TileOp.value(l));
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ public class DrawOperation{
|
||||
public void redo(MapEditor editor){
|
||||
for(int i = 0; i < array.size; i++){
|
||||
long l = array.get(i);
|
||||
array.set(i, TileOp.value(l, get(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l))));
|
||||
array.set(i, TileOp.get(TileOp.x(l), TileOp.y(l), TileOp.type(l), get(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l))));
|
||||
set(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l), TileOp.value(l));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import static io.anuke.mindustry.Vars.ui;
|
||||
//TODO somehow remove or replace this class with a more flexible solution
|
||||
public class EditorTile extends Tile{
|
||||
|
||||
public EditorTile(int x, int y, short floor, short overlay, short wall){
|
||||
public EditorTile(int x, int y, int floor, int overlay, int wall){
|
||||
super(x, y, floor, overlay, wall);
|
||||
}
|
||||
|
||||
@@ -93,6 +93,8 @@ public class EditorTile extends Tile{
|
||||
|
||||
if(block.hasEntity()){
|
||||
entity = block.newEntity();
|
||||
entity.tile = this;
|
||||
entity.block = block;
|
||||
entity.health = block.health;
|
||||
entity.cons = new ConsumeModule(entity);
|
||||
if(block.hasItems) entity.items = new ItemModule();
|
||||
|
||||
@@ -50,11 +50,9 @@ public class MapEditor{
|
||||
reset();
|
||||
|
||||
loading = true;
|
||||
//TODO redundant and does nothing since tiles are overwritten
|
||||
createTiles(map.width, map.height);
|
||||
tags.putAll(map.tags);
|
||||
//TODO this actually creates the tiles, which are not editor tiles
|
||||
MapIO.loadMap(map);
|
||||
MapIO.loadMap(map, EditorTile::new);
|
||||
checkLinkedTiles();
|
||||
renderer.resize(width(), height());
|
||||
loading = false;
|
||||
|
||||
@@ -12,6 +12,7 @@ import io.anuke.mindustry.game.Team;
|
||||
import io.anuke.mindustry.game.Version;
|
||||
import io.anuke.mindustry.maps.Map;
|
||||
import io.anuke.mindustry.world.*;
|
||||
import io.anuke.mindustry.world.Tile.TileConstructor;
|
||||
import io.anuke.mindustry.world.blocks.Floor;
|
||||
|
||||
import java.io.*;
|
||||
@@ -60,6 +61,10 @@ public class MapIO{
|
||||
SaveIO.load(map.file);
|
||||
}
|
||||
|
||||
public static void loadMap(Map map, TileConstructor cons){
|
||||
SaveIO.load(map.file, cons);
|
||||
}
|
||||
|
||||
public static Pixmap generatePreview(Map map) throws IOException{
|
||||
Time.mark();
|
||||
Pixmap floors = new Pixmap(map.width, map.height, Format.RGBA8888);
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.anuke.arc.collection.ObjectMap.Entry;
|
||||
import io.anuke.arc.collection.StringMap;
|
||||
import io.anuke.arc.util.io.CounterInputStream;
|
||||
import io.anuke.arc.util.io.ReusableByteOutStream;
|
||||
import io.anuke.mindustry.world.Tile.TileConstructor;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
@@ -102,7 +103,7 @@ public abstract class SaveFileReader{
|
||||
return map;
|
||||
}
|
||||
|
||||
public abstract void read(DataInputStream stream, CounterInputStream counter) throws IOException;
|
||||
public abstract void read(DataInputStream stream, CounterInputStream counter, TileConstructor tiles) throws IOException;
|
||||
|
||||
public abstract void write(DataOutputStream stream) throws IOException;
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import io.anuke.arc.util.io.CounterInputStream;
|
||||
import io.anuke.arc.util.io.FastDeflaterOutputStream;
|
||||
import io.anuke.mindustry.Vars;
|
||||
import io.anuke.mindustry.io.versions.Save1;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.Tile.TileConstructor;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
@@ -131,28 +133,33 @@ public class SaveIO{
|
||||
}
|
||||
|
||||
public static void load(FileHandle file) throws SaveException{
|
||||
load(file, Tile::new);
|
||||
}
|
||||
|
||||
public static void load(FileHandle file, TileConstructor cons) throws SaveException{
|
||||
try{
|
||||
//try and load; if any exception at all occurs
|
||||
load(new InflaterInputStream(file.read(bufferSize)));
|
||||
load(new InflaterInputStream(file.read(bufferSize)), cons);
|
||||
}catch(SaveException e){
|
||||
e.printStackTrace();
|
||||
FileHandle backup = file.sibling(file.name() + "-backup." + file.extension());
|
||||
if(backup.exists()){
|
||||
load(new InflaterInputStream(backup.read(bufferSize)));
|
||||
load(new InflaterInputStream(backup.read(bufferSize)), cons);
|
||||
}else{
|
||||
throw new SaveException(e.getCause());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void load(InputStream is) throws SaveException{
|
||||
/** Loads from a deflated (!) input stream.*/
|
||||
public static void load(InputStream is, TileConstructor cons) throws SaveException{
|
||||
try(CounterInputStream counter = new CounterInputStream(is); DataInputStream stream = new DataInputStream(counter)){
|
||||
logic.reset();
|
||||
readHeader(stream);
|
||||
int version = stream.readInt();
|
||||
SaveVersion ver = versions.get(version);
|
||||
|
||||
ver.read(stream, counter);
|
||||
ver.read(stream, counter, cons);
|
||||
}catch(Exception e){
|
||||
throw new SaveException(e);
|
||||
}finally{
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.anuke.mindustry.game.*;
|
||||
import io.anuke.mindustry.type.ContentType;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.Tile.TileConstructor;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
@@ -29,18 +30,20 @@ public abstract class SaveVersion extends SaveFileReader{
|
||||
return new SaveMeta(map.getInt("version"), map.getLong("saved"), map.getLong("playtime"), map.getInt("build"), map.get("mapname"), map.getInt("wave"), JsonIO.read(Rules.class, map.get("rules", "{}")));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void write(DataOutputStream stream) throws IOException{
|
||||
write(stream, new StringMap());
|
||||
}
|
||||
|
||||
public final void read(DataInputStream stream, CounterInputStream counter) throws IOException{
|
||||
@Override
|
||||
public final void read(DataInputStream stream, CounterInputStream counter, TileConstructor tiles) throws IOException{
|
||||
region("meta", stream, counter, this::readMeta);
|
||||
region("content", stream, counter, this::readContentHeader);
|
||||
region("map", stream, counter, this::readMap);
|
||||
region("map", stream, counter, in -> readMap(in, tiles));
|
||||
region("entities", stream, counter, this::readEntities);
|
||||
}
|
||||
|
||||
public void write(DataOutputStream stream, StringMap extraTags) throws IOException{
|
||||
public final void write(DataOutputStream stream, StringMap extraTags) throws IOException{
|
||||
region("meta", stream, out -> writeMeta(out, extraTags));
|
||||
region("content", stream, this::writeContentHeader);
|
||||
region("map", stream, this::writeMap);
|
||||
@@ -127,7 +130,7 @@ public abstract class SaveVersion extends SaveFileReader{
|
||||
}
|
||||
}
|
||||
|
||||
public void readMap(DataInput stream) throws IOException{
|
||||
public void readMap(DataInput stream, TileConstructor constructor) throws IOException{
|
||||
int width = stream.readUnsignedShort();
|
||||
int height = stream.readUnsignedShort();
|
||||
|
||||
@@ -144,11 +147,11 @@ public abstract class SaveVersion extends SaveFileReader{
|
||||
short oreid = stream.readShort();
|
||||
int consecutives = stream.readUnsignedByte();
|
||||
|
||||
tiles[x][y] = new Tile(x, y, floorid, oreid, (short)0);
|
||||
tiles[x][y] = constructor.construct(x, y, floorid, oreid, (short)0);
|
||||
|
||||
for(int j = i + 1; j < i + 1 + consecutives; j++){
|
||||
int newx = j % width, newy = j / width;
|
||||
tiles[newx][newy] = new Tile(newx, newy, floorid, oreid, (short)0);
|
||||
tiles[newx][newy] = constructor.construct(newx, newy, floorid, oreid, (short)0);
|
||||
}
|
||||
|
||||
i += consecutives;
|
||||
|
||||
@@ -8,6 +8,7 @@ import io.anuke.mindustry.game.Version;
|
||||
import io.anuke.mindustry.gen.Serialization;
|
||||
import io.anuke.mindustry.io.SaveIO;
|
||||
import io.anuke.mindustry.maps.Map;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -52,7 +53,7 @@ public class NetworkIO{
|
||||
player.resetID(id);
|
||||
player.add();
|
||||
|
||||
SaveIO.getSaveWriter().readMap(stream);
|
||||
SaveIO.getSaveWriter().readMap(stream, Tile::new);
|
||||
}catch(IOException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -36,12 +36,12 @@ public class Tile implements Position, TargetTrait{
|
||||
block = floor = (Floor)Blocks.air;
|
||||
}
|
||||
|
||||
public Tile(int x, int y, short floor, short overlay, short wall){
|
||||
public Tile(int x, int y, int floor, int overlay, int wall){
|
||||
this.x = (short)x;
|
||||
this.y = (short)y;
|
||||
this.floor = (Floor)content.block(floor);
|
||||
this.block = content.block(wall);
|
||||
this.overlay = overlay;
|
||||
this.overlay = (short)overlay;
|
||||
|
||||
//update entity and create it if needed
|
||||
changed();
|
||||
@@ -454,4 +454,8 @@ public class Tile implements Position, TargetTrait{
|
||||
public String toString(){
|
||||
return floor.name + ":" + block.name + ":" + content.block(overlay) + "[" + x + "," + y + "] " + "entity=" + (entity == null ? "null" : (entity.getClass()));
|
||||
}
|
||||
|
||||
public interface TileConstructor{
|
||||
Tile construct(int x, int y, int floor, int overlay, int wall);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user