Implemented saving of conveyor belt contents and turret ammo

This commit is contained in:
Anuken
2017-08-23 20:27:09 -04:00
parent d6050c59db
commit 86c02bf469
7 changed files with 62 additions and 25 deletions

View File

@@ -276,8 +276,6 @@ public class Control extends RendererModule{
public void update(){
if(debug){
if(Inputs.keyUp(Keys.ESCAPE))
Gdx.app.exit();
if(Inputs.keyUp(Keys.SPACE))
Effects.sound("shoot", World.core.worldx(), World.core.worldy());

View File

@@ -24,7 +24,7 @@ public class Vars{
//how much the zoom changes every zoom button press
public static final int zoomScale = Math.round(Unit.dp.inPixels(1));
//if true, player speed will be increased, massive amounts of resources will be given on start, and other debug options will be available
public static boolean debug = true;
public static boolean debug = false;
//number of save slots-- increasing may lead to layout issues
public static final int saveSlots = 4;

View File

@@ -2,6 +2,7 @@ package io.anuke.mindustry.entities;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.badlogic.gdx.utils.ObjectMap;
@@ -32,11 +33,11 @@ public class TileEntity extends Entity{
return this;
}
public void write(DataOutputStream stream){
public void write(DataOutputStream stream) throws IOException{
}
public void read(TileEntity entity, DataInputStream stream){
public void read(DataInputStream stream) throws IOException{
}

View File

@@ -9,20 +9,18 @@ import java.util.Date;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import io.anuke.mindustry.*;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.Weapon;
import io.anuke.mindustry.entities.enemies.*;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.Blocks;
import io.anuke.mindustry.world.blocks.Conveyor.ConveyorEntity;
import io.anuke.mindustry.world.blocks.Turret.TurretEntity;
import io.anuke.ucore.entities.Entities;
import io.anuke.ucore.entities.Entity;
@@ -76,6 +74,7 @@ import io.anuke.ucore.entities.Entity;
* (item list)
* Item ID (byte)
* Item amount (int)
* Additional tile entity data (varies, check the TileEntity classes)
*
*/
public class SaveIO{
@@ -97,17 +96,6 @@ public class SaveIO{
put(enemyIDs.get(value), value);
}};
private static final ObjectMap<Class<? extends TileEntity>, Byte> tileIDs = new ObjectMap<Class<? extends TileEntity>, Byte>(){{
put(TileEntity.class, (byte)0);
put(TurretEntity.class, (byte)1);
put(ConveyorEntity.class, (byte)2);
}};
private static final ObjectMap<Byte, Class<? extends TileEntity>> idTiles = new ObjectMap<Byte, Class<? extends TileEntity>>(){{
for(Class<? extends TileEntity> value : tileIDs.keys())
put(tileIDs.get(value), value);
}};
public static void saveToSlot(int slot){
write(fileFor(slot));
}
@@ -232,18 +220,16 @@ public class SaveIO{
stream.writeInt(tile.block().id); //block ID
if(tile.entity != null){
stream.writeByte(tile.rotation);
stream.writeByte(tile.rotation); //rotation
stream.writeInt(tile.entity.health); //health
stream.writeByte(tile.entity.items.size); //amount of items
//if(strea){
// stream.writeInt(tile.entity.);
//}
for(Item item : tile.entity.items.keys()){
stream.writeByte(item.ordinal()); //item ID
stream.writeInt(tile.entity.items.get(item)); //item amount
}
tile.entity.write(stream);
}
}
}
@@ -311,6 +297,8 @@ public class SaveIO{
int enemies = stream.readInt();
Array<Enemy> enemiesToUpdate = new Array<>();
for(int i = 0; i < enemies; i ++){
byte type = stream.readByte();
int lane = stream.readByte();
@@ -320,11 +308,11 @@ public class SaveIO{
try{
Enemy enemy = (Enemy)ClassReflection.getConstructor(idEnemies.get(type), int.class).newInstance(lane);
enemy.findClosestNode();
enemy.health = health;
enemy.x = x;
enemy.y = y;
enemy.add();
enemiesToUpdate.add(enemy);
}catch (Exception e){
throw new RuntimeException(e);
}
@@ -344,6 +332,10 @@ public class SaveIO{
World.loadMap(mapid, seed);
Renderer.clearTiles();
for(Enemy enemy : enemiesToUpdate){
enemy.findClosestNode();
}
for(int x = 0; x < World.width(); x ++){
for(int y = 0; y < World.height(); y ++){
Tile tile = World.tile(x, y);
@@ -376,6 +368,8 @@ public class SaveIO{
int itemamount = stream.readInt();
tile.entity.items.put(itemEnums[itemid], itemamount);
}
tile.entity.read(stream);
}
}

View File

@@ -2,6 +2,10 @@ package io.anuke.mindustry.world.blocks;
import static io.anuke.mindustry.Vars.tilesize;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.DelayedRemovalArray;
@@ -88,6 +92,31 @@ public class Conveyor extends Block{
public static class ConveyorEntity extends TileEntity{
DelayedRemovalArray<ItemPos> convey = new DelayedRemovalArray<>();
@Override
public void write(DataOutputStream stream) throws IOException{
stream.writeInt(convey.size);
for(ItemPos pos : convey){
stream.writeByte(pos.item.ordinal());
stream.writeByte((int)(pos.pos*255-128));
stream.writeByte((int)(pos.y*255-128));
}
}
@Override
public void read(DataInputStream stream) throws IOException{
int amount = stream.readInt();
for(int i = 0; i < amount; i ++){
Item item = Item.values()[stream.readByte()];
float pos = ((int)stream.readByte()+128)/255f;
float y = ((int)stream.readByte()+128)/255f;
ItemPos out = new ItemPos(item, pos, y);
convey.add(out);
}
}
}
static class ItemPos{
@@ -99,5 +128,6 @@ public class Conveyor extends Block{
this.pos = pos;
this.y = y;
}
}
}

View File

@@ -1,6 +1,10 @@
package io.anuke.mindustry.world.blocks;
import static io.anuke.mindustry.Vars.tilesize;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils;
@@ -125,5 +129,15 @@ public class Turret extends Block{
public TileEntity target;
public int ammo;
public float rotation;
@Override
public void write(DataOutputStream stream) throws IOException{
stream.writeInt(ammo);
}
@Override
public void read(DataInputStream stream) throws IOException{
this.ammo = stream.readInt();
}
}
}