diff --git a/HeatUp/src/de/heatup/mapengine/CollisionHandler.java b/HeatUp/src/de/heatup/mapengine/CollisionHandler.java index b8ecfd6..ed64e57 100644 --- a/HeatUp/src/de/heatup/mapengine/CollisionHandler.java +++ b/HeatUp/src/de/heatup/mapengine/CollisionHandler.java @@ -1,5 +1,6 @@ package de.heatup.mapengine; +import java.awt.Point; import java.util.ArrayList; import de.heatup.logging.Logger; @@ -50,6 +51,32 @@ public class CollisionHandler { // return true; // } } + + public static boolean checkCollision(Point startPosition, char d, int steps) { + boolean isPath = true; + for (int counter = 1; counter <= steps; counter++) { + switch (d) { + case 'l': + isPath = MapGrid.getGridCell(startPosition.x-counter, startPosition.y).isPath(); + break; + case 'r': + isPath = MapGrid.getGridCell(startPosition.x+counter, startPosition.y).isPath(); + break; + case 'd': + isPath = MapGrid.getGridCell(startPosition.x, startPosition.y+counter).isPath(); + break; + case 'u': + isPath = MapGrid.getGridCell(startPosition.x, startPosition.y-counter).isPath(); + break; + } + if ( ! isPath ) { + break; + } + } + return isPath; + } + + private static void playerCollision(Player movingObject, char d) { // if (movingObject.getGridLocation().x== movingObjects.get(0).getGridLocation().x && movingObject.getGridLocation().y == movingObjects.get(0).getGridLocation().y) { //// System.exit(-1); diff --git a/HeatUp/src/de/heatup/resources/maps/map3.txt b/HeatUp/src/de/heatup/resources/maps/map3.txt index aec0e6f..19345ab 100644 --- a/HeatUp/src/de/heatup/resources/maps/map3.txt +++ b/HeatUp/src/de/heatup/resources/maps/map3.txt @@ -1,24 +1,24 @@ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ -################################ \ No newline at end of file +-------------------------------- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-##############################- +-------------------------------- \ No newline at end of file diff --git a/HeatUp/src/de/heatup/testing/Opponent.java b/HeatUp/src/de/heatup/testing/Opponent.java index 1e82012..7287549 100644 --- a/HeatUp/src/de/heatup/testing/Opponent.java +++ b/HeatUp/src/de/heatup/testing/Opponent.java @@ -29,6 +29,9 @@ public class Opponent extends Player { private boolean move = false; private int blockedCounter = 0; private char charDir; + // Direkten Pfad zum Spieler nutzen + private char newd; + public Opponent() { CollisionHandler.addMovingObject(this); @@ -46,7 +49,6 @@ public class Opponent extends Player { if ( redecide ) { while ( true ) { - // Blockade Erkennung if ( this.prevX == this.getGridLocation().x & this.prevY == this.getGridLocation().y ) { this.blockedCounter++; @@ -57,7 +59,7 @@ public class Opponent extends Player { this.prevX = this.getGridLocation().x; this.prevY = this.getGridLocation().y; - if ( d == 'x' ) { + if ( direction == 'x' ) { if ( CollisionHandler.checkCollision(this, 'd') ) { this.move = false; } @@ -65,7 +67,7 @@ public class Opponent extends Player { this.move = false; } } - if ( d == 'y' ) { + if ( direction == 'y' ) { if ( CollisionHandler.checkCollision(this, 'l') ) { this.move = false; } @@ -73,14 +75,48 @@ public class Opponent extends Player { this.move = false; } } + + /* + * Spieler identifizieren und auf "direkte Sicht" prüfen + */ + int playerX = GameLoopD.getPlayer().getGridLocation().x; + int playerY = GameLoopD.getPlayer().getGridLocation().y; + char playerDirection = '0'; + int stepLength = 0; + // Richtung bestimmen wenn sich Spieler und Gegner auf einer Achse befinden + if ( this.getGridLocation().x == playerX) { + //Auf Y-Achse in Richtung Spieler bewegen + playerDirection = (this.getGridLocation().y > playerY ) ? 'u' : 'd'; + stepLength = Math.abs(this.getGridLocation().y - playerY); + this.newd = 'y'; + i = (playerDirection == 'u') ? -1 : 1; + } else if ( this.getGridLocation().y == playerY) { + // Auf X-Achse in Richtung Spieler bewegen + playerDirection = (this.getGridLocation().x > playerX ) ? 'l' : 'r'; + stepLength = Math.abs(this.getGridLocation().x - playerX); + this.newd = 'x'; + i = (playerDirection == 'l') ? -1 : 1; + } + + // Sichtprüfung durchführen + if ( stepLength != 0 && playerDirection != '0' && CollisionHandler.checkCollision(this.getGridLocation(), playerDirection, stepLength) ) { + //Logger.write("Direct path to player, distance: " + stepLength ); + direction = this.newd; + charDir = playerDirection; + this.move = true; + } + + /* + * + */ // Umentscheiden oder weiter in die vorgegeben Richtung if ( ! this.move ) { //Logger.write("Redeciding..."); - this.d = (rand.nextInt(10) <= 4) ? 'x' : 'y'; + this.direction = (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 ) { + if ( redecide && this.direction == this.prevd && this.i != this.previ ) { this.i *= -1; } @@ -100,27 +136,27 @@ public class Opponent extends Player { } // Neue Koordinaten bestimmen - if (d == 'x') { + if (direction == 'x') { testX=this.getGridLocation().x + i; testY=this.getGridLocation().y; - } else if (d == 'y') { + } else if (direction == 'y') { testY=this.getGridLocation().y + i; testX=this.getGridLocation().x; } - if ( d == 'x' && i == -1 ) { + if ( direction == 'x' && i == -1 ) { charDir = 'l'; - } else if ( d == 'x' && i == 1 ) { + } else if ( direction == 'x' && i == 1 ) { charDir = 'r'; - } else if ( d == 'y' && i == -1 ) { + } else if ( direction == 'y' && i == -1 ) { charDir = 'u'; - } else if ( d == 'y' && i == 1 ) { + } else if ( direction == 'y' && i == 1 ) { charDir = 'd'; } if (CollisionHandler.checkCollision(this, charDir)) { this.setGridLocation(testX, testY); - this.prevd = d; + this.prevd = direction; this.previ = i; redecide = false; animate = true; @@ -135,13 +171,13 @@ public class Opponent extends Player { // Speed Multiplikator for (int s = 0; s < this.speedMultiplicator; s++) { - if (d == 'x' && i == -1) { + if (direction == 'x' && i == -1) { newx = newx - 1; - } else if (d == 'x' && i == 1) { + } else if (direction == 'x' && i == 1) { newx = newx + 1; - } else if (d == 'y' && i == -1) { + } else if (direction == 'y' && i == -1) { newy = newy - 1; - } else if (d == 'y' && i == 1) { + } else if (direction == 'y' && i == 1) { newy = newy + 1; } diff --git a/HeatUp/src/de/heatup/testing/Player.java b/HeatUp/src/de/heatup/testing/Player.java index 8981400..fb85cc5 100644 --- a/HeatUp/src/de/heatup/testing/Player.java +++ b/HeatUp/src/de/heatup/testing/Player.java @@ -25,7 +25,7 @@ public class Player extends JComponent { public boolean startMove = false; public boolean animate = false; protected int speedMultiplicator = Config.initialSpeedMultiplicator; - protected char d; + protected char direction; protected int i; final int stepx = 25; final int stepy = 25; @@ -94,7 +94,7 @@ public class Player extends JComponent { } public void step(char d, int i) { - this.d = d; + this.direction = d; this.i = i; } @@ -103,13 +103,13 @@ public class Player extends JComponent { int newx = this.getX(); int newy = this.getY(); for (int s = 0; s < this.speedMultiplicator; s++) { // speed multiplikator - if (d == 'x' && i == -1) { + if (direction == 'x' && i == -1) { newx = newx - 1; - } else if (d == 'x' && i == 1) { + } else if (direction == 'x' && i == 1) { newx = newx + 1; - } else if (d == 'y' && i == -1) { + } else if (direction == 'y' && i == -1) { newy = newy - 1; - } else if (d == 'y' && i == 1) { + } else if (direction == 'y' && i == 1) { newy = newy + 1; } this.setAutoBounds(newx, newy);