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

171 lines
3.7 KiB
C++

#include "WN_GetCardCtrl.h"
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"
#include "WN_SparrowCard.h"
using namespace WNMJ_SPACE;
/////////////////////////////// 听牌 ///////////////////////////////////////////
GetCardCtrl::GetCardCtrl()
{
}
GetCardCtrl::~GetCardCtrl()
{
ResetAll();
}
bool GetCardCtrl::init()
{
if (!Node::init()) {
return false;
}
m_pRootNode = static_cast<Node*>(CSLoader::createNode("Games/WNMJ/GetCardLayer.csb"));
this->addChild(m_pRootNode);
auto imagebg = (ImageView*)m_pRootNode->getChildByName("Image_bg");
ASSERT(imagebg != nullptr);
m_pLeftCardList = (Layout*)imagebg->getChildByName("AllCard");
ASSERT(m_pLeftCardList != nullptr);
m_pCheakCard = (Layout*)imagebg->getChildByName("NowCard");
ASSERT(m_pCheakCard != nullptr);
auto btnClose = (Button*)imagebg->getChildByName("btnClose");
ASSERT(btnClose != nullptr);
if (btnClose)
{
btnClose->addClickEventListener([=](Ref *pSender){
this->HideGetCtrl();
});
}
return true;
}
void GetCardCtrl::ShowCardData(const MasterLeftCard* pLeftCard)
{
if (pLeftCard == nullptr) return;
ResetAll();
// 当前牌;
if (WN_CGameLogic::getInstance()->IsValidCard(pLeftCard->kMasterCheakCard))
{
WN_SparrowCard* card = WN_SparrowCard::createWithDirection(pLeftCard->kMasterCheakCard, SP_SELFSTAND);
if (card)
{
m_pCheakCard->addChild(card);
}
}
// 显示列表;
uint8 __ind = 0;
for (int i = 0; i < MAX_INDEX; i++)
{
if (pLeftCard->kMasterLeftIndex[i] > 0)
{
uint8 cardData = WN_CGameLogic::getInstance()->SwitchToCardData(i);
WN_SparrowCard* card = WN_SparrowCard::createWithDirection(cardData, SP_SELFSTAND);
if (card)
{
// 设置坐标;
Point pos = GetCardPoint(__ind);
card->setPosition(pos);
m_pLeftCardList->addChild(card);
__ind++;
// 增加点击事件;
card->addClickEventListener(CC_CALLBACK_2(GetCardCtrl::onSendCardEvent, this));
}
}
}
// 显示;
this->setVisible(true);
}
void GetCardCtrl::ResetAll()
{
if (m_pLeftCardList) m_pLeftCardList->removeAllChildren();
if (m_pCheakCard) m_pCheakCard->removeAllChildren();
}
void GetCardCtrl::HideGetCtrl()
{
ResetAll();
this->setVisible(false);
}
// 点击出牌
bool GetCardCtrl::onSendCardEvent(cocos2d::Ref *pSender, Vec2 pos)
{
if (pSender == nullptr) return false;
WN_SparrowCard* pCardSprite = (WN_SparrowCard*)pSender;
if (pCardSprite == nullptr) return false;
if (!pCardSprite->GetTop())
{
pCardSprite->SetTop(true);
pCardSprite->setPositionY(pCardSprite->getPositionY() + TOP_CARD_HEIGHT);
auto children = m_pLeftCardList->getChildren();
// 把其他已点击的牌落下
for (ssize_t i = 0; i < children.size(); i++)
{
WN_SparrowCard* childNode = static_cast<WN_SparrowCard*>(children.at(i));
//找到牌;
if (childNode != pCardSprite)
{
if (childNode->GetTop())
{
childNode->setPositionY(childNode->getPositionY() - TOP_CARD_HEIGHT);
childNode->SetTop(false);
}
}
}
}
else
{
//发送数据
CMD_C_MaterCheckCard MaterCheckCard;
MaterCheckCard.cbCheakCard = pCardSprite->m_ValueData;
EventCustom event(CUSTOM_EVENT_GET_CARD);
event.setUserData(&MaterCheckCard);
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
cocos2d::log("self out card %d", pCardSprite->m_ValueData);
this->HideGetCtrl();
}
return true;
}
// 得到手牌坐标
Point GetCardCtrl::GetCardPoint(uint8 cbIndex)
{
ASSERT(cbIndex <= MAX_INDEX);
float width = m_pLeftCardList->getContentSize().width;
int iRow = width / NCMJ_SELF_HAND_CARD_WIDHT; // 单行显示几个麻将牌;
// 根据组合牌数量进行偏移
Point pos;
pos.x = pos.x + NCMJ_SELF_HAND_CARD_WIDHT*(cbIndex%iRow);
pos.y = pos.y + 130*(cbIndex/iRow);
return pos;
}