From 4cee7aaaa55014bb6b9d106f51ca9ea321eb4dad Mon Sep 17 00:00:00 2001 From: andreas Date: Fri, 7 Nov 2014 14:15:58 +0100 Subject: [PATCH] =?UTF-8?q?Kreuzungs=20erkennung=20hinzugef=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HeatUp/src/de/heatup/testing/Opponent.java | 68 +++++++++++++++++----- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/HeatUp/src/de/heatup/testing/Opponent.java b/HeatUp/src/de/heatup/testing/Opponent.java index 4e5498b..b714f44 100644 --- a/HeatUp/src/de/heatup/testing/Opponent.java +++ b/HeatUp/src/de/heatup/testing/Opponent.java @@ -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; + } } - - } + } + + + } }