예제 #1
0
파일: Mario.cpp 프로젝트: apeddicord/CS330
//-------------------------------------------------------------
//method that calculate the intersections of Mario and objects
//to see if Mario runs into anything
void Mario::check() {
    Drawable *objb, *objt, *objl, *objr;

    objt = this->checkTop();
    //All items that can hit Mario from the top
    if (objt)
        switch (objt->objectType()) {
            // if the block is Nonbreakable, stop Mario
            case OFFQUESTION:
            case REGULAR:
                if (this->getYVelocity() > 0) {
                    this->setXVelocity(0.0);
                    this->jumpCount_ = 0;
                }
                break;
            // if the block is question, stop Mario and 
            // generate reward based on Mario's state
            case QUESTION:
                if (this->getYVelocity() > 0) {
                    this->setXVelocity(0.0);
                    this->jumpCount_ = 0;
                }
                if (this->getYVelocity() >= 0) {
                    ((Nonbreakable*)objt)->generateReward(this->getState() != SMALL_STATE);
                }
                break;
            // if the block is a breakable block, stop Mario,
            // check if Mario can break it, and then check if
            // there is an enemy on top of the block and kill
            // the enemy
            case BREAKABLE:
                if (this->getYVelocity() > 0) {
                    this->setXVelocity(0.0);
                    this->jumpCount_ = 0;
                }
                if (this->getYVelocity() >= 0) {
                    ((Breakable*) objt)->breakBlock(this->getState() != SMALL_STATE);
                    (game)->breakBlock(this->getState() != SMALL_STATE);
                    
                    if (this->getState() != SMALL_STATE)
                    {
                        this->setTop(this->top() + 16);
                        objt = this->checkTop();
                        if (objt)
                            Level::sharedLevel()->removeDrawable(objt);
                        
                        this->setTop(this->top() - 16);
                    }
                }
                break;
            // if an enemy falls on top of Mario, 
            // SMALL_STATE = Mario dies
            // if starCount_ > 0, the enemy dies
            // otherwise decrease state and make
            // Mario invincible for 200 frames
            case GOOMBA:
            case SHELL:
            case ENEMYFIREBALL:
            case TURTLE:
                if (state_ > SMALL_STATE)
                {
                    if (hitCount_ == 0){
                        hitCount_ = 200;
                        if (state_ == BIG_STATE)
                        {
                            this->setTop(this->top() - 8.0);
                            state_ = SMALL_STATE;
                        }
                        else if (state_ == FIRE_STATE)
                        {
                            state_ = BIG_STATE;
                        }
                    }
                }
                if (starCount_ > 0)
                {
                    game->jumpEnemy(1);
                    Level::sharedLevel()->removeDrawable(objr);
                }
                else if (starCount_ == 0 && hitCount_ == 0)
                {
                    this->isDead_ = true;
                }
                break;
            // if a reward falls on top of Mario or he jumps
            // into a reward, increase state accordingly or
            // add points
            case MUSHROOM:
                game->addPowerup();
                Level::sharedLevel()->removeDrawable(objt);
                if (this->state_ == SMALL_STATE) {
                    this->state_ = BIG_STATE;
                    this->setTop(this->top() + 8);
                }
                break;
            // Make Mario invincible for 1000 frames
            case STAR:
                game->addPowerup();
                starCount_ = 1000;
                break;
            case FIREFLOWER:
                game->addPowerup();
                if (state_ == SMALL_STATE)
                {
                    this->setTop(this->top() + 8);
                }
                this->state_ = FIRE_STATE;
                Level::sharedLevel()->removeDrawable(objt);
                break;
            case COIN:
                game->addCoin();
                Level::sharedLevel()->removeDrawable(objt);
                break;
                
        }
    objb = this->checkBottom();
    //All objects that can hit Mario from the bottom
    if (objb) {
        Shell *nshell;
        switch (objb->objectType()) {
            // if Mario lands on anything solid
            // stop Mario from falling
            case PIPE:
            case OFFQUESTION:
            case BREAKABLE:
            case REGULAR:
            case QUESTION:
                if (this->getYVelocity() < 0) {
                    this->setYVelocity(0.0);
                }
                break;
            // Mario lands on an enemy
            // if Turtle, generate shell
            case TURTLE:
                nshell = new Shell();
                nshell->setTop(objb->top()-8);
                nshell->setRight(objb->right());
                nshell->setLeft(objb->left());
                nshell->setBottom(objb->bottom());
                Level::sharedLevel()->addMovable(nshell);
                //break;
            // if Goomba, kill enemy and 
            // reset jumpCount to half for bounce
            case GOOMBA:
                game->jumpEnemy(1);
                Level::sharedLevel()->removeDrawable(objb);
                this->jumpCount_ = 25;
                this->setYVelocity(2.0);
            case SHELL:
            case ENEMYFIREBALL:
                break;
            // if Mario lands on a reward, increase state accordingly or
            // add points
            case MUSHROOM:
                game->addPowerup();
                Level::sharedLevel()->removeDrawable(objb);
                if (this->state_ == SMALL_STATE) {
                    this->state_ = BIG_STATE;
                    this->setTop(this->top() + 8);
                }
                break;
            // Make Mario invincible for 1000 frames
            case STAR:
                game->addPowerup();
                starCount_ = 1000;
                break;
            case FIREFLOWER:
                game->addPowerup();
                if (state_ == SMALL_STATE)
                {
                    this->setTop(this->top() + 8);
                }
                this->state_ = FIRE_STATE;
                Level::sharedLevel()->removeDrawable(objb);
                break;
            case COIN:
                game->addCoin();
                Level::sharedLevel()->removeDrawable(objb);
                break;
            // Mario has reached the end of the level
            case FLAG:
                game->touchFlag(this->bottom());
                compleateLevel_=true;
        }
    } else {
        if (this->getYVelocity() == 0.0) {
            this->setYVelocity(-2.0);
        }
    }
    objl = this->checkLeft();
    //All objects that can hit Mario from the left
    if (objl) {
        switch (objl->objectType()) {
            // if Mario runs into anything solid
            // stop Mario from moving through object
            case PIPE:
            case BREAKABLE:
            case REGULAR:
            case OFFQUESTION:
            case QUESTION:
                if (this->getXVelocity() < 0) {
                    this->setXVelocity(0.0);
                }
                break;
            // if Mario runs into an enemy, 
            // SMALL_STATE = Mario dies
            // if starCount_ > 0, the enemy dies
            // otherwise decrease state and make
            // Mario invincible for 200 frames
            case GOOMBA:
            case SHELL:
            case ENEMYFIREBALL:
            case TURTLE:
                if (state_ > SMALL_STATE)
                {
                    if (hitCount_ == 0){
                        hitCount_ = 200;
                        if (state_ == BIG_STATE)
                        {
                            this->setTop(this->top() - 8.0);
                            state_ = SMALL_STATE;
                        }
                        else if (state_ == FIRE_STATE)
                        {
                            state_ = BIG_STATE;
                        }
                    }
                }
                if (starCount_ > 0)
                {
                    game->jumpEnemy(1);
                    Level::sharedLevel()->removeDrawable(objr);
                }
                else if (starCount_ == 0 && hitCount_ == 0)
                {
                    this->isDead_ = true;
                }
                break;
            // Mario runs into a reward, update state accordingly
            // or add points
            case MUSHROOM:
                game->addPowerup();
                Level::sharedLevel()->removeDrawable(objl);
                if (this->state_ == SMALL_STATE) {
                    this->state_ = BIG_STATE;
                    this->setTop(this->top() + 8);
                }
                break;
            // Make Mario invincible for 1000 frames
            case STAR:
                game->addPowerup();
                starCount_ = 1000;
                break;
            case FIREFLOWER:
                game->addPowerup();
                if (state_ == SMALL_STATE)
                {
                    this->setTop(this->top() + 8);
                }
                this->state_ = FIRE_STATE;
                Level::sharedLevel()->removeDrawable(objl);
                break;
            case COIN:
                game->addCoin();
                Level::sharedLevel()->removeDrawable(objl);
                break;
            // Mario has reached the end of the level
            case FLAG:
                game->touchFlag(this->bottom());
                compleateLevel_=true;
                break;
        }
    } 
    else if (leftKey_) {
        if (sprintKey_) {
            this->setXVelocity(-1.2);
        } else {
            this->setXVelocity(-1.0);
        }
    }
    objr = this->checkRight();
    //All objects that can hit Mario from the right
    if (objr) {
        switch (objr->objectType()) {
            // if Mario runs into anything solid
            // stop Mario from moving through object
            case PIPE:
            case OFFQUESTION:
            case BREAKABLE:
            case REGULAR:
            case QUESTION:
                if (this->getXVelocity() > 0) {
                    this->setXVelocity(0.0);
                }
                break;
            // if Mario runs into an enemy, 
            // SMALL_STATE = Mario dies
            // if starCount_ > 0, the enemy dies
            // otherwise decrease state and make
            // Mario invincible for 200 frames
            case GOOMBA:
            case SHELL:
            case ENEMYFIREBALL:
            case TURTLE:
                if (state_ > SMALL_STATE)
                {
                    if (hitCount_ == 0){
                        hitCount_ = 200;
                        if (state_ == BIG_STATE)
                        {
                            this->setTop(this->top() - 8.0);
                            state_ = SMALL_STATE;
                        }
                        else if (state_ == FIRE_STATE)
                        {
                            state_ = BIG_STATE;
                        }
                    }
                }
                if (starCount_ > 0)
                {
                    game->jumpEnemy(1);
                    Level::sharedLevel()->removeDrawable(objr);
                }
                else if (starCount_ == 0 && hitCount_ == 0)
                {
                    this->isDead_ = true;
                }
                break;
            // Mario runs into a reward, update state accordingly
            // or add points
            case MUSHROOM:
                game->addPowerup();
                Level::sharedLevel()->removeDrawable(objr);
                if (this->state_ == SMALL_STATE) {
                    this->state_ = BIG_STATE;
                    this->setTop(this->top() + 8);
                }
                break;
            // Make Mario invincible for 1000 frames
            case STAR:
                game->addPowerup();
                starCount_ = 50;
                break;
            case FIREFLOWER:
                game->addPowerup();
                if (state_ == SMALL_STATE)
                {
                    this->setTop(this->top() + 8);
                }
                this->state_ = FIRE_STATE;
                Level::sharedLevel()->removeDrawable(objr);
                break;
            case COIN:
                game->addCoin();
                Level::sharedLevel()->removeDrawable(objr);
                break;
            // Mario has reached the end of the level
            case FLAG:
                game->touchFlag(this->bottom());
                compleateLevel_=true;
                break;
                
        }
    } 
    else if (rightKey_) {
        if (sprintKey_) {
            this->setXVelocity(1.2);
        } else {
            this->setXVelocity(1.0);
        }
    }
    //stops Mario moving out of the left bound
    if (this->left() < leftBound_ && this->getXVelocity() < 0)
    {
        this->setXVelocity(0.0);
    }
    // if Mario falls off a ledge or if time runs out
    // Mario dies
    if (this->top() <= 0 || game->getTime() <= 0) {
        this->isDead_ = true;
    }
}
예제 #2
0
//-------------------------------------------------------------
//method that calculate the intersections of Mario and objects
//to see if Mario runs into anything
void Mario::check() {
    Drawable *objb, *objt, *objl, *objr;

    objt = this->checkTop();
    //All items that can hit Mario from the top
    if (objt)
        switch (objt->objectType()) {
            case OFFQUESTION:
            case REGULAR:
                if (this->getYVelocity() > 0) {
                    this->setXVelocity(0.0);
                    this->jumpCount_ = 0;
                }
                break;
            case QUESTION:
                if (this->getYVelocity() > 0) {
                    this->setXVelocity(0.0);
                    this->jumpCount_ = 0;
                }
                if (this->getYVelocity() >= 0) {
                    ((Nonbreakable*)objt)->generateReward(this->getState() != SMALL_STATE);
                }
                break;
            case BREAKABLE:
                if (this->getYVelocity() > 0) {
                    this->setXVelocity(0.0);
                    this->jumpCount_ = 0;
                }
                if (this->getYVelocity() >= 0) {
                    ((Breakable*) objt)->breakBlock(this->getState() != SMALL_STATE);
                    (game)->breakBlock(this->getState() != SMALL_STATE);
                    
                    if (this->getState() != SMALL_STATE)
                    {
                        this->setTop(this->top() + 16);
                        objt = this->checkTop();
                        if (objt)
                            Level::sharedLevel()->removeDrawable(objt);
                        
                        this->setTop(this->top() - 16);
                    }
                }
                break;
            case GOOMBA:
            case SHELL:
            case ENEMYFIREBALL:
            case TURTLE:
                if (state_ > SMALL_STATE)
                {
                    if (hitCount_ == 0){
                        hitCount_ = 200;
                        if (state_ == BIG_STATE)
                        {
                            this->setTop(this->top() - 8.0);
                            state_ = SMALL_STATE;
                        }
                        else if (state_ == FIRE_STATE)
                        {
                            state_ = BIG_STATE;
                        }
                    }
                }
                if (starCount_ > 0)
                {
                    game->jumpEnemy(1);
                    Level::sharedLevel()->removeDrawable(objr);
                }
                else if (starCount_ == 0 && hitCount_ == 0)
                {
                    this->isDead_ = true;
                }
                break;
                
            case MUSHROOM:
                game->addPowerup();
                Level::sharedLevel()->removeDrawable(objt);
                if (this->state_ == SMALL_STATE) {
                    this->state_ = BIG_STATE;
                    this->setTop(this->top() + 8);
                }
                break;
            case STAR:
                game->addPowerup();
                starCount_ = 50;
                break;
            case FIREFLOWER:
                game->addPowerup();
                if (state_ == SMALL_STATE)
                {
                    this->setTop(this->top() + 8);
                }
                this->state_ = FIRE_STATE;
                Level::sharedLevel()->removeDrawable(objt);
                break;
            case COIN:
                game->addCoin();
                Level::sharedLevel()->removeDrawable(objt);
                break;
                
        }
    objb = this->checkBottom();
    //All objects that can hit Mario from the bottom
    if (objb) {
        Shell *nshell;
        switch (objb->objectType()) {
            case PIPE:
            case OFFQUESTION:
            case BREAKABLE:
            case REGULAR:
            case QUESTION:
                if (this->getYVelocity() < 0) {
                    this->setYVelocity(0.0);
                }
                break;
            case TURTLE:
                nshell = new Shell();
                nshell->setTop(objb->top()-8);
                nshell->setRight(objb->right());
                nshell->setLeft(objb->left());
                nshell->setBottom(objb->bottom());
                Level::sharedLevel()->addMovable(nshell);
                //break;
            case GOOMBA:
                game->jumpEnemy(1);
                Level::sharedLevel()->removeDrawable(objb);
                this->jumpCount_ = 25;
                this->setYVelocity(2.0);
            case SHELL:
            case ENEMYFIREBALL:
                break;
            case MUSHROOM:
                game->addPowerup();
                Level::sharedLevel()->removeDrawable(objb);
                if (this->state_ == SMALL_STATE) {
                    this->state_ = BIG_STATE;
                    this->setTop(this->top() + 8);
                }
                break;
            case STAR:
                game->addPowerup();
                starCount_ = 1000;
                break;
            case FIREFLOWER:
                game->addPowerup();
                if (state_ == SMALL_STATE)
                {
                    this->setTop(this->top() + 8);
                }
                this->state_ = FIRE_STATE;
                Level::sharedLevel()->removeDrawable(objb);
                break;
            case COIN:
                game->addCoin();
                Level::sharedLevel()->removeDrawable(objb);
                break;
            case FLAG:
                game->touchFlag(this->bottom());
                compleateLevel_=true;
        }
    } else {
        if (this->getYVelocity() == 0.0) {
            this->setYVelocity(-2.0);
        }
    }
    objl = this->checkLeft();
    //All objects that can hit Mario from the left
    if (objl) {
        switch (objl->objectType()) {
            case PIPE:
            case BREAKABLE:
            case REGULAR:
            case OFFQUESTION:
            case QUESTION:
                if (this->getXVelocity() < 0) {
                    this->setXVelocity(0.0);
                }
                break;
            case GOOMBA:
            case SHELL:
            case ENEMYFIREBALL:
            case TURTLE:
                if (state_ > SMALL_STATE)
                {
                    if (hitCount_ == 0){
                        hitCount_ = 200;
                        if (state_ == BIG_STATE)
                        {
                            this->setTop(this->top() - 8.0);
                            state_ = SMALL_STATE;
                        }
                        else if (state_ == FIRE_STATE)
                        {
                            state_ = BIG_STATE;
                        }
                    }
                }
                if (starCount_ > 0)
                {
                    game->jumpEnemy(1);
                    Level::sharedLevel()->removeDrawable(objr);
                }
                else if (starCount_ == 0 && hitCount_ == 0)
                {
                    this->isDead_ = true;
                }
                break;
                
            case MUSHROOM:
                game->addPowerup();
                Level::sharedLevel()->removeDrawable(objl);
                if (this->state_ == SMALL_STATE) {
                    this->state_ = BIG_STATE;
                    this->setTop(this->top() + 8);
                }
                break;
            case STAR:
                game->addPowerup();
                starCount_ = 50;
                break;
            case FIREFLOWER:
                game->addPowerup();
                if (state_ == SMALL_STATE)
                {
                    this->setTop(this->top() + 8);
                }
                this->state_ = FIRE_STATE;
                Level::sharedLevel()->removeDrawable(objl);
                break;
            case COIN:
                game->addCoin();
                Level::sharedLevel()->removeDrawable(objl);
                break;
            case FLAG:
                game->touchFlag(this->bottom());
                compleateLevel_=true;
                break;
        }
    } 
    else if (leftKey_) {
        if (sprintKey_) {
            this->setXVelocity(-1.2);
        } else {
            this->setXVelocity(-1.0);
        }
    }
    objr = this->checkRight();
    //All objects that can hit Mario from the right
    if (objr) {
        switch (objr->objectType()) {
            case PIPE:
            case OFFQUESTION:
            case BREAKABLE:
            case REGULAR:
            case QUESTION:
                if (this->getXVelocity() > 0) {
                    this->setXVelocity(0.0);
                }
                break;
            case GOOMBA:
            case SHELL:
            case ENEMYFIREBALL:
            case TURTLE:
                if (state_ > SMALL_STATE)
                {
                    if (hitCount_ == 0){
                        hitCount_ = 200;
                        if (state_ == BIG_STATE)
                        {
                            this->setTop(this->top() - 8.0);
                            state_ = SMALL_STATE;
                        }
                        else if (state_ == FIRE_STATE)
                        {
                            state_ = BIG_STATE;
                        }
                    }
                }
                if (starCount_ > 0)
                {
                    game->jumpEnemy(1);
                    Level::sharedLevel()->removeDrawable(objr);
                }
                else if (starCount_ == 0 && hitCount_ == 0)
                {
                    this->isDead_ = true;
                }
                break;
            case MUSHROOM:
                game->addPowerup();
                Level::sharedLevel()->removeDrawable(objr);
                if (this->state_ == SMALL_STATE) {
                    this->state_ = BIG_STATE;
                    this->setTop(this->top() + 8);
                }
                break;
            case STAR:
                game->addPowerup();
                starCount_ = 50;
                break;
            case FIREFLOWER:
                game->addPowerup();
                if (state_ == SMALL_STATE)
                {
                    this->setTop(this->top() + 8);
                }
                this->state_ = FIRE_STATE;
                Level::sharedLevel()->removeDrawable(objr);
                break;
            case COIN:
                game->addCoin();
                Level::sharedLevel()->removeDrawable(objr);
                break;
            case FLAG:
                game->touchFlag(this->bottom());
                compleateLevel_=true;
                break;
                
        }
    } 
    else if (rightKey_) {
        if (sprintKey_) {
            this->setXVelocity(1.2);
        } else {
            this->setXVelocity(1.0);
        }
    }
    //stops Mario moving out of the left bound
    if (this->left() < leftBound_ && this->getXVelocity() < 0)
    {
        this->setXVelocity(0.0);
    }
    if (this->top() <= 0 || game->getTime() <= 0) {
        this->isDead_ = true;
    }
}