map editor ground work

This commit is contained in:
Makussu 2023-10-18 14:02:22 +02:00
parent cd98f3bc12
commit 22a429cba8

View File

@ -22,11 +22,34 @@ float actualPos;
PVector tilesize = new PVector(50, 50); PVector tilesize = new PVector(50, 50);
int[][] level;
void setup() { void setup() {
size(800,800); size(800,800);
frameRate(60); frameRate(60);
smooth(4); smooth(4);
level = loadJson("../map/map.json");
}
int[][] loadJson(String jsonfile) {
JSONArray values = loadJSONArray(jsonfile);
// println(values.size());
int[][] arrayfromjson = new int[values.size()][values.size()];
for (int i = 0; i < values.size(); i++) {
JSONArray jsontoarray = values.getJSONArray(i);
int x = jsontoarray.getInt(0);
int y = jsontoarray.getInt(1);
// arrayfromjson_x[i] = x;
arrayfromjson[i][0] = x;
arrayfromjson[i][1] = y;
}
return arrayfromjson;
} }
void draw() { void draw() {
@ -42,6 +65,7 @@ void draw() {
// menu(); // menu();
play(); play();
helper(); helper();
// moveTiles();
} }
boolean ismenu; boolean ismenu;
@ -98,8 +122,8 @@ int[] tiles_x = { 300, 50, 600, 50};
int[] tiles_y = { 500, 50, 600, 200 }; int[] tiles_y = { 500, 50, 600, 200 };
void generate_tiles() { void generate_tiles() {
for (int i = 0; i < tiles_x.length; i++ ) { for (int i = 0; i < level.length; i++ ) {
rect(tiles_x[i], tiles_y[i], tilesize.x, tilesize.y); rect(level[i][0], level[i][1], tilesize.x, tilesize.y);
} }
} }
// TODO Collision erweitern wie in https://happycoding.io/tutorials/processing/collision-detection // TODO Collision erweitern wie in https://happycoding.io/tutorials/processing/collision-detection
@ -110,24 +134,24 @@ void collisionTest() {
} }
// Collision with all the tiles bottom site // Collision with all the tiles bottom site
for (int i = 0; i < tiles_x.length; i++) { for (int i = 0; i < level.length; i++) {
boolean xSideEdges = pos.x > tiles_x[i] - tilesize.x/2 && pos.x < tiles_x[i] + tilesize.x/2; boolean xSideEdges = pos.x > level[i][0] - tilesize.x/2 && pos.x < level[i][0] + tilesize.x/2;
boolean ySideEdges = pos.y < tiles_y[i] + tilesize.y/2 && pos.y > tiles_y[i] - tilesize.y/2; boolean ySideEdges = pos.y < level[i][1] + tilesize.y/2 && pos.y > level[i][1] - tilesize.y/2;
// Bottom // Bottom
if ( (xSideEdges) && (ySideEdges)) { if ( (xSideEdges) && (ySideEdges)) {
pos.set(pos.x, tiles_y[i] + tilesize.y/2); pos.set(pos.x, level[i][1] + tilesize.y/2);
} }
// Top // Top
if ( (xSideEdges) && (pos.y < tiles_y[i] + tilesize.y/2 && pos.y > tiles_y[i] - tilesize.y/2 - playerRadius)) { if ( (xSideEdges) && (pos.y < level[i][1] + tilesize.y/2 && pos.y > level[i][1] - tilesize.y/2 - playerRadius)) {
pos.set(pos.x, tiles_y[i] - tilesize.y/2 - 15); pos.set(pos.x, level[i][1] - tilesize.y/2 - 15);
} }
} }
} }
void moveTiles() { void moveTiles() {
for (int i = 0; i < tiles_x.length; i++) { for (int i = 0; i < level.length; i++) {
tiles_x[i] = tiles_x[i] - 1; level[i][0] = level[i][0] - 1;
} }
} }
@ -175,6 +199,9 @@ void keyPressed() {
// pos.y = pos.y + direction.y * speed; // pos.y = pos.y + direction.y * speed;
// //} // //}
// } // }
} else if (key == 'l') {
println(loadJSONArray("../map/map.json"));
} }
} }