|
| 1 | +package extrasandredux.world.blocks.logic; |
| 2 | + |
| 3 | +import arc.graphics.g2d.*; |
| 4 | +import arc.math.geom.*; |
| 5 | +import arc.scene.ui.*; |
| 6 | +import arc.scene.ui.TextField.*; |
| 7 | +import arc.scene.ui.layout.*; |
| 8 | +import arc.util.*; |
| 9 | +import arc.util.io.*; |
| 10 | +import extrasandredux.util.*; |
| 11 | +import mindustry.game.*; |
| 12 | +import mindustry.gen.*; |
| 13 | +import mindustry.graphics.*; |
| 14 | +import mindustry.logic.*; |
| 15 | +import mindustry.type.*; |
| 16 | +import mindustry.ui.*; |
| 17 | +import mindustry.world.*; |
| 18 | +import mindustry.world.blocks.defense.turrets.Turret.*; |
| 19 | + |
| 20 | +import static mindustry.Vars.*; |
| 21 | + |
| 22 | +public class TurretController extends Block{ |
| 23 | + public TurretController(String name){ |
| 24 | + super(name); |
| 25 | + ESRUtls.applySandboxDefaults(this, Category.logic); |
| 26 | + |
| 27 | + update = true; |
| 28 | + solid = true; |
| 29 | + rotate = true; |
| 30 | + configurable = true; |
| 31 | + saveConfig = false; |
| 32 | + |
| 33 | + config(Integer.class, (TurretControllerBuild tile, Integer state) -> { |
| 34 | + tile.controlState = ControlState.values()[state]; |
| 35 | + }); |
| 36 | + config(Vec2.class, (TurretControllerBuild tile, Vec2 targetSetting) -> { |
| 37 | + tile.targetSetting.set(targetSetting); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public boolean canPlaceOn(Tile tile, Team team, int rotation){ |
| 43 | + //Assumes this block is 1x1. Too lazy to bother with anything larger because I won't make something larger. |
| 44 | + Tile front = tile.nearby(rotation); |
| 45 | + return front != null && front.build instanceof TurretBuild; |
| 46 | + } |
| 47 | + |
| 48 | + public class TurretControllerBuild extends Building{ |
| 49 | + public ControlState controlState = ControlState.off; |
| 50 | + /** x = angle, y = distance */ |
| 51 | + public Vec2 targetSetting = new Vec2(); |
| 52 | + |
| 53 | + @Override |
| 54 | + public void created(){ |
| 55 | + targetSetting.set(90, 80); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void updateTile(){ |
| 60 | + Building front = front(); |
| 61 | + if(front instanceof TurretBuild b && b.team == team){ |
| 62 | + if(controlState == ControlState.on){ |
| 63 | + b.control(LAccess.enabled, 1, 0, 0, 0); |
| 64 | + Tmp.v1.trns(targetSetting.x, targetSetting.y).add(b).scl(1f / tilesize); //Logic control uses tiles instead of world units. |
| 65 | + b.control(LAccess.shoot, Tmp.v1.x, Tmp.v1.y, 1, 0); |
| 66 | + }else if(controlState == ControlState.disable){ |
| 67 | + b.control(LAccess.enabled, 0, 0, 0, 0); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void buildConfiguration(Table table){ |
| 74 | + table.table(Styles.black5, t -> { |
| 75 | + //Mode |
| 76 | + t.table(mode -> { |
| 77 | + ButtonGroup<TextButton> group = new ButtonGroup<>(); |
| 78 | + |
| 79 | + mode.button("On", Styles.flatTogglet, () -> { |
| 80 | + controlState = ControlState.on; |
| 81 | + configure(1); |
| 82 | + }).group(group).wrapLabel(false).grow() |
| 83 | + .update(tb -> tb.setChecked(controlState == ControlState.on)) |
| 84 | + .get().margin(0f, 2f, 0f, 2f); |
| 85 | + mode.row(); |
| 86 | + mode.button("Off", Styles.flatTogglet, () -> { |
| 87 | + controlState = ControlState.off; |
| 88 | + configure(0); |
| 89 | + }).group(group).wrapLabel(false).grow() |
| 90 | + .update(tb -> tb.setChecked(controlState == ControlState.off)) |
| 91 | + .get().margin(0f, 2f, 0f, 2f); |
| 92 | + mode.row(); |
| 93 | + mode.button("Disable", Styles.flatTogglet, () -> { |
| 94 | + controlState = ControlState.disable; |
| 95 | + configure(2); |
| 96 | + }).group(group).wrapLabel(false).grow() |
| 97 | + .update(tb -> tb.setChecked(controlState == ControlState.disable)) |
| 98 | + .get().margin(0f, 2f, 0f, 2f); |
| 99 | + }).top().growY(); |
| 100 | + |
| 101 | + //Target |
| 102 | + t.table(tar -> { //TODO target using a click. Command mode probably. |
| 103 | + tar.add("Angle: "); |
| 104 | + tar.field("" + targetSetting.x, TextFieldFilter.floatsOnly, s -> { |
| 105 | + targetSetting.x = Strings.parseFloat(s); |
| 106 | + configure(targetSetting); |
| 107 | + }); |
| 108 | + tar.row(); |
| 109 | + tar.add("Distance: "); |
| 110 | + tar.field("" + targetSetting.y, TextFieldFilter.floatsOnly, s -> { |
| 111 | + targetSetting.y = Strings.parseFloat(s); |
| 112 | + configure(targetSetting); |
| 113 | + }); |
| 114 | + }).top().growY(); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void drawSelect(){ |
| 120 | + Building front = front(); |
| 121 | + if(front instanceof TurretBuild b && b.team == team){ |
| 122 | + Lines.stroke(1, team.color); |
| 123 | + Tmp.v1.trns(targetSetting.x, targetSetting.y).add(b); |
| 124 | + Lines.line(b.x, b.y, Tmp.v1.x, Tmp.v1.y); |
| 125 | + Drawf.target(Tmp.v1.x, Tmp.v1.y, 4, team.color); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void write(Writes write){ |
| 131 | + write.i(controlState.ordinal()); |
| 132 | + write.f(targetSetting.x); |
| 133 | + write.f(targetSetting.y); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void read(Reads read, byte revision){ |
| 138 | + controlState = ControlState.all[read.i()]; |
| 139 | + float tX = read.f(); |
| 140 | + float tY = read.f(); |
| 141 | + targetSetting.set(tX, tY); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public enum ControlState{ |
| 146 | + off, |
| 147 | + on, |
| 148 | + disable; |
| 149 | + |
| 150 | + public static ControlState[] all = values(); |
| 151 | + |
| 152 | + public ControlState next(){ |
| 153 | + int next = ordinal() + 1; |
| 154 | + if(next >= all.length) next = 0; |
| 155 | + return all[next]; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments