| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 #include "gameplay_scene.h" | |
| 5 #include "physics_layer.h" | |
| 6 | |
| 7 /** | |
| 8 * Tags used by the GameplayScene to identify child elements. Actual values | |
| 9 * here are not important as long as they are unique within the scene. | |
| 10 */ | |
| 11 enum ObjectTags { | |
| 12 TAG_LAYER_PHYSICS = 100, | |
| 13 TAG_LAYER_UI = 101, | |
| 14 TAG_LAYER_OVERLAY = 102, | |
| 15 }; | |
| 16 | |
| 17 USING_NS_CC; | |
| 18 | |
| 19 GameplayScene* GameplayScene::create(int level_number) | |
| 20 { | |
| 21 GameplayScene* scene = new GameplayScene(level_number); | |
| 22 if (!scene) | |
| 23 return NULL; | |
| 24 scene->init(); | |
| 25 return scene; | |
| 26 } | |
| 27 | |
| 28 void GameplayScene::Restart() { | |
| 29 removeChild(getChildByTag(TAG_LAYER_PHYSICS)); | |
| 30 removeChild(getChildByTag(TAG_LAYER_OVERLAY)); | |
| 31 CCLayer* physics = PhysicsLayer::create(level_number_); | |
| 32 addChild(physics, 1, TAG_LAYER_PHYSICS); | |
| 33 } | |
| 34 | |
| 35 void GameplayScene::GameOver(bool success) { | |
| 36 CCSize visible_size = CCDirector::sharedDirector()->getVisibleSize(); | |
| 37 | |
| 38 // Create a black overlay layer with success/failure message | |
| 39 CCLayer* overlay = CCLayerColor::create(ccc4(0x0, 0x0, 0x0, 0x0)); | |
| 40 const char* text = success ? "Success!" : "Failure!"; | |
| 41 CCLabelTTF* label = CCLabelTTF::create(text, "Arial.ttf", 24); | |
| 42 label->setPosition(ccp(visible_size.width/2, visible_size.height/2)); | |
| 43 overlay->addChild(label); | |
| 44 addChild(overlay, 2, TAG_LAYER_OVERLAY); | |
| 45 | |
| 46 // Face the overlay layer into to 50% | |
| 47 CCActionInterval* fadein = CCFadeTo::create(0.5f, 0x7F); | |
| 48 overlay->runAction(fadein); | |
| 49 } | |
| 50 | |
| 51 bool GameplayScene::init() { | |
| 52 if (!CCScene::init()) | |
| 53 return false; | |
| 54 CCLayer* ui = UILayer::create(); | |
| 55 addChild(ui, 3, TAG_LAYER_UI); | |
| 56 | |
| 57 Restart(); | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 void UILayer::Restart(CCObject* sender) { | |
| 62 CCLog("Restart pressed"); | |
| 63 GameplayScene* scene = static_cast<GameplayScene*>(getParent()); | |
| 64 scene->Restart(); | |
| 65 } | |
| 66 | |
| 67 void UILayer::Exit(CCObject* sender) { | |
| 68 CCLog("Exit pressed"); | |
| 69 CCDirector::sharedDirector()->popScene(); | |
| 70 } | |
| 71 | |
| 72 #ifdef COCOS2D_DEBUG | |
| 73 void UILayer::ToggleDebug(CCObject* sender) { | |
| 74 CCLog("ToggleDebug pressed"); | |
| 75 GameplayScene* scene = static_cast<GameplayScene*>(getParent()); | |
| 76 CCNode* physics = scene->getChildByTag(TAG_LAYER_PHYSICS); | |
| 77 static_cast<PhysicsLayer*>(physics)->ToggleDebug(); | |
| 78 } | |
| 79 #endif | |
| 80 | |
| 81 bool UILayer::init() { | |
| 82 if (!CCLayer::init()) | |
| 83 return false; | |
| 84 setTouchEnabled(true); | |
| 85 | |
| 86 // Create the menu. | |
| 87 CCLabelTTF* restart_label = CCLabelTTF::create("Restart", "Arial.ttf", 24); | |
| 88 CCLabelTTF* exit_label = CCLabelTTF::create("Exit", "Arial.ttf", 24); | |
| 89 | |
| 90 CCMenuItemLabel* restart = CCMenuItemLabel::create( | |
| 91 restart_label, this, menu_selector(UILayer::Restart)); | |
| 92 | |
| 93 CCMenuItemLabel* exit = CCMenuItemLabel::create( | |
| 94 exit_label, this, menu_selector(UILayer::Exit)); | |
| 95 | |
| 96 #ifdef COCOS2D_DEBUG | |
| 97 CCLabelTTF* debug_label = CCLabelTTF::create("Toggle Debug", "Arial.ttf", 24); | |
| 98 CCMenuItemLabel* debug = CCMenuItemLabel::create( | |
| 99 debug_label, this, menu_selector(UILayer::ToggleDebug)); | |
| 100 CCMenu* menu = CCMenu::create(restart, exit, debug, NULL); | |
| 101 #else | |
| 102 CCMenu* menu = CCMenu::create(restart, exit, NULL); | |
| 103 #endif | |
| 104 | |
| 105 menu->alignItemsVertically(); | |
| 106 #ifdef COCOS2D_DEBUG | |
| 107 debug->setPositionX(debug->getContentSize().width / 2); | |
| 108 #endif | |
| 109 restart->setPositionX(restart->getContentSize().width / 2); | |
| 110 exit->setPositionX(exit->getContentSize().width / 2); | |
| 111 | |
| 112 addChild(menu); | |
| 113 | |
| 114 // Position the menu | |
| 115 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); | |
| 116 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); | |
| 117 float xpos = origin.x + visibleSize.width * 0.01f; | |
| 118 float ypos = origin.y + visibleSize.height / 2; | |
| 119 menu->setPosition(ccp(xpos, ypos)); | |
| 120 return true; | |
| 121 } | |
| OLD | NEW |