SHARE
TWEET

Level-File Creator

Explosiontime202 May 27th, 2020 759 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.List;
  2.  
  3.  
  4. final int rectSize = 200;
  5. final int[] xArray = {0, 1, 2, 3, 0, 0};
  6. final int[] yArray = {1, 0, 0, 0, 2, 3};
  7. final int[] xSolution = {1, 2, 3, 1, 2, 1};
  8. final int[] ySolution = {1, 1, 1, 2, 2, 3};
  9. final boolean[] turns = {false, true, true, true, false, false};
  10. final int[] indizes = {0, 1, 2, 3, 3, 2};
  11. TextField[][] textFields;
  12. final float offsetX = 200;
  13. final float offsetY = 200;
  14. String[][] texts;
  15. int fileCounter = 0;
  16. SaveFileButton button;
  17. SolutionsBlock[][] solutionBlocks;
  18. int[][] solution;
  19. LoadLevelInput loadLevel;
  20. void setup() {
  21.   fullScreen();
  22.   texts = new String[4][5];
  23.   for (int i=0; i<texts.length; i++) {
  24.     for (int j=0; j<texts[i].length; j++) {
  25.       texts[i][j] = "";
  26.     }
  27.   }
  28.   textFields = new TextField[6][5];
  29.   for (int i=0; i<textFields.length; i++) {
  30.     for (int j=0; j<textFields[i].length; j++) {
  31.       textFields[i][j] = new TextField(rectSize * (xArray[i] + (turns[i] ? 0.2 * j : 0)), rectSize * (yArray[i] +(turns[i] ? 0 : 0.2 * j )), rectSize, turns[i], indizes[i], j, i, j);
  32.     }
  33.   }
  34.   textSize(25);
  35.   button = new SaveFileButton(width * 0.75, height/2, 50);
  36.   solutionBlocks = new SolutionsBlock[6][25];
  37.   for (int i=0; i<solutionBlocks.length; i++) {
  38.     for (int j=0; j<solutionBlocks[i].length; j++) {
  39.       solutionBlocks[i][j] = new SolutionsBlock((xSolution[i]+0.2*(j%5))*rectSize, (ySolution[i]+0.2*floor(j/5))*rectSize, rectSize/5, rectSize/5, i, j);
  40.     }
  41.   }
  42.   solution = new int[6][25];
  43.   loadLevel = new LoadLevelInput(width *0.75, height/3);
  44. }
  45.  
  46. void draw() {
  47.   background(255);
  48.   pushMatrix();
  49.   translate(offsetX, offsetY);
  50.   for (int x=0; x<4; x++) {
  51.     for (int y=0; y<4; y++) {
  52.       strokeWeight(2);
  53.       fill(255);
  54.       rect(rectSize*x, rectSize * y, rectSize, rectSize);
  55.     }
  56.   }
  57.   for (int i=0; i<textFields.length; i++) {
  58.     for (int j=0; j<textFields[i].length; j++) {
  59.       textFields[i][j].draw();
  60.     }
  61.   }
  62.   for (int i=0; i<solutionBlocks.length; i++) {
  63.     for (int j=0; j<solutionBlocks[i].length; j++) {
  64.       solutionBlocks[i][j].draw();
  65.     }
  66.   }
  67.   popMatrix();
  68.   button.draw();
  69.   loadLevel.draw();
  70. }
  71.  
  72.  
  73. class TextField {
  74.   float x;
  75.   float y;
  76.   float h;
  77.   float b;
  78.   boolean active;
  79.   int index1;
  80.   int index2;
  81.   final boolean turn;
  82.   final int[] otherIndex;
  83.   TextField(float x, float y, float rSize, boolean turn, int index1, int index2, int otherIndex1, int otherIndex2) {
  84.     this.x = x;
  85.     this.y = y;
  86.     this.h = turn ? rSize : rSize /5;
  87.     this.b = turn ? rSize / 5 : rSize ;
  88.     this.index1 = index1;
  89.     this.index2 = index2;
  90.     this.turn = turn;
  91.     otherIndex= new int[]{otherIndex1, otherIndex2};
  92.   }
  93.   void mousePressed(float mx, float my) {
  94.     if (mx > x && mx < x + b && my > y && my < y + h) {
  95.       active = true;
  96.       nowIamActive(otherIndex[0], otherIndex[1]);
  97.     }
  98.   }
  99.   void draw() {
  100.     fill(255);
  101.     rect(x, y, b, h);
  102.     textAlign(CENTER, CENTER);
  103.     fill(0);
  104.     pushMatrix();
  105.     translate( x + b/2, y + h/2);
  106.     rotate(radians((turn) ? 270 : 0));
  107.     text(texts[index1][index2], 0, 0);
  108.     popMatrix();
  109.   }
  110.   void keyPressed(char k, int code) {
  111.     if (active) {
  112.       switch(k) {
  113.       case BACKSPACE:
  114.         if (texts[index1][index2].length() > 0) {
  115.           texts[index1][index2] = texts[index1][index2].substring(0, texts[index1][index2].length()-1);
  116.         }
  117.         return;
  118.       case SHIFT:
  119.         return;
  120.       }
  121.       if (key == CODED) return;
  122.       texts[index1][index2] += k;
  123.     }
  124.   }
  125.   void setActive(boolean active) {
  126.     this.active = active;
  127.   }
  128. }
  129.  
  130. class SaveFileButton {
  131.   float x;
  132.   float y;
  133.   float r;
  134.   SaveFileButton(float x, float y, float r) {
  135.     this.x = x;
  136.     this.y = y;
  137.     this.r = r;
  138.   }
  139.   void draw() {
  140.     fill(color(255, 0, 0));
  141.     ellipse(x, y, r*2, r*2);
  142.   }
  143.   void mousePressed(float mx, float my) {
  144.     PVector v = new PVector(mx - x, my - y);
  145.     if (v.mag() < r) {
  146.       generateJSON();
  147.     }
  148.   }
  149. }
  150.  
  151. class SolutionsBlock {
  152.   float x;
  153.   float y;
  154.   float b;
  155.   float h;
  156.   int index1;
  157.   int index2;
  158.   SolutionsBlock(float x, float y, float b, float h, int index1, int index2) {
  159.     this.x = x;
  160.     this.y = y;
  161.     this.b = b;
  162.     this.h = h;
  163.     this.index1 = index1;
  164.     this.index2 = index2;
  165.   }
  166.   void draw() {
  167.     int status = solution[index1][index2];
  168.     fill(255);
  169.     rect(x, y, b, h);
  170.     fill((status == 1) ? color (255, 0, 0) : (status == 2 ? color(0, 0, 255) : 255));
  171.     textAlign(CENTER, CENTER);
  172.     text(status == 1 ? "X": (status == 2 ? "O": " "), x+b/2, y+h/2);
  173.   }
  174.   void mousePressed(float mx, float my) {
  175.     if (mx > x && mx < x + b && my > y && my < y + h) {
  176.       solution[index1][index2]++;
  177.       solution[index1][index2] %= 3;
  178.     }
  179.   }
  180. }
  181.  
  182. class LoadLevelInput {
  183.   float x;
  184.   float y;
  185.   float b;
  186.   float h;
  187.   float r;
  188.   boolean active;
  189.   String text;
  190.   LoadLevelInput(float x, float y) {
  191.     this.x = x;
  192.     this.y = y;
  193.     this.b = 300;
  194.     this.h = 50;
  195.     this.r = 25;
  196.     text = "";
  197.   }
  198.   void draw() {
  199.     fill(255);
  200.     rect(x, y, b, h);
  201.     textAlign(CENTER, CENTER);
  202.     fill(0);
  203.     text(text, x+b/2, y+ h/2);
  204.     fill(0, 255, 0);
  205.     ellipse(x+b*1.25, y + h/2, r*2, r*2);
  206.   }
  207.   void mousePressed(float mx, float my) {
  208.     if (mx > x && mx < x + b && my > y && my < y + h) {
  209.       active = true;
  210.       nowIamActive(-1, -1);
  211.     } else {
  212.       PVector dis = new PVector(mx-x-1.25*b, my-y);
  213.       if (dis.mag() < r) {
  214.         loadLevel(text);
  215.       }
  216.     }
  217.   }
  218.   void keyPressed(char k, int code) {
  219.     if (active) {
  220.       switch(k) {
  221.       case BACKSPACE:
  222.         if (text.length() > 0) {
  223.           text = text.substring(0, text.length()-1);
  224.         }
  225.         return;
  226.       case SHIFT:
  227.         return;
  228.       }
  229.       if (key == CODED) return;
  230.       text += k;
  231.     }
  232.   }
  233.   void setActive(boolean active) {
  234.     this.active = active;
  235.   }
  236.   void loadLevel(String input) {
  237.     String levelPath = "output/" + input;
  238.     try {
  239.       JSONObject in = loadJSONObject(levelPath);
  240.       JSONArray sol =  in.getJSONArray("solution");
  241.       int[][] newSolution = new int[solution.length][solution[0].length];
  242.       for (int i=0; i<newSolution.length; i++) {
  243.         JSONArray s = sol.getJSONArray(i);
  244.         newSolution[i] = s.getIntArray();
  245.       }
  246.       for (int i=0; i<newSolution.length; i++) {
  247.         for (int j=0; j<newSolution[i].length; j++) {
  248.           newSolution[i][j]++;
  249.         }
  250.       }
  251.       solution = newSolution;
  252.       JSONArray tex =  in.getJSONArray("items");
  253.       String[][] newTexts = new String[texts.length][texts[0].length];
  254.       for (int i=0; i<newTexts.length; i++) {
  255.         JSONArray t = tex.getJSONArray(i);
  256.         newTexts[i] = t.getStringArray();
  257.       }
  258.       texts = newTexts;
  259.     }
  260.     catch(Exception e) {
  261.       e.printStackTrace();
  262.     }
  263.   }
  264. }
  265.  
  266. void mousePressed() {
  267.   for (int i=0; i<textFields.length; i++) {
  268.     for (int j=0; j<textFields[i].length; j++) {
  269.       textFields[i][j].mousePressed(mouseX-offsetX, mouseY-offsetY);
  270.     }
  271.   }
  272.   for (int i=0; i<solutionBlocks.length; i++) {
  273.     for (int j=0; j<solutionBlocks[i].length; j++) {
  274.       solutionBlocks[i][j].mousePressed(mouseX - offsetX, mouseY - offsetY);
  275.     }
  276.   }
  277.   button.mousePressed(mouseX, mouseY);
  278.   loadLevel.mousePressed(mouseX, mouseY);
  279. }
  280. void keyPressed() {
  281.   for (int i=0; i<textFields.length; i++) {
  282.     for (int j=0; j<textFields[i].length; j++) {
  283.       textFields[i][j].keyPressed(key, keyCode);
  284.     }
  285.   }
  286.   loadLevel.keyPressed(key, keyCode);
  287. }
  288.  
  289.  
  290. void nowIamActive(int i1, int i2) {
  291.   for (int i=0; i<textFields.length; i++) {
  292.     for (int j=0; j<textFields[i].length; j++) {
  293.       if (!(i == i1 && j == i2)) {
  294.         textFields[i][j].setActive(false);
  295.       }
  296.     }
  297.   }
  298.   if (!(i1 == -1 && i2 == -1)) {
  299.     loadLevel.setActive(false);
  300.   }
  301. }
  302.  
  303.  
  304. void generateJSON() {
  305.   String jsonFileName = "output/" + str(fileCounter) + ".json";
  306.   JSONObject level = new JSONObject();
  307.   level.put("items", texts);
  308.   int[][] sol = solution.clone();
  309.   for (int i=0; i<sol.length; i++) {
  310.     for (int j=0; j<sol[i].length; j++) {
  311.       sol[i][j] -= 1;
  312.     }
  313.   }
  314.   level.put("solution", solution);
  315.   saveJSONObject(level, jsonFileName);
  316.   println("JSON GENERATED!  fileName: " + jsonFileName);
  317.   fileCounter++;
  318. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top