Added repair point block

This commit is contained in:
Anuken
2018-04-30 17:14:35 -04:00
parent 2b96c820fd
commit 0be49002ba
9 changed files with 203 additions and 67 deletions

View File

@@ -114,6 +114,8 @@ public class Recipes {
new Recipe(liquid, LiquidBlocks.pump, stack(Items.steel, 10)),
new Recipe(liquid, LiquidBlocks.fluxpump, stack(Items.steel, 10), stack(Items.densealloy, 5)),
new Recipe(units, UnitBlocks.repairPoint, stack(Items.steel, 10)),
//new Recipe(units, UnitBlocks.droneFactory, stack(Items.steel, 10)),
//new Recipe(units, UnitBlocks.vtolFactory, stack(Items.steel, 10)),
//new Recipe(units, UnitBlocks.droneFactory, stack(Items.steel, 10)),

View File

@@ -4,6 +4,7 @@ import io.anuke.mindustry.content.Items;
import io.anuke.mindustry.content.UnitTypes;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.blocks.types.units.RepairPoint;
import io.anuke.mindustry.world.blocks.types.units.ResupplyPoint;
import io.anuke.mindustry.world.blocks.types.units.UnitFactory;
@@ -40,5 +41,9 @@ public class UnitBlocks {
resupplyPoint = new ResupplyPoint("resupplypoint"){{
size = 2;
itemCapacity = 30;
}},
repairPoint = new RepairPoint("repairpoint"){{
shadow = "repairpoint-shadow";
}};
}

View File

@@ -96,6 +96,29 @@ public class Units {
return result[0];
}
/**Returns the closest ally of this team. Filter by predicate.*/
public static Unit getClosest(Team team, float x, float y, float range, Predicate<Unit> predicate){
Unit[] result = {null};
float[] cdist = {0};
rect.setSize(range*2f).setCenter(x, y);
getNearby(team, rect, e -> {
if (!predicate.test(e))
return;
float dist = Vector2.dst(e.x, e.y, x, y);
if (dist < range) {
if (result[0] == null || dist < cdist[0]) {
result[0] = e;
cdist[0] = dist;
}
}
});
return result[0];
}
/**Iterates over all units in a rectangle.*/
public static void getNearby(Team team, Rectangle rect, Consumer<Unit> cons){

View File

@@ -62,8 +62,7 @@ public class TeamInfo {
}
/**Returns a set of all teams that are enemies of this team.
* For teams not active, an empty set is returned.
*/
* For teams not active, an empty set is returned.*/
public ObjectSet<Team> enemiesOf(Team team) {
boolean ally = allies.contains(team);
boolean enemy = enemies.contains(team);

View File

@@ -0,0 +1,93 @@
package io.anuke.mindustry.world.blocks.types.units;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Rectangle;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.entities.Units;
import io.anuke.mindustry.graphics.Layer;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.flags.BlockFlag;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.graphics.Shapes;
import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.EnumSet;
import io.anuke.ucore.util.Mathf;
public class RepairPoint extends Block{
private static Rectangle rect = new Rectangle();
protected int timerTarget = timers ++;
protected float repairRadius = 50f;
protected float repairSpeed = 0.3f;
protected float powerUsage = 0.2f;
public RepairPoint(String name) {
super(name);
update = true;
solid = true;
flags = EnumSet.of(BlockFlag.repair);
layer = Layer.laser;
hasItems = false;
hasPower = true;
powerCapacity = 20f;
}
@Override
public void drawLayer(Tile tile) {
RepairPointEntity entity = tile.entity();
if(entity.target != null){
float ang = entity.angleTo(entity.target);
float len = 2f;
Draw.color(Color.valueOf("e8ffd7"));
Draw.alpha(entity.strength);
Shapes.laser("laser", "laser-end",
tile.drawx() + Angles.trnsx(ang, len), tile.drawy() + Angles.trnsy(ang, len),
entity.target.x, entity.target.y);
Draw.color();
}
}
@Override
public void update(Tile tile) {
RepairPointEntity entity = tile.entity();
if(entity.target != null && (entity.target.isDead() || entity.target.distanceTo(tile) > repairRadius ||
entity.target.health >= entity.target.maxhealth)){
entity.target = null;
}else if(entity.target != null){
entity.target.health += repairSpeed * Timers.delta() * entity.strength;
entity.target.clampHealth();
}
float powerUse = Math.min(Timers.delta() * powerUsage, powerCapacity);
if(entity.target != null && entity.power.amount >= powerUse){
entity.power.amount -= powerUse;
entity.strength = Mathf.lerpDelta(entity.strength, 1f, 0.07f * Timers.delta());
}else{
entity.strength = Mathf.lerpDelta(entity.strength, 0f, 0.05f * Timers.delta());
}
if(entity.timer.get(timerTarget, 20)) {
rect.setSize(repairRadius * 2).setCenter(tile.drawx(), tile.drawy());
entity.target = Units.getClosest(tile.getTeam(), tile.drawx(), tile.drawy(), repairRadius,
unit -> unit.health < unit.maxhealth);
}
}
@Override
public TileEntity getEntity() {
return new RepairPointEntity();
}
public class RepairPointEntity extends TileEntity{
public Unit target;
public float strength;
}
}