class Floor { float slide_val = 0; float one_tile = 0; float tileRes; float ratio; float x; float y; float ptx = 0; float pty = 0; int w; int h; Tile[][] tiles; PImage[] textures; Crap stomach[] = new Crap[0]; int stomachSize = 3; Orgo player; int NORTH = 0; int EAST = 1; int SOUTH = 2; int WEST = 3; Floor(float ix, float iy, int iw, int ih, float itr, float ir) { x = ix; y = iy; w = iw; h = ih; setTileRes(itr); setRatio(ir); tiles = new Tile[h][w]; textures = new PImage[0]; player = new Orgo(tileRes, ratio); player.x_targ = 3; player.y_targ = 3; player.x_index = 3; player.y_index = 3; player.x_off = 0; player.y_off = 0; } void setTileRes(float itr) { tileRes = itr; updateOT(); } void setRatio(float ir) { ratio = ir; updateOT(); } void updateOT() { one_tile = sqrt(pow(tileRes / 2,2) + pow(ratio * tileRes / 2, 2)); } void display() { noFill(); stroke(0, 250, 0); strokeWeight(.5); pushMatrix(); float slide_off = sign(slide_val) * sqrt((slide_val * slide_val) / (ratio * ratio + 1)); translate(x + slide_off, y + ratio * slide_off); for(int i = 0; i < h; i++) { for(int u = w - 1; u >= 0; u--) { pushMatrix(); translate((u * tileRes / 2) + (i * tileRes / 2), -(u * tileRes * ratio / 2) + (i * tileRes * ratio / 2)); PImage im = textures[0]; if(tiles[i][u].tile != -1) im = textures[tiles[i][u].tile]; tiles[i][u].display(im); popMatrix(); if(player.x_index == i && player.y_index == u) drawPlayer(); } } popMatrix(); pushMatrix(); translate(width - 50, 50); for(int i = 0; i < stomach.length; i++) { if(stomach[i].displayTimed()) { poop(i); } translate(0, 40); } popMatrix(); } void drawPlayer() { if(player.x_targ != player.x_index || player.y_targ != player.y_index) { float[] p_ind = getTileLoc(player.x_index, player.y_index); float[] p_targ = getTileLoc(player.x_targ, player.y_targ); float[] dxdy = {p_targ[0] - p_ind[0], p_targ[1] - p_ind[1]}; float hyp = sqrt(dxdy[0] * dxdy[0] + dxdy[1] * dxdy[1]); player.x_off += dxdy[0] * player.speed / hyp; player.y_off += dxdy[1] * player.speed / hyp; if(sign(dxdy[0]) == sign((p_ind[0] + player.x_off) - p_targ[0]) || sign(dxdy[1]) == sign((p_ind[1] + player.y_off) - p_targ[1])) { player.x_off = 0; player.y_off = 0; player.x_index = player.x_targ; player.y_index = player.y_targ; } } float[] p_ind = getTileLoc(player.x_index, player.y_index); pushMatrix(); translate(p_ind[0], p_ind[1]); player.display(); popMatrix(); } int sign(float val) { if(val == 0) { return 1; } return round(val / (abs(val))); } void addTexture(String newTex) { PImage[] tempArray = new PImage[(textures.length + 1)]; for(int i = 0; i < textures.length; i++) { tempArray[i] = textures[i]; } tempArray[tempArray.length - 1] = loadImage(newTex); textures = tempArray; } void addTextureWithMask(String newTex, String newMask) { PImage img = loadImage(newTex); img.mask(loadImage(newMask)); PImage[] tempArray = new PImage[(textures.length + 1)]; for(int i = 0; i < textures.length; i++) { tempArray[i] = textures[i]; } tempArray[tempArray.length - 1] = img; textures = tempArray; } void fillFloor(int tex) { while(plusTile(tex)) { // pretty neat, huh? } } float[] getTileLoc(int tx, int ty) { float i = sqrt((one_tile * tx * one_tile * tx) / (ratio * ratio + 1)); float u = sqrt((one_tile * ty * one_tile * ty) / (ratio * ratio + 1)); float[] retFloats = {i + u, ratio * (i - u)}; return retFloats; } float getTileX(int tx, int ty) { return sqrt((one_tile * tx * one_tile * tx) / (ratio * ratio + 1)) + sqrt((one_tile * ty * one_tile * ty) / (ratio * ratio + 1)); } float getTileY(int tx, int ty) { return ratio * (sqrt((one_tile * tx * one_tile * tx) / (ratio * ratio + 1)) - sqrt((one_tile * ty * one_tile * ty) / (ratio * ratio + 1))); } boolean plusTile(int tex) { for(int u = 0; u < h; u++) { for(int i = 0; i < w; i++) { if(tiles[u][i] == null) { tiles[u][i] = new Tile(1, 1, tex, tileRes, ratio); return true; } } } return false; } void slide(float d) { slide_val += d; } Tile[] shift(Tile[] in) { Tile[] ret_row = tiles[tiles.length - 1]; for(int i = 1; i < tiles.length; i++) { tiles[tiles.length - i] = tiles[tiles.length - (i + 1)]; } tiles[0] = in; player.x_index++; player.x_targ++; return ret_row; } boolean tileOccupied(int ix, int iy) { if(tiles[ix][iy].mess.length == 0) { return false; } return true; } boolean movePlayer(int dir) { switch(dir) { case 0: // NORTH player.faceDir(NORTH); if(player.x_index != 0 && !tileOccupied(player.x_index - 1, player.y_index)) { player.move(NORTH); return true; }else{ return false; } case 1: // EAST player.faceDir(EAST); if(player.y_index != w - 1 && !tileOccupied(player.x_index, player.y_index + 1)) { player.move(EAST); return true; }else{ return false; } case 2: // SOUTH player.faceDir(SOUTH); if(player.x_index != h - 1 && !tileOccupied(player.x_index + 1, player.y_index)) { player.move(SOUTH); return true; }else{ return false; } case 3: // WEST player.faceDir(WEST); if(player.y_index != 0 && !tileOccupied(player.x_index, player.y_index - 1)) { player.move(WEST); return true; }else{ return false; } default: println("INVALID DIRECTION: " + dir); return false; } } void feed(Crap food) { if(stomach.length < stomachSize) { Crap tStom[] = new Crap[stomach.length + 1]; for(int i = 0; i < stomach.length; i++) { tStom[i] = stomach[i]; } tStom[tStom.length - 1] = food; stomach = tStom; } } Crap poop(int index) { Crap retCrap = null; if(index < stomach.length) { retCrap = stomach[index]; Crap tStom[] = new Crap[stomach.length - 1]; for(int i = 0; i < index; i++) { tStom[i] = stomach[i]; } for(int i = index + 1; i < stomach.length; i++) { tStom[i - 1] = stomach[i]; } stomach = tStom; } return retCrap; } void eatItem() { if(stomach.length < stomachSize) { switch(player.face) { case 0: // NORTH if(tileOccupied(player.x_index - 1, player.y_index)) feed(tiles[player.x_index - 1][player.y_index].popCrap()); break; case 1: // EAST if(tileOccupied(player.x_index, player.y_index + 1)) feed(tiles[player.x_index][player.y_index + 1].popCrap()); break; case 2: // SOUTH if(tileOccupied(player.x_index + 1, player.y_index)) feed(tiles[player.x_index + 1][player.y_index].popCrap()); break; case 3: // WEST if(tileOccupied(player.x_index, player.y_index - 1)) feed(tiles[player.x_index][player.y_index - 1].popCrap()); break; default: println("INVALID DIRECTION: " + player.face); break; } } } }