#include "PDK_Player.h" #include "ImagicDownManager.h" #include "PDK_BigCardSprite.h" #include "PDK_SmallCardSprite.h" #include "ActionEx.h" PDKPlayer::PDKPlayer(WORD wViewID, Layout* pRootLayout) : GamePlayer(NULL), m_bLandlord(false), m_bReady(false), m_cbViewID(wViewID), m_pRootLayout(pRootLayout) { init(); } PDKPlayer::~PDKPlayer() { } bool PDKPlayer::init() { //玩家昵称; m_pTxtNickName = static_cast(m_pRootLayout->getChildByName("nickname")); CC_ASSERT(m_pTxtNickName!=nullptr); m_pTxtNickName->setString(""); //工会名称; m_pTxtUnionName = static_cast(m_pRootLayout->getChildByName("txtUnionName")); CC_ASSERT(m_pTxtUnionName != nullptr); m_pTxtUnionName->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); //积分; m_pTxtScore = static_cast(m_pRootLayout->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_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_pSprSurplusCard = static_cast(m_pRootLayout->getChildByName("sprSurplusCard")); CC_ASSERT(m_pSprSurplusCard != nullptr); //剩余牌文本; m_pTxtAtlSurplus = static_cast(m_pSprSurplusCard->getChildByName("txtAtlSurplus")); CC_ASSERT(m_pTxtAtlSurplus != 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_pHandCardLayout = static_cast(m_pRootLayout->getChildByName("panelHandCard")); return true; } void PDKPlayer::setVisible(bool bShow) { m_pRootLayout->setVisible(bShow); } void PDKPlayer::PlayerEnter() { setInfoVisible(true); std::string strHttp = GetHeadHttp(); cocos2d::log("%s", strHttp.c_str()); uint32 dwUserID = GetUserID(); ImagicDownManager::Instance().addDown(m_pSprHead, strHttp, dwUserID); } void PDKPlayer::PlayerLeave() { setInfoVisible(false); setReadyVisible(false); setOfflineVisible(false); } //显示玩家聊天内容; void PDKPlayer::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 PDKPlayer::showVoiceBubble() { CC_ASSERT(m_pVoiceBubble != nullptr); m_pVoiceBubble->setVisible(true); // 启动帧动画; auto action = CSLoader::createTimeline("Games/DDZ/PlayerVoiceNode.csb"); action->gotoFrameAndPlay(0); m_pSprVoiceAni->runAction(action); } //隐藏语音气泡; void PDKPlayer::hideVoiceBubble() { CC_ASSERT(m_pVoiceBubble != nullptr); m_pSprVoiceAni->stopAllActions(); m_pVoiceBubble->setVisible(false); } //重置界面 void PDKPlayer::resetUI(bool bAll/* = false*/) { if ( bAll ) { setInfoVisible(false); setReadyVisible(false); setOfflineVisible(false); } //倒计时 m_pTxtAtlClock->stopAllActions(); m_pTxtAtlClock->setString(""); m_pTxtAtlClock->getParent()->setVisible(false); //剩余牌 m_pTxtAtlSurplus->setString(""); m_pTxtAtlSurplus->getParent()->setVisible(false); //地主标志 m_pLandFlag->setVisible(false); ////断线标志 //m_pSprOffline->setVisible(false); if (!m_bReady) { //操作标志 m_pSprOper->setVisible(false); } //剩余牌 m_pSprSurplusCard->setVisible(false); //出牌面板 m_pPanelOutCard->removeAllChildren(); m_pPanelOutCard->setVisible(false); } //玩家信息可见性 void PDKPlayer::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_pTxtUnionName != nullptr); m_pTxtUnionName->setString(bVisible ? GetUnionName() : ""); m_pTxtUnionName->setVisible(bVisible); CC_ASSERT(m_pTxtScore != nullptr); SCORE lScore = bVisible? GetUserScore() : 0; m_pTxtScore->setString(utility::toString(lScore)); m_pTxtScore->setVisible(bVisible); } //设置是否为地主; void PDKPlayer::setLand(bool bLandlord) { CC_ASSERT(m_pLandFlag!=nullptr); m_pLandFlag->setVisible(bLandlord); //if (bLandlord) //{ // m_pLandFlag->setTexture(PDK_LANDLORD_FLAG_PNG_FILE); //} //else //{ // m_pLandFlag->setTexture(PDK_FARMER_FLAG_PNG_FILE); //} } //设置准备状态; void PDKPlayer::setReadyVisible(bool bVisible) { CC_ASSERT(m_pSprOper != nullptr); if (bVisible) { m_pSprOper->setTexture(PDK_READY_PNG_FILE); } m_pSprOper->setVisible(bVisible); m_bReady = bVisible; } //设置断线状态; void PDKPlayer::setOfflineVisible(bool bVisible) { CC_ASSERT(m_pSprOffline != nullptr); m_pSprOffline->setVisible(bVisible); } //设置叫分; void PDKPlayer::setCallVisible(bool bVisible, bool bScore) { CC_ASSERT(m_pSprOper != nullptr); if (bVisible) { if (bScore) { m_pSprOper->setTexture(PDK_CALL_SCORE_PNG_FILE); } else { m_pSprOper->setTexture(PDK_PASS_SCORE_PNG_FILE); } } m_pSprOper->setVisible(bVisible); } //设置不出; void PDKPlayer::setPassVisible(bool bVisible) { CC_ASSERT(m_pSprOper != nullptr); if (bVisible) { m_pSprOper->setTexture(PDK_PASS_CARD_PNG_FILE); } m_pSprOper->setVisible(bVisible); } //设置玩家打出去的牌; void PDKPlayer::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 = PDK_BIG_CARD_WIDTH*0.5 + (cbCount - 1) * 25; //if (m_cbViewID == PDK_SELF_VIEW_ID) //{ // iStartPosX = (m_pPanelOutCard->getContentSize().width - iCardWidth) / 2; //} //else if (m_cbViewID == PDK_RIGHT_VIEW_ID) //{ // iStartPosX = m_pPanelOutCard->getContentSize().width - iCardWidth; //} // //m_pPanelOutCard->removeAllChildren(); //for (uint8 i = 0; i < cbCount; i++) //{ // bool bLordSign = false; // if ((i + 1) == cbCount && m_bLandlord) // { // bLordSign = true; // } // // PDKBigCardSprite* pOutCardSprite = PDKBigCardSprite::create(pData[i], bLordSign); // pOutCardSprite->setScale(0.5); // pOutCardSprite->setPosition(iStartPosX + 25 * i, 1.5); // m_pPanelOutCard->addChild(pOutCardSprite); //} CC_ASSERT(m_pPanelOutCard != nullptr); if (pData == nullptr) { m_pPanelOutCard->setVisible(false); } else { m_pPanelOutCard->setVisible(true); } int iStartPosX = 0; int iCardWidth = PDK_BIG_CARD_WIDTH*0.5f + (cbCount - 1) * 25; if (m_cbViewID == PDK_SELF_VIEW_ID) { iStartPosX = (m_pPanelOutCard->getContentSize().width - iCardWidth) / 2; } else if (m_cbViewID == PDK_RIGHT_VIEW_ID) { iStartPosX = m_pPanelOutCard->getContentSize().width - iCardWidth; } m_pPanelOutCard->removeAllChildren(); for (uint8 i = 0; i < cbCount; i++) { bool bLordSign = false; if ((i + 1) == cbCount && m_bLandlord) { bLordSign = true; } PDKBigCardSprite* pOutCardSprite = PDKBigCardSprite::create(pData[i], bLordSign); pOutCardSprite->setScale(0.5f); pOutCardSprite->setPositionX(iStartPosX); 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 PDKPlayer::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)); } //隐藏倒计时; void PDKPlayer::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 PDKPlayer::updateScore() { CC_ASSERT(m_pTxtScore != nullptr); SCORE lScore = GetUserScore(); m_pTxtScore->setString(utility::toString(lScore)); } //更新手中扑克牌数量; void PDKPlayer::setHandCardCount(uint8 cbCount) { CC_ASSERT(m_pSprSurplusCard != nullptr); if ( !m_pSprSurplusCard->isVisible() ) { m_pSprSurplusCard->setVisible(true); } m_pTxtAtlSurplus->setVisible(true); m_pTxtAtlSurplus->setString(utility::toString(cbCount)); } //设置玩家手牌; void PDKPlayer::setHandCardData(uint8* pData, uint8 cbCount) { if (m_pHandCardLayout != nullptr) { m_pHandCardLayout->removeAllChildren(); int nPosY = m_pHandCardLayout->getContentSize().height; for (uint8 i = 0; i < cbCount; i++) { PDKSmallCardSprite* pCardSprite = PDKSmallCardSprite::create(pData[i]); pCardSprite->setPositionY(nPosY - i * 21 - PDK_SMALL_CARD_HEIGHT); pCardSprite->setLocalZOrder(i); m_pHandCardLayout->addChild(pCardSprite); } } } //获取发牌位置 const Vec2 PDKPlayer::getCardPos() { CC_ASSERT(m_pSprSurplusCard!=nullptr); return m_pSprSurplusCard->getPosition() + m_pRootLayout->getPosition(); } //获取出牌位置 const Vec2& PDKPlayer::getOutCardPos() { CC_ASSERT(m_pPanelOutCard != nullptr); return m_pPanelOutCard->getPosition(); } //获取头像位置 const Vec2& PDKPlayer::getHeadPos() { CC_ASSERT(m_pSprHead!=nullptr); return m_pSprHead->getPosition(); } //转换到世界坐标系 Vec2 PDKPlayer::convertToWorldSpace(const Vec2& nodePoint) const { CC_ASSERT(m_pRootLayout != nullptr); return m_pRootLayout->convertToWorldSpace(nodePoint); } //增加子节点 void PDKPlayer::addChild(Node* pNode) { CC_ASSERT(m_pRootLayout != nullptr); m_pRootLayout->addChild(pNode); } void PDKPlayer::upPlayerState() { //状态发生改变的用户 uint8 cbUserStatus = GetUserStatus(); switch (cbUserStatus) { case US_SIT: { break; } case US_READY: { setReadyVisible(true); break; } case US_PLAYING: { setReadyVisible(false); setOfflineVisible(false); break; } case US_OFFLINE: { setOfflineVisible(true); break; } default: break; } }