// テキストの表示 void resultText::displayText() { // 保存データの読み込み UserDefault *userDef = UserDefault::getInstance(); float angle = userDef->getFloatForKey("scoreAngle", 0); float resultAngle = angle; if ( resultAngle < 0 ) { resultAngle = resultAngle * (-1); } float distance = userDef->getFloatForKey("scoreDistance", 0); float resultDistance = distance; // 角度表示 Label *viewAngle = Label::createWithSystemFont("a", "Arial", 50); viewAngle->setPosition(WINSIZE.width/2, WINSIZE.height/1.2); addChild(viewAngle); viewAngle->setString(StringUtils::format("角度:%0.2f°", resultAngle)); log("%0.2f°",resultAngle); // 距離表示 Label *viewPixel = Label::createWithSystemFont("", "Arial", 50); viewPixel->setPosition(WINSIZE.width/2, WINSIZE.height/1.37); addChild(viewPixel); viewPixel->setString(StringUtils::format("背表紙のりまでの間隔:%0.2fピクセル", resultDistance)); log("%0.2fピクセル", resultDistance); // 点数表示 Label *viewScore = Label::createWithSystemFont("", "Arial", 50); viewScore->setPosition(WINSIZE.width/2, WINSIZE.height/1.6); addChild(viewScore); viewScore->setString(StringUtils::format("点数:%ld", score)); // 評価文章表示 Label *viewText1 = Label::createWithSystemFont(arrayText[0], "Arial", 27); viewText1->setPosition(WINSIZE.width/2, 300); addChild(viewText1); Label *viewText2 = Label::createWithSystemFont(arrayText[1], "Arial", 27); viewText2->setPosition(WINSIZE.width/2, 250); addChild(viewText2); Label *viewText3 = Label::createWithSystemFont(arrayText[2], "Arial", 27); viewText3->setPosition(WINSIZE.width/2, 200); addChild(viewText3); viewText1->setString(StringUtils::format("プロジェクトマネージャー...%s", arrayText[0].c_str())); viewText2->setString(StringUtils::format("慇懃無礼な商人...%s", arrayText[1].c_str())); viewText3->setString(StringUtils::format("C...%s", arrayText[2].c_str())); log("ラベル配置"); }
bool GameScene::onContactBegin(PhysicsContact& contact) { auto bodyA = contact.getShapeA()->getBody(); auto bodyB = contact.getShapeB()->getBody(); distanceFromHook = player->getPosition().distance(contact.getShapeA()->getBody()->getPosition()); if ((bodyA->getCategoryBitmask() & bodyB->getCollisionBitmask()) == 0 || (bodyB->getCategoryBitmask() & bodyA->getCollisionBitmask()) == 0) { // Player hits goal if (bodyA->getTag() == PLAYER && bodyB->getTag() == GOAL) { CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); if (!Constant::soundMuted) { CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("Sounds/Goal/IndianWarChant 6077_99.mp3"); } UserDefault *def = UserDefault::getInstance(); def->getFloatForKey(Constant::mapname.c_str(), highscore); if (timeMilliseconds < highscore || highscore == 0) { highscore = timeMilliseconds; def->setFloatForKey(Constant::mapname.c_str(), highscore); } this->GoToLevelMenuScene(this); return false; } if (bodyB->getTag() == PLAYER && bodyA->getTag() == GOAL) { this->GoToLevelMenuScene(this); return false; } // Hook hits player, cancel if (bodyA->getTag() == PLAYER && bodyB->getTag() == HOOK) { return false; } if (bodyB->getTag() == PLAYER && bodyA->getTag() == HOOK) { return false; } // RayBox hits player, cancel if (bodyA->getTag() == PLAYER && bodyB->getTag() == RAYCASTCOLLISIONBOX) { return false; } if (bodyB->getTag() == PLAYER && bodyA->getTag() == RAYCASTCOLLISIONBOX) { return false; } // RayBox hits spikes, cancel if (bodyA->getTag() == SPIKE && bodyB->getTag() == RAYCASTCOLLISIONBOX) { sprite->stopAllActions(); return false; } if (bodyB->getTag() == SPIKE && bodyA->getTag() == RAYCASTCOLLISIONBOX) { sprite->stopAllActions(); return false; } // Player hits spikes, suffer agonizing death. if (bodyA->getTag() == SPIKE && bodyB->getTag() == PLAYER) { this->RestartScene(this); return false; } if (bodyB->getTag() == SPIKE && bodyA->getTag() == PLAYER) { this->RestartScene(this); return false; } // // RayBox hits metal, cancel if (bodyA->getTag() == RAYCASTCOLLISIONBOX && bodyB->getTag() == METAL) { sprite->stopAllActions(); return false; } if (bodyB->getTag() == RAYCASTCOLLISIONBOX && bodyA->getTag() == METAL) { sprite->stopAllActions(); return false; } if (sprite) { // RayBox hits tiles if (player->isTouchHold) { if (bodyA->getTag() == RAYCASTCOLLISIONBOX && bodyB->getTag() == TILE) { sprite->stopAllActions(); boxHitPos = bodyA->getPosition(); player->getPlayer()->stopAllActions(); player->Fleng(); player->isHooked = true; return true; } else if (bodyB->getTag() == RAYCASTCOLLISIONBOX && bodyA->getTag() == TILE) { sprite->stopAllActions(); boxHitPos = bodyA->getPosition(); player->getPlayer()->stopAllActions(); player->isHooked = true; player->Fleng(); return true; } } } } return true; }
Bird::Bird( cocos2d::Layer *layer, b2World *world ) { visibleSize = Director::getInstance()->getVisibleSize(); int visibleWidth = visibleSize.width; int visibleHeight = visibleSize.height; float positionX, positionY; long whichBird; auto region = rand() % 4 + 1; // Adjust random region based on previous regions UserDefault *def = UserDefault::getInstance(); auto velocity = def->getFloatForKey("velocity", 0); auto power3_activated = def->getIntegerForKey("power3_activated",0); auto bird1_region = def->getIntegerForKey("bird1_region", 0); auto bird2_region = def->getIntegerForKey("bird2_region", 0); auto blue1 = def->getIntegerForKey("blue1"); auto blue2 = def->getIntegerForKey("blue2"); auto blue3 = def->getIntegerForKey("blue3"); if(bird1_region == 0) { def->setIntegerForKey("bird1_region", region); whichBird = 1; } else { if(bird2_region == 0){ while(region == bird1_region){ region = rand() % 4 + 1; } def->setIntegerForKey("bird2_region", region); whichBird = 2; } else { while(region == bird1_region || region == bird2_region){ region = rand() % 4 + 1; } whichBird = 3; } } if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { bird = Sprite::create("bird_left.png"); } else { bird = Sprite::create("blue_left.png"); } def->flush(); auto rotation = rand() % 46 + 1; int halfBird = bird->getContentSize().width / 2; auto birdY = rand() % (visibleHeight - halfBird) + halfBird; auto birdX = rand() % (visibleWidth - halfBird) + halfBird; bool top = false; if(region == 1 || region == 3) { if(birdY > visibleHeight / 2){ top = true; rotation = rand() % 26 + 1; } } else { rotation = rotation + 45; if(birdX > visibleWidth / 2) top = true; } auto angle = rotation * M_PI / 180; auto xPos = cos(angle) * (visibleWidth * 1.5); auto yPos = sin(angle) * (visibleWidth * 1.5); // Set the bird's position based on region if(region == 1) // Left { bird->setPosition(Point(-halfBird,birdY)); if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { bird->setTexture("bird_right.png"); } else { bird->setTexture("blue_right.png"); } if(top) // Above middle of screen { bird->setRotation(rotation); bird->setTag(1); positionX = bird->getPositionX() + xPos; positionY = -(bird->getPositionY() + yPos); } else // Below middle of screen { bird->setRotation(-rotation); bird->setTag(2); positionX = bird->getPositionX() + xPos; positionY = bird->getPositionY() + yPos; } } if(region == 2) // Top { bird->setPosition(Point(birdX, visibleHeight + halfBird)); if(top) // Past middle of screen on x axis { bird->setRotation(-rotation); bird->setTag(3); positionX = -(bird->getPositionX() + xPos); positionY = -(bird->getPositionY() + yPos); } else // Before middle of screen on x axis { bird->setTag(4); if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { bird->setTexture("bird_right.png"); } else { bird->setTexture("blue_right.png"); } bird->setRotation(rotation); positionX = bird->getPositionX() + xPos; positionY = -(bird->getPositionY() + yPos); } } if(region == 3) // Right { bird->setPosition(Point(visibleWidth + halfBird, birdY)); if(top) // Above middle of screen { bird->setRotation(-rotation); bird->setTag(5); positionX = -(bird->getPositionX() + xPos); positionY = -(bird->getPositionY() + yPos); } else // Below middle of screen { bird->setRotation(rotation); bird->setTag(6); positionX = -(bird->getPositionX() + xPos); positionY = bird->getPositionY() + yPos; } } if(region == 4) // Bottom { bird->setPosition(Point(birdX, -halfBird)); if(top) // Past middle of screen on x axis { bird->setRotation(rotation); bird->setTag(7); positionX = -(bird->getPositionX() + xPos); positionY = bird->getPositionY() + yPos; } else // Before middle of screen on x axis { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { bird->setTexture("bird_right.png"); } else { bird->setTexture("blue_right.png"); } bird->setRotation(-rotation); bird->setTag(8); positionX = bird->getPositionX() + xPos; positionY = bird->getPositionY() + yPos; } } // Create ball body b2BodyDef ballBodyDef; ballBodyDef.type = b2_dynamicBody; ballBodyDef.userData = bird; ballBodyDef.position.Set(bird->getPositionX() / PTM_RATIO,bird->getPositionY() / PTM_RATIO); b2Body *ballBody = world->CreateBody(&ballBodyDef); // Create circle shape b2CircleShape circle; circle.m_radius = (bird->getContentSize().height / 2) / PTM_RATIO; // Create shape definition and add body b2FixtureDef ballShapeDef; ballShapeDef.shape = &circle; ballShapeDef.density = 1.0f; ballShapeDef.friction = 0.f; ballShapeDef.restitution = 0.f; ballShapeDef.isSensor = true; ballShapeDef.filter.categoryBits = BIRD_COLLISION_BITMASK; ballShapeDef.filter.maskBits = BIRD_COLLISION_BITMASK; ballShapeDef.userData = (void*) whichBird; ballBody->SetLinearVelocity( b2Vec2(positionX * velocity / PTM_RATIO, positionY * velocity / PTM_RATIO)); ballBody->CreateFixture(&ballShapeDef); if(!power3_activated){ auto tmpScale = bird->getScale(); //tmpScale = tmpScale / 1.5; bird->setScale(tmpScale); } else { bird->setScale(1.5); } layer->addChild(bird, 100); //setup touch listener auto touchListener = EventListenerTouchOneByOne::create(); touchListener->setSwallowTouches( true ); touchListener->onTouchBegan = CC_CALLBACK_2( Bird::onTouchBegan, this ); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, bird); }
void MyContactListener::BeginContact(b2Contact* contact) { Size visibleSize = Director::getInstance()->getVisibleSize(); UserDefault *def = UserDefault::getInstance(); int visibleWidth = visibleSize.width; int visibleHeight = visibleSize. height; int halfVisibleWidth = visibleSize.width / 2; int halfVisibleHeight = visibleSize.height / 2; float positionX, positionY; long whichBird; b2Body* bird; b2Body* border; b2Fixture* a = contact->GetFixtureA(); b2Fixture* b = contact->GetFixtureB(); //make sure only one of the fixtures was a sensor bool sensorA = a->IsSensor(); bool sensorB = b->IsSensor(); if ( ! (sensorA && sensorB) ) { if(a->IsSensor()){ bird = a->GetBody(); border = b->GetBody(); whichBird = (long)a->GetUserData(); } else { border = a->GetBody(); bird = b->GetBody(); whichBird = (long)b->GetUserData(); } Sprite* birdSprite = (Sprite*)bird->GetUserData(); float birdX = birdSprite->getPositionX(); float birdY = birdSprite->getPositionY(); auto birdTag = birdSprite->getTag(); long borderCheck = (long)border->GetUserData(); auto blue1 = def->getIntegerForKey("blue1"); auto blue2 = def->getIntegerForKey("blue2"); auto blue3 = def->getIntegerForKey("blue3"); auto velocity = def->getFloatForKey("velocity", 0); auto rotation = rand() % 46 + 1; bool top = false; if(borderCheck == 1 || borderCheck == 3) // bottom and right { if(birdY > visibleHeight / 2){ top = true; rotation = rand() % 26 + 1; } } else { rotation = rotation + 45; if(birdX > visibleWidth / 2) top = true; } auto angle = rotation * M_PI / 180; auto xPos = cos(angle) * (visibleWidth * 1.5); auto yPos = sin(angle) * (visibleWidth * 1.5); // Add missed bird and stop redirection if 3 collisions. GameOver if 3 misses in total if(((birdTag == 1 || birdTag == 4) && (borderCheck == 1 || borderCheck == 3)) || ((birdTag == 2 || birdTag == 8) && (borderCheck == 2 || borderCheck == 3)) || ((birdTag == 3 || birdTag == 5) && (borderCheck == 1 || borderCheck == 4)) || ((birdTag == 6 || birdTag == 7) && (borderCheck == 2 || borderCheck == 4))) { if(whichBird == 1) { auto round1_misses = def->getBoolForKey("bird1_missed", 0); int bird1_collisions = def->getIntegerForKey("bird1_collisions", 0); bird1_collisions++; def->setIntegerForKey("bird1_collisions", bird1_collisions); if(bird1_collisions >= 3 && !round1_misses) { auto misses = def->getIntegerForKey("misses", 0); misses++; def->setIntegerForKey("misses", misses); auto bird_count = def->getIntegerForKey("bird_count", 0); bird_count++; def->setIntegerForKey("bird_count", bird_count); def->setBoolForKey("bird1_missed", 1); Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(birdSprite); return; } else { if(bird1_collisions >= 3) { return; } } } if(whichBird == 2) { auto round2_misses = def->getBoolForKey("bird2_missed", 0); int bird2_collisions = def->getIntegerForKey("bird2_collisions", 0); bird2_collisions++; def->setIntegerForKey("bird2_collisions", bird2_collisions); if(bird2_collisions >= 3 && !round2_misses) { auto misses = def->getIntegerForKey("misses", 0); misses++; def->setIntegerForKey("misses", misses); auto bird_count = def->getIntegerForKey("bird_count", 0); bird_count++; def->setIntegerForKey("bird_count", bird_count); def->setBoolForKey("bird2_missed", 1); Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(birdSprite); return; } else { if(bird2_collisions >= 3) { return; } } } if(whichBird == 3) { auto round3_misses = def->getBoolForKey("bird3_missed", 0); int bird3_collisions = def->getIntegerForKey("bird3_collisions", 0); bird3_collisions++; def->setIntegerForKey("bird3_collisions", bird3_collisions); if(bird3_collisions >= 3 && !round3_misses) { auto misses = def->getIntegerForKey("misses", 0); misses++; def->setIntegerForKey("misses", misses); auto bird_count = def->getIntegerForKey("bird_count", 0); bird_count++; def->setIntegerForKey("bird_count", bird_count); def->setBoolForKey("bird3_missed", 1); Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(birdSprite); return; } else { if(bird3_collisions >= 3) { return; } } } } // Redirect Bird if((birdTag == 1 || birdTag == 4) && (borderCheck == 1 || borderCheck == 3)) // LeftTop || TopLeft -> bottom and right { if(borderCheck == 1) // Bottom { if( birdX > halfVisibleWidth ) { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_left.png"); } else { birdSprite->setTexture("blue_left.png"); } birdSprite->setTag(7); birdSprite->setRotation(rotation); positionX = -(birdX + xPos); positionY = birdY + yPos; } else { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_right.png"); } else { birdSprite->setTexture("blue_right.png"); } birdSprite->setTag(8); birdSprite->setRotation(-rotation); positionX = birdX + xPos; positionY = birdY + yPos; } } else { // Right if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_left.png"); } else { birdSprite->setTexture("blue_left.png"); } if( birdY > halfVisibleHeight ) { birdSprite->setTag(5); birdSprite->setRotation(-rotation); positionX = -(birdX + xPos); positionY = -(birdY + yPos); } else { birdSprite->setTag(6); birdSprite->setRotation(rotation); positionX = -(birdX + xPos); positionY = birdY + yPos; } } } if((birdTag == 2 || birdTag == 8) && (borderCheck == 2 || borderCheck == 3)) // LeftBottom || BottomLeft -> top and right { if(borderCheck == 2) // Top { if( birdX > halfVisibleWidth ) { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_left.png"); } else { birdSprite->setTexture("blue_left.png"); } birdSprite->setTag(3); birdSprite->setRotation(-rotation); positionX = -(birdX + xPos); positionY = -(birdY + yPos); } else { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_right.png"); } else { birdSprite->setTexture("blue_right.png"); } birdSprite->setTag(4); birdSprite->setRotation(rotation); positionX = birdX + xPos; positionY = -(birdY + yPos); } } else { // Right if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_left.png"); } else { birdSprite->setTexture("blue_left.png"); } if( birdY > halfVisibleHeight ) { birdSprite->setTag(5); birdSprite->setRotation(-rotation); positionX = -(birdX + xPos); positionY = -(birdY + yPos); } else { birdSprite->setTag(6); birdSprite->setRotation(rotation); positionX = -(birdX + xPos); positionY = birdY + yPos; } } } if((birdTag == 3 || birdTag == 5) && (borderCheck == 1 || borderCheck == 4)) // TopRight || RightTop -> bottom and left { if(borderCheck == 1) // Bottom { if( birdX > halfVisibleWidth ) { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_left.png"); } else { birdSprite->setTexture("blue_left.png"); } birdSprite->setTag(7); birdSprite->setRotation(rotation); positionX = -(birdX + xPos); positionY = birdY + yPos; } else { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_right.png"); } else { birdSprite->setTexture("blue_right.png"); } birdSprite->setTag(8); birdSprite->setRotation(-rotation); positionX = birdX + xPos; positionY = birdY + yPos; } } else { // Left if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_right.png"); } else { birdSprite->setTexture("blue_right.png"); } if( birdY > halfVisibleHeight ) { birdSprite->setTag(1); birdSprite->setRotation(rotation); positionX = birdX + xPos; positionY = -(birdY + yPos); } else { birdSprite->setTag(2); birdSprite->setRotation(-rotation); positionX = birdX + xPos; positionY = birdY + yPos; } } } if((birdTag == 6 || birdTag == 7) && (borderCheck == 2 || borderCheck == 4)) // RightBottom || BottomRight -> top and left { if(borderCheck == 2) // Top { if( birdX > halfVisibleWidth ) { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_left.png"); } else { birdSprite->setTexture("blue_left.png"); } birdSprite->setTag(3); birdSprite->setRotation(-rotation); positionX = -(birdX + xPos); positionY = -(birdY + yPos); } else { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_right.png"); } else { birdSprite->setTexture("blue_right.png"); } birdSprite->setTag(4); birdSprite->setRotation(rotation); positionX = birdX + xPos; positionY = -(birdY + yPos); } } else { // Left if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { birdSprite->setTexture("bird_right.png"); } else { birdSprite->setTexture("blue_right.png"); } if( birdY > halfVisibleHeight ) { birdSprite->setTag(1); birdSprite->setRotation(rotation); positionX = birdX + xPos; positionY = -(birdY + yPos); } else { birdSprite->setTag(2); birdSprite->setRotation(-rotation); positionX = birdX + xPos; positionY = birdY + yPos; } } } if(((birdTag == 1 || birdTag == 4) && (borderCheck == 1 || borderCheck == 3)) || ((birdTag == 2 || birdTag == 8) && (borderCheck == 2 || borderCheck == 3)) || ((birdTag == 3 || birdTag == 5) && (borderCheck == 1 || borderCheck == 4)) || ((birdTag == 6 || birdTag == 7) && (borderCheck == 2 || borderCheck == 4))) { bird->SetLinearVelocity( b2Vec2(positionX * velocity / PTM_RATIO, positionY * velocity / PTM_RATIO)); } def->flush(); contact->SetEnabled(false); } }