Implemented basic block inventories

This commit is contained in:
Anuken
2018-04-13 20:05:42 -04:00
parent 1ff6a7e423
commit 43c9bc51dc
18 changed files with 238 additions and 60 deletions

View File

@@ -159,7 +159,6 @@ public class Control extends Module{
hiscore = false;
ui.hudfrag.updateItems();
ui.hudfrag.updateWeapons();
ui.hudfrag.fadeRespawn(false);
});

View File

@@ -271,7 +271,6 @@ public class NetClient extends Module {
state.inventory.removeItems(UpgradeRecipes.get(weapon));
control.upgrades().addWeapon(weapon);
ui.hudfrag.updateWeapons();
Effects.sound("purchase");
});
}

View File

@@ -174,8 +174,7 @@ public class Renderer extends RendererModule{
camera.position.set(lastx - deltax, lasty - deltay, 0);
if(debug && !ui.chatfrag.chatOpen())
record(); //this only does something if GdxGifRecorder is on the class path, which it usually isn't
}
}
@@ -506,7 +505,7 @@ public class Renderer extends RendererModule{
Draw.reset();
}
if(Inputs.keyDown("block_info") && target.block().fullDescription != null){
if(Inputs.keyDown("block_info") && target.block().isAccessible()){
Draw.color(Colors.get("accent"));
Lines.crect(target.drawx(), target.drawy(), target.block().size * tilesize, target.block().size * tilesize);
Draw.color();

View File

@@ -30,7 +30,7 @@ import io.anuke.ucore.util.Mathf;
import java.util.Locale;
import static io.anuke.mindustry.Vars.control;
import static io.anuke.mindustry.Vars.*;
import static io.anuke.ucore.scene.actions.Actions.*;
public class UI extends SceneModule{
@@ -61,6 +61,8 @@ public class UI extends SceneModule{
public final LoadingFragment loadfrag = new LoadingFragment();
public final BlockConfigFragment configfrag = new BlockConfigFragment();
public final DebugFragment debugfrag = new DebugFragment();
public final BlockInventoryFragment blockinvfrag = new BlockInventoryFragment();
public final PlayerMenuFragment playermenufrag = new PlayerMenuFragment();
private Locale lastLocale;
@@ -132,6 +134,9 @@ public class UI extends SceneModule{
act();
if(debug && !ui.chatfrag.chatOpen())
renderer.record(); //this only does something if GdxGifRecorder is on the class path, which it usually isn't
if(control.showCursor()) {
Draw.color();
@@ -174,6 +179,8 @@ public class UI extends SceneModule{
chatfrag.build();
listfrag.build();
debugfrag.build();
blockinvfrag.build();
playermenufrag.build();
loadfrag.build();
build.end();

View File

@@ -1,11 +1,11 @@
package io.anuke.mindustry.editor;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.ObjectMap;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.io.MapIO;
import io.anuke.mindustry.io.MapTileData;
@@ -15,7 +15,6 @@ import io.anuke.mindustry.ui.dialogs.FloatingDialog;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.ColorMapper;
import io.anuke.mindustry.world.ColorMapper.BlockPair;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.core.Inputs;
@@ -29,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.Input;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Strings;
@@ -395,20 +395,20 @@ public class MapEditorDialog extends Dialog{
}
//ctrl keys (undo, redo, save)
if(Inputs.keyDown(Keys.CONTROL_LEFT)){
if(Inputs.keyTap(Keys.Z)){
if(Inputs.keyDown(Input.CONTROL_LEFT)){
if(Inputs.keyTap(Input.Z)){
view.undo();
}
if(Inputs.keyTap(Keys.Y)){
if(Inputs.keyTap(Input.Y)){
view.redo();
}
if(Inputs.keyTap(Keys.S)){
if(Inputs.keyTap(Input.S)){
saveDialog.save();
}
if(Inputs.keyTap(Keys.G)){
if(Inputs.keyTap(Input.G)){
view.setGrid(!view.isGrid());
}
}

View File

@@ -1,6 +1,5 @@
package io.anuke.mindustry.editor;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Colors;
import com.badlogic.gdx.graphics.g2d.Batch;
@@ -25,6 +24,7 @@ import io.anuke.ucore.scene.event.InputListener;
import io.anuke.ucore.scene.event.Touchable;
import io.anuke.ucore.scene.ui.TextField;
import io.anuke.ucore.scene.ui.layout.Unit;
import io.anuke.ucore.util.Input;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Tmp;
@@ -156,7 +156,7 @@ public class MapView extends Element implements GestureListener{
super.act(delta);
if(Core.scene.getKeyboardFocus() == null || !(Core.scene.getKeyboardFocus() instanceof TextField) &&
!Inputs.keyDown(Keys.CONTROL_LEFT)) {
!Inputs.keyDown(Input.CONTROL_LEFT)) {
float ax = Inputs.getAxis("move_x");
float ay = Inputs.getAxis("move_y");
offsetx -= ax * 15f / zoom;

View File

@@ -7,4 +7,10 @@ public class UnitInventory {
public final ItemStack item = new ItemStack(Item.getByID(0), 0);
public final LiquidStack liquid = new LiquidStack(Liquid.getByID(0), 0);
public float power = 0f;
public void addItem(Item item, int amount){
if(this.item.item != item) this.item.amount = 0;
this.item.item = item;
this.item.amount += amount;
}
}

View File

@@ -29,6 +29,7 @@ public class DefaultKeybinds {
"chat_history_prev", Input.UP,
"chat_history_next", Input.DOWN,
"chat_scroll", new Axis(Input.SCROLL),
"item_withdraw", Input.SHIFT_LEFT,
"console", Input.GRAVE,
"weapon_1", Input.NUM_1,
"weapon_2", Input.NUM_2,

View File

@@ -91,7 +91,6 @@ public class DesktopInput extends InputHandler{
if(Inputs.keyTap("weapon_" + i)){
player.weaponLeft = player.weaponRight = control.upgrades().getWeapons().get(i - 1);
if(Net.active()) NetEvents.handleWeaponSwitch();
ui.hudfrag.updateWeapons();
}
}
@@ -99,15 +98,18 @@ public class DesktopInput extends InputHandler{
Tile target = cursor == null ? null : cursor.target();
boolean showCursor = false;
if(recipe == null && target != null && !ui.hasMouse() && Inputs.keyDown("block_info")
&& target.block().fullDescription != null){
if(recipe == null && target != null && !ui.hasMouse() && Inputs.keyDown("block_info") && target.block().isAccessible()){
showCursor = true;
if(Inputs.keyTap("select")){
ui.hudfrag.blockfrag.showBlockInfo(target.block());
ui.blockinvfrag.showFor(target);
Cursors.restoreCursor();
}
}
if(!ui.hasMouse() && (target == null || !target.block().isAccessible()) && Inputs.keyTap("select")){
ui.blockinvfrag.hide();
}
if(target != null && target.block().isConfigurable(target)){
showCursor = true;
}

View File

@@ -90,7 +90,6 @@ public class Save16 extends SaveFileVersion {
control.upgrades().addWeapon(Upgrade.getByID(stream.readByte()));
}
ui.hudfrag.updateWeapons();
}else{
byte b = stream.readByte();
for(int i = 0; i < b; i ++) stream.readByte();

View File

@@ -137,7 +137,6 @@ public class NetworkIO {
}
player.weaponLeft = player.weaponRight = control.upgrades().getWeapons().peek();
ui.hudfrag.updateWeapons();
Entities.clear();
player.id = pid;

View File

@@ -0,0 +1,190 @@
package io.anuke.mindustry.ui.fragments;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Colors;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.IntSet;
import io.anuke.mindustry.content.Liquids;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.core.Inputs;
import io.anuke.ucore.scene.actions.Actions;
import io.anuke.ucore.scene.event.HandCursorListener;
import io.anuke.ucore.scene.event.Touchable;
import io.anuke.ucore.scene.ui.Image;
import io.anuke.ucore.scene.ui.layout.Stack;
import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.ucore.util.Strings;
import static io.anuke.mindustry.Vars.player;
import static io.anuke.mindustry.Vars.tilesize;
public class BlockInventoryFragment implements Fragment {
private Table table;
private Tile tile;
@Override
public void build() {
table = new Table();
}
public void showFor(Tile t){
this.tile = t.target();
if(tile == null || tile.entity == null || !tile.block().isAccessible()) return;
rebuild();
}
public void hide(){
table.clear();
table.remove();
table.setTouchable(Touchable.disabled);
table.update(() -> {});
tile = null;
}
private void rebuild(){
IntSet container = new IntSet();
table.clear();
if(table.getParent() == null) Core.scene.add(table);
table.background("clear");
table.setTouchable(Touchable.enabled);
table.update(() -> {
if(tile == null || tile.entity == null || !tile.block().isAccessible()){
hide();
}else {
updateTablePosition();
if(tile.block().hasInventory) {
int[] items = tile.entity.inventory.items;
for (int i = 0; i < items.length; i++) {
if ((items[i] == 0) == container.contains(i)) {
rebuild();
}
}
}
}
});
int cols = 3;
int row = 0;
//table.label(() -> "[accent]"+tile.entity.inventory.totalItems() +"/"+ tile.block().itemCapacity).colspan(cols);
//table.row();
table.margin(3f);
if(tile.block().hasPower){
Table t = new Table();
t.left().bottom();
t.label(() -> round(tile.entity.power.amount)).color(Color.DARK_GRAY);
t.row();
t.label(() -> round(tile.entity.power.amount)).padTop(-22);
Image image = new Image("icon-power");
image.setColor(Colors.get("power"));
Stack stack = new Stack();
stack.add(image);
stack.add(t);
table.add(stack).size(16 * 2).space(6f);
if (row++ % cols == cols - 1) table.row();
}
if(tile.block().hasLiquids){
Table t = new Table();
t.left().bottom();
t.label(() -> round(tile.entity.liquid.amount)).color(Color.DARK_GRAY);
t.row();
t.label(() -> round(tile.entity.liquid.amount)).padTop(-22);
Image image = new Image("icon-liquid");
image.setColor(tile.entity.liquid.liquid == Liquids.none ? Color.GRAY : tile.entity.liquid.liquid.color);
Stack stack = new Stack();
stack.add(image);
stack.add(t);
table.add(stack).size(16 * 2).space(6f);
if (row++ % cols == cols - 1) table.row();
}
if(tile.block().hasInventory) {
int[] items = tile.entity.inventory.items;
for (int i = 0; i < items.length; i++) {
final int f = i;
if (items[i] == 0) continue;
Item item = Item.getByID(i);
container.add(i);
Table t = new Table();
t.left().bottom();
t.label(() -> round(items[f])).color(Color.DARK_GRAY);
t.row();
t.label(() -> round(items[f])).padTop(-22);
Stack stack = new Stack();
stack.add(new Image(item.region));
stack.add(t);
table.add(stack).size(16 * 2).space(6f);
stack.addListener(new HandCursorListener());
stack.tapped(() -> {
if (items[f] > 0) {
int amount = Inputs.keyDown("item_withdraw") ? items[f] : 1;
items[f] -= amount;
Vector2 v = stack.localToStageCoordinates(new Vector2(stack.getWidth() / 2f, stack.getHeight() / 2f));
Vector2 tv = Graphics.screen(player.x, player.y);
float tx = tv.x, ty = tv.y;
float dur = 40f / 60f;
Image image = new Image(item.region);
image.setSize(32f, 32f);
image.setOrigin(Align.center);
image.setPosition(v.x, v.y, Align.center);
image.setTouchable(Touchable.disabled);
image.actions(
Actions.parallel(
Actions.moveToAligned(tx, ty, Align.center, dur, Interpolation.fade),
Actions.scaleTo(0.1f, 0.1f, dur, Interpolation.fade)
),
Actions.call(() -> player.inventory.addItem(item, amount)),
Actions.removeActor()
);
Core.scene.add(image);
}
});
if (row++ % cols == cols - 1) table.row();
}
}
if(row == 0){
table.add("[LIGHT_GRAY]<empty>");
}
updateTablePosition();
}
String round(float f){
f = (int)f;
if(f >= 1000){
return Strings.toFixed(f/1000, 1) + "k";
}else{
return (int)f+"";
}
}
private void updateTablePosition(){
Vector2 v = Graphics.screen(tile.drawx() + tile.block().size*tilesize/2f, tile.drawy() + tile.block().size*tilesize/2f);
table.pack();
table.setPosition(v.x, v.y, Align.topLeft);
}
}

View File

@@ -30,7 +30,7 @@ import io.anuke.ucore.util.Strings;
import static io.anuke.mindustry.Vars.*;
public class BlocksFragment implements Fragment{
private Table desctable, itemtable, blocks, weapons;
private Table desctable, itemtable, blocks;
private Stack stack = new Stack();
private boolean shown = true;
private Recipe hoveredDescriptionRecipe;
@@ -195,41 +195,10 @@ public class BlocksFragment implements Fragment{
end();
}}.right().bottom().uniformX();
row();
if(!android) {
weapons = new table("button").margin(0).fillX().end().get();
}
visible(() -> !state.is(State.menu) && shown);
}}.end().get();
}}.end();
updateWeapons();
}
public void updateWeapons(){
if(android) return;
weapons.clearChildren();
weapons.left();
ButtonGroup<ImageButton> group = new ButtonGroup<>();
for(int i = 0; i < control.upgrades().getWeapons().size; i ++){
Weapon weapon = control.upgrades().getWeapons().get(i);
weapons.addImageButton(weapon.name, "toggle", 8*3, () -> {
player.weaponLeft = player.weaponRight = weapon;
}).left().size(40f, 45f).padRight(-1).group(group);
}
int idx = control.upgrades().getWeapons().indexOf(player.weaponLeft, true);
if(idx != -1)
group.getButtons().get(idx).setChecked(true);
else if(group.getButtons().size > 0)
group.getButtons().get(0).setChecked(true);
}
public void toggle(boolean show, float t, Interpolation ip){

View File

@@ -225,10 +225,6 @@ public class HudFragment implements Fragment{
public void updateItems(){
blockfrag.updateItems();
}
public void updateWeapons(){
blockfrag.updateWeapons();
}
public void fadeRespawn(boolean in){
respawntable.addAction(Actions.color(in ? new Color(0, 0, 0, 0.3f) : Color.CLEAR, 0.3f));

View File

@@ -0,0 +1,9 @@
package io.anuke.mindustry.ui.fragments;
public class PlayerMenuFragment implements Fragment {
@Override
public void build() {
}
}

View File

@@ -192,6 +192,10 @@ public class Block extends BaseBlock {
}
public void update(Tile tile){}
public boolean isAccessible(){
return (hasInventory && itemCapacity > 0) || hasLiquids || hasPower;
}
public void onDestroyed(Tile tile){
float x = tile.worldx(), y = tile.worldy();

View File

@@ -114,7 +114,6 @@ public class WeaponFactory extends Block{
}else{
state.inventory.removeItems(requirements);
control.upgrades().addWeapon(weapon);
ui.hudfrag.updateWeapons();
run.listen();
Effects.sound("purchase");
}