Added unit squads, new spawning, WaveSpawner class

This commit is contained in:
Anuken
2018-06-19 23:46:46 -04:00
parent e79494b5cb
commit 33a278ccb4
16 changed files with 261 additions and 82 deletions
@@ -414,6 +414,8 @@ public class Player extends Unit implements BuilderTrait, CarryTrait {
updateMech();
}
avoidOthers(8f);
float wobblyness = 0.6f;
trail.update(x + Angles.trnsx(rotation + 180f, 6f) + Mathf.range(wobblyness),
@@ -489,7 +491,7 @@ public class Player extends Unit implements BuilderTrait, CarryTrait {
velocity.add(movement);
updateVelocityStatus(mech.drag, mech.maxSpeed);
updateVelocityStatus(mech.drag, debug ? speed : mech.maxSpeed);
if(!movement.isZero()){
walktime += Timers.delta() * velocity.len()*(1f/0.5f)/speed * getFloorOn().speedMultiplier;
@@ -31,6 +31,7 @@ import static io.anuke.mindustry.Vars.world;
public class TileEntity extends BaseEntity implements TargetTrait {
public static final float timeToSleep = 60f*4; //4 seconds to fall asleep
/**This value is only used for debugging.*/
public static int sleepingEntities = 0;
public Tile tile;
+15 -1
View File
@@ -13,12 +13,14 @@ import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.Floor;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.EntityPhysics;
import io.anuke.ucore.entities.impl.DestructibleEntity;
import io.anuke.ucore.entities.trait.DamageTrait;
import io.anuke.ucore.entities.trait.DrawTrait;
import io.anuke.ucore.entities.trait.SolidTrait;
import io.anuke.ucore.util.Geometry;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Translator;
import java.io.DataInput;
import java.io.DataOutput;
@@ -35,6 +37,8 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
/**Maximum absolute value of a velocity vector component.*/
public static final float maxAbsVelocity = 127f/velocityPercision;
private static final Vector2 moveVector = new Vector2();
public UnitInventory inventory = new UnitInventory(this);
public float rotation;
@@ -43,7 +47,7 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
protected Team team = Team.blue;
protected CarryTrait carrier;
protected Vector2 velocity = new Vector2(0f, 0.0001f);
protected Vector2 velocity = new Translator(0f, 0.0001f);
protected float hitTime;
protected float drownTime;
@@ -172,6 +176,16 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
return tile == null ? (Floor) Blocks.air : tile.floor();
}
public void avoidOthers(float avoidRange){
EntityPhysics.getNearby(getGroup(), x, y, avoidRange*2f, t -> {
if(t == this || (t instanceof Unit && ((Unit) t).isDead())) return;
float dst = distanceTo(t);
if(dst > avoidRange) return;
velocity.add(moveVector.set(x, y).sub(t.getX(), t.getY()).setLength(1f * (1f - (dst / avoidRange))));
});
}
/**Updates velocity and status effects.*/
public void updateVelocityStatus(float drag, float maxVelocity){
if(isCarried()){ //carried units do not take into account velocity normally
@@ -10,7 +10,7 @@ import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.entities.EntityGroup;
import io.anuke.ucore.entities.EntityPhysics;
import io.anuke.ucore.entities.impl.BaseEntity;
import io.anuke.ucore.entities.trait.Entity;
import io.anuke.ucore.function.Consumer;
import io.anuke.ucore.function.Predicate;
import io.anuke.ucore.util.Mathf;
@@ -99,9 +99,10 @@ public class Units {
return findTile(x, y, range, tile -> state.teams.areEnemies(team, tile.getTeam()) && pred.test(tile));
}
//TODO optimize, spatial caching of tiles
/**Returns the neareset tile entity in a range.*/
public static TileEntity findTile(float x, float y, float range, Predicate<Tile> pred){
BaseEntity closest = null;
Entity closest = null;
float dst = 0;
int rad = (int)(range/tilesize)+1;
@@ -1,5 +1,6 @@
package io.anuke.mindustry.entities.units;
import com.badlogic.gdx.math.Vector2;
import io.anuke.annotations.Annotations.Loc;
import io.anuke.annotations.Annotations.Remote;
import io.anuke.mindustry.content.fx.ExplosionFx;
@@ -19,10 +20,7 @@ import io.anuke.mindustry.world.meta.BlockFlag;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.EntityGroup;
import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Geometry;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Timer;
import io.anuke.ucore.util.*;
import java.io.DataInput;
import java.io.DataOutput;
@@ -32,6 +30,7 @@ import static io.anuke.mindustry.Vars.*;
public abstract class BaseUnit extends Unit{
private static int timerIndex = 0;
private static Vector2 moveVector = new Translator();
protected static final int timerTarget = timerIndex++;
protected static final int timerReload = timerIndex++;
@@ -39,9 +38,11 @@ public abstract class BaseUnit extends Unit{
protected UnitType type;
protected Timer timer = new Timer(5);
protected StateMachine state = new StateMachine();
protected boolean isWave;
protected TargetTrait target;
protected boolean isWave;
protected Squad squad;
public BaseUnit(UnitType type, Team team){
this.type = type;
this.team = team;
@@ -55,6 +56,11 @@ public abstract class BaseUnit extends Unit{
isWave = true;
}
public void setSquad(Squad squad) {
this.squad = squad;
squad.units ++;
}
public void rotate(float angle){
rotation = Mathf.slerpDelta(rotation, angle, type.rotatespeed);
}
@@ -186,6 +192,12 @@ public abstract class BaseUnit extends Unit{
return;
}
avoidOthers(8f);
if(squad != null){
squad.update();
}
updateTargeting();
state.update();
@@ -23,7 +23,7 @@ public abstract class FlyingUnit extends BaseUnit implements CarryTrait{
protected static float maxAim = 30f;
protected static float wobblyness = 0.6f;
protected Trail trail = new Trail(16);
protected Trail trail = new Trail(8);
protected CarriableTrait carrying;
public FlyingUnit(UnitType type, Team team) {
@@ -79,6 +79,11 @@ public abstract class FlyingUnit extends BaseUnit implements CarryTrait{
Geometry.findClosest(x, y, world.indexer().getAllied(team, BlockFlag.repair)) != null){
setState(retreat);
}
if(squad != null){
squad.direction.add(velocity.x / squad.units, velocity.y / squad.units);
velocity.setAngle(Mathf.slerpDelta(velocity.angle(), squad.direction.angle(), 0.3f));
}
}
@Override
@@ -0,0 +1,22 @@
package io.anuke.mindustry.entities.units;
import com.badlogic.gdx.math.Vector2;
import io.anuke.ucore.util.Translator;
import static io.anuke.mindustry.Vars.threads;
/**Used to group entities together, for formations and such.
* Usually, squads are used by units spawned in the same wave.*/
public class Squad {
public Vector2 direction = new Translator();
public int units;
private long lastUpdated;
protected void update(){
if(threads.getFrameID() != lastUpdated){
direction.setZero();
lastUpdated = threads.getFrameID();
}
}
}