mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-03-09 20:29:06 +07:00
Added basic command center logic
This commit is contained in:
parent
2a6ee6d65b
commit
ea6f88b7f6
@ -156,9 +156,7 @@ project(":core") {
|
||||
|
||||
def comp = System.properties["release"] == null || System.properties["release"] == "false"
|
||||
|
||||
if(!comp){
|
||||
println("Note: Compiling release build.")
|
||||
}
|
||||
if(!comp) println("Note: Compiling release build.")
|
||||
|
||||
if(new File(projectDir.parent, '../uCore').exists() && comp){
|
||||
compile project(":uCore")
|
||||
|
@ -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
|
||||
}
|
@ -12,6 +12,7 @@ import io.anuke.mindustry.entities.traits.CarriableTrait;
|
||||
import io.anuke.mindustry.entities.traits.CarryTrait;
|
||||
import io.anuke.mindustry.entities.traits.ShooterTrait;
|
||||
import io.anuke.mindustry.entities.units.BaseUnit;
|
||||
import io.anuke.mindustry.entities.units.UnitCommand;
|
||||
import io.anuke.mindustry.game.Team;
|
||||
import io.anuke.mindustry.net.Packets.AdminAction;
|
||||
import io.anuke.mindustry.net.Packets.KickReason;
|
||||
@ -185,6 +186,16 @@ public class TypeIO{
|
||||
return AdminAction.values()[buffer.get()];
|
||||
}
|
||||
|
||||
@WriteClass(UnitCommand.class)
|
||||
public static void writeCommand(ByteBuffer buffer, UnitCommand reason){
|
||||
buffer.put((byte) reason.ordinal());
|
||||
}
|
||||
|
||||
@ReadClass(UnitCommand.class)
|
||||
public static UnitCommand readCommand(ByteBuffer buffer){
|
||||
return UnitCommand.values()[buffer.get()];
|
||||
}
|
||||
|
||||
@WriteClass(Effect.class)
|
||||
public static void writeEffect(ByteBuffer buffer, Effect effect){
|
||||
buffer.putShort((short) effect.id);
|
||||
|
@ -1,10 +1,25 @@
|
||||
package io.anuke.mindustry.world.blocks.units;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import io.anuke.annotations.Annotations.Loc;
|
||||
import io.anuke.annotations.Annotations.Remote;
|
||||
import io.anuke.mindustry.entities.Player;
|
||||
import io.anuke.mindustry.entities.TileEntity;
|
||||
import io.anuke.mindustry.entities.units.BaseUnit;
|
||||
import io.anuke.mindustry.entities.units.UnitCommand;
|
||||
import io.anuke.mindustry.net.In;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.meta.BlockFlag;
|
||||
import io.anuke.ucore.graphics.Draw;
|
||||
import io.anuke.ucore.scene.ui.layout.Table;
|
||||
import io.anuke.ucore.util.EnumSet;
|
||||
|
||||
import static io.anuke.mindustry.Vars.unitGroups;
|
||||
import static io.anuke.mindustry.Vars.world;
|
||||
|
||||
public class CommandCenter extends Block{
|
||||
protected TextureRegion[] commandRegions = new TextureRegion[UnitCommand.values().length];
|
||||
|
||||
public CommandCenter(String name){
|
||||
super(name);
|
||||
@ -15,5 +30,48 @@ public class CommandCenter extends Block{
|
||||
configurable = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(){
|
||||
super.load();
|
||||
|
||||
for(UnitCommand cmd : UnitCommand.values()){
|
||||
commandRegions[cmd.ordinal()] = Draw.region("command-" + cmd.name());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Tile tile){
|
||||
CommandCenterEntity entity = tile.entity();
|
||||
super.draw(tile);
|
||||
|
||||
Draw.rect(commandRegions[entity.command.ordinal()], tile.drawx(), tile.drawy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buildTable(Tile tile, Table table){
|
||||
//TODO
|
||||
}
|
||||
|
||||
@Remote(called = Loc.server, forward = true, in = In.blocks, targets = Loc.both)
|
||||
public static void onCommandCenterSet(Player player, Tile tile, UnitCommand command){
|
||||
for(Tile center : world.indexer().getAllied(tile.getTeam(), BlockFlag.comandCenter)){
|
||||
if(center.block() instanceof CommandCenter){
|
||||
CommandCenterEntity entity = tile.entity();
|
||||
entity.command = command;
|
||||
}
|
||||
}
|
||||
|
||||
for(BaseUnit unit : unitGroups[player.getTeam().ordinal()].all()){
|
||||
unit.onCommand(command);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileEntity getEntity(){
|
||||
return new CommandCenterEntity();
|
||||
}
|
||||
|
||||
class CommandCenterEntity extends TileEntity{
|
||||
UnitCommand command = UnitCommand.idle;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user