//import ddf.minim.*; /*************************************************************************** * GLOBAL VARIABLES ***************************************************************************/ //Minim minim; //AudioPlayer player; // Window and drawing rate setup int windowW = 700; int windowH = 590; color backgroundColor = color(184, 219, 124); int frameRate = 30; PImage imgTitle; // Fonts PFont fontBig, fontSmall; // Images for showing stats PImage imgSheep, imgDeadSheep; // Boundaries for sheep wandering int wallWidth = 20; int wallLengthBottom = 300;//160; int spaceBelowSheep = 150; int sheepBoundaryX1 = wallWidth; int sheepBoundaryY1 = wallWidth; int sheepBoundaryX2 = windowW - wallWidth; int sheepBoundaryY2 = windowH - spaceBelowSheep - wallWidth; // List of all sheep on the screen int numSheep = 10; ArrayList listOfSheep; ArrayList listOfInactiveSheep; int numDeadSheep = 0; int numSavedSheep = 0; // The herding dog Dog dog; // The wolf, if there is one. Wolf wolf = null; static final int TIME_BEFORE_WOLF_ARRIVES = 1000; int timeWithoutWolf; // Game mode info boolean choosingSheep; boolean firstStart; boolean onTitleScreen; /*************************************************************************** * Setup ***************************************************************************/ void setup() { //minim = new Minim(this); //player = minim.loadFile("BarkSingle.wav", 2048); // Window and drawing rate size(windowW, windowH); frameRate(frameRate); // Fonts fontBig = loadFont("BrowalliaUPC-Bold-48.vlw"); fontSmall = loadFont("BrowalliaUPC-Bold-24.vlw"); // Images for stats imgSheep = loadImage("RoundUp-Sheep.png"); imgDeadSheep = loadImage("RoundUp-SheepDead.png"); imgTitle = loadImage("RoundUp-Title.png"); firstStart = true; onTitleScreen = true; setupGame(); } /*************************************************************************** * Game setup ***************************************************************************/ void setupGame() { listOfSheep = new ArrayList(); listOfInactiveSheep = new ArrayList(); dog = new Dog("RoundUp-Dog.png", "RoundUp-DogBark.png", 0.7, width/2, height-(spaceBelowSheep/2), listOfSheep, numSheep); dog.setMaxSpeed(5); for (int i=0; i < numSheep; i++) { Sheep sheep = new Sheep("RoundUp-Sheep.png", "RoundUp-SheepDead.png", 0.4, (int)random(windowW), (int)random(windowH), listOfSheep, dog); sheep.setMaxSpeed(3); sheep.setDirection((int)random(0,360)); listOfSheep.add(sheep); } numDeadSheep = numSavedSheep = 0; timeWithoutWolf = 0; wolf = null; choosingSheep = false; onTitleScreen = true; } /*************************************************************************** * Draw ***************************************************************************/ void draw() { if (firstStart || onTitleScreen) { drawTitle(); return; } // Game world background(backgroundColor); drawSheepBoundaries(); // Keyboard shortcuts int instructionSize = 24; textFont(fontSmall); fill(131, 77, 37); textAlign(LEFT, TOP); if (dog.isCircling() && dog.timeLeftForCircling() >= 0) { text("B : Bark", 30, height - spaceBelowSheep + 50 + 0*instructionSize); text("C : Cancel", 30, height - spaceBelowSheep + 50 + 1*instructionSize); } else if (!dog.isWaiting()) { text("C : Cancel", 30, height - spaceBelowSheep + 50); } else if (dog.isWaiting()) { text("H : Herd all sheep", 30, height - spaceBelowSheep + 50 + 0*instructionSize); text("G : Grab one sheep", 30, height - spaceBelowSheep + 50 + 1*instructionSize); } // Stats imageMode(CORNER); image(imgSheep, 550, height - spaceBelowSheep + 40, 25, 25); image(imgDeadSheep, 550, height - spaceBelowSheep + 40 + 35, 25, 25); text("x " + numSavedSheep, 550 + 35, height - spaceBelowSheep + 50); text("x " + numDeadSheep, 550 + 35, height - spaceBelowSheep + 50 + 35); // Time left circling int tCircling = dog.timeLeftForCircling(); if (tCircling >= 0) { fill(240,250,100); textAlign(LEFT, TOP); textFont(fontBig); text(tCircling, wallWidth + 10, wallWidth + 10); fill(255); } // Sheep for (int s=0; s < listOfSheep.size(); s++) { Sheep sheep = (Sheep)listOfSheep.get(s); sheep.move(); if (choosingSheep) { sheep.draw((char)('0'+s)); } else { sheep.draw(); } } // Dog dog.move(); dog.draw(); // Wolf if (wolf != null) { wolf.move(); wolf.draw(); } doGameMaintenance(); } /*************************************************************************** * Draw game title ***************************************************************************/ void drawTitle() { background(backgroundColor); drawSheepBoundaries(); imageMode(CENTER); image(imgTitle, width/2, (height - spaceBelowSheep)/2); textFont(fontBig); fill(131, 77, 37); textAlign(CENTER); if (firstStart) text("Press any key to begin", width/2, height - (spaceBelowSheep/2)); else { text("You saved " + numSavedSheep + " sheep and let " + numDeadSheep + " die.", width/2, height - (spaceBelowSheep/2) - 25); text ("Press any key to play again.", width/2, height - (spaceBelowSheep/2) + 25); } } /*************************************************************************** * Draw sheep boundaries ***************************************************************************/ void drawSheepBoundaries() { fill(140); noStroke(); rectMode(CORNER); rect(0, 0, wallWidth, height - spaceBelowSheep); rect(0, 0, width, wallWidth); rect(width-wallWidth, 0, wallWidth, height - spaceBelowSheep); rect(0, height - spaceBelowSheep - wallWidth, wallLengthBottom, wallWidth); rect(width - wallWidth - wallLengthBottom, height - spaceBelowSheep - wallWidth, wallLengthBottom, wallWidth); } /*************************************************************************** * Take care of managing the game - is it over, etc ***************************************************************************/ void doGameMaintenance() { if (wolf == null) { timeWithoutWolf++; if (timeWithoutWolf > TIME_BEFORE_WOLF_ARRIVES) { int x = 0, y = 0; wolf = new Wolf("RoundUp-Wolf.png", 0.7, x, y, listOfSheep, dog); boolean badSpot = false; do { badSpot = false; x = (int)random(sheepBoundaryX1, sheepBoundaryX2 - wolf.getW()); y = (int)random(sheepBoundaryY1, sheepBoundaryY2 - wolf.getH()); wolf.setLocation(x,y); for (int s=0; s < listOfSheep.size(); s++) { if (wolf.collides((Sheep)listOfSheep.get(s))) { badSpot = true; break; } } } while (badSpot); wolf.setSpeed(7); } } // Remove dead sheep or sheep that are done the game if (!choosingSheep) { Iterator sheepIter = listOfSheep.iterator(); while (sheepIter.hasNext()) { Sheep sheep = (Sheep)sheepIter.next(); // Dead sheep if (sheep.isDead() && !listOfInactiveSheep.contains(sheep)) { //sheepIter.remove(); listOfInactiveSheep.add(sheep); dog.oneLessActiveSheep(); numDeadSheep++; } // Sheep that are done if (sheep.isDone() && !listOfInactiveSheep.contains(sheep)) { //sheepIter.remove(); listOfInactiveSheep.add(sheep); dog.oneLessActiveSheep(); numSavedSheep++; } } } // Check if the game is over if (numSavedSheep + numDeadSheep == numSheep) { onTitleScreen = true; } } /*************************************************************************** * Key released to control the dog ***************************************************************************/ void keyReleased() { if (onTitleScreen) { setupGame(); onTitleScreen = false; firstStart = false; } else { // Sheep choosing mode if (choosingSheep) { if (key >= '0' && key <= '9') { int sheepNum = key - '0'; choosingSheep = false; dog.goGetSheep((Sheep)listOfSheep.get(sheepNum)); } choosingSheep = false; } // Regular mode { if (key == 'b' || key == 'B') { // if (dog.isCircling() && dog.timeLeftForCircling() >= 0) // { // player.rewind(); // player.play(); // } dog.barkButtonPressed(); } if (key == 'c' || key == 'C') { dog.callDogBack(); } if (key == 'g' || key == 'G') { if (dog.isWaiting()) choosingSheep = true; } if (key == 'h' || key == 'H') { dog.startHerding(); } } } }