#include "MainScene.h" #include "SimpleAudioEngine.h" #include "JniFun.h" #include "SetScene.h" #include "HelpScene.h" #include "PopScene.h" #include "JoinScene.h" #include "RecordScene.h" #include "PlayerScene.h" #include "PayScene.h" #include "PayLogScene.h" #include "DuiHuanScene.h" #include "GongHuiScene.h" #include "MissionWeiXin.h" #include "UserInfo.h" #include "ImagicDownManager.h" #include "LoadingScene.h" #include "ShowScene.h" #include "GlobalJosn.h" #include "LogonScene.h" #include "YSAudioEngine.h" #include "GameCreator.h" #include "WN_GameScene.h" #include "NoticeScene.h" #include "CreateScene.h" #include "WN_GameScene.h" #include "DDZ_GameScene.h" #include "NN_GameScene.h" #include "DZ_GameScene.h" #include "13S_GameScene.h" #include "ZJH_GameScene.h" #include "PDK_GameScene.h" MainScene::MainScene() //:m_kUserInGameServer(GlobalJosn::getInstance()->m_strLogonIp.c_str(), GlobalJosn::getInstance()->m_iPort) { m_rootPanel = nullptr; m_btnClickType = BT_CLICK_NULL; UserInfo::Instance().addUpPlayerInfoCB(this,QY_CALLFUNC_SELECTOR(MainScene::upPlayerInfo)); auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->addCustomEventListener(CREATE_PRIVATE_ROOM_EVENT, CC_CALLBACK_1(MainScene::onEventCreatePrivateRoom, this)); dispatcher->addCustomEventListener(JOIN_PRIVATE_ROOM_EVENT, CC_CALLBACK_1(MainScene::onEventJoinPrivateRoom, this)); dispatcher->addCustomEventListener(UPDATE_USER_INSURE, CC_CALLBACK_1(MainScene::onEventUpdateUserInsure, this)); dispatcher->addCustomEventListener(WXSHARE_FRIENDS_SUCCESS, CC_CALLBACK_1(MainScene::WXShardFriendsSuccess, this)); dispatcher->addCustomEventListener(INPUT_UNION_ID, CC_CALLBACK_1(MainScene::onEventInpuUnionID, this)); dispatcher->addCustomEventListener(Get_Union_Room_List, CC_CALLBACK_1(MainScene::onEventGetRoomList, this)); dispatcher->addCustomEventListener(Get_AUTO_ROOM_INFO, CC_CALLBACK_1(MainScene::onEventGetAutoRoomList, this)); dispatcher->addCustomEventListener(CREATE_AUTO_ROOM_EVENT, CC_CALLBACK_1(MainScene::onEventCreateAutoRoomInfo, this)); dispatcher->addCustomEventListener(DEL_AUTO_ROOM_EVENT, CC_CALLBACK_1(MainScene::onEventDeleteAutoRoomInfo, this)); UserInfo::Instance().setIndividualMissionSink(this); MissionWeiXin::Instance().setMissionSink(this); m_kUserInGameServer.setUrl(GlobalJosn::getInstance()->m_strLogonIp.c_str(), GlobalJosn::getInstance()->m_iPort); m_kUserInGameServer.setMissionSink(this); m_IndivMission.setUrl(GlobalJosn::getInstance()->m_strLogonIp.c_str(), GlobalJosn::getInstance()->m_iPort); m_IndivMission.setMissionSink(this); m_kPrivateMission.setMissionSink(this); m_IsStartGame = false; m_SetNode = nullptr; m_JoinNode = nullptr; m_CreateNode = nullptr; m_GameUnionNode = nullptr; } MainScene::~MainScene() { UserInfo::Instance().delCallByPoint(this); UserInfo::Instance().setIndividualMissionSink(nullptr); auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->removeCustomEventListeners(CREATE_PRIVATE_ROOM_EVENT); dispatcher->removeCustomEventListeners(JOIN_PRIVATE_ROOM_EVENT); dispatcher->removeCustomEventListeners(UPDATE_USER_INSURE); dispatcher->removeCustomEventListeners(WXSHARE_FRIENDS_SUCCESS); dispatcher->removeCustomEventListeners(INPUT_UNION_ID); dispatcher->removeCustomEventListeners(Get_AUTO_ROOM_INFO); m_kPrivateMission.stop(); m_IndivMission.stop(); m_kUserInGameServer.stop(); } bool MainScene::init() { if (!Scene::init()) { return false; }; this->setTag(SCENE_TAG_MAIN); m_rootPanel = CSLoader::createNode("Platform/MainScene.csb"); CC_ASSERT(m_rootPanel != nullptr); this->addChild(m_rootPanel); //好友同玩 m_CreateItem = (Button*)m_rootPanel->getChildByName("btnCreateItem"); CC_ASSERT(m_CreateItem != nullptr); m_CreateItem->addClickEventListener([this](Ref*){ onEventBtnClick(BT_CLICK_CREATE); }); //加入游戏 auto btnJoinGame = (Button*)m_rootPanel->getChildByName("btnJoinGame"); CC_ASSERT(btnJoinGame != nullptr); btnJoinGame->addClickEventListener([this](Ref*){ onEventBtnClick(BT_CLICK_JOIN); }); //工会开房 auto btnCreateUnionItem = (Button*)m_rootPanel->getChildByName("btnCreateUnionItem"); CC_ASSERT(btnCreateUnionItem != nullptr); btnCreateUnionItem->addClickEventListener([this](Ref*){ onEventBtnClick(BT_CLICK_UNION); }); //玩法介绍 auto btnHelp = (Button*)m_rootPanel->getChildByName("btnHelp"); CC_ASSERT(btnHelp != nullptr); btnHelp->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); TipNode* pNode = HelpScene::create(); CC_ASSERT(pNode != nullptr); m_rootPanel->addChild(pNode); pNode->pushScene(); }); // 设置按钮 auto btnSetting = (Button*)m_rootPanel->getChildByName("btnSetting"); CC_ASSERT(btnSetting != nullptr); btnSetting->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); if (m_SetNode == nullptr) { m_SetNode = SetScene::create(); m_rootPanel->addChild(m_SetNode); } CC_ASSERT(m_SetNode != nullptr); m_SetNode->pushScene(); }); // 战绩 auto btnRecord = (Button*)m_rootPanel->getChildByName("btnRecord"); ASSERT(btnRecord); btnRecord->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); RecordScene* pNode = RecordScene::create(); CC_ASSERT(pNode != nullptr); m_rootPanel->addChild(pNode); pNode->pushScene(); }); //// 玩家信息 //auto panelUser = (Layout*)m_rootPanel->getChildByName("panelUser"); //CC_ASSERT(panelUser != nullptr); // 银行 auto btnShop = (Button*)m_rootPanel->getChildByName("btnShop"); CC_ASSERT(btnShop != nullptr); btnShop->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); PopScene::Instance().show(GlobalJosn::getInstance()->m_strAgentTip); }); // 分享 auto btnShow = (Button*)m_rootPanel->getChildByName("btnShow"); CC_ASSERT(btnShow); btnShow->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); TipNode* pNode = ShowScene::create(); CC_ASSERT(pNode != nullptr); m_rootPanel->addChild(pNode); pNode->pushScene(); }); // 充值; auto btnChongzhi = (Button*)m_rootPanel->getChildByName("btnChongzhi"); CC_ASSERT(btnChongzhi); btnChongzhi->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); TipNode* pNode = PayScene::create(); CC_ASSERT(pNode != nullptr); m_rootPanel->addChild(pNode); pNode->pushScene(); }); // 充值记录; auto btnPayLog = (Button*)m_rootPanel->getChildByName("btnPayLog"); CC_ASSERT(btnPayLog); btnPayLog->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); TipNode* pNode = PayLogScene::create(); CC_ASSERT(pNode != nullptr); m_rootPanel->addChild(pNode); pNode->pushScene(); }); CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance(); tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData(); if (0==pGlobalUserData->cbInsureEnabled) { btnChongzhi->setVisible(false); btnPayLog->setVisible(false); } // 兑换; auto btnDuiHuan = (Button*)m_rootPanel->getChildByName("btnDuiHuan"); CC_ASSERT(btnDuiHuan); btnDuiHuan->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); TipNode* pNode = DuiHuanScene::create(); CC_ASSERT(pNode != nullptr); m_rootPanel->addChild(pNode); pNode->pushScene(); }); // 公会; auto btnGongHui = (Button*)m_rootPanel->getChildByName("btnGongHui"); CC_ASSERT(btnGongHui); btnGongHui->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); TipNode* pNode = GongHuiScene::create(); CC_ASSERT(pNode != nullptr); m_rootPanel->addChild(pNode); pNode->pushScene(); }); // 公告; m_SystemInfo = (ImageView*)m_rootPanel->getChildByName("System_bg"); CC_ASSERT(m_SystemInfo); m_PlatformNotic = SystemNotice::createSystemNotice("common/Img/lobby_msg_bg.png"); m_PlatformNotic->setPosition(Vec2(m_SystemInfo->getContentSize().width / 2, m_SystemInfo->getContentSize().height / 2)); m_PlatformNotic->setCascadeOpacityEnabled(true); m_PlatformNotic->setCallBack([this](){ m_SystemInfo->setVisible(false); }); m_SystemInfo->addChild(m_PlatformNotic); // 初始化工会信息; initUnionRoom(); //对手机返回键的监听 auto keyListener = EventListenerKeyboard::create(); //和回调函数绑定 keyListener->onKeyReleased = CC_CALLBACK_2(MainScene::onKeyReleased, this); //添加到事件分发器中 _eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this); return true; } void MainScene::initUnionRoom() { m_isRefreshing = false; m_dwSelectUnionCode = 0; m_nRefreshErr = 0; if (m_rootPanel == nullptr) return; m_PanelRoom = (Layout*)m_rootPanel->getChildByName("PanelRoom"); ASSERT(m_PanelRoom != nullptr); if (m_PanelRoom == nullptr) return; m_PanelRoom->setVisible(false); // 刷新按钮; m_btnRefresh = (Button*)m_PanelRoom->getChildByName("btnRefresh"); ASSERT(m_btnRefresh != nullptr); m_btnRefresh->setVisible(false); m_btnRefresh->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); RequsetUnionRoomList(); }); // 选择工会标识; m_txtSelectUnion = (Button*)m_PanelRoom->getChildByName("btnGameInfo"); ASSERT(m_txtSelectUnion != nullptr); m_txtSelectUnion->setVisible(false); // 返回公会列表; m_btnBackUnion = (Button*)m_PanelRoom->getChildByName("btnUnionList"); ASSERT(m_btnBackUnion != nullptr); m_btnBackUnion->setVisible(false); m_btnBackUnion->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); m_dwSelectUnionCode = 0; m_isRefreshing = false; m_UnionList->setVisible(true); m_RoomList->setVisible(false); m_btnBackUnion->setVisible(false); m_btnRefresh->setVisible(false); m_txtSelectUnion->setVisible(true); }); // 列表; m_RoomList = (ScrollView*)m_PanelRoom->getChildByName("RoomList"); ASSERT(m_RoomList != nullptr); m_RoomList->setVisible(false); m_UnionList = (ScrollView*)m_PanelRoom->getChildByName("UnionList"); ASSERT(m_UnionList != nullptr); m_UnionList->setVisible(false); m_btnUnionItem = (Button*)m_rootPanel->getChildByName("btnUninItem"); CC_ASSERT(nullptr != m_btnUnionItem); m_btnUnionItem->setVisible(false); m_btnRoomItem0 = (Button*)m_rootPanel->getChildByName("btnRoomItem0"); ASSERT(m_btnRoomItem0 != nullptr); m_btnRoomItem0->setVisible(false); m_btnRoomItem1 = (Button*)m_rootPanel->getChildByName("btnRoomItem1"); CC_ASSERT(nullptr != m_btnRoomItem1); m_btnRoomItem1->setVisible(false); for (int i = 0; i < 5; i++) { std::string strkey = StringUtils::format("imghead_%d", i); auto sp0 = (ImageView*)m_btnRoomItem0->getChildByName(strkey); assert(sp0 != nullptr); sp0->setVisible(false); auto sp1 = (ImageView*)m_btnRoomItem1->getChildByName(strkey); assert(sp1 != nullptr); sp1->setVisible(false); } } // 定时刷新列表; void MainScene::startUnionRoomTime() { if (m_PanelRoom == nullptr) return; // 先停止; m_PanelRoom->stopAllActions(); int nRefreshTime = GlobalJosn::getInstance()->GetRefreshTime(); // 再启动; auto act = Sequence::createWithTwoActions(DelayTime::create(nRefreshTime), CallFunc::create([this](){ if (m_GameUnionNode != nullptr && m_GameUnionNode->isVisible()) return; if (m_RoomList != nullptr && !m_RoomList->isVisible()) return; RequsetUnionRoomList(false); })); auto seq = RepeatForever::create(act); m_PanelRoom->runAction(seq); } void MainScene::RequsetUnionRoomList(bool isLoading /*= true*/) { if (this->isVisible() && m_PanelRoom->isVisible() && m_dwSelectUnionCode != 0) { // 连续三次刷新失败,强制刷新; if (m_isRefreshing && m_nRefreshErr < 3) { m_nRefreshErr++; return; } if (isLoading) { LoadingScene::Instance().show(this); } CMD_GR_GetUnionRoomInfo getUnionRoom; getUnionRoom.dwUserID = UserInfo::getInstance()->getUserID(); getUnionRoom.dwUnionCode = m_dwSelectUnionCode; m_kUserInGameServer.PerformGetUnionRoomList(&getUnionRoom); m_isRefreshing = true; m_nRefreshErr = 0; } } // 显示工会列表; void MainScene::ShowUnionList(tagUnionAndGameList* pUnionAndGameList) { if (m_UnionList == nullptr) return; if (pUnionAndGameList == nullptr) return; m_UnionList->removeAllChildren(); int iUnionCount = pUnionAndGameList->kUnionList.size(); // 玩家没有工会; if (iUnionCount <= 0) { m_PanelRoom->setVisible(false); return; } m_PanelRoom->setVisible(true); // 获取上次选择的工会; std::string strUnionCode = StringUtils::format("UnionCode_%d", UserInfo::Instance().getGameID()); m_dwSelectUnionCode = UserDefault::getInstance()->getIntegerForKey(strUnionCode.c_str(), 0); uint32 dwMeUserID = CGlobalUserInfo::GetInstance()->getUserID(); // 列表调整; Size svSize = m_RoomList->getInnerContainerSize(); if (iUnionCount > 5) { svSize.height = iUnionCount * (m_btnUnionItem->getContentSize().height + 20); m_UnionList->setInnerContainerSize(svSize); } // 显示工会列表; for (int i = 0; i < iUnionCount; i++) { tagUnionItem* pUnionItem = &pUnionAndGameList->kUnionList[i]; DWORD dwUnionCode = pUnionItem->dwUnionCode; BYTE cbUnionGoldOpen = pUnionItem->cbUnionGoldOpen; Button* btnUnionItem = nullptr; btnUnionItem = (Button*)m_btnUnionItem->clone(); ASSERT(btnUnionItem != nullptr); if (btnUnionItem == nullptr) break; // 名称; Text* txtUnionName = (Text*)btnUnionItem->getChildByName("txtUnoinName"); ASSERT(txtUnionName != nullptr); std::string strUnionName = pUnionItem->szUnionName; if (cbUnionGoldOpen == 1) { strUnionName += "-欢乐场"; } txtUnionName->setString(utility::a_u8(strUnionName)); // 人数; Text* txtPlayerCount = (Text*)btnUnionItem->getChildByName("txtUserCount"); ASSERT(txtPlayerCount != nullptr); std::string strPlayerCount = StringUtils::format("%d人", pUnionItem->lPeople); txtPlayerCount->setString(utility::a_u8(strPlayerCount.c_str())); // 房卡; Text* txtCardCount = (Text*)btnUnionItem->getChildByName("txtCardCount"); ASSERT(txtCardCount != nullptr); std::string strCardCount = StringUtils::format("%d张", pUnionItem->lUnionCard); txtCardCount->setString(utility::a_u8(strCardCount.c_str())); txtCardCount->setVisible(pUnionItem->dwUserID == dwMeUserID); // 选择工会事件; btnUnionItem->addClickEventListener([this, dwUnionCode, cbUnionGoldOpen](Ref* ref){ YSAudioEngine::Instance().playBtnClickEffect(); // 存储当前选择的工会; std::string strUnionCode = StringUtils::format("UnionCode_%d", UserInfo::Instance().getGameID()); UserDefault::getInstance()->setIntegerForKey(strUnionCode.c_str(), dwUnionCode); m_dwSelectUnionCode = dwUnionCode; RequsetUnionRoomList(); }); // 节点调整位置; float x = (svSize.width - btnUnionItem->getContentSize().width) / 2; float y = svSize.height - btnUnionItem->getContentSize().height - (btnUnionItem->getContentSize().height + 20) * i; btnUnionItem->setPositionX(x); btnUnionItem->setPositionY(y); btnUnionItem->setVisible(true); m_UnionList->addChild(btnUnionItem); } // 加载房间; if (m_dwSelectUnionCode != 0 && !m_isRefreshing) { m_txtSelectUnion->setVisible(false); m_UnionList->setVisible(false); m_RoomList->setVisible(true); m_btnBackUnion->setVisible(true); m_btnRefresh->setVisible(true); RequsetUnionRoomList(); } else { m_UnionList->setVisible(true); m_txtSelectUnion->setVisible(true); m_RoomList->setVisible(false); m_btnBackUnion->setVisible(false); m_btnRefresh->setVisible(false); } } // 房间列表; void MainScene::showUnionRoomList(tagRoomItemList *pRoomItemList) { LoadingScene::Instance().hide(); m_isRefreshing = false; if (m_RoomList == nullptr) return; if (pRoomItemList == nullptr) return; // 界面控制; m_UnionList->setVisible(false); m_RoomList->setVisible(true); m_btnBackUnion->setVisible(true); m_btnRefresh->setVisible(true); m_txtSelectUnion->setVisible(false); m_RoomList->removeAllChildren(); // 房间列表; int iUnionCount = pRoomItemList->kList.size(); // 列表调整; Size svSize = m_RoomList->getInnerContainerSize(); if (iUnionCount > 3) { svSize.height = iUnionCount * (m_btnRoomItem0->getContentSize().height + 20); m_RoomList->setInnerContainerSize(svSize); } uint8 cbChairCount = 0; uint32 dwLastUnionCode = 0; for (int i = 0; i < iUnionCount; i++) { tagRoomItem* pUnionRoomItem = &pRoomItemList->kList[i]; uint8 cbUnionGoldOpen = pUnionRoomItem->cbUnionGoldOpen; uint32 dwRoomID = pUnionRoomItem->dwRoomID; uint8 cbGameStatus = pUnionRoomItem->cbGameOK; Button* btnRoomItem = nullptr; // 欢乐场; if (1 == cbUnionGoldOpen) { btnRoomItem = (Button*)m_btnRoomItem0->clone(); } else { btnRoomItem = (Button*)m_btnRoomItem1->clone(); } ASSERT(btnRoomItem != nullptr); if (btnRoomItem == nullptr) break; // 游戏名称; Text* txtGameName = (Text*)btnRoomItem->getChildByName("txtGameName"); ASSERT(txtGameName != nullptr); std::string strGameName; if (DDZ_KIND_ID == pUnionRoomItem->wKindID) { strGameName = DDZ_GAME_NAME; cbChairCount = DDZ_GAME_PLAYER; } else if (WN_KIND_ID == pUnionRoomItem->wKindID) { strGameName = WN_GAME_NAME; cbChairCount = WNMJ_SPACE::WN_GameScene::GetRealChairCount(pUnionRoomItem->dwGameRule); } else if (NN_KIND_ID == pUnionRoomItem->wKindID) { strGameName = NN_GAME_NAME; cbChairCount = NN_GAME_PLAYER; } else if (SSS_KIND_ID == pUnionRoomItem->wKindID) { strGameName = SSS_GAME_NAME; cbChairCount = SSS_GAME_PLAYER; } else if (ZJH_KIND_ID == pUnionRoomItem->wKindID) { strGameName = ZJH_GAME_NAME; cbChairCount = ZJH_GAME_PLAYER; } else if (DZ_KIND_ID == pUnionRoomItem->wKindID) { strGameName = DZ_GAME_NAME; cbChairCount = DZ_GAME_PLAYER; } else if (PDK_KIND_ID == pUnionRoomItem->wKindID) { strGameName = PDK_GAME_NAME; cbChairCount = PDK_SPACE::PDKGameScene::getRealChairCount(pUnionRoomItem->dwGameRule); } txtGameName->setString(utility::a_u8(strGameName)); // 工会名称; Text* txtUnionName = (Text*)btnRoomItem->getChildByName("txtUnoinName"); ASSERT(txtUnionName != nullptr); std::string strUnionName = pUnionRoomItem->szUnionName; txtUnionName->setString(utility::a_u8(strUnionName)); uint8 cbPlaycout = pUnionRoomItem->cbPlayCout; // 游戏规则; Text* txtGameRule = (Text*)btnRoomItem->getChildByName("txtGameRule"); ASSERT(txtGameRule != nullptr); std::string strGameRule = getGameRule(pUnionRoomItem->wKindID, pUnionRoomItem->dwGameRule); if (cbGameStatus == 2) { cbPlaycout++; txtGameRule->setString(utility::a_u8("正在游戏中...")); txtGameRule->setTextColor(Color4B(0xDB, 0x08, 0x93, 0xff)); } else { txtGameRule->setString(utility::a_u8(strGameRule)); txtGameRule->setTextColor(Color4B(0x1a, 0x1a, 0x1a, 0xff)); } if (cbPlaycout > pUnionRoomItem->cbPlayCoutIdex) { cbPlaycout = pUnionRoomItem->cbPlayCoutIdex; } // 房间信息; Text* txtRoomInfo = (Text*)btnRoomItem->getChildByName("txtRoomInfo"); ASSERT(txtRoomInfo != nullptr); Text* txtBaseScore = (Text*)btnRoomItem->getChildByName("txtBaseScore"); ASSERT(txtBaseScore != nullptr); std::string strRoomInfo = ""; std::string strBaseSccore = ""; // 欢乐场; if (1 == cbUnionGoldOpen) { strRoomInfo = StringUtils::format("房号:%d,%d/%d局", dwRoomID, cbPlaycout, pUnionRoomItem->cbPlayCoutIdex); strBaseSccore = StringUtils::format("底分:%d", pUnionRoomItem->cbBaseScore); } else { strRoomInfo = StringUtils::format("房号:%d,%d/%d局, 底分:%d", dwRoomID, cbPlaycout, pUnionRoomItem->cbPlayCoutIdex, pUnionRoomItem->cbBaseScore); } txtRoomInfo->setString(utility::a_u8(strRoomInfo)); txtBaseScore->setString(utility::a_u8(strBaseSccore)); //std::string strRoomInfo = StringUtils::format("房号:%d,%d/%d局", dwRoomID, cbPlaycout, pUnionRoomItem->cbPlayCoutIdex); //txtRoomInfo->setString(utility::a_u8(strRoomInfo)); //// 游戏规则; //Text* txtGameRule = (Text*)btnRoomItem->getChildByName("txtGameRule"); //ASSERT(txtGameRule != nullptr); //std::string strGameRule = getGameRule(pUnionRoomItem->wKindID, pUnionRoomItem->dwGameRule); //txtGameRule->setString(utility::a_u8(strGameRule)); // 邀请玩家; auto btnShare = (Button*)btnRoomItem->getChildByName("btnShare"); ASSERT(btnShare != nullptr); btnShare->addClickEventListener([this, dwRoomID, strGameName, strRoomInfo, strGameRule](Ref* ref){ YSAudioEngine::Instance().playBtnClickEffect(); std::string strText = StringUtils::format("%s, %s.", strRoomInfo.c_str(), strGameRule.c_str()); std::string strUrl = GlobalJosn::getInstance()->getShareInfoByKind(SH_KIND_WEIXIN_URL); std::string strUrlInfo = StringUtils::format("%s?param=%d", strUrl.c_str(), dwRoomID); MissionWeiXin::Instance().shareUrlWeiXin(strUrlInfo, utility::a_u8(strGameName), strText); //SelectShareScene::Instance().shareRoomInfo(strUrlInfo, utility::a_u8(strGameName), strText, 0); }); btnShare->setVisible(cbGameStatus != 2); // 加入房间; btnRoomItem->addClickEventListener([this, dwRoomID](Ref* ref){ YSAudioEngine::Instance().playBtnClickEffect(); joinPrivateRoom(dwRoomID); }); // 玩家头像; for (int nIndex = 0; nIndex < cbChairCount; nIndex++) { std::string strkey = StringUtils::format("imghead_%d", nIndex); auto sphead = (ImageView*)btnRoomItem->getChildByName(strkey); if (sphead != nullptr) { uint32 dwUserID = pUnionRoomItem->dwUserIDs[nIndex]; if (dwUserID != 0 && dwUserID != INVALID_CHAIR) { std::string strHttp = StringUtils::format(HEAD_HTTP_ADDRESS, dwUserID); ImagicDownManager::Instance().addDown(sphead, strHttp, dwUserID, true); } sphead->setVisible(true); } } btnRoomItem->setVisible(true); // 节点调整位置; float x = (svSize.width - btnRoomItem->getContentSize().width)/2; float y = svSize.height - btnRoomItem->getContentSize().height - (btnRoomItem->getContentSize().height + 20) * i; btnRoomItem->setPositionX(x); btnRoomItem->setPositionY(y); m_RoomList->addChild(btnRoomItem); } } //按键处理 void MainScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event * pEvent) { //返回值处理 if (keyCode == EventKeyboard::KeyCode::KEY_ESCAPE) { YSAudioEngine::Instance().playBtnClickEffect(); auto callback = []{ Director::getInstance()->end(); // 清空微信保存信息; UserDefault::getInstance()->setStringForKey("access_token", ""); UserDefault::getInstance()->setStringForKey("refresh_token", ""); UserDefault::getInstance()->setStringForKey("openid", ""); UserDefault::getInstance()->setStringForKey("unionid", ""); UserDefault::getInstance()->flush(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }; PopScene::Instance().show(utility::a_u8("您确定要退出游戏吗?"), callback, nullptr); } } void MainScene::onEnter() { Scene::onEnter(); upPlayerInfo(); InitSystemInfo(); m_IsStartGame = false; } void MainScene::onEnterTransitionDidFinish() { Scene::onEnterTransitionDidFinish(); auto txtVersion = (Text*)m_rootPanel->getChildByName("txtVersion"); CC_ASSERT(txtVersion != nullptr); std::string strVersion = StringUtils::format("版本: v%s", JniFun::getVersionName().c_str());//JniFun::getVersionName(); txtVersion->setString(utility::a_u8(strVersion)); UserInfo::Instance().reqAccountInfo(); upPlayerInfo(); RequsetHttpInfo(); updateServerInfo(); m_isRefreshing = false; onEventInpuUnionID(nullptr); startUnionRoomTime(); } void MainScene::onExit() { Scene::onExit(); } void MainScene::upPlayerInfo() { utility::mlog("MainScene::upPlayerInfo()"); //玩家信息 auto panelUser = (Layout*)m_rootPanel->getChildByName("panelUser"); CC_ASSERT(panelUser != nullptr); //玩家ID auto txtUserID = (Text*)panelUser->getChildByName("txtUserID"); CC_ASSERT(txtUserID != nullptr); txtUserID->setString(StringUtils::format("ID:%d", UserInfo::Instance().getGameID())); //经验 auto txtExp = (Text*)panelUser->getChildByName("txtExp"); CC_ASSERT(txtExp != nullptr); std::string strExp = utility::GetUserLevel(UserInfo::Instance().getUserData()->lGrade) + StringUtils::format("[%d]", UserInfo::Instance().getUserData()->lGrade); txtExp->setString(utility::a_u8(strExp)); //玩家账号 auto txtUserName = (Text*)panelUser->getChildByName("txtUserName"); CC_ASSERT(txtUserName != nullptr); txtUserName->setString(UserInfo::Instance().getUserNicName()); //房卡 auto txtDiamond = (Text*)panelUser->getChildByName("txtDiamond"); CC_ASSERT(txtDiamond != nullptr); txtDiamond->setString(StringUtils::format("%d", UserInfo::Instance().getUserInsure())); //充值 auto btnPay = (Button*)panelUser->getChildByName("btnPay"); CC_ASSERT(btnPay != nullptr); btnPay->addClickEventListener([this](Ref*){ YSAudioEngine::Instance().playBtnClickEffect(); PopScene::Instance().show(GlobalJosn::getInstance()->getShareInfoByKind(SH_KIND_BUY)); std::string strUrl = StringUtils::format("%s/Lobby/UserInfo?uid=%d&t=%ld", WEB_SERVER_ADDRESS, UserInfo::Instance().getUserID(), time(NULL)); CWebReq::getInstance().sendRequestDocumentUrl(strUrl, CC_CALLBACK_1(MainScene::ResponseUserInfoResult, this), nullptr); }); // 显示头像 ShowHead(); } void MainScene::onEventCreatePrivateRoom(EventCustom* event) { tagCreatePrivate* pkSendNet = static_cast(event->getUserData()); CMD_GR_Create_Private CreatePrivate; memcpy(&CreatePrivate, &pkSendNet->CMD_Private, sizeof(CMD_GR_Create_Private)); CGameServerItem * pGameServerItem = CServerListData::shared()->getGameServerByKind(pkSendNet->wServerID); if (pGameServerItem) { tagGameServer& tmpGameServer = pGameServerItem->m_GameServer; WORD wServerID = tmpGameServer.wServerID; m_kPrivateMission.SetCreateInfo(&CreatePrivate); m_kPrivateMission.ConnectServerByServerID(wServerID); } else { PopScene::Instance().show(utility::a_u8("服务器正在维护,请耐心等待!")); } } // 创建代开信息; void MainScene::onEventCreateAutoRoomInfo(EventCustom* event) { CMD_GR_AutoRoomInfo* pkSendNet = static_cast(event->getUserData()); CGameServerItem * pGameServerItem = CServerListData::shared()->getGameServerByKind(pkSendNet->wKindID); if (pGameServerItem) { tagGameServer& tmpGameServer = pGameServerItem->m_GameServer; WORD wServerID = tmpGameServer.wServerID; m_kPrivateMission.SetCreateAutoInfo(pkSendNet); m_kPrivateMission.ConnectServerByServerID(wServerID); } else { PopScene::Instance().show(utility::a_u8("服务器正在维护,请耐心等待!")); } } void MainScene::onEventJoinPrivateRoom(EventCustom* event) { __Integer* pData = (__Integer*)event->getUserData(); uint32 dwJoinRoomNum = pData->getValue(); joinPrivateRoom(dwJoinRoomNum); } void MainScene::onEventInpuUnionID(EventCustom* event) { uint32 dwUserID = UserInfo::Instance().getUserID(); m_kUserInGameServer.PerformGetUnionList(dwUserID); } void MainScene::onEventGetRoomList(EventCustom* event) { //uint32 dwUserID = UserInfo::Instance().getUserID(); //m_kUserInGameServer.PerformGetUnionRoomList(dwUserID); CMD_GR_GetUnionRoomInfo* pkSendNet = static_cast(event->getUserData()); m_dwSelectUnionCode = pkSendNet->dwUnionCode; m_kUserInGameServer.PerformGetUnionRoomList(pkSendNet); } // 获取自动开房信息; void MainScene::onEventGetAutoRoomList(EventCustom* event) { uint32 dwUserID = UserInfo::Instance().getUserID(); m_kUserInGameServer.PerformGetAutoRoomList(dwUserID); } void MainScene::joinPrivateRoom(uint32 dwRoomNum) { int nServerID = dwRoomNum / 10000 - 10; m_kPrivateMission.SetJoinInfo(dwRoomNum); m_kPrivateMission.ConnectServerByServerID(nServerID); } void MainScene::onEventUpdateUserInsure(EventCustom* event) { //房卡信息 auto panelUser = m_rootPanel->getChildByName("panelUser"); CC_ASSERT(panelUser != nullptr); //房卡 auto txtDiamond = (Text*)panelUser->getChildByName("txtDiamond"); CC_ASSERT(txtDiamond != nullptr); txtDiamond->setString(StringUtils::format("%d", UserInfo::Instance().getUserInsure())); } void MainScene::WXShardFriendsSuccess(EventCustom* event) { CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance(); tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData(); // 当天未分享才去获取奖励; if ((0 == pGlobalUserData->cbWXShareTimes) && (m_IndivMission.isAlive() == false)) { m_IndivMission.wxShareSuccess(); } } void MainScene::onWXShardReward(CMD_GP_WXShareReward* pReward) { CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance(); tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData(); pGlobalUserData->lUserInsure += pReward->cbFKCount; pGlobalUserData->cbWXShareTimes++; onEventUpdateUserInsure(nullptr); std::string strInfo = StringUtils::format("恭喜您获得 %d 张房卡!", pReward->cbFKCount); PopScene::Instance().show(utility::a_u8(strInfo.c_str()), nullptr); } void MainScene::onGPNoticeResult(tagGameMatch& pGameMatchInfo, bool bSucess, const char* pStr) { PopScene::Instance().show(utility::a_u8(pStr)); } void MainScene::onGPAccountInfo(CMD_GP_UserAccountInfo* pAccountInfo) { //upPlayerInfo(); } void MainScene::onUserInGameServerID(CMD_GP_InGameSeverID* pNetInfo) { if (pNetInfo->LockServerID != 0) { CGameServerItem* pServer = CServerListData::shared()->SearchGameServer(pNetInfo->LockServerID); if (pServer != nullptr) { m_kUserInGameServer.stop(); StartGameScene(pServer); } else { PopScene::Instance().show(utility::a_u8("未找到相关服务器信息,无法继续游戏!")); } } else { if (m_btnClickType == BT_CLICK_UNION) { //显示加入房间窗口 if (m_GameUnionNode == nullptr) { m_GameUnionNode = GameUnionScene::create(); m_GameUnionNode->setVisible(false); m_rootPanel->addChild(m_GameUnionNode); } CC_ASSERT(m_GameUnionNode != nullptr); m_GameUnionNode->pushScene(); } else if (m_btnClickType == BT_CLICK_CREATE) { if (m_CreateNode == nullptr) { m_CreateNode = CreateScene::create(); CC_ASSERT(m_CreateNode != nullptr); m_rootPanel->addChild(m_CreateNode); } m_CreateNode->pushScene(); } else if (m_btnClickType == BT_CLICK_JOIN) { //显示加入房间窗口; if (m_JoinNode == nullptr) { m_JoinNode = JoinScene::create(); CC_ASSERT(m_JoinNode != nullptr); m_rootPanel->addChild(m_JoinNode); } m_JoinNode->pushScene(); } else { ASSERT(false); } } } void MainScene::StartGameScene(CGameServerItem* pServer) { if (pServer != nullptr) { try { if (m_IsStartGame) return; GameMission::g_pGameServerItem = pServer; GameFrameBase* pGameScene = CGameCreator::getInstance()->startGameClient(pServer->GetKindID()); if (pGameScene) { Director::getInstance()->pushScene(TransitionCrossFade::create(0.3f, pGameScene)); m_IsStartGame = true; if (m_JoinNode && m_JoinNode->isVisible()) { m_JoinNode->setVisible(false); } if (m_CreateNode && m_CreateNode->isVisible()) { m_CreateNode->setVisible(false); } if (m_GameUnionNode != nullptr) { m_GameUnionNode->HideCreateNode(); } } } catch (...) { m_IsStartGame = false; } } } void MainScene::checkInGameServer() { //变量定义 CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance(); tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData(); m_kUserInGameServer.PerformInGameServerID(pGlobalUserData->dwUserID); } void MainScene::InitSystemInfo() { std::string strMusic = StringUtils::format("Music_%d", UserInfo::Instance().getGameID()); std::string strEffect = StringUtils::format("Effect_%d", UserInfo::Instance().getGameID()); std::string strSoundType = StringUtils::format("SoundType_%d", UserInfo::Instance().getGameID()); float fMusic = UserDefault::getInstance()->getFloatForKey(strMusic.c_str(), 100.0); float fEffect = UserDefault::getInstance()->getFloatForKey(strEffect.c_str(), 100.0); BYTE cbSoundType = UserDefault::getInstance()->getIntegerForKey(strSoundType.c_str(), PT_EFFECT); CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(fMusic / 100.0); CocosDenshion::SimpleAudioEngine::getInstance()->setEffectsVolume(fEffect / 100.0); YSAudioEngine::Instance().setSoundType(cbSoundType); if (PLATFORM_MUISCBG) { CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("common/sound/bg_music.mp3", true); } } void MainScene::appWillEnterForeground() { //获取启动参数 std::string strParam = JniFun::getRunParam(); if (!strParam.empty()) { cocos2d::log("------------------------------"); cocos2d::log("%s", strParam.c_str()); cocos2d::log("------------------------------"); joinPrivateRoom(atoi(strParam.c_str())); } // 显示头像 ShowHead(); } // 显示头像 void MainScene::ShowHead() { // 重新显示头像 auto panelUser = (Layout*)m_rootPanel->getChildByName("panelUser"); CC_ASSERT(panelUser != nullptr); auto sphead = (Sprite*)panelUser->getChildByName("head"); CC_ASSERT(sphead != nullptr); //下载头像 uint32 dwUserID = UserInfo::Instance().getUserID(); std::string strHttp = UserInfo::Instance().getHeadHttp(); ImagicDownManager::Instance().addDown(sphead, strHttp, dwUserID, true); sphead->setVisible(true); } // 获取信息; void MainScene::RequsetHttpInfo() { LoadingScene::Instance().show(this); std::string strUrl = StringUtils::format("%s/Lobby/Config?t=%ld", WEB_SERVER_ADDRESS, time(NULL)); CWebReq::getInstance().sendRequestDocumentUrl(strUrl, CC_CALLBACK_1(MainScene::ResponseResult, this), nullptr); } //请求版本信息结果 void MainScene::ResponseResult(rapidjson::Document* pDoc) { LoadingScene::Instance().hide(); if (pDoc == nullptr) return; // 公告; if (pDoc->HasMember("Notices") && (*pDoc)["Notices"].IsArray()) { for (rapidjson::SizeType i = 0; i < (*pDoc)["Notices"].Size(); ++i) { const rapidjson::Value& v = (*pDoc)["Notices"][i]; std::string strNotices = v.GetString(); if (m_PlatformNotic) { m_PlatformNotic->postMessage(strNotices, 0, true); } } } // 代理; if (pDoc->HasMember("AgentTip") && (*pDoc)["AgentTip"].IsString()) { const rapidjson::Value& v = (*pDoc)["AgentTip"]; GlobalJosn::getInstance()->m_strAgentTip = v.GetString(); } // 分享; if (pDoc->HasMember("ShareTip") && (*pDoc)["ShareTip"].IsString()) { const rapidjson::Value& v = (*pDoc)["ShareTip"]; GlobalJosn::getInstance()->m_strShareTip = v.GetString(); } //获取启动参数 std::string strParam = JniFun::getRunParam(); if (!strParam.empty()) { cocos2d::log("%s", strParam.c_str()); joinPrivateRoom(atoi(strParam.c_str())); } else { // 这里显示公告; if (GlobalJosn::getInstance()->m_IsNotice) { CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance(); if (pGlobalUserInfo->GetShowNotice()) { auto noticeNode = NoticeScene::create(); CC_ASSERT(noticeNode != nullptr); this->addChild(noticeNode); noticeNode->pushScene(); } } } } //请求玩家信息; void MainScene::ResponseUserInfoResult(rapidjson::Document* pDoc) { if (pDoc == nullptr) return; do { if (!pDoc->HasMember("ret") || !((*pDoc)["ret"].IsBool()) || !((*pDoc)["ret"].GetBool())) { break; } if (pDoc->HasMember("CardNum") && (*pDoc)["CardNum"].IsInt()) { //玩家信息; auto panelUser = (Layout*)m_rootPanel->getChildByName("panelUser"); CC_ASSERT(panelUser != nullptr); int nCurNum = CWebReq::getDataValueInt(pDoc, "CardNum"); //房卡; auto txtDiamond = (Text*)panelUser->getChildByName("txtDiamond"); CC_ASSERT(txtDiamond != nullptr); txtDiamond->setString(StringUtils::format("%d", nCurNum)); CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance(); tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData(); pGlobalUserData->lUserInsure = nCurNum; } } while (false); } void MainScene::updateServerInfo() { m_kUserInGameServer.GetServerOnLine(); } void MainScene::onUserInUnion(CMD_GP_InUnion* pNetInfo) { if (pNetInfo == nullptr) return; // 在工会; if (pNetInfo->cbResultCode == 0) { if (m_JoinNode != nullptr && m_JoinNode->isVisible()) { m_JoinNode->setVisible(false); } if (m_CreateNode == nullptr) { m_CreateNode = CreateScene::create(); m_rootPanel->addChild(m_CreateNode); } if (m_CreateNode) { m_CreateNode->pushScene(CDT_CREATE_PRIVATE, pNetInfo->dwUnionCode); } } else { PopScene::Instance().show(utility::a_u8(pNetInfo->szDescribeString)); } } void MainScene::onUnionAndGameInfo(tagUnionAndGameList *pUnionAndGameList) { if (pUnionAndGameList == nullptr) return; if (m_GameUnionNode != nullptr) { m_GameUnionNode->ShowUnionList(pUnionAndGameList); } else { ShowUnionList(pUnionAndGameList); } } void MainScene::onUnionRoomInfo(tagRoomItemList *pRoomItemList) { if (pRoomItemList == nullptr) return; if (m_GameUnionNode != nullptr && m_GameUnionNode->isVisible()) { m_GameUnionNode->ShowRoomList(pRoomItemList); } else { showUnionRoomList(pRoomItemList); } } void MainScene::onAutoRoomInfo(tagAutoRoomItemList *pRoomItemList) { if (pRoomItemList == nullptr) return; if (m_GameUnionNode != nullptr) { m_GameUnionNode->ShowAutoRoomList(pRoomItemList); } } void MainScene::onEventBtnClick(BT_CLICK_TYPE btnClickType) { YSAudioEngine::Instance().playBtnClickEffect(); m_btnClickType = btnClickType; if (CServerListData::shared()->GetGameServerCount() > 0) { checkInGameServer(); } else { PopScene::Instance().show(utility::a_u8("服务器正在维护,请稍等!")); } } void MainScene::CreateAutoRoomInfoResulte(CMD_GR_AutoResulte* pAutoResulte) { if (pAutoResulte == nullptr) return; // 成功刷新界面; if (pAutoResulte->lErrorCode == 0) { onEventGetAutoRoomList(nullptr); } else { PopScene::Instance().show(utility::a_u8(pAutoResulte->szDescribeString)); } } // 删除代开信息; void MainScene::onEventDeleteAutoRoomInfo(EventCustom* event) { CMD_GP_DelAutoRoomInfo* pkSendNet = static_cast(event->getUserData()); CGameServerItem * pGameServerItem = CServerListData::shared()->SearchGameServer(pkSendNet->wServerID); if (pGameServerItem) { m_kPrivateMission.SetDelAutoInfo(pkSendNet); m_kPrivateMission.ConnectServerByServerID(pkSendNet->wServerID); } else { PopScene::Instance().show(utility::a_u8("服务器正在维护,请耐心等待!")); } } std::string MainScene::getGameRule(uint16 wKindID, uint32 dwGameRule) { std::string strGameRule; if (DDZ_KIND_ID == wKindID) { strGameRule = DDZ_SPACE::DDZGameScene::getGameRule(dwGameRule); } else if (WN_KIND_ID == wKindID) { strGameRule = WNMJ_SPACE::WN_GameScene::getGameRule(dwGameRule); } else if (NN_KIND_ID == wKindID) { strGameRule = NiuNiu_SPACE::NNGameScene::getGameRule(dwGameRule); } else if (SSS_KIND_ID == wKindID) { strGameRule = SSS_SPACE::SSSGameScene::getGameRule(dwGameRule); } else if (ZJH_KIND_ID == wKindID) { strGameRule = ZJH_SPACE::ZJHGameScene::getGameRule(dwGameRule); } else if (DZ_KIND_ID == wKindID) { strGameRule = DZ_SPACE::DZGameScene::getGameRule(dwGameRule); } else if (PDK_KIND_ID == wKindID) { strGameRule = PDK_SPACE::PDKGameScene::getGameRule(dwGameRule); } return strGameRule; }