Added basic command center logic
This commit is contained in:
@@ -58,9 +58,7 @@ public abstract class BaseUnit extends Unit implements ShooterTrait{
|
||||
protected Squad squad;
|
||||
protected int spawner;
|
||||
|
||||
/**
|
||||
* internal constructor used for deserialization, DO NOT USE
|
||||
*/
|
||||
/**internal constructor used for deserialization, DO NOT USE*/
|
||||
public BaseUnit(){
|
||||
}
|
||||
|
||||
@@ -86,9 +84,10 @@ public abstract class BaseUnit extends Unit implements ShooterTrait{
|
||||
threads.runDelay(unit::remove);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the type and team of this unit. Only call once!
|
||||
*/
|
||||
/**Called when a command is recieved from the command center.*/
|
||||
public abstract void onCommand(UnitCommand command);
|
||||
|
||||
/**Initialize the type and team of this unit. Only call once!*/
|
||||
public void init(UnitType type, Team team){
|
||||
if(this.type != null) throw new RuntimeException("This unit is already initialized!");
|
||||
|
||||
@@ -108,9 +107,7 @@ public abstract class BaseUnit extends Unit implements ShooterTrait{
|
||||
this.spawner = tile.packedPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this to a 'wave' unit, which means it has slightly different AI and will not run out of ammo.
|
||||
*/
|
||||
/**Sets this to a 'wave' unit, which means it has slightly different AI and will not run out of ammo.*/
|
||||
public void setWave(){
|
||||
isWave = true;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,9 @@ import static io.anuke.mindustry.Vars.world;
|
||||
public abstract class FlyingUnit extends BaseUnit implements CarryTrait{
|
||||
protected static Translator vec = new Translator();
|
||||
protected static float wobblyness = 0.6f;
|
||||
|
||||
protected Trail trail = new Trail(8);
|
||||
protected CarriableTrait carrying;
|
||||
protected final UnitState
|
||||
|
||||
resupply = new UnitState(){
|
||||
@@ -115,14 +118,20 @@ public abstract class FlyingUnit extends BaseUnit implements CarryTrait{
|
||||
}
|
||||
}
|
||||
};
|
||||
protected Trail trail = new Trail(8);
|
||||
protected CarriableTrait carrying;
|
||||
|
||||
//instantiation only
|
||||
public FlyingUnit(){
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(UnitCommand command){
|
||||
state.set(command == UnitCommand.retreat ? retreat :
|
||||
(command == UnitCommand.attack ? attack :
|
||||
(command == UnitCommand.idle ? resupply :
|
||||
(null))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarriableTrait getCarry(){
|
||||
return carrying;
|
||||
|
||||
@@ -31,9 +31,11 @@ public abstract class GroundUnit extends BaseUnit{
|
||||
|
||||
protected float walkTime;
|
||||
protected float baseRotation;
|
||||
protected Weapon weapon;
|
||||
|
||||
public final UnitState
|
||||
|
||||
resupply = new UnitState(){
|
||||
resupply = new UnitState(){
|
||||
public void entered(){
|
||||
target = null;
|
||||
}
|
||||
@@ -51,64 +53,71 @@ public abstract class GroundUnit extends BaseUnit{
|
||||
}
|
||||
}
|
||||
},
|
||||
attack = new UnitState(){
|
||||
public void entered(){
|
||||
target = null;
|
||||
}
|
||||
attack = new UnitState(){
|
||||
public void entered(){
|
||||
target = null;
|
||||
}
|
||||
|
||||
public void update(){
|
||||
TileEntity core = getClosestEnemyCore();
|
||||
float dst = core == null ? 0 : distanceTo(core);
|
||||
public void update(){
|
||||
TileEntity core = getClosestEnemyCore();
|
||||
float dst = core == null ? 0 : distanceTo(core);
|
||||
|
||||
if(core != null && inventory.hasAmmo() && dst < inventory.getAmmo().getRange() / 1.1f){
|
||||
target = core;
|
||||
}else{
|
||||
retarget(() -> targetClosest());
|
||||
}
|
||||
if(core != null && inventory.hasAmmo() && dst < inventory.getAmmo().getRange() / 1.1f){
|
||||
target = core;
|
||||
}else{
|
||||
retarget(() -> targetClosest());
|
||||
}
|
||||
|
||||
if(!inventory.hasAmmo()){
|
||||
state.set(resupply);
|
||||
}else if(target != null){
|
||||
if(core != null){
|
||||
if(dst > inventory.getAmmo().getRange() * 0.5f){
|
||||
moveToCore();
|
||||
}
|
||||
|
||||
}else{
|
||||
moveToCore();
|
||||
}
|
||||
|
||||
if(distanceTo(target) < inventory.getAmmo().getRange()){
|
||||
rotate(angleTo(target));
|
||||
|
||||
if(Mathf.angNear(angleTo(target), rotation, 13f)){
|
||||
AmmoType ammo = inventory.getAmmo();
|
||||
|
||||
Vector2 to = Predict.intercept(GroundUnit.this, target, ammo.bullet.speed);
|
||||
|
||||
getWeapon().update(GroundUnit.this, to.x, to.y);
|
||||
}
|
||||
}
|
||||
|
||||
}else{
|
||||
if(!inventory.hasAmmo()){
|
||||
state.set(resupply);
|
||||
}else if(target != null){
|
||||
if(core != null){
|
||||
if(dst > inventory.getAmmo().getRange() * 0.5f){
|
||||
moveToCore();
|
||||
}
|
||||
}
|
||||
},
|
||||
retreat = new UnitState(){
|
||||
public void entered(){
|
||||
target = null;
|
||||
|
||||
}else{
|
||||
moveToCore();
|
||||
}
|
||||
|
||||
public void update(){
|
||||
if(health >= health){
|
||||
state.set(attack);
|
||||
if(distanceTo(target) < inventory.getAmmo().getRange()){
|
||||
rotate(angleTo(target));
|
||||
|
||||
if(Mathf.angNear(angleTo(target), rotation, 13f)){
|
||||
AmmoType ammo = inventory.getAmmo();
|
||||
|
||||
Vector2 to = Predict.intercept(GroundUnit.this, target, ammo.bullet.speed);
|
||||
|
||||
getWeapon().update(GroundUnit.this, to.x, to.y);
|
||||
}
|
||||
|
||||
moveAwayFromCore();
|
||||
}
|
||||
};
|
||||
protected Weapon weapon;
|
||||
|
||||
}else{
|
||||
moveToCore();
|
||||
}
|
||||
}
|
||||
},
|
||||
retreat = new UnitState(){
|
||||
public void entered(){
|
||||
target = null;
|
||||
}
|
||||
|
||||
public void update(){
|
||||
if(health >= health){
|
||||
state.set(attack);
|
||||
}
|
||||
|
||||
moveAwayFromCore();
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onCommand(UnitCommand command){
|
||||
state.set(command == UnitCommand.retreat ? retreat :
|
||||
(command == UnitCommand.attack ? attack :
|
||||
(command == UnitCommand.idle ? resupply :
|
||||
(null))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(UnitType type, Team team){
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.anuke.mindustry.entities.units;
|
||||
|
||||
public enum UnitCommand{
|
||||
attack, retreat, idle
|
||||
}
|
||||
Reference in New Issue
Block a user