//generateReward function void Nonbreakable::generateReward(bool isLarge) { // if it is a question block decide which reward to give to mario if (type_ == QUESTION) { if (reward_ == COIN) { // Create a coin above block it was in and adds drawable coin to the level // uses the left and right of the block and the top and bottom plus 16 Coin *coin = new Coin(); coin->setLeft(this->left()); coin->setRight(this->right()); coin->setTop(this->top()+16); coin->setBottom(this->top()); Level::sharedLevel()->addDrawable(coin); } else if (reward_ == MUSHROOM) { if (isLarge != true) { // Create a mushroom above block it was in and adds drawable coin to the level // uses the left and right of the block and the top and bottom plus 16 Mushroom *mushroom = new Mushroom(); mushroom->setLeft(this->left()); mushroom->setRight(this->right()); mushroom->setTop(this->top()+16); mushroom->setBottom(this->top()); Level::sharedLevel()->addMovable(mushroom); } else { // Create a fireflower above block it was in and adds drawable coin to the level // uses the left and right of the block and the top and bottom plus 16 FireFlower *fireFlower = new FireFlower(); fireFlower->setLeft(this->left()); fireFlower->setRight(this->right()); fireFlower->setTop(this->top()+16); fireFlower->setBottom(this->top()); Level::sharedLevel()->addDrawable(fireFlower); } } else { // Create a star above block it was in and adds drawable coin to the level // uses the left and right of the block and the top and bottom plus 16 Star *star = new Star(); star->setLeft(this->left()); star->setRight(this->right()); star->setTop(this->top()+16); star->setBottom(this->top()); Level::sharedLevel()->addDrawable(star); } // once the block has been hit by mario and the reward generated change type to regular from question type_ = OFFQUESTION; } }
//------------------------------------------------------------ void Level::makeLevel(int levelNumber) { //if the level already exists delete it if ((activeDrawable_.first() != NULL) || (activeMovable_.first() != NULL) || (activeBlocks_.first() != NULL)) { resetLevel(); } // Mac environment variable for home directory char *cHomeDir = NULL; cHomeDir = getenv("HOME"); // I think Windows uses HOMEPATH if (!cHomeDir) { cHomeDir = getenv("HOMEPATH"); } string homeDir = cHomeDir; homeDir += "/CS330/"; string fname; ifstream inFile; if (levelNumber== 1) { fname = homeDir + "levelfiles/level1.txt"; inFile.open(fname.c_str()); } else if (levelNumber== 2) { fname = homeDir + "levelfiles/level3.txt"; inFile.open(fname.c_str()); } int xcoord = 0, ycoord = 0, type, reward; char object; while (inFile.good()) { object= inFile.get(); /*at this point the variable 'object' has the next item in the .txt and here the 'object' and it's xcord and ycord are sent off to the correct LList*/ if (object== 'b') { //create breakable block Breakable *bBlock = new Breakable; bBlock->setTop(ycoord + 16); bBlock->setBottom(ycoord); bBlock->setLeft(xcoord); bBlock->setRight(xcoord + 16); //add block to the list if (xcoord<256){ activeBlocks_.append(bBlock); } else { levelBlocks_.append(bBlock); } } else if (object== 'B' || object=='M' || object=='C' || object=='S'){ // sets the type and reward based off the letter passed and creates the block if (object == 'B') { type = REGULAR; reward = 0; } else if (object == 'M'){ type = QUESTION; reward = MUSHROOM; } else if (object == 'S'){ type = QUESTION; reward = STAR; } else if (object == 'C'){ type = QUESTION; reward = COIN; } Nonbreakable *nBlock = new Nonbreakable(type, reward); nBlock->setTop(ycoord + 16); nBlock->setBottom(ycoord); nBlock->setLeft(xcoord); nBlock->setRight(xcoord + 16); //place in correct list if (xcoord<256) { activeBlocks_.append(nBlock); } else { levelBlocks_.append(nBlock); } } else if (object == 'c') { //create coin Coin *coin = new Coin; coin->setTop(ycoord + 16); coin->setBottom(ycoord); coin->setLeft(xcoord); coin->setRight(xcoord + 16); //place in correct list if (xcoord<256) { activeDrawable_.append(coin); } else { levelDrawable_.append(coin); } } else if (object== 'f'){ //create flag pole Flag *flag = new Flag; flag->setTop(ycoord + 16); flag->setBottom(ycoord); flag->setLeft(xcoord+6); flag->setRight(xcoord + 10); //place in correct list if (xcoord<256){ activeBlocks_.append(flag); } else { levelBlocks_.append(flag); } } else if (object== 'P'){ //create Pipe top Pipe *pipe = new Pipe; pipe->setType(0); pipe->setTop(ycoord + 16); pipe->setBottom(ycoord); pipe->setLeft(xcoord); pipe->setRight(xcoord + 32); //place in correct list if (xcoord<256){ activeBlocks_.append(pipe); } else { levelBlocks_.append(pipe); } } else if (object== 'p'){ //create Pipe body Pipe *pipe = new Pipe; pipe->setType(1); pipe->setTop(ycoord + 16); pipe->setBottom(ycoord); pipe->setLeft(xcoord); pipe->setRight(xcoord + 32); //place in correct list if (xcoord<256){ activeBlocks_.append(pipe); } else { levelBlocks_.append(pipe); } } else if (object== 'g'){ //create goomba Goomba *goomba = new Goomba; goomba->setTop(ycoord + 16); goomba->setBottom(ycoord); goomba->setLeft(xcoord); goomba->setRight(xcoord + 16); //place in correct list if (xcoord<256) { activeMovable_.append(goomba); } else { levelMovable_.append(goomba); } } else if (object == 'k') { //create koopa Turtle *koopa = new Turtle; koopa->setTop(ycoord + 24); koopa->setBottom(ycoord); koopa->setLeft(xcoord); koopa->setRight(xcoord + 16); //place in correct list if (xcoord<256) { activeMovable_.append(koopa); } else { levelMovable_.append(koopa); } } else if (object == 's') { //creates the coordinates for marios starting point leftStart_ = xcoord; bottomStart_ = ycoord; } else if (object== 'u' || object=='y' || object=='w' || object=='1'|| object=='2' || object=='3' || object=='7' || object =='8' || object== 'v' || object== 't' || object== 'd'){ //create bush Background *background = new Background(object); background->setTop(ycoord + background->getHeight()); background->setBottom(ycoord); background->setLeft(xcoord); background->setRight(xcoord + background->getWidth()); //place in correct list if (xcoord<256) { activeDrawable_.append(background); } else { levelDrawable_.append(background); } } if (ycoord < 224) { ycoord += 16; } else if (ycoord == 224) { xcoord += 16; ycoord = 0; } } inFile.close(); }