mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-07-10 07:47:25 +07:00
More optimization / Flying avoidance
This commit is contained in:
@ -6,6 +6,7 @@ import io.anuke.arc.function.Predicate;
|
||||
import io.anuke.arc.graphics.Camera;
|
||||
import io.anuke.arc.math.geom.Rectangle;
|
||||
import io.anuke.mindustry.entities.traits.DrawTrait;
|
||||
import io.anuke.mindustry.entities.traits.Entity;
|
||||
|
||||
public class EntityDraw{
|
||||
private static final Rectangle viewport = new Rectangle();
|
||||
@ -42,15 +43,12 @@ public class EntityDraw{
|
||||
viewport.set(cam.position.x - cam.width / 2, cam.position.y - cam.height / 2, cam.width, cam.height);
|
||||
}
|
||||
|
||||
group.forEach(e -> {
|
||||
if(!(e instanceof DrawTrait)) return;
|
||||
T t = (T) e;
|
||||
|
||||
if(!toDraw.test(t) || !e.isAdded()) return;
|
||||
for(Entity e : group.all()){
|
||||
if(!(e instanceof DrawTrait) || !toDraw.test((T)e) || !e.isAdded()) continue;
|
||||
|
||||
if(!clip || rect.setSize(((DrawTrait) e).drawSize()).setCenter(e.getX(), e.getY()).overlaps(viewport)){
|
||||
cons.accept(t);
|
||||
cons.accept((T)e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,6 +50,7 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
|
||||
private static final Vector2 moveVector = new Vector2();
|
||||
|
||||
private int lastWeightTile = Pos.invalid, lastWeightDelta;
|
||||
private boolean wasFlying = false;
|
||||
|
||||
public float rotation;
|
||||
|
||||
@ -228,13 +229,19 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
|
||||
}
|
||||
|
||||
public void avoidOthers(float scaling){
|
||||
if(isFlying()) return;
|
||||
boolean flying = isFlying();
|
||||
|
||||
if(lastWeightTile != Pos.invalid){
|
||||
Tile tile = world.tile(lastWeightTile);
|
||||
|
||||
if(tile != null){
|
||||
tile.weight -= Math.min(lastWeightDelta, tile.weight);
|
||||
int dec = Math.min(lastWeightDelta, wasFlying ? tile.airWeight : tile.weight);
|
||||
if(!wasFlying){
|
||||
tile.weight -= dec;
|
||||
}else{
|
||||
tile.airWeight -= dec;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -245,25 +252,32 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
|
||||
for(int cy = -rad; cy <= rad; cy++){
|
||||
Tile tile = world.tileWorld(x + cx*tilesize, y + cy*tilesize);
|
||||
if(tile == null) continue;
|
||||
int weight = flying ? tile.airWeight : tile.weight;
|
||||
float scl = (rad - Mathf.dst(tile.worldx(), tile.worldy(), x, y)/(8f * 1.2f * Mathf.sqrt2)) * 0.1f;
|
||||
|
||||
moveVector.add(Mathf.sign(x - tile.worldx()) * scaling * tile.weight * scl, Mathf.sign(y - tile.worldy()) * scaling * tile.weight * scl);
|
||||
moveVector.add(Mathf.sign(x - tile.worldx()) * scaling * weight * scl, Mathf.sign(y - tile.worldy()) * scaling * weight * scl);
|
||||
}
|
||||
}
|
||||
|
||||
moveVector.limit(0.2f);
|
||||
moveVector.limit(flying ? 0.1f : 0.2f);
|
||||
|
||||
applyImpulse(moveVector.x, moveVector.y);
|
||||
|
||||
Tile tile = world.tileWorld(x, y);
|
||||
|
||||
if(tile != null){
|
||||
lastWeightDelta = Math.min((int)(mass()), 127 - tile.weight);
|
||||
int tw = flying ? tile.airWeight : tile.weight;
|
||||
lastWeightDelta = Math.min((int)(mass()), 127 - tw);
|
||||
lastWeightTile = tile.pos();
|
||||
tile.weight += lastWeightDelta;
|
||||
if(!flying){
|
||||
tile.weight += lastWeightDelta;
|
||||
}else{
|
||||
tile.airWeight += lastWeightDelta;
|
||||
}
|
||||
}else{
|
||||
lastWeightTile = Pos.invalid;
|
||||
}
|
||||
wasFlying = flying;
|
||||
}
|
||||
|
||||
public TileEntity getClosestCore(){
|
||||
|
@ -32,7 +32,7 @@ public class Tile implements Position, TargetTrait{
|
||||
/** Tile traversal cost. */
|
||||
public byte cost = 1;
|
||||
/** Weight of [ground] units on this tile.*/
|
||||
public byte weight = 0;
|
||||
public byte weight, airWeight = 0;
|
||||
/** Tile entity, usually null. */
|
||||
public TileEntity entity;
|
||||
public short x, y;
|
||||
|
Reference in New Issue
Block a user