Files
Informatis/src/UnitInfo/ui/draws/LinkDraw.java
2022-04-17 17:35:55 +09:00

184 lines
7.9 KiB
Java

package UnitInfo.ui.draws;
import arc.graphics.Color;
import arc.graphics.g2d.Draw;
import arc.graphics.g2d.Lines;
import arc.math.Angles;
import arc.math.Mathf;
import arc.scene.Element;
import arc.scene.style.TextureRegionDrawable;
import arc.scene.ui.CheckBox;
import arc.scene.ui.layout.Table;
import arc.struct.IntSeq;
import arc.struct.Seq;
import arc.util.Time;
import arc.util.Tmp;
import mindustry.core.Renderer;
import mindustry.gen.Building;
import mindustry.gen.Groups;
import mindustry.graphics.Drawf;
import mindustry.graphics.Layer;
import mindustry.graphics.Pal;
import mindustry.ui.Styles;
import mindustry.world.blocks.distribution.MassDriver;
import mindustry.world.blocks.payloads.PayloadMassDriver;
import static arc.Core.atlas;
import static mindustry.Vars.*;
import static UnitInfo.SVars.*;
public class LinkDraw extends OverDraw {
Seq<MassDriver.MassDriverBuild> linkedMasses = new Seq<>();
Seq<PayloadMassDriver.PayloadDriverBuild> linkedPayloadMasses = new Seq<>();
Seq<Building> linkedNodes = new Seq<>();
boolean node = false, mass = false;
LinkDraw(String name, TextureRegionDrawable icon) {
super(name, icon);
}
@Override
public void draw() {
if(!enabled) return;
Draw.z(Layer.max);
if(target instanceof Building b){
if(node) {
linkedNodes.clear();
drawNodeLink(b);
}
if(mass) {
if (target instanceof MassDriver.MassDriverBuild mass) {
linkedMasses.clear();
drawMassLink(mass);
} else if (target instanceof PayloadMassDriver.PayloadDriverBuild mass) {
linkedPayloadMasses.clear();
drawMassPayloadLink(mass);
}
}
}
}
@Override
public void displayStats(Table parent) {
super.displayStats(parent);
parent.background(Styles.squaret.up);
parent.check("enable power node", node&&enabled, b->node=b&&enabled).disabled(!enabled).row();
parent.check("enable mass driver", mass&&enabled, b->mass=b&&enabled).disabled(!enabled).row();
}
@Override
public <T> void onEnabled(T param) {
super.onEnabled(param);
if(param instanceof Table t) {
for (int i = 0; i < t.getChildren().size; i++) {
Element elem = t.getChildren().get(i);
if (elem instanceof CheckBox cb) cb.setDisabled(!enabled);
}
}
}
void drawMassPayloadLink(PayloadMassDriver.PayloadDriverBuild from){
float sin = Mathf.absin(Time.time, 6f, 1f);
//call every mass drivers that link to this driver
for(Building b : Groups.build) {
if (b != from && b instanceof PayloadMassDriver.PayloadDriverBuild fromMass && world.build(fromMass.link) == from && !linkedPayloadMasses.contains(fromMass)) {
linkedPayloadMasses.add(fromMass);
drawMassPayloadLink(fromMass);
}
}
//get and draw line between this mass driver and linked one
Building target = world.build(from.link);
if(target instanceof PayloadMassDriver.PayloadDriverBuild targetDriver) {
Tmp.v1.set(from.x + from.block.offset, from.y + from.block.offset).sub(targetDriver.x, targetDriver.y).limit(from.block.size * tilesize + sin + 0.5f);
float x2 = from.x - Tmp.v1.x, y2 = from.y - Tmp.v1.y, x1 = targetDriver.x + Tmp.v1.x, y1 = targetDriver.y + Tmp.v1.y;
int segs = (int) (targetDriver.dst(from.x, from.y) / tilesize);
Lines.stroke(4f, Pal.gray);
Lines.dashLine(x1, y1, x2, y2, segs);
Lines.stroke(2f, Pal.placing);
Lines.dashLine(x1, y1, x2, y2, segs);
Lines.stroke(1f, Pal.accent);
Drawf.circles(from.x, from.y, (from.tile.block().size / 2f + 1) * tilesize + sin - 2f, Pal.accent);
Drawf.arrow(from.x, from.y, targetDriver.x, targetDriver.y, from.block.size * tilesize + sin, 4f + sin);
for (Building shooter : from.waitingShooters) {
Drawf.circles(shooter.x, shooter.y, (from.tile.block().size / 2f + 1) * tilesize + sin - 2f);
Drawf.arrow(shooter.x, shooter.y, from.x, from.y, from.block.size * tilesize + sin, 4f + sin);
}
//call method again when target links to another mass driver which isn't stored in array
if(!linkedPayloadMasses.contains(targetDriver)) {
linkedPayloadMasses.add(targetDriver);
drawMassPayloadLink(targetDriver);
}
}
}
void drawMassLink(MassDriver.MassDriverBuild from){
float sin = Mathf.absin(Time.time, 6f, 1f);
//call every mass drivers that link to this driver
for(Building b : Groups.build) {
if (b != from && b instanceof MassDriver.MassDriverBuild fromMass && world.build(fromMass.link) == from && !linkedMasses.contains(fromMass)) {
linkedMasses.add(fromMass);
drawMassLink(fromMass);
}
}
//get and draw line between this mass driver and linked one
Building target = world.build(from.link);
if(target instanceof MassDriver.MassDriverBuild targetDriver) {
Tmp.v1.set(from.x + from.block.offset, from.y + from.block.offset).sub(targetDriver.x, targetDriver.y).limit(from.block.size * tilesize + sin + 0.5f);
float x2 = from.x - Tmp.v1.x, y2 = from.y - Tmp.v1.y, x1 = targetDriver.x + Tmp.v1.x, y1 = targetDriver.y + Tmp.v1.y;
int segs = (int) (targetDriver.dst(from.x, from.y) / tilesize);
Lines.stroke(4f, Pal.gray);
Lines.dashLine(x1, y1, x2, y2, segs);
Lines.stroke(2f, Pal.placing);
Lines.dashLine(x1, y1, x2, y2, segs);
Lines.stroke(1f, Pal.accent);
Drawf.circles(from.x, from.y, (from.tile.block().size / 2f + 1) * tilesize + sin - 2f, Pal.accent);
Drawf.arrow(from.x, from.y, targetDriver.x, targetDriver.y, from.block.size * tilesize + sin, 4f + sin);
for (Building shooter : from.waitingShooters) {
Drawf.circles(shooter.x, shooter.y, (from.tile.block().size / 2f + 1) * tilesize + sin - 2f);
Drawf.arrow(shooter.x, shooter.y, from.x, from.y, from.block.size * tilesize + sin, 4f + sin);
}
//call method again when target links to another mass driver which isn't stored in array
if(!linkedMasses.contains(targetDriver)) {
linkedMasses.add(targetDriver);
drawMassLink(targetDriver);
}
}
}
IntSeq getPowerLinkedBuilds(Building build) {
IntSeq seq = new IntSeq(build.power.links);
seq.addAll(build.proximity().mapInt(Building::pos));
return seq;
}
void drawNodeLink(Building node) {
if(node.power == null) return;
if(!linkedNodes.contains(node)) {
linkedNodes.add(node);
int[] builds = getPowerLinkedBuilds(node).items;
for(int i : builds) {
Building other = world.build(i);
if(other == null || other.power == null) return;
float angle1 = Angles.angle(node.x, node.y, other.x, other.y),
vx = Mathf.cosDeg(angle1), vy = Mathf.sinDeg(angle1),
len1 = node.block.size * tilesize / 2f - 1.5f, len2 = other.block.size * tilesize / 2f - 1.5f;
Draw.color(Color.white, Color.valueOf("98ff98"), (1f - node.power.graph.getSatisfaction()) * 0.86f + Mathf.absin(3f, 0.1f));
Draw.alpha(Renderer.laserOpacity);
Drawf.laser(node.team, atlas.find("unitinfo-Slaser"), atlas.find("unitinfo-Slaser-end"), node.x + vx * len1, node.y + vy * len1, other.x - vx * len2, other.y - vy * len2, 0.25f);
drawNodeLink(other);
}
}
}
}