Blame view

ios/Runner/Wowgame/Classes/game_halloween/HLevelPickerLayer.cpp 2.96 KB
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  //
  //  HLevelPickerLayer.cpp
  //  HalloweenSpaceInvaders-mobile
  //
  //  Created by Katarzyna Kalinowska-Górska on 03/10/2019.
  //
  
  #include <stdio.h>
  #include "HLevelPickerLayer.h"
  #include "HScalingUtils.h"
  #include "HalloweenSimpleButton.h"
  #include "HMiscUtils.h"
  
  HLevelPickerLayer* HLevelPickerLayer::create(float width, float height){
      HLevelPickerLayer * view = new (std::nothrow) HLevelPickerLayer();
      if(view && view->init(width, height))
      {
          view->autorelease();
          return view;
      }
      CC_SAFE_DELETE(view);
      return nullptr;
  }
  
  bool HLevelPickerLayer::init(float width, float height){
      if(!cocos2d::LayerColor::initWithColor(cocos2d::Color4B(0,0,0,220), width, height)){
          return false;
      }
      
      setCascadeOpacityEnabled(true);
      // add the level picker
      std::vector<std::string> levelImagePaths = { "graphics/levels/level_1.png", "graphics/levels/level_2.png", "graphics/levels/level_3.png" };
      std::vector<std::string> levelNames = { "EASY", "MEDIUM", "DIFFICULT" }; //TODO HARD CODED
      _HLevelPickerView = HLevelPickerView::create(levelImagePaths, levelNames);
      addChild(_HLevelPickerView);
      _HLevelPickerView->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
      _HLevelPickerView->setPosition(width/2, height/2);
      _HLevelPickerView->addOnLevelChangedCallback([](int pickedLevel){
          HMiscUtils::saveLastLevel(pickedLevel);
      });
      _HLevelPickerView->selectLevelIndex((int)HMiscUtils::lastLevel(), false);
      
      // add the instruction label
      auto instructionLabel = cocos2d::Label::createWithTTF("CHOOSE YOUR LEVEL:", "fonts/ComicSansMSBold.ttf", 120*HScalingUtils::getScaleForFont()); //magic number
      addChild(instructionLabel);
      auto paddingTop = instructionLabel->getBoundingBox().size.height*1.5f;
      instructionLabel->setPosition(width/2, _HLevelPickerView->getBoundingBox().getMaxY() + paddingTop);
      
      // add the go button
      auto buttonGo = HalloweenSimpleButton::create();
      auto goButtonTexturePath = "buttons/graphics/dark_green.png";
      buttonGo->loadTextures(goButtonTexturePath, goButtonTexturePath, goButtonTexturePath);
      auto buttonBg = cocos2d::Sprite::create("buttons/graphics/button_go.png");
      buttonGo->addChild(buttonBg);
      buttonBg->setPosition(cocos2d::Vec2(buttonGo->getContentSize().width/2,buttonGo->getContentSize().height/2));
      addChild(buttonGo);
      auto paddingBottom = paddingTop;
      buttonGo->setAnchorPoint(cocos2d::Vec2(1, 0.5));
      buttonGo->setPosition(cocos2d::Vec2(_HLevelPickerView->getBoundingBox().getMaxX(), _HLevelPickerView->getBoundingBox().getMinY() - paddingBottom));
      buttonGo->setOnTouchEndedCallback([&](std::string name, cocos2d::ui::Widget::TouchEventType eventType){
          if(_onGoPressedCallback != nullptr){
              _onGoPressedCallback();
          }
          HMiscUtils::hideAndRemoveView(this, true);
      });
      
      return true;
  }
  
  void HLevelPickerLayer::addOnGoPressedCallback(std::function<void()> onGoPressed){
      _onGoPressedCallback = onGoPressed;
  }