mirror of
https://github.com/jokerx3/HeatUp
synced 2026-07-27 10:19:46 +02:00
Erweiterte Collision Detection
This commit is contained in:
@@ -3,6 +3,7 @@ package de.heatup.mapengine;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import de.heatup.logging.Logger;
|
||||
import de.heatup.testing.Opponent;
|
||||
import de.heatup.testing.Player;
|
||||
|
||||
public class CollisionHandler {
|
||||
@@ -14,29 +15,90 @@ public class CollisionHandler {
|
||||
* @return
|
||||
*/
|
||||
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) {
|
||||
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());
|
||||
}
|
||||
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();
|
||||
default:
|
||||
return true;
|
||||
return true; // should not happen
|
||||
}
|
||||
}
|
||||
public static void addMovingObject(Player movingObject) {
|
||||
|
||||
@@ -41,7 +41,7 @@ public class GameLoopD extends JFrame implements Runnable {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Logging ausgabe auf Console aktivieren bzw. deaktivieren.
|
||||
Logger.enableLogging();
|
||||
// Logger.enableLogging();
|
||||
|
||||
/*
|
||||
* hier drin bauen wir unser ui zusammen.
|
||||
@@ -85,7 +85,7 @@ public class GameLoopD extends JFrame implements Runnable {
|
||||
StatusPanelAdder.setPanel(statusPanel);
|
||||
StatusPanelAdder.createPanel();
|
||||
|
||||
player01 = new Player();
|
||||
player01 = new Player(); // !!! PLAYER IMMER ZUERST INSTANZIIEREN, DANACH OPPONENTS !!!
|
||||
// Query.setPlayer(player01);
|
||||
gamePanel.add(player01);
|
||||
player01.setBounds(25,25,25, 25); // !!! MUSS DRIN BLEIBEN TROTZ NACHTRÄGLICHES SPAWN UMSETZEN !!!
|
||||
|
||||
Reference in New Issue
Block a user