Files
wnmj/Classes/Games/DaZha/DZ_HandCardLayer.cpp

432 lines
11 KiB
C++
Raw Normal View History

2026-02-13 14:34:15 +08:00
#include <algorithm>
#include <iostream>
#include "SimpleAudioEngine.h"
#include "DZ_HandCardLayer.h"
#include "DZ_BigCardSprite.h"
#include "DZ_SmallCardSprite.h"
using namespace CocosDenshion;
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
DZHandCardLayer::DZHandCardLayer() : _fnDoUserChooseCard(nullptr)
{
_iCardWidth = DZ_BIG_CARD_WIDTH*DZ_HAND_CARD_SCALE;
_iCardHeight = DZ_BIG_CARD_HEIGHT*DZ_HAND_CARD_SCALE;
_bMultiSelect = true;
_bMultiRows = false;
initUIData();
}
DZHandCardLayer::~DZHandCardLayer()
{
}
DZHandCardLayer* DZHandCardLayer::create(Size csParentSize, const std::function<void(uint8*, uint8)>& fnCallback, uint8 cbCardType/* = DZ_CARD_SPRITE_BIG*/)
{
DZHandCardLayer *pRet = new DZHandCardLayer();
if (pRet && pRet->init(csParentSize, fnCallback, cbCardType))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return nullptr;
}
bool DZHandCardLayer::init(Size csParentSize, const std::function<void(uint8*, uint8)>& fnCallback, uint8 cbCardType/* = DZ_CARD_SPRITE_BIG*/)
{
if (!LayerColor::init())
{
return false;
}
if (cbCardType == DZ_CARD_SPRITE_ASKFOR || cbCardType == DZ_CARD_SPRITE_CALL)
{
_iCardWidth = DZ_SMALL_CARD_WIDTH;
_iCardHeight = DZ_SMALL_CARD_HEIGHT;
}
_cbCardType = cbCardType;
_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(DZHandCardLayer::onTouchBegan, this);
mListener->onTouchMoved = CC_CALLBACK_2(DZHandCardLayer::onTouchMoved, this);
mListener->onTouchEnded = CC_CALLBACK_2(DZHandCardLayer::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 DZHandCardLayer::onEnter()
{
LayerColor::onEnter();
}
void DZHandCardLayer::onExit()
{
this->unscheduleAllCallbacks();
LayerColor::onExit();
}
bool DZHandCardLayer::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 DZHandCardLayer::onTouchMoved(Touch *pTouch, Event *pEvent)
{
if (!_bMultiSelect)
{
return;
}
Vec2 ptTouch = convertTouchToNodeSpace(pTouch);
int iSelectCardIndex = isInCardSpriteRect(ptTouch);
if (iSelectCardIndex != 0xFF)
{
onSelectCardAction(iSelectCardIndex);
}
}
void DZHandCardLayer::onTouchEnded(Touch *pTouch, Event *pEvent)
{
Vec2 ptTouch = convertTouchToNodeSpace(pTouch);
bool bShootCard = true;
uint8 aryShootCard[DZ_MAX_COUNT] = {0};
uint8 cbShootCardCount = 0;
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i = 0; i < unCardCount; i++)
{
DZBaseCardSprite *pCardSprite = _vecHandCardArray[i];
if ((false==pCardSprite->isSelectEnabled())||pCardSprite->isMustShoot())
{
continue;
}
if (pCardSprite->isGray())
{
pCardSprite->toNormal();
if (pCardSprite->isShoot())
{
bShootCard = false;
pCardSprite->downCard();
}
else
{
aryShootCard[cbShootCardCount++] = pCardSprite->getCardData();
pCardSprite->shootCard();
}
}
else if (!_bMultiSelect && pCardSprite->isShoot())
{
pCardSprite->downCard();
}
}
_iBeginSelectIndex = 0;
if (_fnDoUserChooseCard != nullptr)
{
_fnDoUserChooseCard(aryShootCard, cbShootCardCount);
}
}
void DZHandCardLayer::initUIData()
{
_iSentCardCount = 0;
_iBeginSelectIndex = 0;
//_bLordMode = false;
_vecHandCardArray.clear();
this->removeAllChildren();
}
void DZHandCardLayer::downAllCard()
{
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i=0; i<unCardCount; i++)
{
DZBaseCardSprite *pCardSprite = _vecHandCardArray[i];
if (pCardSprite->isShoot())
{
pCardSprite->downCard();
}
}
}
void DZHandCardLayer::shootCardData(uint8 cbCardData[DZ_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++)
{
DZBaseCardSprite *pCardSprite = _vecHandCardArray[j];
if ((pCardSprite->getCardData() == cbCardData[i]) && !pCardSprite->isShoot())
{
pCardSprite->shootCard();
break;
}
}
}
}
void DZHandCardLayer::getShootCardData(uint8 cbCardData[DZ_MAX_COUNT], uint8 &cbCardCount)
{
cbCardCount = 0;
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i=0; i<unCardCount; i++)
{
DZBaseCardSprite *pCardSprite = _vecHandCardArray[i];
if (pCardSprite->isShoot())
{
cbCardData[cbCardCount++] = pCardSprite->getCardData();
}
}
}
void DZHandCardLayer::addSendCard2Panel()
{
_iSentCardCount++;
uint16 iHandCardSpace = getHandCardSpace(DZ_NORMAL_COUNT);
uint16 iHandCardWidth = getHandCardWidth(DZ_NORMAL_COUNT);
uint16 iStartPosX = (_csParentSize.width - iHandCardWidth) / 2;
DZBigCardSprite* pCardSprite = DZBigCardSprite::create(0, false);
pCardSprite->setPosition(iStartPosX + (_iSentCardCount - 1)*iHandCardSpace, 0);
addChild(pCardSprite);
}
void DZHandCardLayer::redrawHandCard(uint8 cbCardData[DZ_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);
DZBaseCardSprite* pCardSprite = nullptr;
if (_cbCardType == DZ_CARD_SPRITE_BIG)
{
pCardSprite = DZBigCardSprite::create(cbCardData[i], bLordMode);
}
else if (_cbCardType == DZ_CARD_SPRITE_ASKFOR)
{
//pCardSprite = DZAskForCardSprite::create(cbCardData[i]);
}
else if (_cbCardType == DZ_CARD_SPRITE_CALL)
{
pCardSprite = DZSmallCardSprite::create(cbCardData[i]);
}
CC_ASSERT(pCardSprite!=nullptr);
pCardSprite->setPosition(iStartPosX + i*iHandCardSpace, 0);
this->addChild(pCardSprite);
_vecHandCardArray.push_back(pCardSprite);
}
}
////<2F><><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>;
//void DZHandCardLayer::insertOneCard(uint8 cbCardData)
//{
// const size_t unCardCount = _vecHandCardArray.size();
//
// int iHandCardSpace = this->getHandCardSpace(unCardCount + 1);
// int iHandCardWidth = this->getHandCardWidth(unCardCount + 1);
// int iStartPosX = (this->_csParentSize.width - iHandCardWidth) / 2;
//
// int nInsertPos = -1;
// for (size_t j = 0; j < unCardCount; j++)
// {
// DZBigCardSprite *pCardSprite = _vecHandCardArray[j];
// CC_ASSERT(pCardSprite != nullptr);
// pCardSprite->setPosition(iStartPosX + j*iHandCardSpace, 0);
// pCardSprite->setLocalZOrder(j);
//
// BYTE cbStoreCardData = pCardSprite->getCardData();
// BYTE cbStoreLogicValue = GetCardLogicValue(cbStoreCardData);
// BYTE cbInsertLogicValue = GetCardLogicValue(cbCardData);
// if (cbInsertLogicValue>cbStoreLogicValue || (cbInsertLogicValue == cbStoreLogicValue&&cbCardData>cbStoreCardData))
// {
// DZBigCardSprite* pNewCardSprite = DZBigCardSprite::create(cbCardData);
// this->addChild(pNewCardSprite);
//
// _vecHandCardArray.insert(_vecHandCardArray.begin()+j, pNewCardSprite);
// pNewCardSprite->shootCard();
//
// pNewCardSprite->setPosition(iStartPosX + (j+1)*iHandCardSpace, 0);
// pNewCardSprite->setLocalZOrder(j + 1);
//
// nInsertPos = j;
// unCardCount++;
// break;
// }
// }
//}
void DZHandCardLayer::setSelectCard(uint8 cbCardData)
{
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i = 0; i < unCardCount; i++)
{
DZBaseCardSprite *pCardSprite = _vecHandCardArray[i];
uint8 cbStoreData = pCardSprite->getCardData();
if ((cbStoreData&0x0F)!=cbCardData)
{
pCardSprite->setSelectEnabled(false);
pCardSprite->toGray();
}
}
}
//<2F><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
void DZHandCardLayer::canSelectAllCard()
{
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i = 0; i < unCardCount; i++)
{
DZBaseCardSprite *pCardSprite = _vecHandCardArray[i];
pCardSprite->setSelectEnabled(true);
pCardSprite->toNormal();
}
}
//<2F><><EFBFBD>ñ<EFBFBD>ѡ<EFBFBD><D1A1>;
void DZHandCardLayer::setMustShootCard(uint8 cbCardData)
{
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i = 0; i < unCardCount; i++)
{
DZBaseCardSprite *pCardSprite = _vecHandCardArray[i];
uint8 cbStoreData = pCardSprite->getCardData();
if (cbStoreData==cbCardData)
{
pCardSprite->shootCard();
pCardSprite->setMustShoot(true);
}
}
}
uint16 DZHandCardLayer::getHandCardSpace(uint8 cbCardCount)
{
if (cbCardCount < 2)
{
return 0;
}
uint16 wHandCardSpace = (_csParentSize.width - _iCardWidth) / (cbCardCount - 1);
wHandCardSpace = __min(wHandCardSpace, DZ_HAND_CARD_SPACE);
return wHandCardSpace;
}
uint16 DZHandCardLayer::getHandCardWidth(uint8 cbCardCount)
{
if (cbCardCount == 0)
{
return 0;
}
if (cbCardCount == 1)
{
return _iCardWidth;
}
uint16 wHandCardWidth = (cbCardCount - 1)*getHandCardSpace(cbCardCount) + _iCardWidth;
return wHandCardWidth;
}
uint8 DZHandCardLayer::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++)
{
DZBaseCardSprite *pCardSprite = _vecHandCardArray[i];
if ((i + 1) == unCardCount)
{
iCardSpace = _iCardWidth;
}
Rect rcCardSprite = Rect(pCardSprite->getPositionX(), pCardSprite->getPositionY(), iCardSpace, _iCardHeight);
if (rcCardSprite.containsPoint(ptTouch))
{
return i;
}
}
}
return 0xFF;
}
void DZHandCardLayer::onSelectCardAction(int iEndSelectIndex)
{
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i = 0; i < unCardCount; i++)
{
DZBaseCardSprite *pCardSprite = _vecHandCardArray[i];
if ((false == pCardSprite->isSelectEnabled()) || pCardSprite->isMustShoot())
{
continue;
}
pCardSprite->toNormal();
if (isInSelectCardIndex(i, iEndSelectIndex))
{
pCardSprite->toGray();
}
}
}
bool DZHandCardLayer::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;
}