こんにちは私はAndengineライブラリを使用して次のコードを持っています。コードは機能します。基本的に、この場合は敵(黒い羊)からプレーヤーまでのパスを取得し、パス修飾子を使用して羊を移動します。パスの最後でプロセスが再開されます。必要に応じて、スレッドを使用して永久運動を作成しています。私の質問は、これがこの種の永続的なパスファインディングを行うための最も効率的なクリーンな方法ですか?ありがとう
public void startThread(){ stop1=true; t1 = new Thread(mMoveBlackSheep); t1.start(); }
public void stopThread(){ if(t1 != null){ stop1=false; t1.interrupt(); }}
public void startThread1(){ stop2=true; t2 = new Thread(mMoveBlackSheep1); t2.start(); }
public void stopThread1(){ if(t2 != null){ stop2=false; t2.interrupt(); }}
private synchronized Path get_coords( int sheep ) {
Path retxy=null;
try {
final float[] playerFootCordinates = player.convertLocalToSceneCoordinates( 12,31 );
final TMXTile tmxTile = tmxLayer.getTMXTileAt(playerFootCordinates[Constants.VERTEX_INDEX_X], playerFootCordinates[Constants.VERTEX_INDEX_Y]);
int pColPlayer = 0;
int pRowPlayer = 0;
if(tmxTile != null) {
pColPlayer = tmxTile.getTileColumn();
pRowPlayer = tmxTile.getTileRow();
}
AnimatedSprite blacksheep = black_sheep.get(sheep);
int pColBlackSheep = 0;
int pRowBlackSheep = 0;
final float[] MonsterFootCordinates = blacksheep.convertLocalToSceneCoordinates( 12,31 );
final TMXTile tmxTile1 = tmxLayer.getTMXTileAt(MonsterFootCordinates[Constants.VERTEX_INDEX_X], MonsterFootCordinates [Constants.VERTEX_INDEX_Y]);
if(tmxTile1 != null) {
pColBlackSheep = tmxTile1.getTileColumn();
pRowBlackSheep = tmxTile1.getTileRow();
}
int xx1 = (int)(player.getX() / TileWidth );
int yy1 = (int)(player.getY() / TileHeight );
int selectedx = pRowBlackSheep;
int selectedy = pColBlackSheep;
pathfinding.Path path=null;
Random r1 = new Random();
int d1 = r1.nextInt(20);
path = finder_sheep.findPath(new UnitMover(1), selectedx, selectedy, pRowPlayer , pColPlayer );
// still null restart thread.
if (path==null)
{
try {
int randomAmountOfTime=500;
if ( !Thread.interrupted() ) {
Thread.sleep(randomAmountOfTime);
}
} catch (InterruptedException e) {
return null;
}
catch (Exception e) { e.printStackTrace(); }
}
if (path != null) {
int p1=path.getLength();
float[] x1 = new float[p1];
float[] y1 = new float[p1];
int[] direction = new int[p1];
for (int i=0; i<p1; i++)
{
int vecX = path.getX(i) * TileWidth;
int vecY = path.getY(i) * TileHeight;
x1[i] = vecX;
y1[i] = vecY;
if ( i > 0 ) {
int vecXPrev = path.getX(i-1) * TileWidth;
int vecYPrev = path.getY(i-1) * TileHeight;
} else { direction[0]=0; }
}
retxy = new Path(y1 , x1 );
Log.d("END CREATE XY ARRAY", "END CREATE XY ARRAY");
} else { retxy=null; }
}
catch (Exception e) { }
return retxy;
}
Runnable mMoveBlackSheep = new Runnable() {public void run() { if (t1!=null ) { while(!t1.isInterrupted()) { run1(); }}}};
Runnable mMoveBlackSheep1 = new Runnable() {public void run() { if (t2!=null) { while(!t2.isInterrupted()) { run2(); }}}};
private void run1() {
try {
Path co1 = get_coords(0);
if (co1 != null) {
followPath(co1 , 0 ,0 );
}
} catch (Exception e) {
//Log.d("run1", e.getMessage());
}
}
private void run2() {
try {
// get the Column / Row for the player.
Path co1 = get_coords(1);
if (co1 != null) {
followPath(co1 , 1 ,1 );
}
} catch (Exception e) {
//Log.d("run1", e.getMessage());
}
}
private synchronized void followPath( final Path p1 , final int current_sheep , final int threadnum )
{
try
{
float speedOfPlayer= 4;
black_sheep.get(current_sheep).registerEntityModifier(new PathModifier(speedOfPlayer, p1, null, new IPathModifierListener() {
public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {
}
//@Override
public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
final long[] frameDurations = new long[3];
Arrays.fill(frameDurations, 500);
black_sheep.get(current_sheep).animate(frameDurations, 0, 2, true);
}
//@Override
public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
}
//@Override
public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
if ( current_sheep==0) {
stopThread();
startThread(); }
if ( current_sheep==1) {
stopThread1();
startThread1(); }
}
}));
}
catch (Exception e) {
//Log.d("ERROR~~~", e.getMessage() );
}
}