map editor now integrated in main menu

This commit is contained in:
Makussu 2023-10-18 23:25:20 +02:00
parent 22a429cba8
commit 53b9f7b136
6 changed files with 1380 additions and 67 deletions

View File

@ -24,15 +24,6 @@ PVector tilesize = new PVector(50, 50);
int[][] level;
void setup() {
size(800,800);
frameRate(60);
smooth(4);
level = loadJson("../map/map.json");
}
int[][] loadJson(String jsonfile) {
JSONArray values = loadJSONArray(jsonfile);
// println(values.size());
@ -44,31 +35,19 @@ int[][] loadJson(String jsonfile) {
int x = jsontoarray.getInt(0);
int y = jsontoarray.getInt(1);
int rectwidth = jsontoarray.getInt(2);
int rectheight = jsontoarray.getInt(3);
// arrayfromjson_x[i] = x;
arrayfromjson[i][0] = x;
arrayfromjson[i][1] = y;
arrayfromjson[i][2] = rectwidth;
arrayfromjson[i][3] = rectheight;
}
return arrayfromjson;
}
void draw() {
// rectMode(CENTER);
// rect(50, 50, 100, 50);
// textAlign(CENTER);
// fill(0);
// text("test", 50, 50);
// noFill();
// menu();
play();
helper();
// moveTiles();
}
boolean ismenu;
void menu() {
rectWithText(width/2, height/2, 100, 50, "test", 40);
}
@ -90,8 +69,6 @@ void rectWithText(float rectX, float rectY, float rectWidth, float rectHeight, S
}
void mouseClicked() {
}
// The actual game
void play() {
@ -104,6 +81,7 @@ void play() {
rectMode(CENTER);
rect(pos.x, pos.y, playerRadius, playerRadius);
moveTiles();
baseline();
// jumpToJumpheight();
newJump();
@ -121,11 +99,6 @@ void play() {
int[] tiles_x = { 300, 50, 600, 50};
int[] tiles_y = { 500, 50, 600, 200 };
void generate_tiles() {
for (int i = 0; i < level.length; i++ ) {
rect(level[i][0], level[i][1], tilesize.x, tilesize.y);
}
}
// TODO Collision erweitern wie in https://happycoding.io/tutorials/processing/collision-detection
void collisionTest() {
// Collision with baseline
@ -151,7 +124,7 @@ void collisionTest() {
void moveTiles() {
for (int i = 0; i < level.length; i++) {
level[i][0] = level[i][0] - 1;
level[i][0] = level[i][0] - 3;
}
}
@ -170,40 +143,6 @@ void newJump() {
}
}
// actions for moving
void keyPressed() {
if (key == CODED)
{
if (keyCode == LEFT)
{
direction.x = -1;
pos.x = pos.x + direction.x * speed;
}
else if (keyCode == RIGHT)
{
//if (directionX<0) {
direction.x = +1;
pos.x = pos.x + direction.x * speed;
//}
}
if (keyCode == UP)
{
//if (directionY<0) {
jumpspeed =+ 25;
//}
}
// else if (keyCode == DOWN)
// {
// //if (directionY<0) {
// direction.y = +1;
// pos.y = pos.y + direction.y * speed;
// //}
// }
} else if (key == 'l') {
println(loadJSONArray("../map/map.json"));
}
}
// Neat helper function, that gives us coordinates of point while clicking
void helper() {

34
game/keys.pde Normal file
View File

@ -0,0 +1,34 @@
// } else if (key == 'l') {
// println(loadJSONArray("../map/map.json"));
void keyPressed () {
if (ismenu) {
} else if (isgame) {
if (key == CODED) {
if (keyCode == RIGHT) {
direction.x = +1;
pos.x = pos.x + direction.x * speed;
} else if (keyCode == LEFT) {
direction.x = -1;
pos.x = pos.x + direction.x * speed;
}
} else if (key == ' ') {
jumpspeed =+ 25;
}
} else if (iseditor) {
if (key == 's') {
savetofile("save");
println("saved");
} else if (key == CODED) {
if (keyCode == RIGHT) {
screenx = screenx + 50;
println("moved right!");
println(screenx);
} else if (keyCode == LEFT) {
screenx = screenx - 50;
println("moved right!");
println(screenx);
}
}
}
}

77
game/main.pde Normal file
View File

@ -0,0 +1,77 @@
void setup() {
size(800,800);
frameRate(60);
smooth(4);
// saveFileChecker();
level = loadJson("./saves/save.json");
}
void saveFileChecker() {
File f = dataFile("./saves/save.json");
String filePath = f.getPath();
boolean exist = f.isFile();
println(filePath, exist);
if (!exist) {
}
};
void draw() {
// rectMode(CENTER);
// rect(50, 50, 100, 50);
// textAlign(CENTER);
// fill(0);
// text("test", 50, 50);
// noFill();
// play();
// helper();
if (ismenu) {
mainMenu();
} else if (isgame) {
play();
} else if (iseditor) {
mapEditor();
}
}
boolean ismenu = true;
boolean isgame = false;
boolean iseditor = false;
void mainMenu() {
if (drawRectWithColission(width/2, height/2, 100, 100)) {
if (mousePressed) {
ismenu = false;
isgame = true;
}
}
if (drawRectWithColission(width/2, height/2+200, 100, 100)) {
if (mousePressed) {
ismenu = false;
iseditor = true;
}
}
}
void mapEditor() {
background(255);
generate_tiles();
}
boolean drawRectWithColission(int x, int y, int rectwidth, int rectheight) {
// Draw A Rect
rectMode(CENTER);
rect(x, y, rectwidth, rectheight);
// Check it for collission
if((mouseX > x - 2/rectwidth && mouseX < x + rectwidth/2) && (mouseY > y - rectheight/2 && mouseY < y + rectheight/2)) {
return true;
} else {
return false;
}
}

59
game/map.pde Normal file
View File

@ -0,0 +1,59 @@
void generate_tiles() {
for (int i = 0; i < level.length; i++ ) {
rect(level[i][0]-screenx, level[i][1], level[i][2], level[i][3]);
}
}
int runnerRect = 0;
void mouseReleased() {
if(iseditor) {
level[runnerRect][0] = mouseX+screenx;
level[runnerRect][1] = mouseY;
level[runnerRect][2] = 50;
level[runnerRect][3] = 50;
println("generated");
runnerRect = runnerRect + 1;
println(runnerRect);
}
}
// TODO Check if json is empty. If it is generate level
// int[][] level = new int[100][4];
JSONArray json;
int savetofile(String filename) {
json = new JSONArray();
for (int i = 0; i < level.length; i++) {
JSONArray mapsave = new JSONArray();
mapsave.setInt(0, level[i][0]);
mapsave.setInt(1, level[i][1]);
mapsave.setInt(2, level[i][2]);
mapsave.setInt(3, level[i][3]);
json.setJSONArray(i, mapsave);
}
saveJSONArray(json,"./saves/" + filename + ".json");
return 1;
}
int screenx = 0;
// void keyPressed() {
// if (key == 's') {
// println("saved");
// savetofile("save");
// } else if (key == CODED) {
// if (keyCode == RIGHT) {
// screenx = screenx + 50;
// println("moved right!");
// println(screenx);
// } else if (keyCode == LEFT) {
// screenx = screenx - 50;
// println("moved right!");
// println(screenx);
// }
// }
// }

602
game/saves/save.json Normal file
View File

@ -0,0 +1,602 @@
[
[
405,
595,
50,
50
],
[
823,
337,
50,
50
],
[
1002,
560,
50,
50
],
[
885,
678,
50,
50
],
[
1027,
338,
50,
50
],
[
929,
686,
50,
50
],
[
925,
322,
50,
50
],
[
1024,
717,
50,
50
],
[
927,
344,
50,
50
],
[
1013,
564,
50,
50
],
[
825,
463,
50,
50
],
[
1012,
454,
50,
50
],
[
1622,
495,
50,
50
],
[
1687,
130,
50,
50
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
]
]

602
map/save.json Normal file
View File

@ -0,0 +1,602 @@
[
[
132,
131,
50,
50
],
[
307,
381,
50,
50
],
[
582,
155,
50,
50
],
[
568,
582,
50,
50
],
[
120,
440,
50,
50
],
[
624,
283,
50,
50
],
[
517,
562,
50,
50
],
[
1174,
145,
50,
50
],
[
1126,
492,
50,
50
],
[
858,
596,
50,
50
],
[
963,
201,
50,
50
],
[
1342,
589,
50,
50
],
[
1622,
495,
50,
50
],
[
1687,
130,
50,
50
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
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