Added BlockConsumeFragment
This commit is contained in:
@@ -228,7 +228,9 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
|
|||||||
x += velocity.x / getMass() * Timers.delta();
|
x += velocity.x / getMass() * Timers.delta();
|
||||||
y += velocity.y / getMass() * Timers.delta();
|
y += velocity.y / getMass() * Timers.delta();
|
||||||
|
|
||||||
elevation = Mathf.lerpDelta(elevation, tile.elevation, 0.04f);
|
if(tile != null){
|
||||||
|
elevation = Mathf.lerpDelta(elevation, tile.elevation, 0.04f);
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
boolean onLiquid = floor.isLiquid;
|
boolean onLiquid = floor.isLiquid;
|
||||||
|
|
||||||
|
|||||||
@@ -101,8 +101,7 @@ public abstract class InputHandler extends InputAdapter{
|
|||||||
boolean tileTapped(Tile tile){
|
boolean tileTapped(Tile tile){
|
||||||
tile = tile.target();
|
tile = tile.target();
|
||||||
|
|
||||||
boolean consumed = false;
|
boolean consumed = false, showedInventory = false, showedConsume = false;
|
||||||
boolean showedInventory = false;
|
|
||||||
|
|
||||||
//check if tapped block is configurable
|
//check if tapped block is configurable
|
||||||
if(tile.block().configurable && tile.getTeam() == player.getTeam()){
|
if(tile.block().configurable && tile.getTeam() == player.getTeam()){
|
||||||
@@ -129,16 +128,28 @@ public abstract class InputHandler extends InputAdapter{
|
|||||||
//consume tap event if necessary
|
//consume tap event if necessary
|
||||||
if(tile.getTeam() == player.getTeam() && tile.block().consumesTap){
|
if(tile.getTeam() == player.getTeam() && tile.block().consumesTap){
|
||||||
consumed = true;
|
consumed = true;
|
||||||
}else if(tile.getTeam() == player.getTeam() && tile.block().synthetic() && tile.block().hasItems && tile.entity.items.total() > 0 && !consumed){
|
}else if(tile.getTeam() == player.getTeam() && tile.block().synthetic() && !consumed) {
|
||||||
frag.inv.showFor(tile);
|
if(tile.block().hasItems && tile.entity.items.total() > 0) {
|
||||||
consumed = true;
|
frag.inv.showFor(tile);
|
||||||
showedInventory = true;
|
consumed = true;
|
||||||
|
showedInventory = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(tile.block().consumes.hasAny()){
|
||||||
|
frag.consume.show(tile);
|
||||||
|
consumed = true;
|
||||||
|
showedConsume = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!showedInventory){
|
if(!showedInventory){
|
||||||
frag.inv.hide();
|
frag.inv.hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!showedConsume){
|
||||||
|
frag.consume.hide();
|
||||||
|
}
|
||||||
|
|
||||||
return consumed;
|
return consumed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,7 +177,8 @@ public abstract class InputHandler extends InputAdapter{
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean canMine(Tile tile){
|
boolean canMine(Tile tile){
|
||||||
return tile.floor().drops != null && tile.floor().drops.item.hardness <= player.mech.drillPower
|
return !ui.hasMouse()
|
||||||
|
&& tile.floor().drops != null && tile.floor().drops.item.hardness <= player.mech.drillPower
|
||||||
&& !tile.floor().playerUnmineable
|
&& !tile.floor().playerUnmineable
|
||||||
&& player.inventory.canAcceptItem(tile.floor().drops.item)
|
&& player.inventory.canAcceptItem(tile.floor().drops.item)
|
||||||
&& Units.getClosestEnemy(player.getTeam(), tile.worldx(), tile.worldy(), 40f, e -> true) == null //don't being mining when an enemy is near
|
&& Units.getClosestEnemy(player.getTeam(), tile.worldx(), tile.worldy(), 40f, e -> true) == null //don't being mining when an enemy is near
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package io.anuke.mindustry.ui.fragments;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
import com.badlogic.gdx.utils.Align;
|
||||||
|
import com.badlogic.gdx.utils.ObjectSet;
|
||||||
|
import io.anuke.mindustry.core.GameState.State;
|
||||||
|
import io.anuke.mindustry.entities.TileEntity;
|
||||||
|
import io.anuke.mindustry.world.Block;
|
||||||
|
import io.anuke.mindustry.world.Tile;
|
||||||
|
import io.anuke.mindustry.world.consumers.Consume;
|
||||||
|
import io.anuke.ucore.core.Graphics;
|
||||||
|
import io.anuke.ucore.scene.Group;
|
||||||
|
import io.anuke.ucore.scene.ui.layout.Table;
|
||||||
|
|
||||||
|
import static io.anuke.mindustry.Vars.state;
|
||||||
|
import static io.anuke.mindustry.Vars.tilesize;
|
||||||
|
|
||||||
|
public class BlockConsumeFragment extends Fragment {
|
||||||
|
private Table table;
|
||||||
|
private boolean visible;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Group parent) {
|
||||||
|
table = new Table();
|
||||||
|
table.setVisible(() -> !state.is(State.menu) && visible);
|
||||||
|
table.setTransform(true);
|
||||||
|
parent.setTransform(true);
|
||||||
|
parent.addChild(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void show(Tile tile){
|
||||||
|
ObjectSet<Consume> consumers = new ObjectSet<>();
|
||||||
|
TileEntity entity = tile.entity;
|
||||||
|
Block block = tile.block();
|
||||||
|
|
||||||
|
//table.background("clear");
|
||||||
|
rebuild(block, entity);
|
||||||
|
visible = true;
|
||||||
|
|
||||||
|
table.update(() -> {
|
||||||
|
|
||||||
|
if(tile.entity == null){
|
||||||
|
hide();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean rebuild = false;
|
||||||
|
|
||||||
|
for(Consume c : block.consumes.array()){
|
||||||
|
boolean valid = c.isOptional() || c.valid(block, entity);
|
||||||
|
|
||||||
|
if(consumers.contains(c) == valid){
|
||||||
|
if(valid){
|
||||||
|
consumers.remove(c);
|
||||||
|
}else{
|
||||||
|
consumers.add(c);
|
||||||
|
}
|
||||||
|
rebuild = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(rebuild){
|
||||||
|
rebuild(block, entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
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.topRight);
|
||||||
|
});
|
||||||
|
|
||||||
|
table.act(Gdx.graphics.getDeltaTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void hide(){
|
||||||
|
table.clear();
|
||||||
|
table.update(() -> {});
|
||||||
|
visible = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void rebuild(Block block, TileEntity entity){
|
||||||
|
table.clearChildren();
|
||||||
|
|
||||||
|
for(Consume c : block.consumes.array()){
|
||||||
|
if(!c.isOptional() && !c.valid(block, entity)){
|
||||||
|
c.build(table);
|
||||||
|
table.row();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import io.anuke.ucore.scene.Group;
|
|||||||
public class OverlayFragment extends Fragment{
|
public class OverlayFragment extends Fragment{
|
||||||
public final BlockInventoryFragment inv;
|
public final BlockInventoryFragment inv;
|
||||||
public final BlockConfigFragment config;
|
public final BlockConfigFragment config;
|
||||||
|
public final BlockConsumeFragment consume;
|
||||||
|
|
||||||
private Group group = new Group();
|
private Group group = new Group();
|
||||||
private InputHandler input;
|
private InputHandler input;
|
||||||
@@ -16,6 +17,7 @@ public class OverlayFragment extends Fragment{
|
|||||||
|
|
||||||
inv = new BlockInventoryFragment(input);
|
inv = new BlockInventoryFragment(input);
|
||||||
config = new BlockConfigFragment(input);
|
config = new BlockConfigFragment(input);
|
||||||
|
consume = new BlockConsumeFragment();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -25,6 +27,7 @@ public class OverlayFragment extends Fragment{
|
|||||||
|
|
||||||
inv.build(group);
|
inv.build(group);
|
||||||
config.build(group);
|
config.build(group);
|
||||||
|
consume.build(group);
|
||||||
|
|
||||||
input.buildUI(group);
|
input.buildUI(group);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package io.anuke.mindustry.world.consumers;
|
package io.anuke.mindustry.world.consumers;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.graphics.Color;
|
||||||
|
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||||
import io.anuke.mindustry.entities.TileEntity;
|
import io.anuke.mindustry.entities.TileEntity;
|
||||||
import io.anuke.mindustry.world.Block;
|
import io.anuke.mindustry.world.Block;
|
||||||
import io.anuke.mindustry.world.meta.BlockStats;
|
import io.anuke.mindustry.world.meta.BlockStats;
|
||||||
|
import io.anuke.ucore.scene.ui.Tooltip;
|
||||||
|
import io.anuke.ucore.scene.ui.layout.Table;
|
||||||
|
|
||||||
public abstract class Consume {
|
public abstract class Consume {
|
||||||
private boolean optional;
|
private boolean optional;
|
||||||
@@ -26,8 +30,22 @@ public abstract class Consume {
|
|||||||
return update;
|
return update;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(TileEntity entity){
|
public void build(Table table){
|
||||||
|
Table t = new Table("clear");
|
||||||
|
t.margin(4);
|
||||||
|
buildTooltip(t);
|
||||||
|
|
||||||
|
table.table("clear", out -> {
|
||||||
|
out.addImage(getIcon()).size(10*4).color(Color.RED);
|
||||||
|
}).size(10*4).get().addListener(new Tooltip<>(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void buildTooltip(Table table){
|
||||||
|
table.add("no " + ClassReflection.getSimpleName(getClass()).replace("Consume", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIcon(){
|
||||||
|
return "icon-power";
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void update(Block block, TileEntity entity);
|
public abstract void update(Block block, TileEntity entity);
|
||||||
|
|||||||
@@ -8,10 +8,12 @@ import io.anuke.mindustry.type.ItemStack;
|
|||||||
import io.anuke.mindustry.type.Liquid;
|
import io.anuke.mindustry.type.Liquid;
|
||||||
import io.anuke.mindustry.world.Block;
|
import io.anuke.mindustry.world.Block;
|
||||||
import io.anuke.ucore.function.Consumer;
|
import io.anuke.ucore.function.Consumer;
|
||||||
|
import io.anuke.ucore.util.ThreadArray;
|
||||||
|
|
||||||
public class Consumers {
|
public class Consumers {
|
||||||
private ObjectMap<Class<? extends Consume>, Consume> map = new ObjectMap<>();
|
private ObjectMap<Class<? extends Consume>, Consume> map = new ObjectMap<>();
|
||||||
private ObjectSet<Class<? extends Consume>> required = new ObjectSet<>();
|
private ObjectSet<Class<? extends Consume>> required = new ObjectSet<>();
|
||||||
|
private ThreadArray<Consume> results = new ThreadArray<>();
|
||||||
|
|
||||||
public void require(Class<? extends Consume> type){
|
public void require(Class<? extends Consume> type){
|
||||||
required.add(type);
|
required.add(type);
|
||||||
@@ -23,6 +25,10 @@ public class Consumers {
|
|||||||
throw new RuntimeException("Missing required consumer of type \"" + ClassReflection.getSimpleName(c) + "\" in block \"" + block.name + "\"!");
|
throw new RuntimeException("Missing required consumer of type \"" + ClassReflection.getSimpleName(c) + "\" in block \"" + block.name + "\"!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(Consume cons : map.values()){
|
||||||
|
results.add(cons);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConsumePower power(float amount){
|
public ConsumePower power(float amount){
|
||||||
@@ -93,6 +99,10 @@ public class Consumers {
|
|||||||
return map.values();
|
return map.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ThreadArray<Consume> array() {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean hasAny(){
|
public boolean hasAny(){
|
||||||
return map.size > 0;
|
return map.size > 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user