Changed inventory system to use real core inventory

This commit is contained in:
Anuken
2018-04-14 11:14:00 -04:00
parent 86b7966027
commit fcf62ada8f
16 changed files with 77 additions and 118 deletions

View File

@@ -142,8 +142,6 @@ public class Control extends Module{
Core.camera.position.set(player.x, player.y, 0);
ui.hudfrag.updateItems();
state.set(State.playing);
});
@@ -159,7 +157,6 @@ public class Control extends Module{
respawntime = -1;
hiscore = false;
ui.hudfrag.updateItems();
ui.hudfrag.fadeRespawn(false);
});
@@ -324,11 +321,6 @@ public class Control extends Module{
saves.update();
if(state.inventory.isUpdated() && (Timers.get("updateItems", 8) || state.is(State.paused))){
ui.hudfrag.updateItems();
state.inventory.setUpdated(false);
}
if(!state.is(State.menu)){
input.update();

View File

@@ -42,6 +42,8 @@ public class Logic extends Module {
if(state.mode.infiniteResources){
state.inventory.fill();
}else{
state.inventory.clearItems();
}
Events.fire(PlayEvent.class);
@@ -53,7 +55,6 @@ public class Logic extends Module {
state.wavetime = wavespace * state.difficulty.timeScaling;
state.enemies = 0;
state.gameOver = false;
state.inventory.clearItems();
state.teams = new TeamInfo();
state.teams.add(Team.blue, true);
state.teams.add(Team.red, false);

View File

@@ -144,13 +144,11 @@ public class NetClient extends Module {
Net.handleClient(StateSyncPacket.class, packet -> {
System.arraycopy(packet.items, 0, state.inventory.getItems(), 0, packet.items.length);
System.arraycopy(packet.items, 0, state.inventory.writeItems(), 0, packet.items.length);
state.enemies = packet.enemies;
state.wavetime = packet.countdown;
state.wave = packet.wave;
ui.hudfrag.updateItems();
});
Net.handleClient(PlacePacket.class, (packet) -> {

View File

@@ -446,7 +446,7 @@ public class NetServer extends Module{
if(timer.get(timerStateSync, itemSyncTime)){
StateSyncPacket packet = new StateSyncPacket();
packet.items = state.inventory.getItems();
packet.items = state.inventory.readItems();
packet.countdown = state.wavetime;
packet.enemies = state.enemies;
packet.wave = state.wave;

View File

@@ -185,7 +185,7 @@ public class Player extends Unit{
}
float backTrns = 4f, itemSize = 5f;
if(inventory.hasItem() && !control.input().isDroppingItem()){
if(inventory.hasItem()){
ItemStack stack = inventory.getItem();
Draw.rect(stack.item.region, x + Angles.trnsx(rotation + 180f, backTrns), y + Angles.trnsy(rotation + 180f, backTrns), itemSize, itemSize, rotation);
//Draw.tint(Color.WHITE);

View File

@@ -1,33 +1,27 @@
package io.anuke.mindustry.game;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectSet;
import io.anuke.mindustry.content.Items;
import io.anuke.mindustry.game.TeamInfo.TeamData;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.world.Tile;
import java.util.Arrays;
import static io.anuke.mindustry.Vars.debug;
import static io.anuke.mindustry.Vars.state;
public class Inventory {
private final int[] items = new int[Item.getAllItems().size];
private boolean updated;
public boolean isUpdated(){
return updated;
}
public void setUpdated(boolean updated){
this.updated = updated;
}
private final int[] empty = new int[Item.getAllItems().size];
public void clearItems(){
updated = true;
Arrays.fill(items, 0);
Arrays.fill(items(), 0);
if(debug){
for(Item item : Item.getAllItems()){
if(item.material) items[item.id] = 99999;
if(item.material) items()[item.id] = 99999;
}
}else{
addItem(Items.iron, 40);
@@ -35,16 +29,15 @@ public class Inventory {
}
public void fill(){
Arrays.fill(items, 999999999);
Arrays.fill(items(), 999999999);
}
public int getAmount(Item item){
return items[item.id];
return items()[item.id];
}
public void addItem(Item item, int amount){
updated = true;
items[item.id] += amount;
items()[item.id] += amount;
}
public boolean hasItems(ItemStack[] items){
@@ -62,29 +55,42 @@ public class Inventory {
}
public boolean hasItem(ItemStack req){
updated = true;
return items[req.item.id] >= req.amount;
return items()[req.item.id] >= req.amount;
}
public boolean hasItem(Item item, int amount){
updated = true;
return items[item.id] >= amount;
return items()[item.id] >= amount;
}
public void removeItem(ItemStack req){
updated = true;
items[req.item.id] -= req.amount;
if(items[req.item.id] < 0) items[req.item.id] = 0; //prevents negative item glitches in multiplayer
items()[req.item.id] -= req.amount;
if(items()[req.item.id] < 0) items()[req.item.id] = 0; //prevents negative item glitches in multiplayer
}
public void removeItems(ItemStack... reqs){
updated = true;
for(ItemStack req : reqs)
removeItem(req);
}
public int[] writeItems(){
return items();
}
public int[] readItems(){
return items();
}
/*
public int[] getItems(){
updated = true;
return items;
return items();
}*/
private int[] items(){
ObjectSet<TeamData> set = state.teams.getTeams(true);
if(set.size == 0) return empty;
Array<Tile> tiles = set.first().cores;
if(tiles.size == 0) return empty;
return tiles.first().entity.inventory.items;
}
}

View File

@@ -12,7 +12,6 @@ import io.anuke.mindustry.game.Difficulty;
import io.anuke.mindustry.game.GameMode;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.io.SaveFileVersion;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.resource.Upgrade;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
@@ -25,7 +24,6 @@ import io.anuke.ucore.util.Bits;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;
import static io.anuke.mindustry.Vars.*;
@@ -95,20 +93,6 @@ public class Save16 extends SaveFileVersion {
for(int i = 0; i < b; i ++) stream.readByte();
}
//inventory
int totalItems = stream.readByte();
Arrays.fill(state.inventory.getItems(), 0);
for(int i = 0; i < totalItems; i ++){
Item item = Item.getByID(stream.readByte());
int amount = stream.readInt();
state.inventory.getItems()[item.id] = amount;
}
if(!headless) ui.hudfrag.updateItems();
//enemies
byte teams = stream.readByte();
@@ -242,26 +226,6 @@ public class Save16 extends SaveFileVersion {
stream.writeByte(0);
}
//--INVENTORY--
int l = state.inventory.getItems().length;
int itemsize = 0;
for(int i = 0; i < l; i ++){
if(state.inventory.getItems()[i] > 0){
itemsize ++;
}
}
stream.writeByte(itemsize); //amount of items
for(int i = 0; i < l; i ++){
if(state.inventory.getItems()[i] > 0){
stream.writeByte(i); //item ID
stream.writeInt(state.inventory.getItems()[i]); //item amount
}
}
//--ENEMIES--
stream.writeByte(Team.values().length); //amount of total teams (backwards compatibility)

View File

@@ -41,12 +41,6 @@ public class NetworkIO {
stream.writeInt(player.id); //player remap ID
stream.writeBoolean(player.isAdmin);
//--INVENTORY--
for(int i = 0; i < state.inventory.getItems().length; i ++){ //items
stream.writeInt(state.inventory.getItems()[i]);
}
stream.writeByte(upgrades.size); //upgrade data
for(int i = 0; i < upgrades.size; i ++){
@@ -120,13 +114,6 @@ public class NetworkIO {
int pid = stream.readInt();
boolean admin = stream.readBoolean();
//inventory
for(int i = 0; i < state.inventory.getItems().length; i ++){
state.inventory.getItems()[i] = stream.readInt();
}
ui.hudfrag.updateItems();
control.upgrades().getWeapons().clear();
control.upgrades().getWeapons().add(Weapons.blaster);

View File

@@ -94,15 +94,7 @@ public class LevelDialog extends FloatingDialog{
Table inset = new Table("pane-button");
inset.add("[accent]" + Bundles.get("map."+map.name+".name", map.name)).pad(3f);
inset.row();
inset.label((() ->{
try{
return Bundles.format("text.level.highscore", Settings.getInt("hiscore" + map.name));
}catch (Exception e){
Settings.defaults("hiscore" + map.name, 1);
return Bundles.format("text.level.highscore", 0);
}
}))
.pad(3f);
inset.label((() -> Bundles.format("text.level.highscore", Settings.getInt("hiscore" + map.name, 0)))).pad(3f);
inset.pack();
float images = 154f;

View File

@@ -5,10 +5,14 @@ import com.badlogic.gdx.graphics.Colors;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.IntSet;
import io.anuke.mindustry.content.Recipes;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.input.InputHandler;
import io.anuke.mindustry.resource.*;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.resource.Recipe;
import io.anuke.mindustry.resource.Section;
import io.anuke.mindustry.ui.dialogs.FloatingDialog;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.BlockStats;
@@ -24,6 +28,7 @@ import io.anuke.ucore.scene.ui.*;
import io.anuke.ucore.scene.ui.layout.Stack;
import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.ucore.util.Bundles;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Strings;
@@ -34,6 +39,7 @@ public class BlocksFragment implements Fragment{
private Stack stack = new Stack();
private boolean shown = true;
private Recipe hoveredDescriptionRecipe;
private IntSet itemset = new IntSet();
public void build(){
InputHandler input = control.input();
@@ -48,6 +54,16 @@ public class BlocksFragment implements Fragment{
itemtable = new Table("button");
itemtable.setVisible(() -> input.recipe == null && !state.mode.infiniteResources);
itemtable.update(() -> {
int[] items = state.inventory.readItems();
for(int i = 0; i < items.length; i ++){
if(itemset.contains(items[i]) != (items[i] > 0)){
Log.info("Updating items.");
updateItems();
break;
}
}
});
desctable = new Table("button");
desctable.setVisible(() -> hoveredDescriptionRecipe != null || input.recipe != null);
@@ -326,23 +342,27 @@ public class BlocksFragment implements Fragment{
d.show();
}
public void updateItems(){
private void updateItems(){
itemtable.clear();
itemtable.left();
if(state.mode.infiniteResources){
return;
}
};
int index = 0;
int[] items = state.inventory.readItems();
for(int i = 0; i < state.inventory.getItems().length; i ++){
int amount = state.inventory.getItems()[i];
if(amount == 0) continue;
String formatted = amount > 99999999 ? "inf" : format(amount);
for(int i = 0; i < items.length; i ++){
int amount = items[i];
if(amount == 0){
itemset.remove(i);
continue;
}
itemset.add(i);
Image image = new Image(Item.getByID(i).region);
Label label = new Label(formatted);
Label label = new Label(() -> format(amount));
label.setFontScale(fontscale*1.5f);
itemtable.add(image).size(8*3);
itemtable.add(label).expandX().left();
@@ -351,7 +371,9 @@ public class BlocksFragment implements Fragment{
}
String format(int number){
if(number > 1000000) {
if(number > 99999999){
return "inf";
}else if(number > 1000000) {
return Strings.toFixed(number/1000000f, 1) + "[gray]mil";
}else if(number > 10000){
return number/1000 + "[gray]k";

View File

@@ -221,10 +221,6 @@ public class HudFragment implements Fragment{
l.setTouchable(!paused ? Touchable.enabled : Touchable.disabled);
});
}
public void updateItems(){
blockfrag.updateItems();
}
public void fadeRespawn(boolean in){
respawntable.addAction(Actions.color(in ? new Color(0, 0, 0, 0.3f) : Color.CLEAR, 0.3f));

View File

@@ -7,7 +7,6 @@ import io.anuke.mindustry.world.Tile;
import static io.anuke.mindustry.Vars.state;
public class CoreBlock extends StorageBlock {
protected int capacity = 1000;
public CoreBlock(String name) {
super(name);
@@ -16,7 +15,8 @@ public class CoreBlock extends StorageBlock {
destructible = true;
unbreakable = true;
size = 3;
hasInventory = false;
hasInventory = true;
itemCapacity = 2000;
}
public void onDestroyed(Tile tile){
@@ -30,8 +30,9 @@ public class CoreBlock extends StorageBlock {
@Override
public void handleItem(Item item, Tile tile, Tile source){
if(Net.server() || !Net.active()) state.inventory.addItem(item, 1);
if(Net.server() || !Net.active()) super.handleItem(item, tile, source);
}
/*
@Override
public boolean acceptItem(Item item, Tile tile, Tile source){
@@ -57,5 +58,5 @@ public class CoreBlock extends StorageBlock {
}
}
return false;
}
}*/
}