mirror of
https://github.com/jokerx3/HeatUp
synced 2026-07-27 12:29:44 +02:00
Kollisionserkennung in eigene Klasse ausgelagert.
Außerdem auch Kollisionserkennung mit Hardware Triggern hinzugefügt.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package de.heatup.mapengine;
|
||||
|
||||
import de.heatup.testing.Player;
|
||||
|
||||
public class CollisionHandler {
|
||||
/**
|
||||
* Prüfe ob eine Kollision vorliegt
|
||||
* @param movingObject Kann alles sein, dass sich bewegt. Spieler oder KI.
|
||||
* @param d Richtung in die sich bewegt werden soll. Möglichkeiten: l, r, u, d
|
||||
* @return
|
||||
*/
|
||||
public static boolean checkCollision(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()) {
|
||||
System.out.println("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()) {
|
||||
System.out.println("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()) {
|
||||
System.out.println("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()) {
|
||||
System.out.println("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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,6 @@ import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.Point;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
@@ -15,8 +11,6 @@ import de.heatup.mapengine.MapEngine;
|
||||
import de.heatup.mapengine.SpawnAssistant;
|
||||
import de.heatup.objects.DynamicObject;
|
||||
import de.heatup.objects.StaticObject;
|
||||
import de.heatup.objects.logic;
|
||||
import de.heatup.ui.GameInterface;
|
||||
import de.heatup.ui.KeyBindings;
|
||||
|
||||
|
||||
|
||||
@@ -5,19 +5,12 @@ import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JComponent;
|
||||
|
||||
import de.heatup.objects.ImageLoader;
|
||||
|
||||
public class Player extends JComponent {
|
||||
private static final long serialVersionUID = 1L;
|
||||
// private Image image;
|
||||
|
||||
@@ -3,7 +3,7 @@ package de.heatup.ui;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import de.heatup.mapengine.MapGrid;
|
||||
import de.heatup.mapengine.CollisionHandler;
|
||||
import de.heatup.testing.Player;
|
||||
|
||||
public class KeyBindings extends KeyAdapter {
|
||||
@@ -58,10 +58,7 @@ public class KeyBindings extends KeyAdapter {
|
||||
|
||||
public void checkKeyActivity() {
|
||||
|
||||
/*
|
||||
* rudimentäre kollisionserkennung, exportiere ich gerade in eigenes modul :)
|
||||
*/
|
||||
if (isLeft() && MapGrid.getGridCell(player.getGridLocation().x-1, player.getGridLocation().y).isPath()) {
|
||||
if (isLeft() && CollisionHandler.checkCollision(player, 'l')) {
|
||||
if (!player.animate ) {
|
||||
player.setGridLocation(player.getGridLocation().x-1, player.getGridLocation().y);
|
||||
player.step('x', -1);
|
||||
@@ -69,7 +66,7 @@ public class KeyBindings extends KeyAdapter {
|
||||
}
|
||||
player.animate = true;
|
||||
}
|
||||
if (isRight() && MapGrid.getGridCell(player.getGridLocation().x+1, player.getGridLocation().y).isPath()) {
|
||||
if (isRight() && CollisionHandler.checkCollision(player, 'r')) {
|
||||
if (!player.animate ) {
|
||||
player.setGridLocation(player.getGridLocation().x+1, player.getGridLocation().y);
|
||||
player.step('x', 1);
|
||||
@@ -77,7 +74,7 @@ public class KeyBindings extends KeyAdapter {
|
||||
}
|
||||
player.animate = true;
|
||||
}
|
||||
if (isUp() && MapGrid.getGridCell(player.getGridLocation().x, player.getGridLocation().y-1).isPath()) {
|
||||
if (isUp() && CollisionHandler.checkCollision(player, 'u')) {
|
||||
if (!player.animate ) {
|
||||
player.setGridLocation(player.getGridLocation().x, player.getGridLocation().y-1);
|
||||
player.step('y', -1);
|
||||
@@ -85,7 +82,7 @@ public class KeyBindings extends KeyAdapter {
|
||||
}
|
||||
player.animate = true;
|
||||
}
|
||||
if (isDown() && MapGrid.getGridCell(player.getGridLocation().x, player.getGridLocation().y+1).isPath()) {
|
||||
if (isDown() && CollisionHandler.checkCollision(player, 'd')) {
|
||||
if (!player.animate ) {
|
||||
player.setGridLocation(player.getGridLocation().x, player.getGridLocation().y+1);
|
||||
player.step('y', 1);
|
||||
|
||||
Reference in New Issue
Block a user