Added many new block types, changed land unit AI, bugfixes

This commit is contained in:
Anuken
2018-03-26 20:26:36 -04:00
parent f049706539
commit 707e57e72d
83 changed files with 1054 additions and 672 deletions

View File

@@ -5,10 +5,13 @@ import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ObjectSet;
import io.anuke.mindustry.entities.units.BaseUnit;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.entities.Entities;
import io.anuke.ucore.entities.Entity;
import io.anuke.ucore.entities.EntityGroup;
import io.anuke.ucore.function.Consumer;
import io.anuke.ucore.function.Predicate;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.*;
@@ -16,6 +19,43 @@ import static io.anuke.mindustry.Vars.*;
public class Units {
private static Rectangle rect = new Rectangle();
public static TileEntity findAllyTile(Team team, float x, float y, float range, Predicate<Tile> pred){
return findTile(x, y, range, tile -> !state.teams.areEnemies(team, tile.getTeam()) && pred.test(tile));
}
public static TileEntity findEnemyTile(Team team, float x, float y, float range, Predicate<Tile> pred){
return findTile(x, y, range, tile -> state.teams.areEnemies(team, tile.getTeam()) && pred.test(tile));
}
public static TileEntity findTile(float x, float y, float range, Predicate<Tile> pred){
Entity closest = null;
float dst = 0;
int rad = (int)(range/tilesize)+1;
int tilex = Mathf.scl2(x, tilesize);
int tiley = Mathf.scl2(y, tilesize);
for(int rx = -rad; rx <= rad; rx ++){
for(int ry = -rad; ry <= rad; ry ++){
Tile other = world.tile(rx+tilex, ry+tiley);
if(other != null) other = other.target();
if(other == null || other.entity == null || !pred.test(other)) continue;
TileEntity e = other.entity;
float ndst = Vector2.dst(x, y, e.x, e.y);
if(ndst < range && (closest == null || ndst < dst)){
dst = ndst;
closest = e;
}
}
}
return (TileEntity) closest;
}
/**Iterates over all units on all teams, including players.*/
public static void allUnits(Consumer<Unit> cons){
//check all unit groups first

View File

@@ -4,7 +4,6 @@ import io.anuke.mindustry.entities.Bullet;
import io.anuke.mindustry.entities.BulletType;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.game.Team;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.Entity;
import io.anuke.ucore.entities.SolidEntity;
import io.anuke.ucore.util.Mathf;
@@ -35,7 +34,6 @@ public class BaseUnit extends Unit {
@Override
public void move(float x, float y){
walkTime += Timers.delta();
baseRotation = Mathf.slerpDelta(baseRotation, Mathf.atan2(x, y), type.baseRotateSpeed);
super.move(x, y);
}

View File

@@ -53,6 +53,7 @@ public class FlyingUnitType extends UnitType {
vec.set(unit.target.x - unit.x, unit.target.y - unit.y);
float ang = vec.angle();
float len = vec.len();
float circleLength = 40f;
@@ -64,7 +65,7 @@ public class FlyingUnitType extends UnitType {
unit.velocity.add(vec); //TODO clamp it so it doesn't glitch out at low fps
if(unit.timer.get(timerReload, reload)){
if(unit.timer.get(timerReload, reload) && len < range){
shoot(unit, BulletType.shot, ang, 4f);
}
}

View File

@@ -1,25 +1,45 @@
package io.anuke.mindustry.entities.units;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ObjectSet;
import io.anuke.mindustry.entities.BulletType;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.entities.Units;
import io.anuke.mindustry.game.TeamInfo.TeamData;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Translator;
import static io.anuke.mindustry.Vars.state;
public abstract class GroundUnitType extends UnitType{
//only use for drawing!
protected Translator tr1 = new Translator();
//only use for updating!
protected Translator tr2 = new Translator();
protected float stopDistance = 30f;
public GroundUnitType(String name) {
super(name);
maxVelocity = 2f;
speed = 0.05f;
}
@Override
public void update(BaseUnit unit) {
super.update(unit);
unit.rotation = unit.velocity.angle();
}
@Override
public void draw(BaseUnit unit) {
Draw.alpha(unit.hitTime / Unit.hitDuration);
float walktime = unit.walkTime; //TODO!
float walktime = unit.walkTime;
float ft = Mathf.sin(walktime, 6f, 2f);
@@ -36,10 +56,50 @@ public abstract class GroundUnitType extends UnitType{
}
@Override
public void behavior(BaseUnit unit) {
tr2.set(unit.target.x, unit.target.y).sub(unit.x, unit.y).limit(speed);
public void updateTargeting(BaseUnit unit) {
if(!unit.timer.get(timerTarget, 20)) return;
unit.move(tr2.x, tr2.y);
unit.rotate(tr2.angle());
unit.target = Units.findEnemyTile(unit.team, unit.x, unit.y, range, t -> true);
if(unit.target != null) return;
ObjectSet<TeamData> teams = state.teams.enemyDataOf(unit.team);
Tile closest = null;
float cdist = 0f;
for(TeamData data : teams){
for(Tile tile : data.cores){
float dist = Vector2.dst(unit.x, unit.y, tile.drawx(), tile.drawy());
if(closest == null || dist < cdist){
closest = tile;
cdist = dist;
}
}
}
if(closest != null){
unit.target = closest.entity;
}else{
unit.target = null;
}
}
@Override
public void behavior(BaseUnit unit) {
//TODO actually pathfind
tr2.set(unit.target.x, unit.target.y).sub(unit.x, unit.y);
if(tr2.len() > stopDistance){
tr2.limit(speed);
unit.walkTime += Timers.delta();
unit.velocity.add(tr2);
}
if(unit.timer.get(timerReload, reload)){
shoot(unit, BulletType.shot, tr2.angle(), 4f);
}
}
}

View File

@@ -75,8 +75,12 @@ public abstract class UnitType {
//TODO logic
unit.velocity.limit(maxVelocity);
unit.x += unit.velocity.x / mass;
unit.y += unit.velocity.y / mass;
if(isFlying) {
unit.x += unit.velocity.x / mass;
unit.y += unit.velocity.y / mass;
}else{
unit.move(unit.velocity.x / mass, unit.velocity.y / mass);
}
unit.velocity.scl(Mathf.clamp(1f-drag* Timers.delta()));

View File

@@ -0,0 +1,12 @@
package io.anuke.mindustry.entities.units.types;
import io.anuke.mindustry.entities.units.FlyingUnitType;
public class Drone extends FlyingUnitType {
public Drone(String name) {
super(name);
}
}