Esempio n. 1
0
/////////////////////////////////////
//移動
/////////////////////////////////////
void NPC::move(float _distance, unsigned int _onAttack)
{
	//攻撃可能距離にいる場合
	if (_distance < _onAttack * _onAttack)
	{
		//ステップ可能になったら行動する
		if (stepCount <= 0)
		{
			const float stepSpeed = 0.8f;
			if (rand() % 2 == 0)
			{//左右に移動する(回避行動
				speed.x += cos(yaw * M_PI / 180) * stepSpeed;
				speed.z -= sin(yaw * M_PI / 180) * stepSpeed;
			}
			else
			{
				speed.x -= cos(yaw * M_PI / 180) * stepSpeed;
				speed.z += sin(yaw * M_PI / 180) * stepSpeed;
			}

			stepCount = 60 + rand() % 60;//回避間隔の初期化(1〜2秒
		}
	}
	else//攻撃できる距離まで移動する
	{
		const auto adjustSpeed = 0.5f;

		glm::vec3 targetVec(targetPos.x - pos.x, 0, targetPos.z - pos.z);
		speed = glm::normalize(targetVec) * adjustSpeed;
	}

	speed *= 0.9f;	//減速
	pos += speed;	//位置の移動

	pos.y = field->intersect(pos);	//y座標位置

	playerCollision();
	poleCollision();
	NPCCollision(enemy);
	NPCCollision(supporter);

	lastPos = pos;
}
Esempio n. 2
0
int main()
{
    //create obj
    //X
    megaman megamanX;
    megamanX.r = 62;
    megamanX.c = 30;
    megamanX.mapX = megamanX.c;
    megamanX.mapY = megamanX.r;
    megamanX.inAir = 0;
    megamanX.lives = 20;
    //blast
    Blast blast;;
    blast.r = 0;
    blast.c = megamanX.c;
    blast.fired = 0;
    blast.speed = 8;
    //drlight
    Drlight drlight;
    drlight.r = 0;
    drlight.c = 0;
    //minion
    Minion minion;
    minion.r = 0;
    minion.c = 0;
    minion.lives = 5;
    //turret
    Turret turret;
    turret.r = 0;
    turret.c = 0;
    turret.lives = 10;
    //bullet
    Bullet bullet;
    bullet.r = 0;
    bullet.c = 0;

    //worldMap ground set up
    WorldMap worldmap[1688];
    for(int x=0; x<535; x++) {
        worldmap[x].y = 94;
    }
    for(int x=545; x<905; x++) {
        worldmap[x].y = 74;
    }
    for(int x=921; x<1688; x++) {
        worldmap[x].y = 84;
    }

// 	1. Set up REG_DISPCNT
// Enable objects (sprites) (by setting bit 10) and set the sprite dimension type (given to you by nin10kit)
// 2. Copy the palette given to into the sprite palette (SPRITEPAL)
// 3 Copy the sprite tile graphics to character block 5
// 4. For each of the 128 sprites set its attribute 0 to hide them (ATTR0_HIDE)
// 5. When you want to use a sprite pick a OamEntry.
// a. Modify its attribute 0 with its x and or this with the palette type (from nin10kit) and shape (nin10kit)
// b. Modify its attribute 1 with its y and size (nin10kit)
// c. Modify its attribute 2 with the sprite id (nin10kit)
    REG_DISPCTL = MODE4 | BG2_ENABLE | OBJ_ENABLE | X_DIMENSION_TYPE;

    DMA[3].src = X_palette;
    DMA[3].dst = SPRITEPAL;
    DMA[3].cnt = DMA_ON | X_PALETTE_SIZE;

    DMA[3].src = X;
    DMA[3].dst = SPRITEDATA;
    DMA[3].cnt = DMA_ON | X_SIZE;
    //copy
    OamEntry shadow[128];
    //make all sprites transparent
    for(int i=0; i<128; i++) {
        shadow[i].attr0 = ATTR0_HIDE;
    }

    //helper
    //((SPRITEMEM[0].attr0 &~ 0x00FF)|(20))
    //((SPRITEMEM[1].attr0 &~ 0x01FF)|(0))
    int state=0;
    int hold=0;
    while(1) {
        if(gameState==0) {
            DMA[3].src = title_palette;
            DMA[3].dst = PALETTE;
            DMA[3].cnt = DMA_ON | TITLE_PALETTE_SIZE;
            stage_stop();
            megaman_play();
        }
        while(gameState==0) {
            drawImage4(0,0,240,160,title);
            if(KEY_DOWN_NOW(BUTTON_A) || KEY_DOWN_NOW(BUTTON_START)) {
                gameState=1;
                megaman_stop();
                break;
            }
            waitForVblank();
            flipPage();
        }

        if(gameState==1) {
            //setup stage palette
            DMA[3].src = stage_palette;
            DMA[3].dst = PALETTE;
            DMA[3].cnt = DMA_ON | STAGE_PALETTE_SIZE;

            //music
            stage_play();
        }
        while(gameState==1) {

            if(KEY_DOWN_NOW(BUTTON_SELECT)) {
                for(int i=0; i<128; i++) {
                    shadow[i].attr0 = ATTR0_HIDE;
                }
                hurting=0;
                minionSpawn=0;
                turretSpawn=0;
                turretFired=0;
                megamanX.r = 62;
                megamanX.c = 30;
                megamanX.mapX = megamanX.c;
                megamanX.mapY = megamanX.r;
                megamanX.inAir = 0;
                megamanX.lives = 20;
                started=0;
                telCount=0;
                gameState=0;
            }
            //conversation
            if(started==0) {
                drawStage(30,STAGE_WIDTH,STAGE_HEIGHT,stage);
                switch (state) {
                case 0:
                    drawHollowRect4(100,0,59,239,0);
                    drawRect4(102,2,56,236,140);
                    char buffer[50];
                    sprintf(buffer, "X:");
                    drawString4(105,5,buffer,0);
                    sprintf(buffer, "What happened here?");
                    drawString4(120,5,buffer,0);
                    if(KEY_DOWN_NOW(BUTTON_A) && hold==0) {
                        state=1;
                        hold=1;
                    }
                    break;
                case 1:
                    drawHollowRect4(100,0,59,239,0);
                    drawRect4(102,2,56,236,140);
                    char buffer2[50];
                    sprintf(buffer2, "X:");
                    drawString4(105,5,buffer2,0);
                    sprintf(buffer2, "The highway is destroyed!");
                    drawString4(120,5,buffer2,0);
                    if(KEY_DOWN_NOW(BUTTON_A) && hold==0) {
                        state=2;
                        hold=1;
                    }
                    break;
                case 2:
                    drawHollowRect4(100,0,59,239,0);
                    drawRect4(102,2,56,236,140);
                    char buffer3[50];
                    sprintf(buffer3, "X:");
                    drawString4(105,5,buffer3,0);
                    sprintf(buffer3, "I need to turn in my CS2110 homework!");
                    drawString4(120,5,buffer3,0);
                    if(KEY_DOWN_NOW(BUTTON_A) && hold==0) {
                        state=3;
                        hold=1;
                    }
                    break;
                case 3:
                    drawHollowRect4(100,0,59,239,0);
                    drawRect4(102,2,56,236,140);
                    char buffer4[50];
                    sprintf(buffer4, "Press Right to walk, Up to jump,");
                    drawString4(105,5,buffer4,0);
                    char buffer5[50];
                    sprintf(buffer5, "B to dash, A to fire.");
                    drawString4(120,5,buffer5,0);
                    if(KEY_DOWN_NOW(BUTTON_A) && hold==0) {
                        started=1;
                        state=0;
                        hold=1;
                    }
                    break;
                }
                if(!KEY_DOWN_NOW(BUTTON_A)) {
                    hold=0;
                }
            }
            //teleports
            if(started==1) {
                drawStage(30,STAGE_WIDTH,STAGE_HEIGHT,stage);
                if(telCount<80) {
                    drawRect4(telCount,megamanX.c-1+14,30,6,149);
                    drawRect4(telCount+1,megamanX.c+14,26,2,0);
                    telCount+=8;
                }
                else if(telCount>=80 && telCount<=120) {
                    shadow[2].attr0 = megamanX.r | X_PALETTE_TYPE | X_SPRITE_SHAPE;
                    shadow[2].attr1 = megamanX.c | X_SPRITE_SIZE;
                    shadow[2].attr2 = CHARGES_ID;
                    telCount+=8;
                }
                else {
                    shadow[2].attr0 = ATTR0_HIDE;
                    //draw megaman
                    shadow[0].attr0 = megamanX.r | X_PALETTE_TYPE | X_SPRITE_SHAPE;
                    shadow[0].attr1 = megamanX.c | X_SPRITE_SIZE;
                    shadow[0].attr2 = X_ID;
                    started=2;
                    telCount=0;
                }
            }
            //in Game
            if(started==2) {
                //update background
                moveScreen(megamanX.mapX);
                //status check
                if(!borderCheck(megamanX.r, megamanX.c)) {
                    megamanX.lives-=1;
                    hurting=1;
                }
                if(!checkHealth(&megamanX)) {
                    //need delay here
                    // for(int i=0; i<160;i++) {
                    // 	hurt(&megamanX, shadow);
                    // }
                    for(int i=0; i<128; i++) {
                        shadow[i].attr0 = ATTR0_HIDE;
                    }
                    refreshBlast(&megamanX, &blast, shadow, 1);
                    hurting=0;
                    minionSpawn=0;
                    turretSpawn=0;
                    turretFired=0;
                    started=0;
                    gameState=3;
                }

                //buttons
                if(KEY_DOWN_NOW(BUTTON_A) && hurting==0) {
                    if(charge<102)
                        charge++;
                    if(charge>20) {
                        drawCharges(&megamanX, shadow);
                    }
                }
                if(!KEY_DOWN_NOW(BUTTON_A) && charge<=20 && charge>0 && hurting==0) {
                    blast.fired = 1;
                    blast.r = megamanX.r;
                    charge = 0;
                    blastType = 0;
                }
                else if(!KEY_DOWN_NOW(BUTTON_A) && charge<=100 && charge>20 && hurting==0) {
                    blast.fired = 1;
                    blast.r = megamanX.r;
                    charge = 0;
                    blastType = 1;
                    shadow[2].attr0 = ATTR0_HIDE;
                    chargesDelay = 0;
                }
                else if(!KEY_DOWN_NOW(BUTTON_A) && charge<=102 && charge>100 && hurting==0) {
                    blast.fired = 1;
                    blast.r = megamanX.r;
                    charge = 0;
                    blastType = 2;
                    shadow[2].attr0 = ATTR0_HIDE;
                    chargesDelay = 0;
                }
                if((KEY_DOWN_NOW(BUTTON_B) || (KEY_DOWN_NOW(BUTTON_B) && KEY_DOWN_NOW(BUTTON_RIGHT)))
                        && stopMove==0 && hurting==0) {
                    dash(&megamanX, shadow);
                    moving = 2;
                }
                if(KEY_DOWN_NOW(BUTTON_RIGHT) && !KEY_DOWN_NOW(BUTTON_UP) &&
                        !KEY_DOWN_NOW(BUTTON_LEFT) && !KEY_DOWN_NOW(BUTTON_B) &&
                        megamanX.inAir==0 && hurting==0) {
                    stopMove = 0;
                    dashCounter = 0;
                    megamanX.mapX++;
                    walk(&megamanX, shadow);
                    moving = 1;
                }
                if(KEY_DOWN_NOW(BUTTON_LEFT) && !KEY_DOWN_NOW(BUTTON_UP) &&
                        !KEY_DOWN_NOW(BUTTON_RIGHT) && !KEY_DOWN_NOW(BUTTON_B) &&
                        megamanX.inAir==0 && hurting==0) {
                    stopMove = 0;
                    dashCounter = 0;
                    if(megamanX.mapX-25>0)
                        megamanX.mapX--;
                    walk(&megamanX, shadow);
                    moving = -1;
                }
                if(KEY_DOWN_NOW(BUTTON_RIGHT) && KEY_DOWN_NOW(BUTTON_UP)
                        && megamanX.inAir==0 && hurting==0) {
                    megamanX.inAir = 1;
                    jumpForward = 1;
                    moving = 1;
                }
                if(KEY_DOWN_NOW(BUTTON_UP) && megamanX.inAir==0 && hurting==0) {
                    megamanX.inAir = 1;
                    moving = 0;
                }
                //default position
                if(!KEY_DOWN_NOW(BUTTON_START) && !KEY_DOWN_NOW(BUTTON_SELECT) &&
                        !KEY_DOWN_NOW(BUTTON_B) &&
                        !KEY_DOWN_NOW(BUTTON_R) && !KEY_DOWN_NOW(BUTTON_L) &&
                        !KEY_DOWN_NOW(BUTTON_LEFT) && !KEY_DOWN_NOW(BUTTON_RIGHT) &&
                        !KEY_DOWN_NOW(BUTTON_DOWN) && !KEY_DOWN_NOW(BUTTON_UP) &&
                        megamanX.inAir==0 && hurting==0) {
                    stopMove = 0;
                    jumpCounter = 0;
                    dashCounter = 0;
                    shadow[0].attr2 = X_ID;
                    moving = 0;
                }
                //draw blast
                drawBlast(&blast, &megamanX, shadow);
                //gap check when walking
                if(((megamanX.mapX>=535 && megamanX.mapX<=545)
                        || (megamanX.mapX>=905 && megamanX.mapX<=921))) {
                    if(megamanX.inAir==0) {
                        megamanX.inAir=1;
                        falling=1;
                        jumpCounter=4;
                    }
                }
                if(megamanX.inAir==1) {
                    jump(&megamanX, shadow);
                }
                if(falling==1) {
                    groundCheck(&megamanX, worldmap);
                }
                geoCheck(&megamanX);

                drawLives(&megamanX);
                //restore
                if(hurtTime<=0) {
                    restore(shadow);
                }

                blastCollision(&megamanX, &blast, &minion, &turret,shadow);
                if(explosionDelay==0) {
                    shadow[2].attr0 = ATTR0_HIDE;
                    explosionDelay=12;
                }
                else {
                    explosionDelay--;
                }

                //player collision
                if(hurting==0) {
                    playerCollision(&megamanX, &minion, &turret, &bullet, shadow);
                }
                //check hurt is second priority
                if(hurting==1) {
                    hurt(&megamanX, shadow);
                }

                // for testing
                // char string[50];
                // char string2[50];
                // sprintf(string, "world x: %d, y: %d", megamanX.mapX, worldmap[megamanX.mapX].y-32);
                // sprintf(string2, "I am x: %d, y: %d", megamanX.mapX, megamanX.r);
                // drawString4(10,0,string,5);
                // drawString4(20,0,string2,5);

                //Enemy
                if(((megamanX.mapX>=50 && megamanX.mapX<=250) || (megamanX.mapX>=922 && megamanX.mapX<=1122)
                        || (megamanX.mapX>=1200 && megamanX.mapX<=1300)) && minionSpawn==0) {	//more condition here to spawn in area
                    minionSpawn = 1;
                    minion.lives=5;
                    minion.c = megamanX.mapX%240+240;
                    if(megamanX.mapX>=50 && megamanX.mapX<=250) {
                        minion.r = 67;	//it's short
                    }
                    else {
                        minion.r = 57;
                    }
                }
                if(minionSpawn==1) {
                    drawMinion(&minion, shadow);
                    if(!borderCheck(minion.r,minion.c)) {
                        minionSpawn=0;
                        shadow[3].attr0 = ATTR0_HIDE;
                    }
                    if(moving!=0 && hurting==0) {
                        minion.c-=moving;
                    }
                }
                //turret
                if(((megamanX.mapX>=300 && megamanX.mapX<=650)
                        || (megamanX.mapX>=950 && megamanX.mapX<=1050)) && turretSpawn==0) {
                    turretSpawn = 1;
                    turret.lives = 10;
                    turret.c = megamanX.mapX%240+240;
                    if(megamanX.mapX>=300 && megamanX.mapX<=650) {
                        turret.r = 36;	//it's short
                    }
                    else {
                        turret.r = 46;
                    }
                }

                if(turretSpawn==1) {
                    drawTurret(&turret, shadow);
                    if(!borderCheck(turret.r,turret.c)) {
                        turretSpawn=0;
                        shadow[4].attr0 = ATTR0_HIDE;
                    }
                    else if(turretFired==0) {
                        bullet.c = turret.c-10;
                        if(megamanX.mapX>=300 && megamanX.mapX<=650) {
                            bullet.r = 45;	//it's short
                        }
                        else {
                            bullet.r = 55;
                        }
                    }
                    if(moving!=0 && hurting==0) {
                        bullet.c-=moving;
                    }

                }
                //fire
                if(turretFired==1) {
                    bulletAnimate(&bullet, shadow);
                }
                if(!borderCheck(bullet.r,bullet.c)) {
                    turretFired=0;
                    shadow[5].attr0 = ATTR0_HIDE;
                }
                else if(turretSpawn==1) {
                    if(bulletDelay<=0) {
                        turretFired=1;
                        bulletDelay=100;
                    }
                    else {
                        bulletDelay--;
                    }
                }
                if(moving!=0 && hurting==0) {
                    turret.c-=moving;
                }


                //spawn drlight when reach end
                if(megamanX.mapX>=STAGE_WIDTH-270) {	//more condition here to spawn in area
                    started=3;
                }
            }

            if(started==3) {
                for(int i=0; i<128; i++) {
                    shadow[i].attr0 = ATTR0_HIDE;
                }
                drawStage(STAGE_WIDTH-270,STAGE_WIDTH,STAGE_HEIGHT,stage);
                shadow[0].attr0 = 52 | X_PALETTE_TYPE | X_SPRITE_SHAPE;
                shadow[0].attr1 = 30 | X_SPRITE_SIZE;
                shadow[0].attr2 = X_ID;
                drlight.c = megamanX.c+160;
                drlight.r = megamanX.r;
                shadow[6].attr0 = drlight.r | X_PALETTE_TYPE | X_SPRITE_SHAPE;
                shadow[6].attr1 = drlight.c | X_SPRITE_SIZE;
                shadow[6].attr2 = DRLIGHT_ID;
                switch (state) {
                case 0:
                    drawHollowRect4(100,0,59,239,0);
                    drawRect4(102,2,56,236,140);
                    char buffer[50];
                    sprintf(buffer, "X:");
                    drawString4(105,5,buffer,0);
                    sprintf(buffer, "Professor!");
                    drawString4(120,5,buffer,0);
                    if(KEY_DOWN_NOW(BUTTON_A) && hold==0) {
                        state=1;
                        hold=1;
                    }
                    break;
                case 1:
                    drawHollowRect4(100,0,59,239,0);
                    drawRect4(102,2,56,236,140);
                    char buffer2[50];
                    sprintf(buffer2, "Professor Light:");
                    drawString4(105,5,buffer2,0);
                    sprintf(buffer2, "Finally, you have come.");
                    drawString4(120,5,buffer2,0);
                    if(KEY_DOWN_NOW(BUTTON_A) && hold==0) {
                        state=2;
                        hold=1;
                    }
                    break;
                case 2:
                    drawHollowRect4(100,0,59,239,0);
                    drawRect4(102,2,56,236,140);
                    char buffer3[50];
                    sprintf(buffer3, "X:");
                    drawString4(105,5,buffer3,0);
                    sprintf(buffer3, "Here's my assignment");
                    drawString4(120,5,buffer3,0);
                    if(KEY_DOWN_NOW(BUTTON_A) && hold==0) {
                        state=3;
                        hold=1;
                    }
                    break;
                case 3:
                    drawHollowRect4(100,0,59,239,0);
                    drawRect4(102,2,56,236,140);
                    char buffer4[50];
                    sprintf(buffer4, "Professor Light:");
                    drawString4(105,5,buffer4,0);
                    char buffer5[50];
                    sprintf(buffer5, "Goodjob X.");
                    drawString4(120,5,buffer5,0);
                    if(KEY_DOWN_NOW(BUTTON_A) && hold==0) {
                        started=4;
                        state=0;
                        hold=1;
                        break;
                    }
                    break;
                }
                if(!KEY_DOWN_NOW(BUTTON_A)) {
                    hold=0;
                }
            }

            if(started==4) {
                drawStage(STAGE_WIDTH-270,STAGE_WIDTH,STAGE_HEIGHT,stage);
                if(telCount<40) {
                    //hide megaman
                    shadow[0].attr0 = ATTR0_HIDE;
                    shadow[2].attr0 = megamanX.r | X_PALETTE_TYPE | X_SPRITE_SHAPE;
                    shadow[2].attr1 = megamanX.c | X_SPRITE_SIZE;
                    shadow[2].attr2 = CHARGES_ID;
                    telCount+=8;
                }
                else if(telCount>=40 && telCount<=120) {
                    shadow[2].attr0 = ATTR0_HIDE;
                    drawRect4(121-telCount,megamanX.c-1+14,30,6,149);
                    drawRect4(121-telCount+1,megamanX.c+14,26,2,0);
                    telCount+=8;
                }
                else {
                    for(int i=0; i<128; i++) {
                        shadow[i].attr0 = ATTR0_HIDE;
                    }
                    megamanX.r = 62;
                    megamanX.c = 30;
                    megamanX.mapX = megamanX.c;
                    megamanX.mapY = megamanX.r;
                    megamanX.inAir = 0;
                    megamanX.lives = 20;
                    started=0;
                    telCount=0;
                    gameState=2;
                }
            }

            waitForVblank();
            flipPage();
            //update spritememory
            DMA[3].src = shadow;
            DMA[3].dst = SPRITEMEM;
            DMA[3].cnt = 128 * 4 | DMA_ON;
        }

        if(gameState==2) {
            stage_stop();
            DMA[3].src = win_palette;
            DMA[3].dst = PALETTE;
            DMA[3].cnt = DMA_ON | WIN_PALETTE_SIZE;
        }
        while(gameState==2) {
            drawImage4(0,0,240,160,win);
            if(KEY_DOWN_NOW(BUTTON_A) || KEY_DOWN_NOW(BUTTON_START) || KEY_DOWN_NOW(BUTTON_SELECT)) {
                gameState=0;
                break;
            }
            waitForVblank();
            flipPage();
        }

        if(gameState==3) {
            stage_stop();
            DMA[3].src = lost_palette;
            DMA[3].dst = PALETTE;
            DMA[3].cnt = DMA_ON | LOST_PALETTE_SIZE;
        }
        while(gameState==3) {
            drawImage4(0,0,240,160,lost);
            if(KEY_DOWN_NOW(BUTTON_A) || KEY_DOWN_NOW(BUTTON_START)) {
                hurting=0;
                megamanX.r = 62;
                megamanX.c = 30;
                megamanX.mapX = megamanX.c;
                megamanX.mapY = megamanX.r;
                megamanX.inAir = 0;
                megamanX.lives = 20;
                started=0;
                telCount=0;
                gameState=1;
                break;
            }
            if(KEY_DOWN_NOW(BUTTON_SELECT)) {
                megamanX.r = 62;
                megamanX.c = 30;
                megamanX.mapX = megamanX.c;
                megamanX.mapY = megamanX.r;
                megamanX.inAir = 0;
                megamanX.lives = 20;
                started=0;
                telCount=0;
                gameState=0;
                break;
            }
            waitForVblank();
            flipPage();
        }
    }
}
//-------------------------------------------------------------------------------------
bool MainApplication::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
    if(mWindow->isClosed())
    return false;

    if(mShutDown)
        return false;

    //Need to capture/update each device
    mKeyboard->capture();
    mMouse->capture();

    Ogre::Vector3 shotsSpeed(500, 0, 0);
    Ogre::Vector3 enemySpeed(-500, rand() % 10 - 5, 0);

    Ogre::Vector3 playerPosition = mSceneMgr -> getSceneNode("NinjaNode")->getPosition();
    Ogre::Vector3 playerDirection = mSceneMgr -> getSceneNode("NinjaNode") -> getOrientation() * Ogre::Vector3(0,0,1);
    //Ogre::Vector3 direction = mSceneMgr -> getSceneNode("NinjaNode") ->_getDerivedOrientation() * Ogre::Vector3::NEGATIVE_UNIT_Z;

    if(playerCollision(playerPosition, playerDirection))
    {
        mSceneMgr -> getSceneNode("NinjaNode") -> setPosition(Ogre::Vector3(0, 0, -500));
    }


    mTrayMgr->frameRenderingQueued(evt);
    mSceneMgr -> getSceneNode("NinjaNode") -> translate(mDirection * evt.timeSinceLastFrame, Ogre::Node::TS_WORLD);
    if(shotCounter)
    {
        //Ogre::String number2 = Ogre::StringConverter::toString(shotCounter); 
        for(int i = 0; i < shots.size(); i++)
        {
            Ogre::Vector3 shotPosition = mSceneMgr -> getSceneNode(shots[i])->getPosition();
            Ogre::Vector3 shotDirection = mSceneMgr -> getSceneNode(shots[i]) -> getOrientation() * Ogre::Vector3(0,0,1);
            if(bulletCollision(shotPosition, shotDirection))
            {
                mSceneMgr -> getSceneNode("NinjaNode") -> setPosition(Ogre::Vector3(0, 0, 0));
                //mDistance = Ogre::Vector3::ZERO;
               //mShutDown = true;
            }
            mSceneMgr -> getSceneNode(shots[i]) -> translate(shotsSpeed * evt.timeSinceLastFrame, Ogre::Node::TS_WORLD);
        }
    }
    if(enemyCounter)
    {
        //Ogre::String number = Ogre::StringConverter::toString(enemyCounter); 
        for(int i = 0; i < enemies.size(); i++)
        {
            mSceneMgr -> getSceneNode(enemies[i]) -> translate(enemySpeed * evt.timeSinceLastFrame, Ogre::Node::TS_WORLD);
        }
    }
    //enemytranslate
    mDistance = mSceneMgr-> getSceneNode("NinjaNode")->getPosition();
    if(mDistance.x >= MAX_RIGHT) 
    {
        mDirection.x = 0;
        mSceneMgr -> getSceneNode("NinjaNode") -> setPosition(Ogre::Vector3(MAX_RIGHT, mDistance.y, mDistance.z));
    }
    if(mDistance.x <= MAX_LEFT)
    {
        mDirection.x = 0;
        mSceneMgr -> getSceneNode("NinjaNode") -> setPosition(Ogre::Vector3(MAX_LEFT, mDistance.y, mDistance.z));
    }
    if(mDistance.y >= MAX_UP) 
    {
        mDirection.y = 0;
        mSceneMgr -> getSceneNode("NinjaNode") -> setPosition(Ogre::Vector3(mDistance.x, MAX_UP, mDistance.z));
    }
    if(mDistance.y <= MAX_DOWN) 
    {
        mDirection.y = 0;
        mSceneMgr -> getSceneNode("NinjaNode") -> setPosition(Ogre::Vector3(mDistance.x, MAX_DOWN, mDistance.z));
    }
    timer++;
    if(timer%20 == 0)
    {
        spawn_enemy();
    }
    return true;
}
Esempio n. 4
0
void Player::updatePlayer()
{
	//Animation logic stuff
	if (flameAnim > animSteps){
		animAscend = false;
	}
	else if (flameAnim < -animSteps){
		animAscend = true;
	}



	//Collision checks
	//Borders
	if (!getExplosionState()){
		if (playerSprite.getPosition().y <= (playerSprite.getGlobalBounds().height / 2) || playerSprite.getPosition().y + (playerSprite.getGlobalBounds().height / 2) >= VideoMode::getDesktopMode().height){

			for (int i = 0; i < 2; i++){
				decreaseHealth(difficulty);
				if (getPlayerHealth() < 0){
					short num = -(getPlayerHealth());
					increaseHealth(num);
				}

				if (hardcore) setExplosionState(true);

				updateCrashSound();

				if (showParticles){
					crashDebrisSpeed.push_back(Vector2f(-(getObstacleBaseSpeed() / 2), getRandom(-2, 2)));
	
					crashDebris.push_back(RectangleShape());
					short ramNum = getRandom(5, 8);
					crashDebris.back().setSize(Vector2f(ramNum, ramNum));
					crashDebris.back().setOrigin(ramNum / 2, ramNum / 2);
					ramNum = getRandom(50, 160);
					crashDebris.back().setFillColor(Color::Color(ramNum, ramNum, ramNum));
					crashDebris.back().setPosition(collisionCircle[0].getPosition().x + getRandom(-5, 5), collisionCircle[0].getPosition().y + getRandom(-5, 5));
				}	
			}
		}

		//Obstacles
		for (int i=0; i<getNumberOfObstacles(); i++){
			if (playerCollision(&collisionCircle[0], &getObstacleObject(i))){
				if (getPlayerHealth() > 0) decreaseHealth(difficulty);
				if (getPlayerHealth() < 0){
					short num = -(getPlayerHealth());
					increaseHealth(num);
				}

				if (hardcore) setExplosionState(true);

				updateCrashSound();
	
				if (showParticles){
					crashDebrisSpeed.push_back(Vector2f(-(getObstacleBaseSpeed() / 2), getRandom(-2, 2)));
			
					crashDebris.push_back(RectangleShape());
					short ramNum = getRandom(5, 8);
					crashDebris.back().setSize(Vector2f(ramNum, ramNum));
					crashDebris.back().setOrigin(ramNum / 2, ramNum / 2);
					ramNum = getRandom(50, 160);
					crashDebris.back().setFillColor(Color::Color(ramNum, ramNum, ramNum));
					crashDebris.back().setPosition(collisionCircle[0].getPosition().x + getRandom(-5, 5), collisionCircle[0].getPosition().y + (getRandom(-5, 5)));
				}
			}

			if (playerCollision(&collisionCircle[1], &getObstacleObject(i))){
				if (getPlayerHealth() > 0) decreaseHealth(difficulty);
				if (getPlayerHealth() < 0){
					short num = -(getPlayerHealth());
					increaseHealth(num);
				}

				if (hardcore) setExplosionState(true);

				updateCrashSound();
			
				if (showParticles){
					crashDebrisSpeed.push_back(Vector2f(-(getObstacleBaseSpeed() / 2), getRandom(-2, 2)));

					crashDebris.push_back(RectangleShape());
					short ramNum = getRandom(4, 7);
					crashDebris.back().setSize(Vector2f(ramNum, ramNum));
					crashDebris.back().setOrigin(ramNum / 2, ramNum / 2);
					ramNum = getRandom(50, 160);
					crashDebris.back().setFillColor(Color::Color(ramNum, ramNum, ramNum));
					crashDebris.back().setPosition(collisionCircle[1].getPosition().x + getRandom(-3, 3), collisionCircle[1].getPosition().y + (getRandom(-3, 3)));
				}
			}

			if (playerCollision(&collisionCircle[2], &getObstacleObject(i))){
				if (getPlayerHealth() > 0) decreaseHealth(difficulty);
				if (getPlayerHealth() < 0){
					short num = -(getPlayerHealth());
					increaseHealth(num);
				}

				if (hardcore) setExplosionState(true);

				updateCrashSound();
			
				if (showParticles){
					crashDebrisSpeed.push_back(Vector2f(-(getObstacleBaseSpeed() / 2), getRandom(-2, 2)));

					crashDebris.push_back(RectangleShape());
					short ramNum = getRandom(4, 7);
					crashDebris.back().setSize(Vector2f(ramNum, ramNum));
					crashDebris.back().setOrigin(ramNum / 2.0f, ramNum / 2.0f);
					ramNum = getRandom(50, 160);
					crashDebris.back().setFillColor(Color::Color(ramNum, ramNum, ramNum));
					crashDebris.back().setPosition(collisionCircle[2].getPosition().x + getRandom(-3, 3), collisionCircle[2].getPosition().y + (getRandom(-3, 3)));
				}
			}
		}
	}

	//Explosion
	if (getPlayerHealth() <= 0 || getExplosionState()){
		setExplosionState(true);
		decreaseFuel(getPlayerFuel());
		updateExplosionText();
		stopRocketSound();
		stopAlarmSound();
		stopCrashSound();

		if (initExplosion){
			for (int i = 0; i < 200; i++){
				crashDebrisSpeed2.push_back(Vector2f(getRandom(-2, 2), getRandom(-2, 2)));

				crashDebris2.push_back(RectangleShape());
				short ramNum = getRandom(4, 7);
				crashDebris2.back().setSize(Vector2f(ramNum, ramNum));
				crashDebris2.back().setOrigin(ramNum / 2.0f, ramNum / 2.0f);
				ramNum = getRandom(50, 160);
				crashDebris2.back().setFillColor(Color::Color(ramNum, ramNum, ramNum));
				crashDebris2.back().setPosition(collisionCircle[0].getPosition().x + getRandom(-3, 3), collisionCircle[0].getPosition().y + (getRandom(-3, 3)));


				damageParticleSpeed2.push_back(Vector2f(getRandom(-3, 3), getRandom(-3, 3)));

				damageParticle2.push_back(CircleShape());
				damageParticle2.back().setRadius(getRandom(1, 2));
				damageParticle2.back().setFillColor(Color::Color(255, 153, 51));
				int j = getRandom(0, 2);
				damageParticle2.back().setPosition(collisionCircle[0].getPosition().x + getRandom(-3, 3), collisionCircle[0].getPosition().y + (getRandom(-3, 3)));
			}

			playExplosionSound();
		}

		for (int i = 0; i < crashDebris2.size(); i++){
			crashDebris2[i].move(crashDebrisSpeed2[i].x - explosionSpeedCounter, crashDebrisSpeed2[i].y);
			if (i % 2 == 0){
				crashDebris2[i].rotate(2);
			}
			else crashDebris2[i].rotate(-2);

			crashDebrisSpeed2[i].x = crashDebrisSpeed2[i].x * 0.985;
			crashDebrisSpeed2[i].y = crashDebrisSpeed2[i].y * 0.985;

			if (crashDebris2[i].getPosition().x < -10){
				crashDebris2.erase(crashDebris2.begin() + i);
				crashDebrisSpeed2.erase(crashDebrisSpeed2.begin() + i);
			}
		}
		for (int i = 0; i < damageParticleSpeed2.size(); i++){
			damageParticle2[i].move(damageParticleSpeed2[i].x - explosionSpeedCounter, damageParticleSpeed2[i].y);

			damageParticleSpeed2[i].x = damageParticleSpeed2[i].x * 0.985;
			damageParticleSpeed2[i].y = damageParticleSpeed2[i].y * 0.985;

			if (damageParticle2[i].getPosition().x < -10){
				damageParticle2.erase(damageParticle2.begin() + i);
				damageParticleSpeed2.erase(damageParticleSpeed2.begin() + i);
			}

		}

		if (explosionSpeedCounter <= 10.0){
			explosionSpeedCounter *= 1.03;
		}

		initExplosion = false;

		if (Keyboard::isKeyPressed(Keyboard::R)){
			setCrashState(true);
			setExplosionState(false);
		}
		else if (Keyboard::isKeyPressed(Keyboard::Escape)) setExitState(true);
	}


	//Updates
	//Crash debris
	if (showParticles){
		for (int i = 0; i < crashDebris.size(); i++){
			crashDebris[i].move(crashDebrisSpeed[i].x, crashDebrisSpeed[i].y);
		
			if (i % 2 == 0) crashDebris[i].rotate(10);
				else crashDebris[i].rotate(-10);

			if (crashDebris[i].getPosition().x < -20){
				crashDebris.erase(crashDebris.begin() + i);
				crashDebrisSpeed.erase(crashDebrisSpeed.begin() + i);
			}
		}

	//Damage particles
		if (getPlayerHealth() > 0 && getPlayerHealth() <= 75){
		
			if (1 >= getRandom(0, 10)){
				damageParticleSpeed.push_back(Vector2f(-(getObstacleBaseSpeed()), 0));

				damageParticle.push_back(CircleShape());

				damageParticle.back().setRadius(1);
				damageParticle.back().setFillColor(Color::Color(255, 153, 51));
				int i = getRandom(0, 2);
				damageParticle.back().setPosition(collisionCircle[i].getPosition().x + getRandom(-3, 3), collisionCircle[i].getPosition().y + (getRandom(-3, 3)));
			}
		}
		if (getPlayerHealth() > 0 && getPlayerHealth() <= 50){
		
			if (1 >= getRandom(0, 5)){
				damageParticleSpeed.push_back(Vector2f(-(getObstacleBaseSpeed()), 0));

				damageParticle.push_back(CircleShape());
			
				damageParticle.back().setRadius(1);
				damageParticle.back().setFillColor(Color::Color(255, 153, 51));
				int i = getRandom(0, 2);
				damageParticle.back().setPosition(collisionCircle[i].getPosition().x + getRandom(-3, 3), collisionCircle[i].getPosition().y + (getRandom(-3, 3)));
			}
		}
		if (getPlayerHealth() > 0 && getPlayerHealth() <= 25){
		
			if (1 >= getRandom(0, 2)){
				damageParticleSpeed.push_back(Vector2f(-(getObstacleBaseSpeed()), 0));

				damageParticle.push_back(CircleShape());
			
				damageParticle.back().setRadius(1);
				damageParticle.back().setFillColor(Color::Color(255, 153, 51));
				int i = getRandom(0, 2);
				damageParticle.back().setPosition(collisionCircle[i].getPosition().x + getRandom(-3, 3), collisionCircle[i].getPosition().y + (getRandom(-3, 3)));
			}

			if (1 >= getRandom(0, 50)){

			}
		}

		for (int i = 0; i < damageParticle.size(); i++){
			damageParticle[i].move(damageParticleSpeed[i].x, damageParticleSpeed[i].y);

			if (damageParticle[i].getPosition().x < -10){
				damageParticle.erase(damageParticle.begin() + i);
				damageParticleSpeed.erase(damageParticleSpeed.begin() + i);
			}
		}
	}

	if (getPlayerFuel() > 0 && getPlayerHealth() > 0){

		if (controlScheme == 1){
			if (Keyboard::isKeyPressed(Keyboard::Space)){
				if (playerRotation >= -30){
					playerRotation -= 15;
					if (playerRotation < -30) playerRotation = playerRotation - (playerRotation + 30);
					playerSprite.setRotation(playerRotation);
				}

				playerSprite.move(0, baseClimingSpeed); //Climing speed is always constant
	
				playerSpeed = -(baseFallingSpeed +3); //Set speed value so that transition from climing to falling feels smooth
			}
			else {
				if (playerRotation <= 20){
					playerRotation += 5;
					playerSprite.setRotation(playerRotation);
				}

				playerSprite.move(0, baseFallingSpeed + playerSpeed);
		
				if (playerSpeed <= 0) playerSpeed = playerSpeed + 1;
				else if ((playerSpeed <= baseFallingSpeed) && (playerSpeed > 0)) playerSpeed = playerSpeed * 1.02;
			}
		}
		else if (controlScheme == 2){
			if (Keyboard::isKeyPressed(Keyboard::Left) && playerRotation >= -30){
				playerRotation -= 3;
			}
			
			else if (Keyboard::isKeyPressed(Keyboard::Right) && playerRotation <= 30){
				playerRotation += 3;
			}
			else if (Keyboard::isKeyPressed(Keyboard::Space)){
				playerRotation = 0;
			}
			
			playerSprite.setRotation(playerRotation);
			playerSprite.move(0, playerRotation / 3);
		}
		else if (controlScheme == 3){
			if (Keyboard::isKeyPressed(Keyboard::Left)){
				if (playerRotation >= -30) playerRotation -= 5;
			}
			
			else if (Keyboard::isKeyPressed(Keyboard::Right)){
				if (playerRotation <= 30) playerRotation += 5;
			}
			else {
				if (playerRotation < 0){
					playerRotation += 5;
				}
				else if (playerRotation > 0) playerRotation -= 5;
			}
			
			playerSprite.setRotation(playerRotation);
			playerSprite.move(0, playerRotation / 3);
		}

		if (getPlayerHealth() > 0) updateRocketSound();
	}
	else if (getPlayerFuel() <= 0){
		if (!playedShutdownSound){
			playShutdownSound();
			playedShutdownSound = true;
		}

		stopRocketSound();

		if (playerRotation < 0) playerSprite.move(0, playerRotation / 6);
		else if (playerRotation > 0) playerSprite.move(0, playerRotation / 3);

		if (playerRotation < 0) playerSprite.rotate(-1);
		else if (playerRotation > 0) playerSprite.rotate(1);
	}
	

	//Flame Sprite Bottom
	flameSpriteBottom.setPosition((playerSprite.getPosition().x - 45) - (playerRotation / 2.4), (playerSprite.getPosition().y + 28) + -(playerRotation / 1.05));
	flameSpriteBottom.setRotation(playerSprite.getRotation());

	if (animAscend){
		flameScale += 0.012;
		flameSpriteBottom.setScale(flameScale, flameSpriteBottom.getScale().y);
		flameAnim++;
	}
	else if (!animAscend){
		flameScale -= 0.012;
		flameSpriteBottom.setScale(flameScale, flameSpriteBottom.getScale().y);
		flameAnim--;
	}


	//Flame Sprite Top
	flameSpriteTop.setPosition(playerSprite.getPosition().x - 45, (playerSprite.getPosition().y - 7) + -(playerRotation / 1.05));
	flameSpriteTop.setRotation(playerSprite.getRotation());

	if (animAscend){
		flameScale += 0.012;
		flameSpriteTop.setScale(flameScale, flameSpriteBottom.getScale().y);
		flameAnim++;
	}
	else if (!animAscend){
		flameScale -= 0.012;
		flameSpriteTop.setScale(flameScale, flameSpriteBottom.getScale().y);
		flameAnim--;
	}

	collisionCircle[0].setPosition(playerSprite.getPosition().x + 20, playerSprite.getPosition().y + ((playerRotation / 3) + 2));
	collisionCircle[1].setPosition(playerSprite.getPosition().x - (24 - (playerRotation / 5)), playerSprite.getPosition().y - ((playerRotation / 2) + 12));
	collisionCircle[2].setPosition(playerSprite.getPosition().x - (24 + (playerRotation / 5)), playerSprite.getPosition().y - ((playerRotation / 2) - 14));
}