Files
wnmj/Classes/Games/NiuNiu/NN_GamePlayer.cpp
2026-02-13 14:34:15 +08:00

517 lines
12 KiB
C++

#include "NN_GamePlayer.h"
#include "ImagicDownManager.h"
#include "NN_BigCardSprite.h"
#include "ActionEx.h"
#include "NN_CMD.h"
#define HAND_CARD_SCALE (0.8f) // 手牌缩放比例;
const Vec2 NN_GamePlayer::g_ptCardSpace = Vec2(197 * HAND_CARD_SCALE, 268 * HAND_CARD_SCALE);
const Vec2 NN_GamePlayer::g_ptCardFirst = Vec2(-197 * HAND_CARD_SCALE / 2, 0);
NN_GamePlayer::NN_GamePlayer(WORD wViewID, Layout* pRootLayout) :
GamePlayer(NULL), m_bReady(false), m_cbViewID(wViewID), m_pRootLayout(pRootLayout)
{
zeromemory(m_aryHandCard, sizeof(m_aryHandCard));
init();
}
NN_GamePlayer::~NN_GamePlayer()
{
}
bool NN_GamePlayer::init()
{
//玩家昵称;
m_pTxtNickName = static_cast<Text*>(m_pRootLayout->getChildByName("txtNickName"));
CC_ASSERT(m_pTxtNickName!=nullptr);
m_pTxtNickName->setString("");
//工会名称;
m_pTxtUnionName = static_cast<Text*>(m_pRootLayout->getChildByName("txtUnionName"));
CC_ASSERT(m_pTxtUnionName != nullptr);
m_pTxtUnionName->setString("");
//头像;
m_pSprHead = static_cast<Sprite*>(m_pRootLayout->getChildByName("head"));
CC_ASSERT(m_pSprHead!=nullptr);
m_pSprHead->setVisible(false);
//积分;
m_pTxtScore = static_cast<TextBMFont*>(m_pRootLayout->getChildByName("txtScore"));
CC_ASSERT(m_pTxtScore != nullptr);
m_pTxtScore->setString("");
//筹码;
m_pTxtChip = static_cast<TextBMFont*>(m_pRootLayout->getChildByName("txtUserChip"));
CC_ASSERT(m_pTxtChip != nullptr);
m_pTxtChip->setString("");
//房主标志;
m_pSprOwner = static_cast<Sprite*>(m_pRootLayout->getChildByName("owner"));
CC_ASSERT(m_pSprOwner != nullptr);
m_pSprOwner->setVisible(false);
//庄标志;
m_pSprZhuang = static_cast<Sprite*>(m_pRootLayout->getChildByName("zhuang"));
CC_ASSERT(m_pSprZhuang != nullptr);
m_pSprZhuang->setVisible(false);
//准备标志;
m_pSprReady = static_cast<Sprite*>(m_pRootLayout->getChildByName("imgReadyIcon"));
CC_ASSERT(m_pSprReady != nullptr);
m_pSprReady->setVisible(false);
//牛标志;
m_pArmatureSprCow = static_cast<Armature*>(m_pRootLayout->getChildByName("ArmatureType"));
CC_ASSERT(m_pArmatureSprCow != nullptr);
m_pArmatureSprCow->setVisible(false);
//断线标志;
m_pSprOffline = static_cast<Sprite*>(m_pRootLayout->getChildByName("offline"));
CC_ASSERT(m_pSprOffline != nullptr);
m_pSprOffline->setVisible(false);
//出牌面板;
m_pPanelHandCard = static_cast<Layout*>(m_pRootLayout->getChildByName("panelHandCard"));
CC_ASSERT(m_pPanelHandCard != nullptr);
// 获取每张手牌;
for (uint8 i = 0; i < NN_MAX_COUNT; i++)
{
std::string strKey = StringUtils::format("cardNode_%d", i);
auto cardNoe = static_cast<Node*>(m_pPanelHandCard->getChildByName(strKey));
ASSERT(cardNoe != nullptr);
if (cardNoe != nullptr)
{
m_aryHandCard[i] = new NNBigCardSprite(cardNoe, 0x0);
m_aryHandCard[i]->setVisible(false);
}
}
//聊天框背景;
m_pImgChatView = static_cast<ImageView*>(m_pRootLayout->getChildByName("imgChat"));
CC_ASSERT(m_pImgChatView != nullptr);
m_pImgChatView->setVisible(false);
auto panelChat = static_cast<Layout*>(m_pImgChatView->getChildByName("panelChat"));
CC_ASSERT(panelChat != nullptr);
//聊天内容;
m_pTxtChatInfo = static_cast<Text*>(panelChat->getChildByName("txtMsg"));
CC_ASSERT(m_pTxtChatInfo != nullptr);
//语音气泡;
m_pVoiceBubble = static_cast<Node*>(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);
return true;
}
void NN_GamePlayer::PlayerEnter()
{
setInfoVisible(true);
std::string strHttp = GetHeadHttp();
cocos2d::log("%s", strHttp.c_str());
uint32 dwUserID = GetUserID();
ImagicDownManager::Instance().addDown(m_pSprHead, strHttp, dwUserID);
//showChatInfo(utility::a_u8("作用:让目标动作赋予反弹力,且以目标动作结束位子开始反弹"));
}
void NN_GamePlayer::PlayerLeave()
{
if (m_isAllowLeave)
{
setInfoVisible(false);
setReadyVisible(false);
setOfflineVisible(false);
}
}
//显示玩家聊天内容;
void NN_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;
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 NN_GamePlayer::showVoiceBubble()
{
CC_ASSERT(m_pVoiceBubble != nullptr);
m_pVoiceBubble->setVisible(true);
// 启动帧动画;
auto action = CSLoader::createTimeline("Games/NiuNiu/PlayerVoiceNode.csb");
action->gotoFrameAndPlay(0);
m_pSprVoiceAni->runAction(action);
}
//隐藏语音气泡;
void NN_GamePlayer::hideVoiceBubble()
{
CC_ASSERT(m_pVoiceBubble != nullptr);
m_pSprVoiceAni->stopAllActions();
m_pVoiceBubble->setVisible(false);
}
//重置界面;
void NN_GamePlayer::resetUI(bool bAll/* = false*/)
{
if ( bAll )
{
setInfoVisible(false);
setReadyVisible(false);
setOfflineVisible(false);
}
//下注信息;
m_pTxtChip->setString("");
//断线标志;
m_pSprOffline->setVisible(false);
//手牌面板;
for (uint8 i = 0; i < NN_MAX_COUNT; i++)
{
m_aryHandCard[i]->setVisible(false);
}
m_pArmatureSprCow->setVisible(false);
m_pSprZhuang->setVisible(false);
}
//玩家信息可见性;
void NN_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_pTxtUnionName != nullptr);
m_pTxtUnionName->setString(bVisible ? GetUnionName() : "");
m_pTxtUnionName->setVisible(bVisible);
CC_ASSERT(m_pTxtScore != nullptr);
SCORE lGameScore = bVisible ? GetUserScore() : 0;
if (lGameScore >= 0)
{
m_pTxtScore->setString(StringUtils::format(".%d", lGameScore));
}
else
{
m_pTxtScore->setString(StringUtils::format("/%d", std::abs(lGameScore)));
}
//m_pTxtScore->setString(utility::toString<SCORE>(lScore));
m_pTxtScore->setVisible(bVisible);
}
//设置是否为房主;
void NN_GamePlayer::setOwner(bool bOwenr)
{
CC_ASSERT(m_pSprOwner != nullptr);
m_pSprOwner->setVisible(bOwenr);
}
//设置是否为庄家;
bool NN_GamePlayer::setBankerVisible(bool bVisible)
{
CC_ASSERT(m_pSprZhuang!=nullptr);
if (!m_pSprZhuang->isVisible())
{
float fscale = m_pSprZhuang->getScale();
m_pSprZhuang->setScale(0.2f);
auto actCardType = Sequence::create(Show::create(), EaseElasticOut::create(ScaleTo::create(0.8f, fscale)), nullptr);
m_pSprZhuang->runAction(actCardType);
m_pSprZhuang->setVisible(bVisible);
return true;
}
return false;
}
//设置准备状态;
void NN_GamePlayer::setReadyVisible(bool bVisible)
{
CC_ASSERT(m_pSprReady != nullptr);
m_pSprReady->setVisible(bVisible);
m_bReady = bVisible;
}
//设置断线状态;
void NN_GamePlayer::setOfflineVisible(bool bVisible)
{
CC_ASSERT(m_pSprOffline != nullptr);
m_pSprOffline->setVisible(bVisible);
}
//设置抢庄状态;
void NN_GamePlayer::setRobIconVisible(bool bVisible)
{
//CC_ASSERT(m_pSprQiang!=nullptr);
//if (bVisible)
//{
// m_pSprQiang->stopAllActions();
// auto actRepeat = Repeat::create(Sequence::create(ScaleTo::create(0.3f, 0.5f), ScaleTo::create(0.3f, 1.5f), nullptr), 5);
// auto act = Sequence::create(Show::create(), actRepeat, Hide::create(), nullptr);
// m_pSprQiang->runAction(act);
//}
//else
//{
// m_pSprQiang->stopAllActions();
// m_pSprQiang->setVisible(false);
//}
}
//设置玩家手牌;
void NN_GamePlayer::setHandCardData(uint8* pData, uint8 cbCount)
{
//CC_ASSERT(m_pPanelHandCard != nullptr);
//if (pData == nullptr)
//{
// m_pPanelHandCard->setVisible(false);
//}
//else
//{
// m_pPanelHandCard->setVisible(true);
//}
//m_pPanelHandCard->removeAllChildren();
////int iStartPosX = 0;
////for (uint8 i = 0; i < cbCount; i++)
////{
//// NNBigCardSprite* pCardSprite = NNBigCardSprite::create(pData[i]);
//// pCardSprite->setPositionX(iStartPosX);
//// //pCardSprite->setPositionX(iStartPosX + 25 * i);
//// m_pPanelHandCard->addChild(pCardSprite);
//// if (i > 0)
//// {
//// pCardSprite->runAction(MoveBy::create(0.4f, Vec2(25 * i, 0)));
//// }
////}
}
//增加牌;
void NN_GamePlayer::addCard(uint8 cbData, uint8 cbIndex)
{
CC_ASSERT(m_aryHandCard[cbIndex] != nullptr);
if (m_aryHandCard[cbIndex] != nullptr)
{
m_aryHandCard[cbIndex]->updateUIData(cbData);
}
}
//更新积分;
void NN_GamePlayer::updateScore()
{
CC_ASSERT(m_pTxtScore != nullptr);
SCORE lGameScore = GetUserScore();
if (lGameScore >= 0)
{
m_pTxtScore->setString(StringUtils::format(".%d", lGameScore));
}
else
{
m_pTxtScore->setString(StringUtils::format("/%d", std::abs(lGameScore)));
}
//m_pTxtScore->setString(utility::toString<SCORE>(lScore));
}
//更新筹码;
void NN_GamePlayer::updateUserChip(SCORE lChipScore)
{
CC_ASSERT(m_pTxtChip != nullptr);
std::string strChipScore = StringUtils::format("x%d", lChipScore);
m_pTxtChip->setString(strChipScore);
}
//显示牌型;
void NN_GamePlayer::showCardType(int cbType)
{
if (cbType >= 0 && cbType <= NN_MAX_NIU_TYPE)
{
cocostudio::ArmatureAnimation* pArmature = m_pArmatureSprCow->getAnimation();
if (pArmature != nullptr)
{
std::string strArmature = StringUtils::format("NNtype%d", cbType);
pArmature->play(strArmature);
m_pArmatureSprCow->setVisible(true);
}
}
else
{
//m_pSprCow->setVisible(false);
m_pArmatureSprCow->setVisible(false);
}
}
//显示牌;
void NN_GamePlayer::showCard(uint8 aryCardData[], uint8 cbCardCount)
{
//m_pPanelHandCard->removeAllChildren();
for (uint8 i = 0; i < cbCardCount; i++)
{
CC_ASSERT(m_aryHandCard[i] != nullptr);
if (m_aryHandCard[i] != nullptr)
{
m_aryHandCard[i]->updateUIData(aryCardData[i]);
}
}
}
//显示牌;
void NN_GamePlayer::showCard(uint8 aryCardData[], uint8 cbCardCount, int cbType)
{
//m_pPanelHandCard->removeAllChildren();
for (uint8 i = 0; i < cbCardCount; i++)
{
//addCard(aryCardData[i], cbType);
CC_ASSERT(m_aryHandCard[i] != nullptr);
if (m_aryHandCard[i] != nullptr)
{
m_aryHandCard[i]->updateUIData(aryCardData[i]);
}
}
showCardType(cbType);
}
//提示牛;
void NN_GamePlayer::tipCard(NN_CMD_S_OpenCard &stOpenCard)
{
if ((NIU_TYPE_1 <= stOpenCard.cbCardType) && (stOpenCard.cbCardType <= NIU_TYPE_10))
{
for (uint8 i = 0; i < NN_MAX_COUNT - 2; i++)
{
uint8 cbCardData = stOpenCard.cbCardData[i];
for (uint8 j = 0; j < NN_MAX_COUNT; j++)
{
CC_ASSERT(m_aryHandCard[j] != nullptr);
NNBigCardSprite* pCardSprite = m_aryHandCard[j];
if (pCardSprite != nullptr && pCardSprite->getCardData() == cbCardData && !pCardSprite->isShoot())
{
pCardSprite->shootCard();
break;
}
}
}
}
}
//获取头像位置;
const Vec2 NN_GamePlayer::getHeadPos()
{
CC_ASSERT(m_pSprHead!=nullptr);
Vec2 pos = m_pSprHead->convertToWorldSpace(Vec2(m_pSprHead->getContentSize().width/2, m_pSprHead->getContentSize().height/2));
return Vec2(pos.x, pos.y);
}
//获取手牌位置;
const Vec2 NN_GamePlayer::getHandCardPos(uint8 index)
{
CC_ASSERT(m_aryHandCard[index]);
return m_aryHandCard[index]->getPosition();
}
//转换到世界坐标系;
Vec2 NN_GamePlayer::convertToWorldSpace(const Vec2& nodePoint) const
{
CC_ASSERT(m_pRootLayout != nullptr);
return m_pRootLayout->convertToWorldSpace(nodePoint);
}
//增加子节点;
void NN_GamePlayer::addChild(Node* pNode)
{
CC_ASSERT(m_pRootLayout != nullptr);
m_pRootLayout->addChild(pNode);
}
void NN_GamePlayer::upPlayerState()
{
//状态发生改变的用户;
uint8 cbUserStatus = GetUserStatus();
switch (cbUserStatus)
{
case US_SIT:
{
break;
}
case US_READY:
{
setReadyVisible(true);
setOfflineVisible(false);
break;
}
case US_PLAYING:
{
setReadyVisible(false);
setOfflineVisible(false);
break;
}
case US_OFFLINE:
{
setOfflineVisible(true);
break;
}
default:
break;
}
}