Box player, wallA, wallB; //Box bxx; Vector boxes; Vector junk; PFont fontSans; int bg; int up = 1; int down = 2; int left = 3; int right = 4; int lastMove = 0; float wallstep; int max_boxes = 10; int max_junk = 5; int GAMESTATE; int DEAD = 2; int PLAYING = 1; int TITLE = 0; int WIN = 4; void setup() { size(800, 480); frameRate(30); fill(255, 204); noStroke(); fontSans = loadFont("FreeSansBold-64.vlw"); textFont(fontSans, 64); //GAMESTATE = PLAYING; GAMESTATE = TITLE; restart(); // setup level } void draw() { if (GAMESTATE == PLAYING) { inputs(); updateWorld(); } if (GAMESTATE == DEAD) { bg = 128; } background(bg, 0, 0); player.draw(); //bxx.draw(); for (int i = 0; i < boxes.size(); i++) { Box b = (Box) boxes.get(i); b.draw(); } for (int i = 0; i < junk.size(); i++) { Box j = (Box) junk.get(i); j.draw(); } wallA.draw(); wallB.draw(); if (GAMESTATE == DEAD) { // DEAD TEXT fill(color(255,0,0)); text("DEAD", 300+random(-30,30), 220); title_inputs(); } if (GAMESTATE == WIN) { // WIN TEXT fill(color(0,0,255)); text("WIN", 300+random(-30,30), 220); title_inputs(); } if (GAMESTATE == TITLE) { title_inputs(); fill(color(0,128,255)); text("COMPACTOR", 200+random(10),150+random(10)); fill(color(0,random(128,255),255)); text("Press up to start", 150, 300); } } // this collision detection currently returns true if // one corner of a is within b boolean collide(Box a, Box b) { // top left of a within b if ( (a.x >= b.x) && (a.x <= b.x+b.w) && (a.y >= b.y) && (a.y <= b.y+b.h) ) { return true; } // top right of a within b if ( (a.x+a.w >= b.x) && (a.x+a.w <= b.x+b.w) && (a.y >= b.y) && (a.y <= b.y+b.h) ) { return true; } // bottom left of a within b if ( (a.x >= b.x) && (a.x <= b.x+b.w) && (a.y+a.h >= b.y) && (a.y+a.h <= b.y+b.h) ) { return true; } // bottom right of a within b if ( (a.x+a.w >= b.x) && (a.x+a.w <= b.x+b.w) && (a.y+a.h >= b.y) && (a.y+a.h <= b.y+b.h) ) { return true; } return false; } void updateWorld() { wallA.w += wallstep; wallB.x -= wallstep; wallB.w += wallstep; for (int a = 0; a < boxes.size(); a++) { Box bx = (Box) boxes.get(a); // player pushes box around if (collide(bx, player)) { if (player.lastMove == up) { bx.up(); } if (player.lastMove == down) { bx.down(); } if (player.lastMove == left) { bx.left(); } if (player.lastMove == right) { bx.right(); } } // box-wall collisions if (collide(bx, wallA) && collide(bx, wallB)) { wallstep = 0; bx.touchingWall = 3; bx.wallLinked = 3; } if (collide(bx, wallA)) { bx.right(); bx.touchingWall = 1; bx.wallLinked = 1; } if (collide(bx, wallB)) { bx.left(); bx.touchingWall = 2; bx.wallLinked = 2; } // box-box collisions for (int b= 0; b < boxes.size(); b++) { Box bxB = (Box) boxes.get(b); if (collide(bx, bxB)) { // label the colliding box with a tag for the touching wall, if untagged if ( ((bx.touchingWall == 1) || (bx.touchingWall == 2)) && (bxB.wallLinked == 0) ) { bxB.wallLinked = bx.touchingWall; } // if a box tagged or touching the opposite wall is collided, we win if (bx.wallLinked != 0 && bxB.wallLinked != 0) { if ( ((bx.touchingWall + bxB.touchingWall) == 3) || ((bx.touchingWall + bxB.wallLinked) == 3) || ((bx.wallLinked + bxB.touchingWall) == 3) || ((bx.wallLinked + bxB.wallLinked) == 3) ) { // Stop walls, WIN ! // TODO wallstep = 0; GAMESTATE = WIN; } } if (bx.lastMove == up) { bxB.up(); } if (bx.lastMove == down) { bxB.down(); } if (bx.lastMove == left) { bxB.left(); } if (bx.lastMove == right) { bxB.right(); } } } }// close box loop // player wall collisons if (collide(player, wallA) && collide(player, wallB)) { wallstep = 0; player.touchingWall = 3; GAMESTATE = DEAD; } if (collide(player, wallA)) { player.right(); player.touchingWall = 1; } if (collide(player, wallB)) { player.left(); player.touchingWall = 2; } // player-junk collisions for (int j=0; j < junk.size(); j++) { Box jnk = (Box) junk.get(j); if (collide(player, jnk)) { if (player.lastMove == up) { player.down(); } else if (player.lastMove == down) { player.up(); } else if (player.lastMove == left) { player.right(); } else if (player.lastMove == right) { player.left(); } if (player.touchingWall != 0 || player.wallLinked != 0) { GAMESTATE = DEAD; } } } } void title_inputs() { if(keyPressed) { if (key == CODED) { if (keyCode == UP) { GAMESTATE = PLAYING; restart(); } } } } void inputs() { if(keyPressed) { if (key == CODED) { if(keyCode == UP) { player.up(); } if(keyCode == DOWN) { player.down(); } if(keyCode == LEFT) { player.left(); player.touchingWall = 0; player.wallLinked = 0; } if(keyCode == RIGHT) { player.right(); player.touchingWall = 0; player.wallLinked = 0; } } } } void restart() { // setup level color green = color(0,255,0); int player_size = 40; player = new Box(200.0, 200.0, player_size, player_size, green); player.step = 2; //bxx = new Box(250.0, 250.0, 20.0, 20.0, 128); boxes = new Vector(); for (int i = 0; i < max_boxes; i++) { boxes.add(new Box(random(100,700.0), random(100.0, 380.0), random(8.0, 15.0), random(8.0, 15.0), color(128)) ); } junk = new Vector(); for (int i = 0; i < max_junk; i++) { Box piece = new Box(random(100,700.0), random(100.0, 380.0), random(40.0, 100.0), random(40.0, 100.0), color(255) ); if (!(collide(player, piece))) { junk.add(piece); } } wallA = new Box(0.0, 0.0, 0.0, 480.0, color(48)); wallB = new Box(800.0, 0.0, 0.0, 480.0, color(48)); wallstep = 0.25; bg = 0; lastMove = 0; } class Box { float w; // single bar width float x; // rect xposition float h; // rect height float y; // rect yposition color colr; int step = 2; int lastMove; int touchingWall; // 0=none, 1=wallA, 2=wallB, 3=both int wallLinked; // a label for which wall this is being pushed by Box(float ix, float iy, float iw, float ih, color col) { w = iw; x = ix; h = ih; y = iy; colr = col; lastMove = 0; touchingWall = 0; wallLinked = 0; } void draw() { fill(colr); rect(x, y, w, h); resetHistory(); } void resetHistory() { lastMove = 0; } void up() { y = constrain((y - step), 0, 480-h); lastMove = up; } void down() { y = constrain((y + step), 0, 480-h); lastMove = down; } void left() { x = constrain((x - step), 0, 800-w); lastMove = left; } void right() { x = constrain((x + step), 0, 800-w); lastMove = right; } }