|
| 1 | +package sw.world.blocks.units; |
| 2 | + |
| 3 | +import arc.Core; |
| 4 | +import arc.Events; |
| 5 | +import arc.graphics.g2d.Draw; |
| 6 | +import arc.graphics.g2d.TextureRegion; |
| 7 | +import arc.math.Mathf; |
| 8 | +import arc.struct.Seq; |
| 9 | +import arc.util.io.Reads; |
| 10 | +import arc.util.io.Writes; |
| 11 | +import mindustry.Vars; |
| 12 | +import mindustry.content.UnitTypes; |
| 13 | +import mindustry.game.EventType.UnitCreateEvent; |
| 14 | +import mindustry.gen.Building; |
| 15 | +import mindustry.gen.Unit; |
| 16 | +import mindustry.graphics.Drawf; |
| 17 | +import mindustry.graphics.Layer; |
| 18 | +import mindustry.type.ItemStack; |
| 19 | +import mindustry.ui.ItemImage; |
| 20 | +import mindustry.world.Block; |
| 21 | +import mindustry.world.blocks.units.UnitFactory.UnitPlan; |
| 22 | +import mindustry.world.meta.Stat; |
| 23 | + |
| 24 | +public class SingleUnitFactory extends Block { |
| 25 | + public TextureRegion topRegion; |
| 26 | + public UnitPlan unitPlan = new UnitPlan(UnitTypes.flare, 60f, ItemStack.empty); |
| 27 | + public int unitCapacity = 1; |
| 28 | + |
| 29 | + public SingleUnitFactory(String name) { |
| 30 | + super(name); |
| 31 | + destructible = update = sync = true; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void setStats() { |
| 36 | + super.setStats(); |
| 37 | + stats.add(Stat.input, t -> {for (ItemStack stack : unitPlan.requirements) t.add(new ItemImage(stack)).pad(3f);}); |
| 38 | + stats.add(Stat.output, t -> t.image(unitPlan.unit.fullIcon).size(32f).pad(5)); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void load() { |
| 43 | + super.load(); |
| 44 | + topRegion = Core.atlas.find(name + "-top"); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public TextureRegion[] icons() { |
| 49 | + return new TextureRegion[]{region, topRegion}; |
| 50 | + } |
| 51 | + |
| 52 | + public class SingleUnitFactoryBuild extends Building { |
| 53 | + public float progress, totalProgress, warmup; |
| 54 | + public boolean canCreate() { |
| 55 | + Seq<Building> otherFactories = team.data().getBuildings(block); |
| 56 | + return unitPlan.unit.useUnitCap ? team.data().countType(unitPlan.unit) < team.data().unitCap : team.data().countType(unitPlan.unit) < otherFactories.size * unitCapacity; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void updateTile() { |
| 61 | + if (efficiency > 0 && canCreate()) { |
| 62 | + progress += getProgressIncrease(unitPlan.time) * warmup * Vars.state.rules.unitBuildSpeedMultiplier; |
| 63 | + totalProgress += edelta() * Vars.state.rules.unitBuildSpeedMultiplier; |
| 64 | + warmup = Mathf.approachDelta(warmup, 1, 0.014f * Vars.state.rules.unitBuildSpeedMultiplier); |
| 65 | + |
| 66 | + if (progress >= 1f) { |
| 67 | + progress %= 1f; |
| 68 | + |
| 69 | + Unit unit = unitPlan.unit.spawn(team, this); |
| 70 | + Events.fire(new UnitCreateEvent(unit, this)); |
| 71 | + } |
| 72 | + } else { |
| 73 | + warmup = Mathf.approachDelta(warmup, 0, 0.014f); |
| 74 | + progress = Mathf.approachDelta(warmup, 0, 0.014f); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void draw() { |
| 80 | + super.draw(); |
| 81 | + Draw.draw(Layer.blockOver, () -> Drawf.construct(this, unitPlan.unit, 0, progress, warmup * Vars.state.rules.unitBuildSpeedMultiplier, totalProgress)); |
| 82 | + Draw.z(Layer.blockOver + 0.01f); |
| 83 | + Draw.rect(topRegion, x, y, 0); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void write(Writes write) { |
| 88 | + super.write(write); |
| 89 | + write.f(progress); |
| 90 | + write.f(totalProgress); |
| 91 | + write.f(warmup); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void read(Reads read, byte revision) { |
| 96 | + super.read(read, revision); |
| 97 | + progress = read.f(); |
| 98 | + totalProgress = read.f(); |
| 99 | + warmup = read.f(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments