Advertisement
MrMusAddict

House Builder

Mar 21st, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.11 KB | None | 0 0
  1. ////// Main Tab //////
  2. Wall w;
  3. Entity e;
  4.  
  5. void setup() {
  6.   size(600, 600);
  7.   w = new Wall(-200, -100);
  8.   e = new Entity();
  9. }
  10.  
  11. void draw() {
  12.   background(51);
  13.   translate(width/2, height/2);
  14.   w.show();
  15.   e.show();
  16. }
  17.  
  18.  
  19.  
  20.  
  21.  
  22. ////// Brick Tab //////
  23. class Brick {
  24.   PVector pos; //where the brick currently is
  25.   PVector home; //where the brick needs to go
  26.  
  27.   boolean isHome = false; //once the entity changes the position to match home, set to true
  28.  
  29.   Brick(float x, float y) {
  30.     pos = new PVector(random(width)-width/2, random(height) - height/2); //spawns the brick at a random location
  31.     home = new PVector(x, y); //sets the XY coordinates of its home per the constructor inputs
  32.   }
  33.  
  34.   void show() {
  35.     rect(pos.x, pos.y, 40, 20);
  36.   }
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43. ////// Entity Tab //////
  44. class Entity {
  45.   int state = 0; // 0 = getting new block, 1 = moving to block, 2 = carrying block, 3 = task complete
  46.  
  47.   PVector pos;
  48.  
  49.   Brick tBrick; //the "target brick" we're currently focusing on
  50.   PVector tPos; //the "target position" we're currently moving to (could be the brick pos, or the brick home)
  51.  
  52.   float speed = 10;
  53.  
  54.   Entity() {
  55.     pos = new PVector(0, 0);
  56.   }
  57.  
  58.   void show() {
  59.     update();
  60.     ellipse(pos.x, pos.y, 20, 20);
  61.     text(state, -width/2 + 20, -height/2 + 20);
  62.   }
  63.  
  64.   void update() {
  65.     //if state = 0, start walking to the next brick (state 1)
  66.     //if next brick is null, then we're done. Switch to state 3
  67.     if (state == 0) {
  68.       tBrick = w.getNextBrick();
  69.       if (tBrick == null) {
  70.         state = 3;
  71.       } else {
  72.         state = 1;
  73.         tPos = new PVector(tBrick.pos.x, tBrick.pos.y);
  74.       }
  75.     }
  76.    
  77.     //if state = 2, increment your position towards the brick's 'home' by a distance of 'speed'
  78.     //if distance is less than 'speed', then arrive at 'home', and switch to state 0
  79.     //pull the brick back with you (update the brick position)
  80.     if(state == 2){
  81.       PVector move = new PVector(tPos.x - pos.x, tPos.y - pos.y);
  82.      
  83.       if(move.mag() < speed){
  84.         pos.x = tPos.x;
  85.         pos.y = tPos.y;
  86.         state = 0;
  87.         tBrick.isHome = true;
  88.       }else{
  89.         move.normalize().mult(speed);
  90.         pos.add(move);
  91.       }
  92.       tBrick.pos.x = pos.x;
  93.       tBrick.pos.y = pos.y;
  94.     }
  95.    
  96.     //if state = 1, increment your position towards the brick's position by a distance of 'speed'
  97.     //if distance is less than 'speed', then arrive at the brick, and switch to state 2
  98.     if(state == 1){
  99.       PVector move = new PVector(tPos.x - pos.x, tPos.y - pos.y);
  100.      
  101.       if(move.mag() < speed){
  102.         pos.x = tPos.x;
  103.         pos.y = tPos.y;
  104.         state = 2;
  105.         tPos = new PVector(tBrick.home.x, tBrick.home.y);
  106.       }else{
  107.         move.normalize().mult(speed);
  108.         pos.add(move);
  109.       }
  110.     }
  111.    
  112.   }
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119. ////// Wall Tab //////
  120. //these are effectively boolean arrays (0 vs 1)
  121. //which tells us where the bricks go in each row
  122. int[] roof1 = {0, 0, 0, 0, 1, 1, 0, 0, 1, 0};
  123. int[] roof2 = {0, 0, 0, 1, 1, 1, 1, 0, 1, 0};
  124. int[] roof3 = {0, 0, 1, 1, 1, 1, 1, 1, 1, 0};
  125. int[] roof4 = {0, 1, 1, 1, 1, 1, 1, 1, 1, 0};
  126. int[] fullRow = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  127. int[] doorWindow = {1, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  128. int[] doorRow = {1, 0, 0, 0, 1, 1, 1, 1, 1, 1};
  129.  
  130.  
  131. class Wall {
  132.   BrickRow[] bRows; //the full wall has an array of rows of bricks
  133.   PVector pos;
  134.  
  135.   Wall(float x, float y) {
  136.     pos = new PVector(x, y); //set position per copnstructor inputs
  137.    
  138.     bRows = new BrickRow[14]; //initialize array of brick rows
  139.    
  140.     //for each row, create a new row with the defined pattern (roof1, roof2, etc)
  141.     //y + multiples of 20 is to account for the brick height of 20
  142.     bRows[0] = new BrickRow(x, y, roof1);
  143.     bRows[1] = new BrickRow(x, y+20, roof2);
  144.     bRows[2] = new BrickRow(x, y+40, roof3);
  145.     bRows[3] = new BrickRow(x, y+60, roof4);
  146.     for (int i = 4; i < 6; i++) {
  147.       bRows[i] = new BrickRow(x, y + i*20, fullRow);
  148.     }
  149.     for (int i = 6; i < 10; i++) {
  150.       bRows[i] = new BrickRow(x, y + i*20, doorWindow);
  151.     }
  152.     for (int i = 10; i < 14; i++) {
  153.       bRows[i] = new BrickRow(x, y + i*20, doorRow);
  154.     }
  155.   }
  156.  
  157.   //show each brick row
  158.   void show() {
  159.     for (int i = 0; i < bRows.length; i++) {
  160.       bRows[i].show();
  161.     }
  162.   }
  163.  
  164.   //cycle through each brick row to verify if it's completed.
  165.   //If not, return the next brick that needs to be laid
  166.   Brick getNextBrick() {
  167.     Brick temp = null;
  168.  
  169.     for (int i = bRows.length - 1; i >= 0; i--) {
  170.       if (!bRows[i].isComplete()) {
  171.         return bRows[i].getNextBrick(); //separate function in the BrickRow class
  172.       }
  173.     }
  174.  
  175.     return temp;
  176.   }
  177. }
  178.  
  179.  
  180. class BrickRow {
  181.   Brick[] bricks; //each row of bricks contains an array of bricks
  182.   PVector pos;
  183.  
  184.   //constructor calls for an XY position, and a int boolean pattern (1 vs 0)
  185.   BrickRow(float x, float y, int[] b) {
  186.     //initialize the array of bricks
  187.     bricks = new Brick[b.length];
  188.    
  189.     //set the row's position per the XY values in the constructor call
  190.     pos = new PVector(x, y);
  191.    
  192.     //for each index in the int[] pattern
  193.     //add a brick if it's a 1
  194.     //keep the brick null if it's a 0
  195.     for (int i = 0; i < b.length; i++) {
  196.       if (b[i] == 0) {
  197.         bricks[i] = null;
  198.       } else {
  199.         bricks[i] = new Brick(pos.x + i*40, pos.y);
  200.       }
  201.     }
  202.   }
  203.  
  204.   //show all bricks
  205.   void show() {
  206.     for (int i = 0; i < bricks.length; i++) {
  207.       if (bricks[i] != null) {
  208.         bricks[i].show();
  209.       }
  210.     }
  211.   }
  212.  
  213.   //checks all non-null bricks
  214.   //if any of them aren't "home", return false
  215.   boolean isComplete() {
  216.     boolean temp = true;
  217.  
  218.     for (int i = 0; i < bricks.length; i++) {
  219.       if (bricks[i] != null) {
  220.         if (!bricks[i].isHome) temp = false;
  221.       }
  222.     }
  223.  
  224.     return temp;
  225.   }
  226.  
  227.   //get the first brick in the array that is not null, and "isHome" is false
  228.   Brick getNextBrick() {
  229.     Brick temp = null;
  230.  
  231.     for (int i = 0; i < bricks.length; i++) {
  232.       if (bricks[i] != null) {
  233.         if (!bricks[i].isHome) return bricks[i];
  234.       }
  235.     }
  236.  
  237.     return temp;
  238.   }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement