/********************************************/ /* Rob Seward */ /* March, 2005 */ /********************************************/ int m_idCount; //this takes the place of a static private variable in the Player Class final int ROW = 7; final int COL = 5; final int CYCLE_TIME = 250; final int NUM_PLAYERS = 3; final int OB_DIST = 10000000; final int KILL_CARRIER = 1; final int TAG = 0; final int GAME_MODE = TAG; //0 = tag, 1 = kill the carrier Grid g; Player p1, p2, p3; Player[] playerArray = new Player[NUM_PLAYERS]; int m_tagOccurred = 0; void setup() { m_idCount = 0; size(250,350); g = new Grid(ROW, COL); p1 = new Player( 0, 0, 1, true); p2 = new Player( COL - 1, ROW - 1, 0, false); p3 = new Player( 0, ROW - 1, 0, false); playerArray[0] = p1; playerArray[1] = p2; playerArray[2] = p3; } boolean flag = false; void draw() { if(millis() % CYCLE_TIME > CYCLE_TIME /2 && !flag) //execute below only at cycle time { p1.move(playerArray); p2.move(playerArray); p3.move(playerArray); flag = true; } else if(millis() % CYCLE_TIME < CYCLE_TIME /2) flag = false; g.drawGrid(); p1.drawPlayer(); p2.drawPlayer(); p3.drawPlayer(); } /****************************/ class Grid { int ROW = 7; int COL = 5; Grid(int r, int c) { ROW = r; COL = c; } void drawGrid() { int i, j, rowSpace, colSpace, x, y; rowSpace = height / (ROW * 2); colSpace = width / (COL * 2); for(i=1; i <= ROW*2; i += 2) { for(j=1; j < COL*2; j += 2) { fill(255); ellipse(j * colSpace, i * rowSpace, rowSpace * 1.5, rowSpace * 1.5); } } } } /***************************/ class Player{ int type; //0 = computer, 1 = human boolean it; //1 = "it" int id; int x, y; int xOld, yOld; Player(int x1, int y1, int t, boolean i) { it = i; type = t; x = x1; y = y1; id = m_idCount; m_idCount = m_idCount + 1; } void move(Player[] pArray) { //Freeze code if(GAME_MODE == KILL_CARRIER){ if(!it && m_tagOccurred > 0){ m_tagOccurred++; if(m_tagOccurred > 10) m_tagOccurred = 0; return; } } if(GAME_MODE == TAG){ if(it && m_tagOccurred > 0){ m_tagOccurred++; if(m_tagOccurred > 10) m_tagOccurred = 0; return; } } xOld = x; yOld = y; if(type == 1) //if human { //if(keyPressed){ if(keyCode == UP) y--; if(keyCode == DOWN) y++; if(keyCode == RIGHT) x++; if(keyCode == LEFT) x--; // } keyCode = 0; } if(type == 0) //if computer autoMove(pArray); checkBoundries(); //TO DO : collision detection collisionDetection(pArray); } void autoMove(Player[] pArray) { int itNum = 0, xIt, yIt, i; //find "it" player for( i=0; i < NUM_PLAYERS; i++){ if(pArray[i].it) itNum = i; } xIt = abs(pArray[itNum].x); yIt = abs(pArray[itNum].y); //println("ItNUM: " + xIt + " " + yIt); float[] distances = new float[5]; //up, down, left, right, stay distances[0] = checkAndCalcDist( x, y - 1, xIt, yIt); distances[1] = checkAndCalcDist( x, y + 1, xIt, yIt); distances[2] = checkAndCalcDist( x - 1, y, xIt, yIt); distances[3] = checkAndCalcDist( x+1, y, xIt, yIt); distances[4] = checkAndCalcDist( x, y, xIt, yIt); //find minimum distance (ATTACK!) if((!it && GAME_MODE == KILL_CARRIER) || (it && GAME_MODE == TAG)){ float mini = min(distances[0], distances[1], distances[2]); if(mini >= distances[3]) mini = distances[3]; if(mini >= distances[4]) mini = distances[4]; for(i=0; i < 5; i++) { if(abs(distances[i] - mini) < .0001) break; } } //find maximum distance (Run away!) if((it && GAME_MODE == KILL_CARRIER) || (!it && GAME_MODE == TAG)){ for(i=0; i < 5; i++){ if(distances[i] == OB_DIST) distances[i] = -1; } float maxi = max(distances[0], distances[1], distances[2]); if(maxi <= distances[3]) maxi = distances[3]; if(maxi <= distances[4]) maxi = distances[4]; for(i=0; i < 5; i++){ if(abs(distances[i] - maxi) < .0001) break; } } //printarr(distances); if(distances[i] == OB_DIST || distances[i] == -1) return; else moveDirection(i); } float checkAndCalcDist(int x1, int y1, int xIt, int yIt){ //check boundries if(x1 < 0 || y1 < 0 || x1 > COL - 1 || y1 > ROW - 1) return OB_DIST; return dist(x1, y1, xIt, yIt); } void moveDirection(int i) { if(i == 0) //UP y--; if(i == 1) //DOWN y++; if(i == 2) //LEFT x--; if(i == 3) //RIGHT x++; if(i == 4) //STAY (do nothing) return; } void checkBoundries() { if(x < 0) x = 0; if(y < 0) y = 0; if(x > COL - 1) x = COL -1; if(y > ROW - 1) y = ROW - 1; } void collisionDetection(Player[] pArray) { int i; for(i=0; i < NUM_PLAYERS; i++){ if(pArray[i].id == id) continue; if(x == pArray[i].x && y == pArray[i].y && !pArray[i].it && !it){ //collide with notIt x = xOld; y = yOld; } if(x == pArray[i].x && y == pArray[i].y && (pArray[i].it || it) && m_tagOccurred == 0){ //collide with IT! x = xOld; y = yOld; if(it){ pArray[i].it = true; it = false; } else{ pArray[i].it = false; it = true; } m_tagOccurred = 1; return; } } } /****Drawing Methods****/ void drawPlayer() { if (it){ if(millis() % (CYCLE_TIME/2) > CYCLE_TIME /4) //blink speed drawPlayer2(1); else drawPlayer2(0); } else drawPlayer2(1); } void drawPlayer2(int fillColor) { int rowSpace, colSpace, x1, y1; x1 = (x * 2) + 1; y1 = (y * 2) + 1; rowSpace = height / (ROW * 2); colSpace = width / (COL * 2); if(fillColor == 1 && id != 0) fill(255,0,0); else if (fillColor == 1 && id == 0) fill(0,0,255); else fill(255,255,255); ellipse(x1 * colSpace, y1 * rowSpace, rowSpace * 1.5, rowSpace * 1.5); } } /***************************/ void myWait(int time) { long t1, t2; t1 = millis(); t2 = millis(); while (t2 - t1 < time){ t2 = millis(); } }