mirror of
https://github.com/jokerx3/HeatUp
synced 2026-07-27 09:59:45 +02:00
MapGrid zur Erstellung und Abfrage des Grid-Layers.
Vorbereitung zu Player und Gegner Movement sowie einfach Kollisionserkennung!
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package de.heatup.mapengine;
|
||||
|
||||
public class GridCell {
|
||||
private boolean isPath = false;
|
||||
private boolean isTrigger = false;
|
||||
private char correspondingHardwareToTrigger;
|
||||
public char getCorrespondingHardwareToTrigger() {
|
||||
return correspondingHardwareToTrigger;
|
||||
}
|
||||
public void setCorrespondingHardwareToTrigger(char correspondingHardwareToTrigger) {
|
||||
this.correspondingHardwareToTrigger = correspondingHardwareToTrigger;
|
||||
}
|
||||
public boolean isPath() {
|
||||
return isPath;
|
||||
}
|
||||
protected void setPath(boolean isPath) {
|
||||
this.isPath = isPath;
|
||||
}
|
||||
public boolean isTrigger() {
|
||||
return isTrigger;
|
||||
}
|
||||
protected void setTrigger(boolean isTrigger) {
|
||||
this.isTrigger = isTrigger;
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,58 @@ public class MapEngine {
|
||||
while (input.hasNextLine() && lineCount <= mapHeight) {
|
||||
String currentLine = input.nextLine();
|
||||
for (int i = 0; i<mapWidth; i++) {
|
||||
/* MapGrid implementation */
|
||||
if (checkIfPath(currentLine.charAt(i))) {
|
||||
if (MapGrid.getGridCell(i, lineCount) == null) {
|
||||
MapGrid.addGrid(i, lineCount, true);
|
||||
} else {
|
||||
MapGrid.getGridCell(i, lineCount).setPath(true);
|
||||
}
|
||||
} else {
|
||||
if (checkIfHardware(currentLine.charAt(i))) {
|
||||
if (MapGrid.getGridCell(i, lineCount) == null) {
|
||||
MapGrid.addGrid(i, lineCount, false);
|
||||
} else {
|
||||
MapGrid.getGridCell(i, lineCount).setPath(false);
|
||||
}
|
||||
/* trigger setzen */
|
||||
/* trigger 1 */
|
||||
if (MapGrid.getGridCell(i+1, lineCount) == null) {
|
||||
MapGrid.addGrid(i+1, lineCount, true, currentLine.charAt(i));
|
||||
} else {
|
||||
MapGrid.getGridCell(i+1, lineCount).setTrigger(true);
|
||||
MapGrid.getGridCell(i+1, lineCount).setCorrespondingHardwareToTrigger(currentLine.charAt(i));
|
||||
}
|
||||
/* trigger 2 */
|
||||
if (MapGrid.getGridCell(i-1, lineCount) == null) {
|
||||
MapGrid.addGrid(i-1, lineCount, true, currentLine.charAt(i));
|
||||
} else {
|
||||
MapGrid.getGridCell(i-1, lineCount).setTrigger(true);
|
||||
MapGrid.getGridCell(i-1, lineCount).setCorrespondingHardwareToTrigger(currentLine.charAt(i));
|
||||
}
|
||||
/* trigger 3 */
|
||||
if (MapGrid.getGridCell(i, lineCount+1) == null) {
|
||||
MapGrid.addGrid(i, lineCount+1, true, currentLine.charAt(i));
|
||||
} else {
|
||||
MapGrid.getGridCell(i, lineCount+1).setTrigger(true);
|
||||
MapGrid.getGridCell(i, lineCount+1).setCorrespondingHardwareToTrigger(currentLine.charAt(i));
|
||||
}
|
||||
/* trigger 4 */
|
||||
if (MapGrid.getGridCell(i, lineCount-1) == null) {
|
||||
MapGrid.addGrid(i, lineCount-1, true, currentLine.charAt(i));
|
||||
} else {
|
||||
MapGrid.getGridCell(i, lineCount-1).setTrigger(true);
|
||||
MapGrid.getGridCell(i, lineCount-1).setCorrespondingHardwareToTrigger(currentLine.charAt(i));
|
||||
}
|
||||
} else {
|
||||
if (MapGrid.getGridCell(i, lineCount) == null) {
|
||||
MapGrid.addGrid(i, lineCount, false);
|
||||
} else {
|
||||
MapGrid.getGridCell(i, lineCount).setPath(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* End of MapGrid implementation */
|
||||
if (!allMapTiles.containsKey(currentLine.charAt(i))) {
|
||||
ArrayList<Point> newTileType = new ArrayList<Point>();
|
||||
newTileType.add(new Point(x,y));
|
||||
@@ -140,4 +192,15 @@ public class MapEngine {
|
||||
public ArrayList<StaticObject> getAllHardwareTriggerObjects() {
|
||||
return MapObjectHandler.getAllHardwareTriggerObjects();
|
||||
}
|
||||
private boolean checkIfPath(char c) {
|
||||
return (c=='#');
|
||||
}
|
||||
private boolean checkIfHardware(char c) {
|
||||
for (char j = '0'; j <= '9'; j++) {
|
||||
if (j==c) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package de.heatup.mapengine;
|
||||
|
||||
import de.heatup.cfg.Config;
|
||||
|
||||
public class MapGrid {
|
||||
private static GridCell[][] allGridCells = new GridCell[Config.mapMaxTilesWidth][Config.mapMaxTilesHeight];
|
||||
private static GridCell dummyCell = new GridCell(); // wird zurückgegeben, falls der MapGrid index ausserhalb unserer allGridCells dimension zeigt.
|
||||
protected static void addGrid(int x, int y, boolean isPath) {
|
||||
if (!(x < 0 || x > Config.mapMaxTilesWidth || y < 0 || y > Config.mapMaxTilesHeight)) {
|
||||
GridCell newCell = new GridCell();
|
||||
newCell.setPath(isPath);
|
||||
allGridCells[x][y] = newCell;
|
||||
}
|
||||
}
|
||||
protected static void addGrid(int x, int y, boolean isPath, boolean isTrigger, char correspondingHardwareToTrigger) {
|
||||
if (!(x < 0 || x > Config.mapMaxTilesWidth || y < 0 || y > Config.mapMaxTilesHeight)) {
|
||||
GridCell newCell = new GridCell();
|
||||
newCell.setPath(isPath);
|
||||
newCell.setTrigger(isTrigger);
|
||||
newCell.setCorrespondingHardwareToTrigger(correspondingHardwareToTrigger);
|
||||
allGridCells[x][y] = newCell;
|
||||
}
|
||||
}
|
||||
protected static void addGrid(int x, int y, boolean isTrigger, char correspondingHardwareToTrigger) {
|
||||
if (!(x < 0 || x > Config.mapMaxTilesWidth || y < 0 || y > Config.mapMaxTilesHeight)) {
|
||||
GridCell newCell = new GridCell();
|
||||
newCell.setTrigger(isTrigger);
|
||||
newCell.setCorrespondingHardwareToTrigger(correspondingHardwareToTrigger);
|
||||
allGridCells[x][y] = newCell;
|
||||
}
|
||||
}
|
||||
public static GridCell getGridCell(int x, int y) {
|
||||
if (!(x < 0 || x > Config.mapMaxTilesWidth || y < 0 || y > Config.mapMaxTilesHeight)) {
|
||||
return allGridCells[x][y];
|
||||
}
|
||||
return dummyCell;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import java.awt.Point;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import de.heatup.cfg.Config;
|
||||
import de.heatup.logging.Logger;
|
||||
import de.heatup.objects.StaticObject;
|
||||
|
||||
public class MapObjectHandler {
|
||||
@@ -41,7 +40,7 @@ public class MapObjectHandler {
|
||||
Point currentTriggerTilesFirstPoint = new Point(firstPoint.x-(triggerAreaAddition/2),firstPoint.y-(triggerAreaAddition/2));
|
||||
|
||||
StaticObject newStaticObject = new StaticObject(currentType, firstPoint, currentTilesTotalWidth, currentTilesTotalHeight);
|
||||
// Trigger Object
|
||||
// Trigger Object - auskommentieren sobald implementiert.
|
||||
// StaticObject newTriggerObject = new StaticObject(currentType, firstPoint, currentTilesTotalWidth, currentTilesTotalHeight, true);
|
||||
MapObjectHandler.allMapObjects.add(newStaticObject);
|
||||
// MapObjectHandler.allMapObjects.add(newTriggerObject);
|
||||
|
||||
Reference in New Issue
Block a user