Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////// Main Tab //////
- Wall w;
- Entity e;
- void setup() {
- size(600, 600);
- w = new Wall(-200, -100);
- e = new Entity();
- }
- void draw() {
- background(51);
- translate(width/2, height/2);
- w.show();
- e.show();
- }
- ////// Brick Tab //////
- class Brick {
- PVector pos; //where the brick currently is
- PVector home; //where the brick needs to go
- boolean isHome = false; //once the entity changes the position to match home, set to true
- Brick(float x, float y) {
- pos = new PVector(random(width)-width/2, random(height) - height/2); //spawns the brick at a random location
- home = new PVector(x, y); //sets the XY coordinates of its home per the constructor inputs
- }
- void show() {
- rect(pos.x, pos.y, 40, 20);
- }
- }
- ////// Entity Tab //////
- class Entity {
- int state = 0; // 0 = getting new block, 1 = moving to block, 2 = carrying block, 3 = task complete
- PVector pos;
- Brick tBrick; //the "target brick" we're currently focusing on
- PVector tPos; //the "target position" we're currently moving to (could be the brick pos, or the brick home)
- float speed = 10;
- Entity() {
- pos = new PVector(0, 0);
- }
- void show() {
- update();
- ellipse(pos.x, pos.y, 20, 20);
- text(state, -width/2 + 20, -height/2 + 20);
- }
- void update() {
- //if state = 0, start walking to the next brick (state 1)
- //if next brick is null, then we're done. Switch to state 3
- if (state == 0) {
- tBrick = w.getNextBrick();
- if (tBrick == null) {
- state = 3;
- } else {
- state = 1;
- tPos = new PVector(tBrick.pos.x, tBrick.pos.y);
- }
- }
- //if state = 2, increment your position towards the brick's 'home' by a distance of 'speed'
- //if distance is less than 'speed', then arrive at 'home', and switch to state 0
- //pull the brick back with you (update the brick position)
- if(state == 2){
- PVector move = new PVector(tPos.x - pos.x, tPos.y - pos.y);
- if(move.mag() < speed){
- pos.x = tPos.x;
- pos.y = tPos.y;
- state = 0;
- tBrick.isHome = true;
- }else{
- move.normalize().mult(speed);
- pos.add(move);
- }
- tBrick.pos.x = pos.x;
- tBrick.pos.y = pos.y;
- }
- //if state = 1, increment your position towards the brick's position by a distance of 'speed'
- //if distance is less than 'speed', then arrive at the brick, and switch to state 2
- if(state == 1){
- PVector move = new PVector(tPos.x - pos.x, tPos.y - pos.y);
- if(move.mag() < speed){
- pos.x = tPos.x;
- pos.y = tPos.y;
- state = 2;
- tPos = new PVector(tBrick.home.x, tBrick.home.y);
- }else{
- move.normalize().mult(speed);
- pos.add(move);
- }
- }
- }
- }
- ////// Wall Tab //////
- //these are effectively boolean arrays (0 vs 1)
- //which tells us where the bricks go in each row
- int[] roof1 = {0, 0, 0, 0, 1, 1, 0, 0, 1, 0};
- int[] roof2 = {0, 0, 0, 1, 1, 1, 1, 0, 1, 0};
- int[] roof3 = {0, 0, 1, 1, 1, 1, 1, 1, 1, 0};
- int[] roof4 = {0, 1, 1, 1, 1, 1, 1, 1, 1, 0};
- int[] fullRow = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
- int[] doorWindow = {1, 0, 0, 0, 1, 0, 0, 0, 0, 1};
- int[] doorRow = {1, 0, 0, 0, 1, 1, 1, 1, 1, 1};
- class Wall {
- BrickRow[] bRows; //the full wall has an array of rows of bricks
- PVector pos;
- Wall(float x, float y) {
- pos = new PVector(x, y); //set position per copnstructor inputs
- bRows = new BrickRow[14]; //initialize array of brick rows
- //for each row, create a new row with the defined pattern (roof1, roof2, etc)
- //y + multiples of 20 is to account for the brick height of 20
- bRows[0] = new BrickRow(x, y, roof1);
- bRows[1] = new BrickRow(x, y+20, roof2);
- bRows[2] = new BrickRow(x, y+40, roof3);
- bRows[3] = new BrickRow(x, y+60, roof4);
- for (int i = 4; i < 6; i++) {
- bRows[i] = new BrickRow(x, y + i*20, fullRow);
- }
- for (int i = 6; i < 10; i++) {
- bRows[i] = new BrickRow(x, y + i*20, doorWindow);
- }
- for (int i = 10; i < 14; i++) {
- bRows[i] = new BrickRow(x, y + i*20, doorRow);
- }
- }
- //show each brick row
- void show() {
- for (int i = 0; i < bRows.length; i++) {
- bRows[i].show();
- }
- }
- //cycle through each brick row to verify if it's completed.
- //If not, return the next brick that needs to be laid
- Brick getNextBrick() {
- Brick temp = null;
- for (int i = bRows.length - 1; i >= 0; i--) {
- if (!bRows[i].isComplete()) {
- return bRows[i].getNextBrick(); //separate function in the BrickRow class
- }
- }
- return temp;
- }
- }
- class BrickRow {
- Brick[] bricks; //each row of bricks contains an array of bricks
- PVector pos;
- //constructor calls for an XY position, and a int boolean pattern (1 vs 0)
- BrickRow(float x, float y, int[] b) {
- //initialize the array of bricks
- bricks = new Brick[b.length];
- //set the row's position per the XY values in the constructor call
- pos = new PVector(x, y);
- //for each index in the int[] pattern
- //add a brick if it's a 1
- //keep the brick null if it's a 0
- for (int i = 0; i < b.length; i++) {
- if (b[i] == 0) {
- bricks[i] = null;
- } else {
- bricks[i] = new Brick(pos.x + i*40, pos.y);
- }
- }
- }
- //show all bricks
- void show() {
- for (int i = 0; i < bricks.length; i++) {
- if (bricks[i] != null) {
- bricks[i].show();
- }
- }
- }
- //checks all non-null bricks
- //if any of them aren't "home", return false
- boolean isComplete() {
- boolean temp = true;
- for (int i = 0; i < bricks.length; i++) {
- if (bricks[i] != null) {
- if (!bricks[i].isHome) temp = false;
- }
- }
- return temp;
- }
- //get the first brick in the array that is not null, and "isHome" is false
- Brick getNextBrick() {
- Brick temp = null;
- for (int i = 0; i < bricks.length; i++) {
- if (bricks[i] != null) {
- if (!bricks[i].isHome) return bricks[i];
- }
- }
- return temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement