Erweiterte Collision Detection

This commit is contained in:
toksikk
2014-11-07 16:00:57 +01:00
parent 0d76bfdc02
commit 945837642e
2 changed files with 68 additions and 6 deletions
@@ -3,6 +3,7 @@ package de.heatup.mapengine;
import java.util.ArrayList; import java.util.ArrayList;
import de.heatup.logging.Logger; import de.heatup.logging.Logger;
import de.heatup.testing.Opponent;
import de.heatup.testing.Player; import de.heatup.testing.Player;
public class CollisionHandler { public class CollisionHandler {
@@ -14,29 +15,90 @@ public class CollisionHandler {
* @return * @return
*/ */
public static boolean checkCollision(Player movingObject, char d) { public static boolean checkCollision(Player movingObject, char d) {
// if (movingObject == (movingObjects.get(0))) {
if (movingObject.equals(movingObjects.get(0))) {
enemyCollision(movingObject, d);
hardwareCollision(movingObject, d);
return normalCheck(movingObject, d);
} else {
playerCollision(movingObject, d);
return normalCheck(movingObject, d);
}
// switch (d) {
// case 'l':
// if (MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isTrigger()) {
// Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).getCorrespondingHardwareToTrigger());
// }
// return MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isPath();
// case 'r':
// if (MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isTrigger()) {
// Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).getCorrespondingHardwareToTrigger());
// }
// return MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isPath();
// case 'd':
// if (MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isTrigger()) {
// Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).getCorrespondingHardwareToTrigger());
// }
// return MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isPath();
// case 'u':
// if (MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isTrigger()) {
// Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).getCorrespondingHardwareToTrigger());
// }
// return MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isPath();
// default:
// return true;
// }
}
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);
}
}
private static void enemyCollision(Player movingObject, char d ) {
for (int i=1; i<movingObjects.size();i++) {
if (movingObject.getGridLocation().x== movingObjects.get(i).getGridLocation().x && movingObject.getGridLocation().y == movingObjects.get(i).getGridLocation().y) {
// System.exit(-1);
}
}
}
private static void hardwareCollision(Player movingObject, char d) {
switch (d) { switch (d) {
case 'l': case 'l':
if (MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isTrigger()) { if (MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isTrigger()) {
Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).getCorrespondingHardwareToTrigger()); Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).getCorrespondingHardwareToTrigger());
} }
return MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isPath();
case 'r': case 'r':
if (MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isTrigger()) { if (MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isTrigger()) {
Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).getCorrespondingHardwareToTrigger()); Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).getCorrespondingHardwareToTrigger());
} }
return MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isPath();
case 'd': case 'd':
if (MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isTrigger()) { if (MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isTrigger()) {
Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).getCorrespondingHardwareToTrigger()); Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).getCorrespondingHardwareToTrigger());
} }
return MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isPath();
case 'u': case 'u':
if (MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isTrigger()) { if (MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isTrigger()) {
Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).getCorrespondingHardwareToTrigger()); Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).getCorrespondingHardwareToTrigger());
} }
default:
}
}
private static boolean normalCheck(Player movingObject, char d) {
switch (d) {
case 'l':
return MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isPath();
case 'r':
return MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isPath();
case 'd':
return MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isPath();
case 'u':
return MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isPath(); return MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isPath();
default: default:
return true; return true; // should not happen
} }
} }
public static void addMovingObject(Player movingObject) { public static void addMovingObject(Player movingObject) {
+2 -2
View File
@@ -41,7 +41,7 @@ public class GameLoopD extends JFrame implements Runnable {
public static void main(String[] args) { public static void main(String[] args) {
// Logging ausgabe auf Console aktivieren bzw. deaktivieren. // Logging ausgabe auf Console aktivieren bzw. deaktivieren.
Logger.enableLogging(); // Logger.enableLogging();
/* /*
* hier drin bauen wir unser ui zusammen. * hier drin bauen wir unser ui zusammen.
@@ -85,7 +85,7 @@ public class GameLoopD extends JFrame implements Runnable {
StatusPanelAdder.setPanel(statusPanel); StatusPanelAdder.setPanel(statusPanel);
StatusPanelAdder.createPanel(); StatusPanelAdder.createPanel();
player01 = new Player(); player01 = new Player(); // !!! PLAYER IMMER ZUERST INSTANZIIEREN, DANACH OPPONENTS !!!
// Query.setPlayer(player01); // Query.setPlayer(player01);
gamePanel.add(player01); gamePanel.add(player01);
player01.setBounds(25,25,25, 25); // !!! MUSS DRIN BLEIBEN TROTZ NACHTRÄGLICHES SPAWN UMSETZEN !!! player01.setBounds(25,25,25, 25); // !!! MUSS DRIN BLEIBEN TROTZ NACHTRÄGLICHES SPAWN UMSETZEN !!!