mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-08-02 16:09:38 +07:00
Implemented inventory capacity
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
#Autogenerated file. Do not modify.
|
||||
#Sat Apr 14 11:12:26 EDT 2018
|
||||
#Sat Apr 14 11:58:08 EDT 2018
|
||||
version=release
|
||||
androidBuildCode=918
|
||||
name=Mindustry
|
||||
|
@ -21,10 +21,7 @@ import io.anuke.ucore.core.Settings;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.entities.SolidEntity;
|
||||
import io.anuke.ucore.graphics.Draw;
|
||||
import io.anuke.ucore.util.Angles;
|
||||
import io.anuke.ucore.util.Mathf;
|
||||
import io.anuke.ucore.util.Timer;
|
||||
import io.anuke.ucore.util.Translator;
|
||||
import io.anuke.ucore.util.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@ -187,10 +184,16 @@ public class Player extends Unit{
|
||||
float backTrns = 4f, itemSize = 5f;
|
||||
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);
|
||||
//Lines.circle(x + Angles.trnsx(rotation + 180f, backTrns), y + Angles.trnsy(rotation + 180f, backTrns), 3f + Mathf.absin(Timers.time(), 3f, 0.8f));
|
||||
//Draw.tint(Color.WHITE);
|
||||
int stored = Mathf.clamp(stack.amount / 6, 1, 8);
|
||||
|
||||
for(int i = 0; i < stored; i ++) {
|
||||
float angT = i == 0 ? 0 : Mathf.randomSeedRange(i + 1, 60f);
|
||||
float lenT = i == 0 ? 0 : Mathf.randomSeedRange(i + 2, 1f) - 1f;
|
||||
Draw.rect(stack.item.region,
|
||||
x + Angles.trnsx(rotation + 180f + angT, backTrns + lenT),
|
||||
y + Angles.trnsy(rotation + 180f + angT, backTrns + lenT),
|
||||
itemSize, itemSize, rotation);
|
||||
}
|
||||
}
|
||||
|
||||
Draw.alpha(1f);
|
||||
|
@ -18,7 +18,7 @@ public abstract class Unit extends SyncEntity {
|
||||
public static final float hitDuration = 9f;
|
||||
|
||||
public StatusController status = new StatusController();
|
||||
public UnitInventory inventory = new UnitInventory();
|
||||
public UnitInventory inventory = new UnitInventory(100);
|
||||
public Team team = Team.blue;
|
||||
public Vector2 velocity = new Vector2();
|
||||
public float hitTime;
|
||||
|
@ -4,8 +4,28 @@ import io.anuke.mindustry.resource.*;
|
||||
|
||||
public class UnitInventory {
|
||||
private final AmmoEntry ammo = new AmmoEntry(AmmoType.getByID(0), 0);
|
||||
|
||||
private CarryItem item;
|
||||
private int capacity;
|
||||
|
||||
public UnitInventory(int capacity) {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public int capacity(){
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public int itemCapacityUsed(Item type){
|
||||
if(canAcceptItem(type)){
|
||||
return !hasItem() ? capacity : (capacity - getItem().amount);
|
||||
}else{
|
||||
return capacity;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canAcceptItem(Item type){
|
||||
return !hasItem() || (getItem().item == type && capacity - getItem().amount > 0);
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
item = null;
|
||||
|
@ -64,12 +64,13 @@ public abstract class InputHandler extends InputAdapter{
|
||||
|
||||
public void dropItem(Tile tile, ItemStack stack){
|
||||
if(tile.block().acceptStack(stack, tile, player)){
|
||||
tile.block().handleStack(stack, tile, player);
|
||||
player.inventory.clear();
|
||||
int accepted = tile.block().handleStack(stack, tile, player);
|
||||
stack.amount -= accepted;
|
||||
if(stack.amount == 0) player.inventory.clear();
|
||||
|
||||
float backTrns = 3f;
|
||||
|
||||
int sent = Mathf.clamp(stack.amount/3, 1, 8);
|
||||
int sent = Mathf.clamp(accepted/4, 1, 8);
|
||||
for(int i = 0; i < sent; i ++){
|
||||
Timers.run(i * 3, () -> {
|
||||
new ItemAnimationEffect(stack.item,
|
||||
|
@ -8,12 +8,15 @@ 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.core.GameState.State;
|
||||
import io.anuke.mindustry.resource.Item;
|
||||
import io.anuke.mindustry.ui.ItemImage;
|
||||
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.core.Timers;
|
||||
import io.anuke.ucore.function.BooleanProvider;
|
||||
import io.anuke.ucore.function.Callable;
|
||||
import io.anuke.ucore.graphics.Draw;
|
||||
import io.anuke.ucore.scene.actions.Actions;
|
||||
@ -22,10 +25,10 @@ import io.anuke.ucore.scene.event.Touchable;
|
||||
import io.anuke.ucore.scene.ui.Image;
|
||||
import io.anuke.ucore.scene.ui.layout.Table;
|
||||
import io.anuke.ucore.util.Angles;
|
||||
import io.anuke.ucore.util.Mathf;
|
||||
import io.anuke.ucore.util.Strings;
|
||||
|
||||
import static io.anuke.mindustry.Vars.player;
|
||||
import static io.anuke.mindustry.Vars.tilesize;
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class BlockInventoryFragment implements Fragment {
|
||||
private Table table;
|
||||
@ -34,6 +37,7 @@ public class BlockInventoryFragment implements Fragment {
|
||||
@Override
|
||||
public void build() {
|
||||
table = new Table();
|
||||
table.setVisible(() -> !state.is(State.menu));
|
||||
}
|
||||
|
||||
public void showFor(Tile t){
|
||||
@ -122,14 +126,30 @@ public class BlockInventoryFragment implements Fragment {
|
||||
t.row();
|
||||
t.label(() -> round(items[f])).padTop(-22);
|
||||
|
||||
BooleanProvider canPick = () -> player.inventory.canAcceptItem(item);
|
||||
|
||||
HandCursorListener l = new HandCursorListener();
|
||||
l.setEnabled(canPick);
|
||||
|
||||
ItemImage image = new ItemImage(item.region, () -> round(items[f]), Color.WHITE);
|
||||
image.addListener(new HandCursorListener());
|
||||
image.addListener(l);
|
||||
image.tapped(() -> {
|
||||
if(!canPick.get()) return;
|
||||
if (items[f] > 0) {
|
||||
int amount = Inputs.keyDown("item_withdraw") ? items[f] : 1;
|
||||
int amount = Math.min(Inputs.keyDown("item_withdraw") ? items[f] : 1, player.inventory.itemCapacityUsed(item));
|
||||
items[f] -= amount;
|
||||
|
||||
move(item.region, image, () -> player.inventory.addItem(item, amount), Color.WHITE);
|
||||
int sent = Mathf.clamp(amount/3, 1, 8);
|
||||
int per = Math.min(amount/sent, 5);
|
||||
int[] soFar = {amount};
|
||||
Vector2 v = image.localToStageCoordinates(new Vector2(image.getWidth() / 2f, image.getHeight() / 2f));
|
||||
for(int j = 0; j < sent; j ++){
|
||||
boolean all = j == sent-1;
|
||||
Timers.run(j*5, () -> move(item.region, v, () -> {
|
||||
player.inventory.addItem(item, all ? soFar[0] : per);
|
||||
soFar[0] -= per;
|
||||
}, Color.WHITE));
|
||||
}
|
||||
}
|
||||
});
|
||||
table.add(image);
|
||||
@ -145,8 +165,7 @@ public class BlockInventoryFragment implements Fragment {
|
||||
updateTablePosition();
|
||||
}
|
||||
|
||||
private void move(TextureRegion region, ItemImage image, Callable c, Color color){
|
||||
Vector2 v = image.localToStageCoordinates(new Vector2(image.getWidth() / 2f, image.getHeight() / 2f));
|
||||
private void move(TextureRegion region, Vector2 v, Callable c, Color color){
|
||||
Vector2 tv = Graphics.screen(player.x + Angles.trnsx(player.rotation + 180f, 5f), player.y + Angles.trnsy(player.rotation + 180f, 5f));
|
||||
float tx = tv.x, ty = tv.y;
|
||||
float dur = 40f / 60f;
|
||||
|
@ -28,7 +28,6 @@ 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;
|
||||
|
||||
@ -58,7 +57,6 @@ public class BlocksFragment implements Fragment{
|
||||
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;
|
||||
}
|
||||
|
@ -18,12 +18,14 @@ public abstract class BaseBlock {
|
||||
public float powerCapacity = 10f;
|
||||
|
||||
public boolean acceptStack(ItemStack item, Tile tile, Unit source){
|
||||
return acceptItem(item.item, tile, tile) && hasInventory && source.team == tile.getTeam()
|
||||
&& tile.entity.inventory.totalItems() + item.amount <= itemCapacity;
|
||||
return acceptItem(item.item, tile, tile) && hasInventory && source.team == tile.getTeam();
|
||||
}
|
||||
|
||||
public void handleStack(ItemStack item, Tile tile, Unit source){
|
||||
tile.entity.inventory.addItem(item.item, item.amount);
|
||||
public int handleStack(ItemStack item, Tile tile, Unit source){
|
||||
int total = tile.entity.inventory.totalItems();
|
||||
int canAccept = Math.min(itemCapacity - total, item.amount);
|
||||
tile.entity.inventory.addItem(item.item, canAccept);
|
||||
return canAccept;
|
||||
}
|
||||
|
||||
public void handleItem(Item item, Tile tile, Tile source){
|
||||
|
Reference in New Issue
Block a user