Made core drone pick up dropped items

This commit is contained in:
Anuken
2018-06-24 20:18:56 -04:00
parent ca10ab0274
commit c7fb517e6c
5 changed files with 90 additions and 15 deletions

View File

@@ -127,7 +127,7 @@ public class UnitInventory implements Saveable{
} }
public boolean canAcceptItem(Item type, int amount){ public boolean canAcceptItem(Item type, int amount){
return !hasItem() || (item.item == type && item.amount + amount <= unit.getItemCapacity()); return (!hasItem() && amount <= unit.getItemCapacity()) || (item.item == type && item.amount + amount <= unit.getItemCapacity());
} }
public void clear(){ public void clear(){

View File

@@ -8,8 +8,11 @@ import io.anuke.annotations.Annotations.Loc;
import io.anuke.annotations.Annotations.Remote; import io.anuke.annotations.Annotations.Remote;
import io.anuke.mindustry.content.fx.UnitFx; import io.anuke.mindustry.content.fx.UnitFx;
import io.anuke.mindustry.entities.Player; import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.entities.traits.SaveTrait; import io.anuke.mindustry.entities.traits.SaveTrait;
import io.anuke.mindustry.entities.traits.SyncTrait; import io.anuke.mindustry.entities.traits.SyncTrait;
import io.anuke.mindustry.entities.traits.TargetTrait;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.gen.CallEntity; import io.anuke.mindustry.gen.CallEntity;
import io.anuke.mindustry.net.In; import io.anuke.mindustry.net.In;
import io.anuke.mindustry.net.Interpolator; import io.anuke.mindustry.net.Interpolator;
@@ -33,7 +36,7 @@ import java.io.IOException;
import static io.anuke.mindustry.Vars.*; import static io.anuke.mindustry.Vars.*;
public class ItemDrop extends SolidEntity implements SaveTrait, SyncTrait, DrawTrait, VelocityTrait, TimeTrait, Poolable { public class ItemDrop extends SolidEntity implements SaveTrait, SyncTrait, DrawTrait, VelocityTrait, TimeTrait, TargetTrait, Poolable {
public static int typeID = -1; public static int typeID = -1;
private static final float sinkLifetime = 80f; private static final float sinkLifetime = 80f;
@@ -77,6 +80,24 @@ public class ItemDrop extends SolidEntity implements SaveTrait, SyncTrait, DrawT
hitboxTile.setSize(5f); hitboxTile.setSize(5f);
} }
public Item getItem() {
return item;
}
public int getAmount(){
return amount;
}
@Override
public boolean isDead() {
return !isAdded();
}
@Override
public Team getTeam() {
return Team.none;
}
@Override @Override
public int getTypeID() { public int getTypeID() {
return typeID; return typeID;
@@ -109,7 +130,7 @@ public class ItemDrop extends SolidEntity implements SaveTrait, SyncTrait, DrawT
@Override @Override
public void collision(SolidTrait other, float x, float y) { public void collision(SolidTrait other, float x, float y) {
Player player = (Player)other; Unit player = (Unit)other;
if(player.inventory.canAcceptItem(item, 1)){ if(player.inventory.canAcceptItem(item, 1)){
int used = Math.min(amount, player.inventory.capacity() - player.inventory.getItem().amount); int used = Math.min(amount, player.inventory.capacity() - player.inventory.getItem().amount);
player.inventory.addItem(item, used); player.inventory.addItem(item, used);

View File

@@ -6,6 +6,7 @@ import com.badlogic.gdx.utils.Queue;
import io.anuke.mindustry.content.Items; import io.anuke.mindustry.content.Items;
import io.anuke.mindustry.entities.TileEntity; import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.Units; import io.anuke.mindustry.entities.Units;
import io.anuke.mindustry.entities.effect.ItemDrop;
import io.anuke.mindustry.entities.traits.BuilderTrait; import io.anuke.mindustry.entities.traits.BuilderTrait;
import io.anuke.mindustry.entities.units.BaseUnit; import io.anuke.mindustry.entities.units.BaseUnit;
import io.anuke.mindustry.entities.units.FlyingUnit; import io.anuke.mindustry.entities.units.FlyingUnit;
@@ -24,12 +25,15 @@ import io.anuke.mindustry.world.meta.BlockFlag;
import io.anuke.ucore.core.Events; import io.anuke.ucore.core.Events;
import io.anuke.ucore.core.Timers; import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.EntityGroup; import io.anuke.ucore.entities.EntityGroup;
import io.anuke.ucore.entities.EntityPhysics;
import io.anuke.ucore.graphics.Draw; import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.graphics.Shapes; import io.anuke.ucore.graphics.Shapes;
import io.anuke.ucore.util.*; import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Geometry;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.ThreadQueue;
import static io.anuke.mindustry.Vars.unitGroups; import static io.anuke.mindustry.Vars.*;
import static io.anuke.mindustry.Vars.world;
public class Drone extends FlyingUnit implements BuilderTrait { public class Drone extends FlyingUnit implements BuilderTrait {
public static int typeID = -1; public static int typeID = -1;
@@ -181,6 +185,23 @@ public class Drone extends FlyingUnit implements BuilderTrait {
targetItem = Mathf.findMin(toMine, (a, b) -> -Integer.compare(entity.items.getItem(a), entity.items.getItem(b))); targetItem = Mathf.findMin(toMine, (a, b) -> -Integer.compare(entity.items.getItem(a), entity.items.getItem(b)));
} }
protected boolean findItemDrop(){
TileEntity core = getClosestCore();
if(core == null) return false;
//find nearby dropped items to pick up if applicable
ItemDrop drop = EntityPhysics.getClosest(itemGroup, x, y, 60f,
item -> core.tile.block().acceptStack(item.getItem(), item.getAmount(), core.tile, Drone.this) == item.getAmount() &&
inventory.canAcceptItem(item.getItem(), 1));
if(drop != null){
setState(pickup);
target = drop;
return true;
}
return false;
}
public final UnitState public final UnitState
build = new UnitState(){ build = new UnitState(){
@@ -256,6 +277,10 @@ public class Drone extends FlyingUnit implements BuilderTrait {
} }
public void update() { public void update() {
TileEntity entity = getClosestCore();
if(entity == null) return;
if(targetItem == null) { if(targetItem == null) {
findItem(); findItem();
} }
@@ -270,6 +295,10 @@ public class Drone extends FlyingUnit implements BuilderTrait {
} }
retarget(() -> { retarget(() -> {
if(findItemDrop()){
return;
}
if(getMineTile() == null){ if(getMineTile() == null){
findItem(); findItem();
} }
@@ -279,7 +308,7 @@ public class Drone extends FlyingUnit implements BuilderTrait {
target = world.indexer().findClosestOre(x, y, targetItem); target = world.indexer().findClosestOre(x, y, targetItem);
}); });
if(target != null) { if(target instanceof Tile) {
moveTo(type.range/1.5f); moveTo(type.range/1.5f);
if (distanceTo(target) < type.range) { if (distanceTo(target) < type.range) {
@@ -293,6 +322,33 @@ public class Drone extends FlyingUnit implements BuilderTrait {
setMineTile(null); setMineTile(null);
} }
}, },
pickup = new UnitState() {
public void entered() {
target = null;
}
public void update() {
ItemDrop item = (ItemDrop)target;
if(inventory.isFull() || !inventory.canAcceptItem(item.getItem(), 1)){
setState(drop);
return;
}
if(distanceTo(item) < 4){
item.collision(Drone.this, x, y);
}
//item has been picked up
if(item.getAmount() == 0){
if(!findItemDrop()){
setState(drop);
}
}
moveTo(0f);
}
},
drop = new UnitState() { drop = new UnitState() {
public void entered() { public void entered() {
target = null; target = null;

View File

@@ -44,7 +44,7 @@ public class FogRenderer implements Disposable{
for (int y = 0; y < world.height(); y++) { for (int y = 0; y < world.height(); y++) {
Tile tile = world.tile(x, y); Tile tile = world.tile(x, y);
if(tile.getTeam() == players[0].getTeam() && tile.block().viewRange > 0){ if(tile.getTeam() == players[0].getTeam() && tile.block().synthetic() && tile.block().viewRange > 0){
changeQueue.add(tile); changeQueue.add(tile);
} }
} }
@@ -52,7 +52,7 @@ public class FogRenderer implements Disposable{
}); });
Events.on(TileChangeEvent.class, tile -> threads.runGraphics(() -> { Events.on(TileChangeEvent.class, tile -> threads.runGraphics(() -> {
if(tile.getTeam() == players[0].getTeam() && tile.block().viewRange > 0){ if(tile.getTeam() == players[0].getTeam() && tile.block().synthetic() && tile.block().viewRange > 0){
changeQueue.add(tile); changeQueue.add(tile);
} }
})); }));
@@ -106,7 +106,6 @@ public class FogRenderer implements Disposable{
buffer.end(); buffer.end();
region.setTexture(buffer.getColorBufferTexture()); region.setTexture(buffer.getColorBufferTexture());
//region.setRegion(0, 0, 1, 1);
region.setRegion(u, v2, u2, v); region.setRegion(u, v2, u2, v);
Core.batch.setProjectionMatrix(Core.camera.combined); Core.batch.setProjectionMatrix(Core.camera.combined);
@@ -114,7 +113,6 @@ public class FogRenderer implements Disposable{
renderer.pixelSurface.getBuffer().begin(); renderer.pixelSurface.getBuffer().begin();
Graphics.begin(); Graphics.begin();
// Core.batch.draw(buffer.getColorBufferTexture(), px + 50, py, 200, 200 * world.height()/(float)world.width());
Core.batch.draw(region, px, py, vw, vh); Core.batch.draw(region, px, py, vw, vh);
Graphics.end(); Graphics.end();

View File

@@ -121,7 +121,7 @@ public class Block extends BaseBlock implements UnlockableContent{
/**The color of this block when displayed on the minimap or map preview.*/ /**The color of this block when displayed on the minimap or map preview.*/
public Color minimapColor = Color.CLEAR; public Color minimapColor = Color.CLEAR;
/**View range of this block type. Use a value < 0 to disable.*/ /**View range of this block type. Use a value < 0 to disable.*/
public float viewRange = -1; public float viewRange = 10;
public Block(String name) { public Block(String name) {
this.name = name; this.name = name;
@@ -354,10 +354,10 @@ public class Block extends BaseBlock implements UnlockableContent{
if(icon == null) { if(icon == null) {
if (Draw.hasRegion(name + "-icon")) { if (Draw.hasRegion(name + "-icon")) {
icon = new TextureRegion[]{Draw.region(name + "-icon")}; icon = new TextureRegion[]{Draw.region(name + "-icon")};
} else if (Draw.hasRegion(name)){
icon = new TextureRegion[]{Draw.region(name)};
} else if (Draw.hasRegion(name + "1")) { } else if (Draw.hasRegion(name + "1")) {
icon = new TextureRegion[]{Draw.region(name + "1")}; icon = new TextureRegion[]{Draw.region(name + "1")};
} else if (Draw.hasRegion(name)){
icon = new TextureRegion[]{Draw.region(name)};
}else{ }else{
icon = new TextureRegion[]{}; icon = new TextureRegion[]{};
} }