mirror of
https://github.com/jokerx3/HeatUp
synced 2026-07-27 12:19:45 +02:00
Kreuzungs erkennung hinzugefügt.
This commit is contained in:
@@ -9,6 +9,7 @@ import javax.imageio.ImageIO;
|
||||
import de.heatup.cfg.Config;
|
||||
import de.heatup.logging.Logger;
|
||||
import de.heatup.mapengine.MapGrid;
|
||||
import de.heatup.mapengine.MapGridCell;
|
||||
|
||||
public class Opponent extends Player {
|
||||
|
||||
@@ -26,6 +27,7 @@ public class Opponent extends Player {
|
||||
private int prevX;
|
||||
private int prevY;
|
||||
private boolean blocked = false;
|
||||
private boolean move = false;
|
||||
private int blockedCounter = 0;
|
||||
|
||||
@Override protected void loadBufferedImage() {
|
||||
@@ -37,17 +39,9 @@ public class Opponent extends Player {
|
||||
}
|
||||
|
||||
@Override public void tick() {
|
||||
|
||||
if ( redecide ) {
|
||||
while ( true ) {
|
||||
|
||||
this.d = (rand.nextInt(10) <= 4) ? 'x' : 'y';
|
||||
this.i = (rand.nextInt(10) <= 4) ? 1 : -1;
|
||||
|
||||
// Sicher stellen das man nicht in die Richtung geht aus der man gekommen ist.
|
||||
if ( redecide && this.d == this.prevd && this.i != this.previ ) {
|
||||
this.i *= -1;
|
||||
}
|
||||
// Blockade Erkennung
|
||||
if ( this.prevX == this.getGridLocation().x & this.prevY == this.getGridLocation().y ) {
|
||||
this.blockedCounter++;
|
||||
@@ -57,13 +51,47 @@ public class Opponent extends Player {
|
||||
this.blocked = (this.blockedCounter >= 30) ? true : false;
|
||||
this.prevX = this.getGridLocation().x;
|
||||
this.prevY = this.getGridLocation().y;
|
||||
|
||||
// Kreuzung erkennen
|
||||
if ( d == 'x' ) {
|
||||
if ( MapGrid.getGridCell(this.getGridLocation().x, this.getGridLocation().y+1).isPath() ) {
|
||||
this.move = false;
|
||||
}
|
||||
if ( MapGrid.getGridCell(this.getGridLocation().x, this.getGridLocation().y-1).isPath() ) {
|
||||
this.move = false;
|
||||
}
|
||||
}
|
||||
if ( d == 'y' ) {
|
||||
if ( MapGrid.getGridCell(this.getGridLocation().x-1, this.getGridLocation().y).isPath() ) {
|
||||
this.move = false;
|
||||
}
|
||||
if ( MapGrid.getGridCell(this.getGridLocation().x+1, this.getGridLocation().y).isPath() ) {
|
||||
this.move = false;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Bei Kreuzung umentscheiden
|
||||
|
||||
// Umentscheiden oder weiter in die vorgegeben Richtung
|
||||
if ( ! this.move ) {
|
||||
Logger.write("Redeciding...");
|
||||
this.d = (rand.nextInt(10) <= 4) ? 'x' : 'y';
|
||||
this.i = (rand.nextInt(10) <= 4) ? 1 : -1;
|
||||
// Sicher stellen das man nicht in die Richtung geht aus der man gekommen ist.
|
||||
if ( redecide && this.d == this.prevd && this.i != this.previ ) {
|
||||
this.i *= -1;
|
||||
}
|
||||
|
||||
} else {
|
||||
Logger.write("Moving...");
|
||||
}
|
||||
|
||||
this.move = true;
|
||||
|
||||
// Blockade auflösen, gehe doch in die Richtung aus man gekommen ist
|
||||
if ( this.blocked ) {
|
||||
Logger.write("Warning: Block detected!");
|
||||
this.i *= -1;
|
||||
this.move = false;
|
||||
this.blocked = false;
|
||||
this.blockedCounter = 0;
|
||||
}
|
||||
@@ -85,8 +113,7 @@ public class Opponent extends Player {
|
||||
redecide = false;
|
||||
animate = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +121,8 @@ public class Opponent extends Player {
|
||||
int newx = this.getX();
|
||||
int newy = this.getY();
|
||||
|
||||
for (int s = 0; s < this.speedMultiplicator; s++) { // speed multiplikator
|
||||
// Speed Multiplikator
|
||||
for (int s = 0; s < this.speedMultiplicator; s++) {
|
||||
if (d == 'x' && i == -1) {
|
||||
newx = newx - 1;
|
||||
} else if (d == 'x' && i == 1) {
|
||||
@@ -104,20 +132,28 @@ public class Opponent extends Player {
|
||||
} else if (d == 'y' && i == 1) {
|
||||
newy = newy + 1;
|
||||
}
|
||||
|
||||
|
||||
this.setAutoBounds(newx, newy);
|
||||
stepCounter--;
|
||||
if ( stepCounter <= 0 ) {
|
||||
animate = false;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (stepCounter <= 0) {
|
||||
stepCounter = 25;
|
||||
redecide = true;
|
||||
if ( ! MapGrid.getGridCell(newx,newy).isPath()) {
|
||||
redecide = true;
|
||||
animate = false;
|
||||
} else {
|
||||
animate = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user