Skip to content

Commit

Permalink
Better unit selection visuals / Bigger unit physics collision circles
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuken committed Nov 27, 2024
1 parent d65f226 commit ec0ba9f
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 38 deletions.
2 changes: 2 additions & 0 deletions core/src/mindustry/Vars.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public class Vars implements Loadable{
public static final float finalWorldBounds = 250;
/** default range for building */
public static final float buildingRange = 220f;
/** scaling for unit circle collider radius, based on hitbox size */
public static final float unitCollisionRadiusScale = 0.6f;
/** range for moving items */
public static final float itemTransferRange = 220f;
/** range for moving items for logic units */
Expand Down
4 changes: 2 additions & 2 deletions core/src/mindustry/ai/UnitGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class UnitGroup{
public int collisionLayer;
public volatile float[] positions, originalPositions;
public volatile boolean valid;

public void calculateFormation(Vec2 dest, int collisionLayer){
this.collisionLayer = collisionLayer;

Expand Down Expand Up @@ -72,7 +72,7 @@ public void calculateFormation(Vec2 dest, int collisionLayer){
positions[a * 2] = v1.x;
positions[a * 2 + 1] = v1.y;

float rad = units.get(a).hitSize/2f;
float rad = units.get(a).hitSize * Vars.unitCollisionRadiusScale;

maxDst = Math.max(maxDst, v1.dst(0f, 0f) + rad);
totalArea += Mathf.PI * rad * rad;
Expand Down
2 changes: 1 addition & 1 deletion core/src/mindustry/async/PhysicsProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void begin(){
body.x = entity.x;
body.y = entity.y;
body.mass = entity.mass();
body.radius = entity.hitSize / 2f;
body.radius = entity.hitSize * Vars.unitCollisionRadiusScale;

PhysicRef ref = new PhysicRef(entity, body);
refs.add(ref);
Expand Down
4 changes: 2 additions & 2 deletions core/src/mindustry/content/UnitTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static void load(){

reign = new UnitType("reign"){{
speed = 0.4f;
hitSize = 26f;
hitSize = 30f;
rotateSpeed = 1.65f;
health = 24000;
armor = 18f;
Expand Down Expand Up @@ -1012,7 +1012,7 @@ public static void load(){
accel = 0.08f;
drag = 0.016f;
flying = true;
hitSize = 10f;
hitSize = 11f;
targetAir = false;
engineOffset = 7.8f;
range = 140f;
Expand Down
2 changes: 2 additions & 0 deletions core/src/mindustry/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ public void draw(){
Draw.draw(Layer.effect + 0.02f, bloom::render);
}

control.input.drawCommanded();

Draw.draw(Layer.plans, overlays::drawBottom);

if(animateShields && Shaders.shield != null){
Expand Down
16 changes: 12 additions & 4 deletions core/src/mindustry/graphics/Drawf.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public static void shadow(TextureRegion region, float x, float y){
Draw.rect(region, x, y);
Draw.color();
}

public static void shadow(TextureRegion region, float x, float y, float width, float height, float rotation){
Draw.color(Pal.shadow);
Draw.rect(region, x, y, width, height, rotation);
Expand Down Expand Up @@ -361,6 +361,14 @@ public static void square(float x, float y, float radius, float rotation, Color
Draw.reset();
}

public static void poly(float x, float y, int sides, float radius, float rotation, Color color){
Lines.stroke(3f, Pal.gray);
Lines.poly(x, y, sides, radius + 1f, rotation);
Lines.stroke(1f, color);
Lines.poly(x, y, sides, radius + 1f, rotation);
Draw.reset();
}

public static void square(float x, float y, float radius, float rotation){
square(x, y, radius, rotation, Pal.accent);
}
Expand Down Expand Up @@ -436,7 +444,7 @@ public static void construct(Building t, UnlockableContent content, float rotati
public static void construct(float x, float y, TextureRegion region, float rotation, float progress, float alpha, float time){
construct(x, y, region, Pal.accent, rotation, progress, alpha, time);
}

public static void construct(float x, float y, TextureRegion region, Color color, float rotation, float progress, float alpha, float time){
Shaders.build.region = region;
Shaders.build.progress = progress;
Expand All @@ -458,7 +466,7 @@ public static void construct(Building t, TextureRegion region, float rotation, f
public static void construct(Building t, TextureRegion region, Color color, float rotation, float progress, float alpha, float time){
construct(t, region, color, rotation, progress, alpha, time, t.block.size * tilesize - 4f);
}

public static void construct(Building t, TextureRegion region, Color color, float rotation, float progress, float alpha, float time, float size){
Shaders.build.region = region;
Shaders.build.progress = progress;
Expand All @@ -477,7 +485,7 @@ public static void construct(Building t, TextureRegion region, Color color, floa

Draw.reset();
}

/** Draws a sprite that should be light-wise correct, when rotated. Provided sprite must be symmetrical in shape. */
public static void spinSprite(TextureRegion region, float x, float y, float r){
float a = Draw.getColorAlpha();
Expand Down
1 change: 1 addition & 0 deletions core/src/mindustry/graphics/OverlayRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public void drawTop(){
}

input.drawTop();
input.drawUnitSelection();

buildFade = Mathf.lerpDelta(buildFade, input.isPlacing() || input.isUsingSchematic() ? 1f : 0f, 0.06f);

Expand Down
3 changes: 0 additions & 3 deletions core/src/mindustry/input/DesktopInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ public void drawTop(){
}
}


drawCommanded();

Draw.reset();
}

Expand Down
79 changes: 56 additions & 23 deletions core/src/mindustry/input/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1070,23 +1070,36 @@ public void commandTap(float screenX, float screenY, boolean queue){
}

public void drawCommand(Unit sel){
Drawf.square(sel.x, sel.y, sel.hitSize / 1.4f + Mathf.absin(4f, 1f), selectedUnits.contains(sel) ? Pal.remove : Pal.accent);
Drawf.poly(sel.x, sel.y, 6, sel.hitSize / 1.1f + Mathf.absin(4f, 1f), 0f, selectedUnits.contains(sel) ? Pal.remove : Pal.accent);
}

public void drawCommanded(){
Draw.draw(Layer.plans, () -> {
drawCommanded(true);
});

Draw.draw(Layer.groundUnit - 1, () -> {
drawCommanded(false);
});
}

public void drawCommanded(boolean flying){
float lineLimit = 6.5f;

if(commandMode){
//happens sometimes
selectedUnits.removeAll(u -> !u.isCommandable());

//draw command overlay UI
for(Unit unit : selectedUnits){
if(unit.isFlying() != flying) continue;
CommandAI ai = unit.command();
Position lastPos = ai.attackTarget != null ? ai.attackTarget : ai.targetPos;

//draw target line
if(ai.targetPos != null && ai.currentCommand().drawTarget){
Position lineDest = ai.attackTarget != null ? ai.attackTarget : ai.targetPos;
Drawf.limitLine(unit, lineDest, unit.hitSize / 2f, 3.5f);
Drawf.limitLine(unit, lineDest, unit.hitSize / 1.1f + 1f, lineLimit);

if(ai.attackTarget == null){
Drawf.square(lineDest.getX(), lineDest.getY(), 3.5f);
Expand All @@ -1100,7 +1113,23 @@ public void drawCommanded(){
}
}

Drawf.square(unit.x, unit.y, unit.hitSize / 1.4f + 1f);
float rad = unit.hitSize / 1.1f + 1f;
int sides = 6;

Fill.lightInner(unit.x, unit.y, sides,
Math.max(0f, unit.hitSize / 1.1f + 1f - 4f),
rad,
0f,
Tmp.c3.set(Pal.accent).a(0f),
Tmp.c2.set(Pal.accent).a(0.7f)
);

Lines.stroke(1f);
Draw.color(Pal.accent);
Lines.poly(unit.x, unit.y, sides, rad + 0.5f);
//Draw.color(Pal.gray);
//Lines.poly(unit.x, unit.y, sides, rad + 1.5f);
Draw.reset();

if(ai.attackTarget != null && ai.currentCommand().drawTarget){
Drawf.target(ai.attackTarget.getX(), ai.attackTarget.getY(), 6f, Pal.remove);
Expand All @@ -1113,7 +1142,7 @@ public void drawCommanded(){
//draw command queue
if(ai.currentCommand().drawTarget && ai.commandQueue.size > 0){
for(var next : ai.commandQueue){
Drawf.limitLine(lastPos, next, 3.5f, 3.5f);
Drawf.limitLine(lastPos, next, lineLimit, lineLimit);
lastPos = next;

if(next instanceof Vec2 vec){
Expand All @@ -1139,42 +1168,46 @@ public void drawCommanded(){
}
}

for(var commandBuild : commandBuildings){
if(commandBuild != null){
Drawf.square(commandBuild.x, commandBuild.y, commandBuild.hitSize() / 1.4f + 1f);
var cpos = commandBuild.getCommandPosition();
if(flying){
for(var commandBuild : commandBuildings){
if(commandBuild != null){
Drawf.square(commandBuild.x, commandBuild.y, commandBuild.hitSize() / 1.4f + 1f);
var cpos = commandBuild.getCommandPosition();

if(cpos != null){
Drawf.limitLine(commandBuild, cpos, commandBuild.hitSize() / 2f, 3.5f);
Drawf.square(cpos.x, cpos.y, 3.5f);
if(cpos != null){
Drawf.limitLine(commandBuild, cpos, commandBuild.hitSize() / 2f, lineLimit);
Drawf.square(cpos.x, cpos.y, 3.5f);
}
}
}
}

if(commandMode && !commandRect){
Unit sel = selectedCommandUnit(input.mouseWorldX(), input.mouseWorldY());

if(sel != null && !(!multiUnitSelect() && selectedUnits.size == 1 && selectedUnits.contains(sel))){
if(sel != null && sel.isFlying() == flying && !(!multiUnitSelect() && selectedUnits.size == 1 && selectedUnits.contains(sel))){
drawCommand(sel);
}
}

if(commandRect){
float x2 = input.mouseWorldX(), y2 = input.mouseWorldY();
var units = selectedCommandUnits(commandRectX, commandRectY, x2 - commandRectX, y2 - commandRectY);
for(var unit : units){
drawCommand(unit);
}

Draw.color(Pal.accent, 0.3f);
Fill.crect(commandRectX, commandRectY, x2 - commandRectX, y2 - commandRectY);
}
}

Draw.reset();

}

public void drawUnitSelection(){
if(commandRect && commandMode){
float x2 = input.mouseWorldX(), y2 = input.mouseWorldY();
var units = selectedCommandUnits(commandRectX, commandRectY, x2 - commandRectX, y2 - commandRectY);
for(var unit : units){
drawCommand(unit);
}

Draw.color(Pal.accent, 0.3f);
Fill.crect(commandRectX, commandRectY, x2 - commandRectX, y2 - commandRectY);
}
}

public void drawBottom(){

}
Expand Down
2 changes: 0 additions & 2 deletions core/src/mindustry/input/MobileInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,6 @@ public void drawTop(){
}else if(mode == rebuildSelect){
drawRebuildSelection(lineStartX, lineStartY, lastLineX, lastLineY);
}

drawCommanded();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ org.gradle.caching=true
org.gradle.internal.http.socketTimeout=100000
org.gradle.internal.http.connectionTimeout=100000
android.enableR8.fullMode=false
archash=ed5dd285b7
archash=ae657a7db3

0 comments on commit ec0ba9f

Please sign in to comment.