#include "DZ_GamePlayer.h" #include "ImagicDownManager.h" #include "DZ_BigCardSprite.h" #include "DZ_SmallCardSprite.h" #include "ActionEx.h" #include "DZ_CMD.h" #include "GameMission.h" DZ_GamePlayer::DZ_GamePlayer(WORD wViewID, Layout* pRootLayout) : GamePlayer(NULL), m_bLandlord(false), m_bReady(false), m_cbViewID(wViewID), m_pRootLayout(pRootLayout) { init(); } DZ_GamePlayer::~DZ_GamePlayer() { } bool DZ_GamePlayer::init() { //玩家昵称; m_pTxtNickName = static_cast(m_pRootLayout->getChildByName("nickname")); CC_ASSERT(m_pTxtNickName!=nullptr); m_pTxtNickName->setString(""); //闹钟背景; auto sprNaoZhong = static_cast(m_pRootLayout->getChildByName("naozhong")); CC_ASSERT(sprNaoZhong!=nullptr); //闹钟倒计时; m_pTxtAtlClock = static_cast(sprNaoZhong->getChildByName("txtAtlTime")); CC_ASSERT(m_pTxtAtlClock != nullptr); //头像; m_pSprHead = static_cast(m_pRootLayout->getChildByName("head")); CC_ASSERT(m_pSprHead!=nullptr); m_pSprHead->setVisible(false); auto imgScoreBg = static_cast(m_pRootLayout->getChildByName("imgScoreBg")); CC_ASSERT(imgScoreBg != nullptr); //积分; m_pTxtScore = static_cast(imgScoreBg->getChildByName("score")); CC_ASSERT(m_pTxtScore != nullptr); m_pTxtScore->setVisible(false); //地主标志; m_pLandFlag = static_cast(m_pRootLayout->getChildByName("landlord")); CC_ASSERT(m_pLandFlag!=nullptr); m_pFarmerFlag = static_cast(m_pRootLayout->getChildByName("farmer")); CC_ASSERT(m_pFarmerFlag != nullptr); //操作标志; m_pSprOper = static_cast(m_pRootLayout->getChildByName("oper")); CC_ASSERT(m_pSprOper != nullptr); m_pSprOper->setVisible(false); //断线标志; m_pSprOffline = static_cast(m_pRootLayout->getChildByName("offline")); CC_ASSERT(m_pSprOffline != nullptr); m_pSprOffline->setVisible(false); //房主标志; m_pSprOwner = static_cast(m_pRootLayout->getChildByName("owner")); CC_ASSERT(m_pSprOwner != nullptr); m_pSprOwner->setVisible(false); //托管标志; m_pSprTrust = static_cast(m_pRootLayout->getChildByName("trust")); CC_ASSERT(m_pSprTrust != nullptr); m_pSprTrust->setVisible(false); //剩余牌; m_pSprSurplusCard = static_cast(m_pRootLayout->getChildByName("sprSurplusCard")); CC_ASSERT(m_pSprSurplusCard != nullptr); //剩余牌文本; m_pTxtSurplus = static_cast(m_pSprSurplusCard->getChildByName("txtNum")); CC_ASSERT(m_pTxtSurplus != nullptr); //出牌面板; m_pPanelOutCard = static_cast(m_pRootLayout->getChildByName("panelOutCard")); CC_ASSERT(m_pPanelOutCard != nullptr); //聊天框背景; m_pImgChatView = static_cast(m_pRootLayout->getChildByName("imgChat")); CC_ASSERT(m_pImgChatView != nullptr); m_pImgChatView->setVisible(false); auto panelChat = static_cast(m_pImgChatView->getChildByName("panelChat")); CC_ASSERT(panelChat != nullptr); //聊天内容; m_pTxtChatInfo = static_cast(panelChat->getChildByName("txtMsg")); CC_ASSERT(m_pTxtChatInfo != nullptr); //语音气泡; m_pVoiceBubble = static_cast(m_pRootLayout->getChildByName("voice")); CC_ASSERT(m_pVoiceBubble != nullptr); m_pVoiceBubble->setVisible(false); auto speek_bg = (Sprite*)m_pVoiceBubble->getChildByName("speek_bg"); CC_ASSERT(speek_bg != nullptr); m_pSprVoiceAni = (Sprite*)speek_bg->getChildByName("voice_ing"); CC_ASSERT(m_pSprVoiceAni != nullptr); //排名; m_pSprRanking = static_cast(m_pRootLayout->getChildByName("sprRanking")); //CC_ASSERT(m_pSprRanking != nullptr); if (m_pSprRanking != nullptr) { m_pSprRanking->setVisible(false); } ////庄家叫的牌; //m_pNodeCallCard = static_cast(m_pRootLayout->getChildByName("nodeCallCard")); ////CC_ASSERT(m_pNodeCallCard != nullptr); //if (m_pNodeCallCard != nullptr) //{ // m_pNodeCallCard->setVisible(false); //} m_pHandCardLayout = static_cast(m_pRootLayout->getChildByName("panelHandCard")); ////加载没有在COCOSSTUDIO资源放入缓存 //SpriteFrameCache* pSpriteFrameCache = SpriteFrameCache::getInstance(); ////农民头像; //Sprite* pSprFarmer = Sprite::create("DDZ/Table/farmer.png"); //pSpriteFrameCache->addSpriteFrame(pSprFarmer->getSpriteFrame(), "DDZ/Table/farmer.png"); return true; } //显示本地信息; void DZ_GamePlayer::showUserInfo() { std::string strNickName = CGlobalUserInfo::GetInstance()->getNickName(); std::string strHeadHttp = CGlobalUserInfo::GetInstance()->getHeadHttp(); uint32 dwUserID = CGlobalUserInfo::GetInstance()->getUserID(); SCORE lScore = CGlobalUserInfo::GetInstance()->getUserScore(); CC_ASSERT(m_pSprHead != nullptr); ImagicDownManager::Instance().addDown(m_pSprHead, strHeadHttp, dwUserID); m_pSprHead->setVisible(true); CC_ASSERT(m_pTxtNickName != nullptr); m_pTxtNickName->setString(strNickName); m_pTxtNickName->setVisible(true); CC_ASSERT(m_pTxtScore != nullptr); m_pTxtScore->setString(utility::toString(lScore)); m_pTxtScore->setVisible(true); } void DZ_GamePlayer::PlayerEnter() { setInfoVisible(true); std::string strHttp = GetHeadHttp(); cocos2d::log("%s", strHttp.c_str()); uint32 dwUserID = GetUserID(); ImagicDownManager::Instance().addDown(m_pSprHead, strHttp, dwUserID); } void DZ_GamePlayer::PlayerLeave() { setInfoVisible(false); setReadyVisible(false); setOfflineVisible(false); } //显示玩家聊天内容; void DZ_GamePlayer::showChatInfo(const std::string strChatString) { auto label = Label::createWithSystemFont(strChatString, "", 28); CC_ASSERT(label!=nullptr); m_pTxtChatInfo->setString(strChatString); const Size& labelSize = label->getContentSize(); const Size& frameSize = m_pTxtChatInfo->getContentSize(); int nRows = labelSize.width / frameSize.width; //int nRemainder = (int)labelSize.width % (int)frameSize.width; //if (nRemainder>0) //{ // nRows++; //} m_pImgChatView->setVisible(true); m_pTxtChatInfo->setPositionY(28.0f); auto callback = CallFunc::create([=](){ m_pImgChatView->setVisible(false); }); //有几行文字就向上移动几次; if (nRows > 0) { auto move = EaseSineInOut::create(MoveBy::create(0.4f, Vec2(0, labelSize.height))); auto seq = Sequence::create(DelayTime::create(2.5f), move, nullptr); m_pTxtChatInfo->runAction(Sequence::create(Repeat::create(seq, nRows), DelayTime::create(2.5f), callback, nullptr)); } else { m_pTxtChatInfo->runAction(Sequence::create(DelayTime::create(2.5f), callback, nullptr)); } } //显示语音气泡; void DZ_GamePlayer::showVoiceBubble() { CC_ASSERT(m_pVoiceBubble != nullptr); m_pVoiceBubble->setVisible(true); // 启动帧动画; auto action = CSLoader::createTimeline("Games/DaZha/PlayerVoiceNode.csb"); action->gotoFrameAndPlay(0); m_pSprVoiceAni->runAction(action); } //隐藏语音气泡; void DZ_GamePlayer::hideVoiceBubble() { CC_ASSERT(m_pVoiceBubble != nullptr); m_pSprVoiceAni->stopAllActions(); m_pVoiceBubble->setVisible(false); } //重置界面; void DZ_GamePlayer::resetUI(bool bAll/* = false*/) { if ( bAll ) { setInfoVisible(false); setReadyVisible(false); setOfflineVisible(false); } //倒计时; m_pTxtAtlClock->stopAllActions(); m_pTxtAtlClock->setString(""); m_pTxtAtlClock->getParent()->setVisible(false); //剩余牌; m_pTxtSurplus->setString(""); m_pTxtSurplus->getParent()->setVisible(false); //地主标志; m_pLandFlag->setVisible(false); m_pFarmerFlag->setVisible(false); if (m_pSprRanking!=nullptr) { //排名标志; m_pSprRanking->setVisible(false); } //if (m_pNodeCallCard != nullptr) //{ // //删除庄家叫的牌; // m_pNodeCallCard->removeAllChildren(); //} ////断线标志; //m_pSprOffline->setVisible(false); if (!m_bReady) { //操作标志; m_pSprOper->setVisible(false); } if (m_pSprTrust!=nullptr) { m_pSprTrust->setVisible(false); } //剩余牌; m_pSprSurplusCard->setVisible(false); //出牌面板; m_pPanelOutCard->removeAllChildren(); m_pPanelOutCard->setVisible(false); } //玩家信息可见性; void DZ_GamePlayer::setInfoVisible(bool bVisible) { CC_ASSERT(m_pSprHead!=nullptr); m_pSprHead->setVisible(bVisible); CC_ASSERT(m_pTxtNickName != nullptr); m_pTxtNickName->setString(bVisible ? GetNickName() : ""); m_pTxtNickName->setVisible(bVisible); CC_ASSERT(m_pTxtScore != nullptr); SCORE lScore = bVisible? GetUserScore() : 0; m_pTxtScore->setString(utility::toString(lScore)); m_pTxtScore->setVisible(bVisible); } ////设置名称是否可见; //void DZ_GamePlayer::setNickNameVisible(bool bVisible) //{ // m_bNickNameVisible = bVisible; //} //设置是否为地主; void DZ_GamePlayer::setLand(uint8 cbFlag) { CC_ASSERT(m_pLandFlag!=nullptr); //隐藏标志; if ( 0==cbFlag ) { m_pLandFlag->setVisible(false); m_pFarmerFlag->setVisible(false); } else { //黑桃四标志; if (1==cbFlag) { m_pFarmerFlag->setVisible(true); m_pLandFlag->setVisible(false); } //包牌标志; else { m_pLandFlag->setVisible(true); m_pFarmerFlag->setVisible(false); } } } //设置准备状态; void DZ_GamePlayer::setReadyVisible(bool bVisible) { CC_ASSERT(m_pSprOper != nullptr); if (bVisible) { m_pSprOper->setTexture(DZ_READY_PNG_FILE); } m_pSprOper->setVisible(bVisible); m_bReady = bVisible; } //设置断线状态; void DZ_GamePlayer::setOfflineVisible(bool bVisible) { CC_ASSERT(m_pSprOffline != nullptr); m_pSprOffline->setVisible(bVisible); } //设置房主标志状态; void DZ_GamePlayer::setOwnerVisible(bool bVisible) { CC_ASSERT(m_pSprOwner != nullptr); m_pSprOwner->setVisible(bVisible); } //设置托管状态; void DZ_GamePlayer::setTrustVisible(bool bVisible) { CC_ASSERT(m_pSprTrust != nullptr); m_pSprTrust->setVisible(bVisible); } //设置叫分; void DZ_GamePlayer::setCallVisible(bool bVisible, uint8 cbScore/* = 255*/) { CC_ASSERT(m_pSprOper != nullptr); if (bVisible && 255==cbScore) { m_pSprOper->setTexture(DZ_PASS_SCORE_PNG_FILE); } m_pSprOper->setVisible(bVisible); } //设置不出; void DZ_GamePlayer::setPassVisible(bool bVisible) { CC_ASSERT(m_pSprOper != nullptr); if (bVisible) { m_pSprOper->setTexture(DZ_PASS_CARD_PNG_FILE); } m_pSprOper->setVisible(bVisible); } //设置玩家手牌; void DZ_GamePlayer::setHandCardData(uint8* pData, uint8 cbCount, bool bHorizontal) { if (m_pHandCardLayout!=nullptr) { m_pHandCardLayout->removeAllChildren(); int nWidth = m_pHandCardLayout->getContentSize().width; int nColumn = nWidth / 20; for (uint8 i = 0; i < cbCount; i++) { DZSmallCardSprite* pCardSprite = DZSmallCardSprite::create(pData[i]); if (i < nColumn) { pCardSprite->setPositionX(i * 20); } else { pCardSprite->setPositionX((i - nColumn) * 20); pCardSprite->setPositionY(0 - DZ_SMALL_CARD_HEIGHT*(i / nColumn)*2/3); } pCardSprite->setLocalZOrder(i); m_pHandCardLayout->addChild(pCardSprite); } //if (bHorizontal) //{ // for (uint8 i = 0; i < cbCount; i++) // { // DZSmallCardSprite* pCardSprite = DZSmallCardSprite::create(pData[i]); // pCardSprite->setPositionX(i * 20); // pCardSprite->setLocalZOrder(i); // m_pHandCardLayout->addChild(pCardSprite); // } //} //else //{ // int nPosY = m_pHandCardLayout->getContentSize().height; // for (uint8 i = 0; i < cbCount; i++) // { // DZSmallCardSprite* pCardSprite = DZSmallCardSprite::create(pData[i]); // pCardSprite->setPositionY(nPosY - i * 21 - DZ_SMALL_CARD_HEIGHT); // pCardSprite->setLocalZOrder(i); // m_pHandCardLayout->addChild(pCardSprite); // } //} } } //设置玩家打出去的牌; void DZ_GamePlayer::setOutCardData(uint8* pData, uint8 cbCount, bool bDelCard/* = false*/) { CC_ASSERT(m_pPanelOutCard != nullptr); if (pData == nullptr) { m_pPanelOutCard->setVisible(false); } else { m_pPanelOutCard->setVisible(true); } int iStartPosX = 0; int iCardWidth = DZ_BIG_CARD_WIDTH*0.5f + (cbCount - 1) * 25; if (m_cbViewID == DZ_SELF_VIEW_ID) { iStartPosX = (m_pPanelOutCard->getContentSize().width - iCardWidth) / 2; //iStartPosX = -iCardWidth / 2; } else if (m_cbViewID == DZ_RIGHT_VIEW_ID) { iStartPosX = m_pPanelOutCard->getContentSize().width - iCardWidth; //iStartPosX = -iCardWidth; } //if (bCallCard) //{ // if (m_cbViewID == DZ_SELF_VIEW_ID) // { // iStartPosX = (m_pPanelOutCard->getContentSize().width - iCardWidth) / 2; // //iStartPosX = -iCardWidth / 2; // } // else if (m_cbViewID == DZ_RIGHT_VIEW_ID) // { // iStartPosX = m_pPanelOutCard->getContentSize().width - iCardWidth; // //iStartPosX = -iCardWidth; // } //} //else //{ // if (m_cbViewID == DZ_SELF_VIEW_ID) // { // iStartPosX = (m_pPanelOutCard->getContentSize().width - iCardWidth) / 2; // //iStartPosX = -iCardWidth / 2; // } // else if (m_cbViewID == DZ_RIGHT_VIEW_ID) // { // iStartPosX = m_pPanelOutCard->getContentSize().width - iCardWidth; // //iStartPosX = -iCardWidth; // } //} m_pPanelOutCard->removeAllChildren(); for (uint8 i = 0; i < cbCount; i++) { bool bLordSign = false; if ((i + 1) == cbCount && m_bLandlord) { bLordSign = true; } DZBigCardSprite* pOutCardSprite = DZBigCardSprite::create(pData[i], bLordSign); pOutCardSprite->setScale(0.5f); pOutCardSprite->setPositionX(iStartPosX); //pOutCardSprite->setPositionX(iStartPosX + 25 * i); m_pPanelOutCard->addChild(pOutCardSprite); if (i > 0) { pOutCardSprite->runAction(MoveBy::create(0.4f, Vec2(25 * i, 0))); } else if (bDelCard && (i == 0)) { pOutCardSprite->runAction(Sequence::createWithTwoActions(DelayTime::create(2.0f), RemoveSelf::create())); } } } //显示倒计时; void DZ_GamePlayer::showClock(uint16 wSeconds, const std::function& callback/* = nullptr*/) { CC_ASSERT(m_pTxtAtlClock!=nullptr); Node* pNode = m_pTxtAtlClock->getParent(); CC_ASSERT(pNode!=nullptr); if (!(pNode->isVisible())) { pNode->setVisible(true); } m_pTxtAtlClock->setVisible(true); m_pTxtAtlClock->stopAllActions(); if (callback) { m_pTxtAtlClock->runAction(cocos2d::MoveExTxtTimeCallBack::create(wSeconds, wSeconds, 0, callback, 0)); } else { m_pTxtAtlClock->runAction(cocos2d::MoveExTxtTime::create(wSeconds)); } //float fDelayTime = 0.1f; //if (wSeconds>3) //{ // fDelayTime = wSeconds - 3; //} //auto rotCallFunc = CallFuncN::create([=](Node* target){ // float fTime = 0.05f; // RotateTo* rotate1 = RotateTo::create(fTime, 10); // RotateTo* rerotate1 = RotateTo::create(fTime, 0); // RotateTo* rotate2 = RotateTo::create(fTime, -10); // RepeatForever* actForever = RepeatForever::create(Sequence::create(rotate1, rerotate1, rotate2, rerotate1, nullptr)); // target->runAction(actForever); //}); ////重置旋转角度; //pNode->setRotation(0); //pNode->runAction(Sequence::create(DelayTime::create(fDelayTime), rotCallFunc, nullptr)); //pNode->runAction(actForever); } //隐藏倒计时 void DZ_GamePlayer::killClock() { CC_ASSERT(m_pTxtAtlClock != nullptr); Node* pNode = m_pTxtAtlClock->getParent(); CC_ASSERT(pNode != nullptr); pNode->stopAllActions(); pNode->setVisible(false); m_pTxtAtlClock->stopAllActions(); m_pTxtAtlClock->setVisible(false); } //显示排名; void DZ_GamePlayer::showRanking(uint8 cbRanking) { CC_ASSERT(m_pSprRanking!=nullptr); if (cbRanking>0 && cbRanking<=4) { m_pSprRanking->setVisible(true); string strPath = StringUtils::format("Games/DaZha/Table/imgRanking%d.png", cbRanking); m_pSprRanking->setTexture(strPath); } else { m_pSprRanking->setVisible(false); } } //显示叫的牌; void DZ_GamePlayer::showCallCard(uint8 cbCardData) { //CC_ASSERT(m_pNodeCallCard != nullptr); //m_pNodeCallCard->setVisible(true); //DZSmallCardSprite* pCardSprite = DZSmallCardSprite::create(cbCardData); //pCardSprite->ignoreAnchorPointForPosition(false); //pCardSprite->setAnchorPoint(Vec2(0.5f, 0.5f)); //m_pNodeCallCard->addChild(pCardSprite); } //更新积分; void DZ_GamePlayer::updateScore(bool bVisible) { CC_ASSERT(m_pTxtScore != nullptr); SCORE lScore = 0; if (bVisible) { lScore = GetUserScore(); } m_pTxtScore->setString(utility::toString(lScore)); } //更新手中扑克牌数量; void DZ_GamePlayer::setHandCardCount(uint8 cbCount, bool isVisble) { CC_ASSERT(m_pSprSurplusCard != nullptr); if (m_pSprSurplusCard->isVisible() != isVisble) { m_pSprSurplusCard->setVisible(isVisble); } if (cbCount > 5) { m_pTxtSurplus->setVisible(false); } else { m_pTxtSurplus->setVisible(true); } m_pTxtSurplus->setString(utility::toString(cbCount)); } //设置手牌数量不可见; void DZ_GamePlayer::setHandCardCountVisble(bool isVisble) { CC_ASSERT(m_pSprSurplusCard != nullptr); m_pSprSurplusCard->setVisible(isVisble); } //获取发牌位置; const Vec2 DZ_GamePlayer::getCardPos() { CC_ASSERT(m_pSprSurplusCard!=nullptr); return m_pSprSurplusCard->convertToWorldSpace(Vec2(m_pSprSurplusCard->getContentSize().width / 2, m_pSprSurplusCard->getContentSize().height/2)); //return m_pSprSurplusCard->getPosition() + m_pRootLayout->getPosition(); } //获取出牌位置; const Vec2& DZ_GamePlayer::getOutCardPos() { CC_ASSERT(m_pPanelOutCard != nullptr); return m_pPanelOutCard->getPosition(); } //获取头像位置; const Vec2& DZ_GamePlayer::getHeadPos() { CC_ASSERT(m_pSprHead!=nullptr); return m_pSprHead->getPosition(); } //转换到世界坐标系; Vec2 DZ_GamePlayer::convertToWorldSpace(const Vec2& nodePoint) const { CC_ASSERT(m_pRootLayout != nullptr); return m_pRootLayout->convertToWorldSpace(nodePoint); } //增加子节点; void DZ_GamePlayer::addChild(Node* pNode) { CC_ASSERT(m_pRootLayout != nullptr); m_pRootLayout->addChild(pNode); } void DZ_GamePlayer::upPlayerState() { //状态发生改变的用户; uint8 cbUserStatus = GetUserStatus(); switch (cbUserStatus) { case US_SIT: { break; } case US_READY: { //CGameServerItem* pGameServerItem = GameMission::g_pGameServerItem; //if (!CServerRule::IsAllowAvertCheatMode(pGameServerItem->GetServerRule())) //{ // setReadyVisible(true); //} setReadyVisible(true); break; } case US_PLAYING: { setReadyVisible(false); setOfflineVisible(false); break; } case US_OFFLINE: { setOfflineVisible(true); break; } default: break; } }