Files
wnmj/Classes/Games/PDK/PDK_HandCardLayer.cpp

304 lines
7.3 KiB
C++
Raw Normal View History

2026-02-13 14:34:15 +08:00
#include <algorithm>
#include <iostream>
#include "SimpleAudioEngine.h"
#include "PDK_HandCardLayer.h"
using namespace CocosDenshion;
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
PDKHandCardLayer::PDKHandCardLayer() : _fnDoUserChooseCard(nullptr)
{
initUIData();
}
PDKHandCardLayer::~PDKHandCardLayer()
{
}
PDKHandCardLayer* PDKHandCardLayer::create(Size csParentSize, const std::function<void(uint8*, uint8)>& fnCallback)
{
PDKHandCardLayer *pRet = new PDKHandCardLayer();
if (pRet && pRet->init(csParentSize, fnCallback))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return nullptr;
}
bool PDKHandCardLayer::init(Size csParentSize, const std::function<void(uint8*, uint8)>& fnCallback)
{
if (!LayerColor::init())
{
return false;
}
_csParentSize = csParentSize;
_fnDoUserChooseCard = fnCallback;
this->setContentSize(_csParentSize);
this->setPosition(Vec2::ZERO);
auto mListener = EventListenerTouchOneByOne::create();
//ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
mListener->onTouchBegan = CC_CALLBACK_2(PDKHandCardLayer::onTouchBegan, this);
mListener->onTouchMoved = CC_CALLBACK_2(PDKHandCardLayer::onTouchMoved, this);
mListener->onTouchEnded = CC_CALLBACK_2(PDKHandCardLayer::onTouchEnded, this);
mListener->setSwallowTouches(true);
//<2F><><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>ɾ<EFBFBD><C9BE>(addEventListenerWithFixedPriority<74><79>Ҫ<EFBFBD>ֶ<EFBFBD>ɾ<EFBFBD><C9BE>)
_eventDispatcher->addEventListenerWithSceneGraphPriority(mListener, this);
return true;
}
void PDKHandCardLayer::onEnter()
{
LayerColor::onEnter();
}
void PDKHandCardLayer::onExit()
{
this->unscheduleAllCallbacks();
LayerColor::onExit();
}
bool PDKHandCardLayer::onTouchBegan(Touch *pTouch, Event *pEvent)
{
Vec2 ptTouch = convertTouchToNodeSpace(pTouch);
int iSelectCardIndex = isInCardSpriteRect(ptTouch);
if (iSelectCardIndex != 0xFF)
{
_iBeginSelectIndex = iSelectCardIndex;
onSelectCardAction(iSelectCardIndex);
return true;
}
return false;
}
void PDKHandCardLayer::onTouchMoved(Touch *pTouch, Event *pEvent)
{
Vec2 ptTouch = convertTouchToNodeSpace(pTouch);
int iSelectCardIndex = isInCardSpriteRect(ptTouch);
if (iSelectCardIndex != 0xFF)
{
onSelectCardAction(iSelectCardIndex);
}
}
void PDKHandCardLayer::onTouchEnded(Touch *pTouch, Event *pEvent)
{
Vec2 ptTouch = convertTouchToNodeSpace(pTouch);
bool bShootCard = true;
uint8 aryShootCard[PDK_MAX_COUNT] = {0};
uint8 cbShootCardCount = 0;
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i = 0; i < unCardCount; i++)
{
PDKBigCardSprite *pCardSprite = _vecHandCardArray[i];
if (pCardSprite->isGray())
{
pCardSprite->toNormal();
if (pCardSprite->isShoot())
{
bShootCard = false;
pCardSprite->downCard();
}
else
{
aryShootCard[cbShootCardCount++] = pCardSprite->getCardData();
pCardSprite->shootCard();
}
}
}
_iBeginSelectIndex = 0;
if (_fnDoUserChooseCard != nullptr)
{
_fnDoUserChooseCard(aryShootCard, cbShootCardCount);
}
}
void PDKHandCardLayer::initUIData()
{
_iSentCardCount = 0;
_iBeginSelectIndex = 0;
//_bLordMode = false;
_vecHandCardArray.clear();
this->removeAllChildren();
}
void PDKHandCardLayer::downAllCard()
{
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i=0; i<unCardCount; i++)
{
PDKBigCardSprite *pCardSprite = _vecHandCardArray[i];
if (pCardSprite->isShoot())
{
pCardSprite->downCard();
}
}
}
void PDKHandCardLayer::shootCardData(uint8 cbCardData[PDK_MAX_COUNT], uint8 cbCardCount)
{
downAllCard();
const size_t unCardCount = _vecHandCardArray.size();
for (uint8 i = 0; i < cbCardCount; i++)
{
for (size_t j = 0; j < unCardCount; j++)
{
PDKBigCardSprite *pCardSprite = _vecHandCardArray[j];
if (pCardSprite->getCardData() == cbCardData[i])
{
pCardSprite->shootCard();
}
}
}
}
void PDKHandCardLayer::getShootCardData(uint8 cbCardData[PDK_MAX_COUNT], uint8 &cbCardCount)
{
cbCardCount = 0;
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i=0; i<unCardCount; i++)
{
PDKBigCardSprite *pCardSprite = _vecHandCardArray[i];
if (pCardSprite->isShoot())
{
cbCardData[cbCardCount++] = pCardSprite->getCardData();
}
}
}
void PDKHandCardLayer::addSendCard2Panel()
{
_iSentCardCount++;
uint16 iHandCardSpace = getHandCardSpace(PDK_NORMAL_COUNT);
uint16 iHandCardWidth = getHandCardWidth(PDK_NORMAL_COUNT);
uint16 iStartPosX = (_csParentSize.width - iHandCardWidth) / 2;
PDKBigCardSprite* pCardSprite = PDKBigCardSprite::create(0, false);
pCardSprite->setPosition(iStartPosX + (_iSentCardCount - 1)*iHandCardSpace, 0);
addChild(pCardSprite);
}
void PDKHandCardLayer::redrawHandCard(uint8 cbCardData[PDK_MAX_COUNT], uint8 cbCardCount)
{
this->initUIData();
int iHandCardSpace = this->getHandCardSpace(cbCardCount);
int iHandCardWidth = this->getHandCardWidth(cbCardCount);
int iStartPosX = (this->_csParentSize.width - iHandCardWidth) / 2;
for (uint8 i = 0; i < cbCardCount; i++)
{
bool bLordMode = (_bLordMode == true && (i + 1) == cbCardCount);
PDKBigCardSprite* pCardSprite = PDKBigCardSprite::create(cbCardData[i], bLordMode);;
CC_ASSERT(pCardSprite!=nullptr);
pCardSprite->setPosition(iStartPosX + i*iHandCardSpace, 0);
this->addChild(pCardSprite);
_vecHandCardArray.push_back(pCardSprite);
}
}
uint16 PDKHandCardLayer::getHandCardSpace(uint8 cbCardCount)
{
if (cbCardCount < 2)
{
return 0;
}
uint16 wHandCardSpace = (_csParentSize.width - PDK_BIG_CARD_WIDTH*PDK_HAND_CARD_SCALE) / (cbCardCount - 1);
wHandCardSpace = __min(wHandCardSpace, PDK_HAND_CARD_SPACE);
return wHandCardSpace;
}
uint16 PDKHandCardLayer::getHandCardWidth(uint8 cbCardCount)
{
if (cbCardCount == 0)
{
return 0;
}
if (cbCardCount == 1)
{
return PDK_BIG_CARD_WIDTH*PDK_HAND_CARD_SCALE;
}
uint16 wHandCardWidth = (cbCardCount - 1)*getHandCardSpace(cbCardCount) + PDK_BIG_CARD_WIDTH*PDK_HAND_CARD_SCALE;
return wHandCardWidth;
}
uint8 PDKHandCardLayer::isInCardSpriteRect(Vec2 ptTouch)
{
Rect rect = getBoundingBox();
if (rect.containsPoint(ptTouch))
{
const size_t unCardCount = _vecHandCardArray.size();
int iCardSpace = getHandCardSpace(unCardCount);
for (size_t i = 0; i < unCardCount; i++)
{
PDKBigCardSprite *pCardSprite = _vecHandCardArray[i];
if ((i + 1) == unCardCount)
{
iCardSpace = PDK_BIG_CARD_WIDTH*PDK_HAND_CARD_SCALE;
}
Rect rcCardSprite = Rect(pCardSprite->getPositionX(), pCardSprite->getPositionY(), iCardSpace, PDK_BIG_CARD_HEIGHT*PDK_HAND_CARD_SCALE);
if (rcCardSprite.containsPoint(ptTouch))
{
return i;
}
}
}
return 0xFF;
}
void PDKHandCardLayer::onSelectCardAction(int iEndSelectIndex)
{
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i = 0; i < unCardCount; i++)
{
PDKBigCardSprite *pCardSprite = _vecHandCardArray[i];
pCardSprite->toNormal();
if (isInSelectCardIndex(i, iEndSelectIndex))
{
pCardSprite->toGray();
}
}
}
bool PDKHandCardLayer::isInSelectCardIndex(int iIndex, int iEndSelectIndex)
{
if (_iBeginSelectIndex < 0 || _iBeginSelectIndex==0xFF)
{
return false;
}
int iMinIndex = __min(_iBeginSelectIndex, iEndSelectIndex);
int iMaxIndex = __max(_iBeginSelectIndex, iEndSelectIndex);
if (iIndex >= iMinIndex && iIndex <= iMaxIndex)
{
return true;
}
return false;
}