Files
wnmj-normal/Classes/Games/WNMJ/WN_GamePlayer.cpp
2026-03-03 13:56:44 +08:00

392 lines
8.8 KiB
C++

#include "WN_GamePlayer.h"
#include "ImagicDownManager.h"
#include "ActionEx.h"
#include "GameMission.h"
using namespace WNMJ_SPACE;
WNGamePlayer::WNGamePlayer(WORD wViewID, Node* pRootLayout, Sprite* pReady) :
GamePlayer(NULL), m_cbViewID(wViewID), m_pRootLayout(pRootLayout), m_pSprOper(pReady)
{
m_bReady = false;
m_UnionScore = 0;
m_isAllowLeave = false;
init();
m_pRootLayout->setVisible(false);
}
WNGamePlayer::~WNGamePlayer()
{
m_pRootLayout->setVisible(false);
}
bool WNGamePlayer::init()
{
//聊天框背景;
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);
//操作显示;
auto btnNode = (Button*)m_pRootLayout->getChildByName("btnNode");
ASSERT(btnNode != nullptr);
//昵称;
m_pTxtNickName = static_cast<Text*>(btnNode->getChildByName("txtNickName"));
CC_ASSERT(m_pTxtNickName != nullptr);
m_pTxtNickName->setString("");
//工会名称;
m_pTxtUnionName = static_cast<Text*>(btnNode->getChildByName("txtUnionName"));
CC_ASSERT(m_pTxtUnionName != nullptr);
m_pTxtUnionName->setString("");
//积分;
m_pTxtExp = static_cast<Text*>(btnNode->getChildByName("txtExp"));
CC_ASSERT(m_pTxtExp != nullptr);
m_pTxtExp->setString("");
//经验;
m_pTxtScore = static_cast<Text*>(btnNode->getChildByName("txtGold"));
CC_ASSERT(m_pTxtScore != nullptr);
m_pTxtScore->setString("");
//头像;
m_pSprHead = static_cast<Sprite*>(btnNode->getChildByName("face"));
CC_ASSERT(m_pSprHead != nullptr);
//断线标志;
m_pSprOffline = static_cast<Sprite*>(btnNode->getChildByName("off_line"));
CC_ASSERT(m_pSprOffline != nullptr);
m_pSprOffline->setVisible(false);
//房主标志;
m_pSprOwner = static_cast<Sprite*>(btnNode->getChildByName("create"));
CC_ASSERT(m_pSprOwner != nullptr);
m_pSprOwner->setVisible(false);
//房主标志;
m_pSpZhuang = static_cast<Sprite*>(btnNode->getChildByName("BankerFlag"));
CC_ASSERT(m_pSpZhuang != nullptr);
m_pSpZhuang->setVisible(false);
return true;
}
void WNGamePlayer::setVisible(bool bShow)
{
m_pRootLayout->setVisible(bShow);
}
//显示本地信息;
void WNGamePlayer::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<SCORE>(lScore));
m_pTxtScore->setVisible(true);
}
// 进入;
void WNGamePlayer::PlayerEnter()
{
setInfoVisible(true);
std::string strHttp = GetHeadHttp();
cocos2d::log("%s", strHttp.c_str());
uint32 dwUserID = GetUserID();
ImagicDownManager::Instance().addDown(m_pSprHead, strHttp, dwUserID);
m_pRootLayout->setVisible(true);
}
// 离开;
void WNGamePlayer::PlayerLeave()
{
if (m_isAllowLeave)
{
setInfoVisible(false);
setReadyVisible(false);
setOfflineVisible(false);
setZhuangVisible(false);
m_pRootLayout->setVisible(false);
}
}
//显示玩家聊天内容;
void WNGamePlayer::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 WNGamePlayer::showVoiceBubble()
{
CC_ASSERT(m_pVoiceBubble != nullptr);
m_pVoiceBubble->setVisible(true);
// 启动帧动画;
auto action = CSLoader::createTimeline("Games/WNMJ/PlayerVoiceNode.csb");
action->gotoFrameAndPlay(0);
m_pSprVoiceAni->runAction(action);
}
//隐藏语音气泡;
void WNGamePlayer::hideVoiceBubble()
{
CC_ASSERT(m_pVoiceBubble != nullptr);
m_pSprVoiceAni->stopAllActions();
m_pVoiceBubble->setVisible(false);
}
//更新状态;
void WNGamePlayer::upPlayerState()
{
//状态发生改变的用户;
uint8 cbUserStatus = GetUserStatus();
switch (cbUserStatus)
{
case US_SIT:
{
break;
}
case US_READY:
case US_CAN_READY:
{
setReadyVisible(true);
setOfflineVisible(false);
break;
}
case US_PLAYING:
{
setReadyVisible(false);
setOfflineVisible(false);
break;
}
case US_OFFLINE:
{
setOfflineVisible(true);
break;
}
default:
break;
}
}
//重置界面;
void WNGamePlayer::resetUI(bool bAll/* = false*/)
{
if (bAll)
{
setInfoVisible(false);
setReadyVisible(false);
setOfflineVisible(false);
setRoomCreate(false);
}
setZhuangVisible(false);
//操作标志;
if (!m_bReady)
{
m_pSprOper->setVisible(false);
}
setTrustVisible(false);
}
//玩家信息可见性;
void WNGamePlayer::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<SCORE>(lScore));
m_pTxtScore->setVisible(bVisible);
CC_ASSERT(m_pTxtUnionName != nullptr);
m_pTxtUnionName->setString(bVisible ? GetUnionName() : "");
m_pTxtUnionName->setVisible(bVisible);
CC_ASSERT(m_pTxtExp != nullptr);
std::string strExp = utility::GetUserLevel(GetUserGrade());
m_pTxtExp->setString(bVisible ? utility::a_u8(strExp) : "");
m_pTxtExp->setVisible(bVisible);
}
//设置房主状态;
void WNGamePlayer::setRoomCreate(bool bVisible)
{
CC_ASSERT(m_pSprOwner != nullptr);
m_pSprOwner->setVisible(bVisible);
}
//设置准备状态;
void WNGamePlayer::setZhuangVisible(bool bVisible)
{
CC_ASSERT(m_pSpZhuang != nullptr);
m_pSpZhuang->setVisible(bVisible);
}
//设置准备状态;
void WNGamePlayer::setReadyVisible(bool bVisible)
{
CC_ASSERT(m_pSprOper != nullptr);
m_pSprOper->setVisible(bVisible);
m_bReady = bVisible;
}
//设置断线状态;
void WNGamePlayer::setOfflineVisible(bool bVisible)
{
CC_ASSERT(m_pSprOffline != nullptr);
m_pSprOffline->setVisible(bVisible);
}
//设置房主标志状态;
void WNGamePlayer::setOwnerVisible(bool bVisible)
{
CC_ASSERT(m_pSprOwner != nullptr);
m_pSprOwner->setVisible(bVisible);
}
//设置托管状态;
void WNGamePlayer::setTrustVisible(bool bVisible)
{
//CC_ASSERT(m_pSprTrust != nullptr);
//if (m_pSprTrust)
//{
// m_pSprTrust->setVisible(bVisible);
//}
}
//更新积分;
void WNGamePlayer::updateScore()
{
CC_ASSERT(m_pTxtScore != nullptr);
SCORE lScore = GetUserScore();
if (m_isAllowLeave)
{
lScore = GetUserScore() + m_UnionScore;
}
m_pTxtScore->setString(utility::toString<SCORE>(lScore));
}
//设置欢乐积分
void WNGamePlayer::setUnionScore(SCORE lscore)
{
m_UnionScore = lscore;
m_isAllowLeave = true;
updateScore();
}
//获取头像位置;
Vec2 WNGamePlayer::getHeadPos()
{
CC_ASSERT(m_pSprHead != nullptr);
const Size& size = m_pSprHead->getContentSize();
return m_pSprHead->convertToWorldSpace(Vec2(size.width/2, size.height/2));
}
//转换到世界坐标系;
Vec2 WNGamePlayer::convertToWorldSpace(const Vec2& nodePoint) const
{
CC_ASSERT(m_pRootLayout != nullptr);
return m_pRootLayout->convertToWorldSpace(nodePoint);
}
//获取准备坐标;
Vec2 WNGamePlayer::getReadyWorldPos()
{
return convertToWorldSpace(m_pSprOper->getPosition());
}
//增加子节点;
void WNGamePlayer::addChild(Node* pNode)
{
CC_ASSERT(m_pRootLayout != nullptr);
m_pRootLayout->addChild(pNode);
}
// 获取玩家头像纹理;
Texture2D* WNGamePlayer::GetTextureHead()
{
if (m_pSprHead == nullptr) return nullptr;
return m_pSprHead->getTexture();
}