Improved pathfinding

This commit is contained in:
Anuken 2017-05-03 11:52:54 -04:00
parent c374073e6f
commit 0719223146
8 changed files with 50 additions and 31 deletions

BIN
core/assets/maps/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 608 B

View File

@ -87,7 +87,7 @@ public class Control extends RendererModule{
if(enemies <= 0)
wavetime -= delta();
if(wavetime <= 0){
if(wavetime <= 0 || (debug && Inputs.keyUp(Keys.F))){
GameState.runWave();
}

View File

@ -175,6 +175,7 @@ public class UI extends SceneModule{
tutorial.content().row();
tutorial.content().addCheck("Don't show again", b->{
Settings.putBool("tutorial", !b);
Settings.save();
}).padTop(4);
restart = new Dialog("The core was destroyed.", "dialog"){

View File

@ -33,7 +33,7 @@ public class Vars{
public static float breaktime = 0;
public static final String[] maps = {"delta", "canyon", "pit"};
public static final String[] maps = {"delta", "canyon", "pit", "test"};
public static Pixmap[] mapPixmaps;
public static Texture[] mapTextures;
public static int worldsize = 128;

View File

@ -9,13 +9,12 @@ import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.entities.Enemy;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.entities.Entities;
import io.anuke.ucore.entities.Entity;
public class Pathfind{
static MHueristic heuristic = new MHueristic();
static PassTileGraph passgraph = new PassTileGraph();
static IndexedAStarPathFinder<Tile> passpathfinder;
static Array<DefaultGraphPath<Tile>> paths = new Array<>();
static Tile[][] pathSequences;
static Vector2 vector = new Vector2();
static public Vector2 find(Enemy enemy){
@ -26,17 +25,17 @@ public class Pathfind{
//-1 is only possible here if both pathfindings failed, which should NOT happen
//check graph code
DefaultGraphPath<Tile> path = paths.get(enemy.spawn);
Tile[] path = enemy.path;
Tile target = path.get(enemy.node);
Tile target = path[enemy.node];
float dst = Vector2.dst(enemy.x, enemy.y, target.worldx(), target.worldy());
if(dst < 2){
if(enemy.node <= path.getCount()-2)
if(enemy.node <= path.length-2)
enemy.node ++;
target = path.get(enemy.node);
target = path[enemy.node];
}
@ -46,45 +45,52 @@ public class Pathfind{
static public void reset(){
paths.clear();
pathSequences = null;
passpathfinder = new IndexedAStarPathFinder<Tile>(passgraph);
}
static public void updatePath(){
if(paths.size == 0){
pathSequences = new Tile[3][0];
for(int i = 0; i < spawnpoints.size; i ++){
DefaultGraphPath<Tile> path = new DefaultGraphPath<>();
paths.add(path);
}
}
int i = 0;
for(DefaultGraphPath<Tile> path : paths){
for(int i = 0; i < paths.size; i ++){
DefaultGraphPath<Tile> path = paths.get(i);
path.clear();
passpathfinder.searchNodePath(
spawnpoints.get(i),
core, heuristic, path);
//for(Tile tile : path){
// Effects.effect("ind", tile.worldx(), tile.worldy());
///}
i++;
}
for(Entity e : Entities.all()){
if(e instanceof Enemy){
findNode((Enemy)e);
pathSequences[i] = new Tile[path.getCount()];
for(int node = 0; node < path.getCount(); node ++){
Tile tile = path.get(node);
pathSequences[i][node] = tile;
}
/*
for(Tile tile : path){
Effects.effect("ind", tile.worldx(), tile.worldy());
}
*/
}
}
static void findNode(Enemy enemy){
DefaultGraphPath<Tile> path = paths.get(enemy.spawn);
enemy.path = pathSequences[enemy.spawn];
Tile[] path = enemy.path;
Tile closest = null;
float ldst = 0f;
int cindex = -1;
for(int i = 0; i < path.getCount(); i ++){
Tile tile = path.get(i);
for(int i = 0; i < path.length; i ++){
Tile tile = path[i];
float dst = Vector2.dst(tile.worldx(), tile.worldy(), enemy.x, enemy.y);
if(closest == null || dst < ldst){

View File

@ -39,6 +39,13 @@ public class Bullet extends BulletEntity{
super.update();
}
@Override
public boolean collides(SolidEntity other){
if(owner instanceof TileEntity && other instanceof Player)
return false;
return super.collides(other);
}
@Override
public void collision(SolidEntity other){
super.collision(other);

View File

@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.World;
import io.anuke.mindustry.ai.Pathfind;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Draw;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.entities.*;
@ -12,6 +13,7 @@ import io.anuke.ucore.util.Timers;
public class Enemy extends DestructibleEntity{
public Vector2 direction = new Vector2();
public Tile[] path;
public float xvelocity, yvelocity;
public float speed = 0.3f;
public int node = -1;
@ -40,21 +42,22 @@ public class Enemy extends DestructibleEntity{
move(vec.x*delta, vec.y*delta);
//if(Timers.get(this, 10))
target = World.findTileTarget(x, y, null, range, false);
if(Timers.get(this, 15)){
target = World.findTileTarget(x, y, null, range, false);
//no tile found
if(target == null)
target = Entities.getClosest(x, y, range, e->{
return e instanceof Player;
});
//no tile found
if(target == null)
target = Entities.getClosest(x, y, range, e->{
return e instanceof Player;
});
}
if(target != null){
if(Timers.get(this, reload)){
if(Timers.get(hashCode()+"reload", reload)){
shoot();
Effects.sound(shootsound, this);
}
}
}

View File

@ -2,6 +2,7 @@ package io.anuke.mindustry.desktop;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import io.anuke.mindustry.Mindustry;
public class DesktopLauncher {
@ -9,6 +10,7 @@ public class DesktopLauncher {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setTitle("Mindustry");
config.setMaximized(true);
config.useVsync(false);
new Lwjgl3Application(new Mindustry(), config);
}
}