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

304 lines
7.3 KiB
C++

#include <algorithm>
#include <iostream>
#include "SimpleAudioEngine.h"
#include "DDZ_HandCardLayer.h"
using namespace CocosDenshion;
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
DDZHandCardLayer::DDZHandCardLayer() : _fnDoUserChooseCard(nullptr)
{
initUIData();
}
DDZHandCardLayer::~DDZHandCardLayer()
{
}
DDZHandCardLayer* DDZHandCardLayer::create(Size csParentSize, const std::function<void(uint8*, uint8)>& fnCallback)
{
DDZHandCardLayer *pRet = new DDZHandCardLayer();
if (pRet && pRet->init(csParentSize, fnCallback))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return nullptr;
}
bool DDZHandCardLayer::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();
//指定触摸的回调函数
mListener->onTouchBegan = CC_CALLBACK_2(DDZHandCardLayer::onTouchBegan, this);
mListener->onTouchMoved = CC_CALLBACK_2(DDZHandCardLayer::onTouchMoved, this);
mListener->onTouchEnded = CC_CALLBACK_2(DDZHandCardLayer::onTouchEnded, this);
mListener->setSwallowTouches(true);
//不用手动删除(addEventListenerWithFixedPriority需要手动删除)
_eventDispatcher->addEventListenerWithSceneGraphPriority(mListener, this);
return true;
}
void DDZHandCardLayer::onEnter()
{
LayerColor::onEnter();
}
void DDZHandCardLayer::onExit()
{
this->unscheduleAllCallbacks();
LayerColor::onExit();
}
bool DDZHandCardLayer::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 DDZHandCardLayer::onTouchMoved(Touch *pTouch, Event *pEvent)
{
Vec2 ptTouch = convertTouchToNodeSpace(pTouch);
int iSelectCardIndex = isInCardSpriteRect(ptTouch);
if (iSelectCardIndex != 0xFF)
{
onSelectCardAction(iSelectCardIndex);
}
}
void DDZHandCardLayer::onTouchEnded(Touch *pTouch, Event *pEvent)
{
Vec2 ptTouch = convertTouchToNodeSpace(pTouch);
bool bShootCard = true;
uint8 aryShootCard[DDZ_MAX_COUNT] = {0};
uint8 cbShootCardCount = 0;
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i = 0; i < unCardCount; i++)
{
DDZBigCardSprite *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 DDZHandCardLayer::initUIData()
{
_iSentCardCount = 0;
_iBeginSelectIndex = 0;
//_bLordMode = false;
_vecHandCardArray.clear();
this->removeAllChildren();
}
void DDZHandCardLayer::downAllCard()
{
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i=0; i<unCardCount; i++)
{
DDZBigCardSprite *pCardSprite = _vecHandCardArray[i];
if (pCardSprite->isShoot())
{
pCardSprite->downCard();
}
}
}
void DDZHandCardLayer::shootCardData(uint8 cbCardData[DDZ_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++)
{
DDZBigCardSprite *pCardSprite = _vecHandCardArray[j];
if (pCardSprite->getCardData() == cbCardData[i])
{
pCardSprite->shootCard();
}
}
}
}
void DDZHandCardLayer::getShootCardData(uint8 cbCardData[DDZ_MAX_COUNT], uint8 &cbCardCount)
{
cbCardCount = 0;
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i=0; i<unCardCount; i++)
{
DDZBigCardSprite *pCardSprite = _vecHandCardArray[i];
if (pCardSprite->isShoot())
{
cbCardData[cbCardCount++] = pCardSprite->getCardData();
}
}
}
void DDZHandCardLayer::addSendCard2Panel()
{
_iSentCardCount++;
uint16 iHandCardSpace = getHandCardSpace(DDZ_NORMAL_COUNT);
uint16 iHandCardWidth = getHandCardWidth(DDZ_NORMAL_COUNT);
uint16 iStartPosX = (_csParentSize.width - iHandCardWidth) / 2;
DDZBigCardSprite* pCardSprite = DDZBigCardSprite::create(0, false);
pCardSprite->setPosition(iStartPosX + (_iSentCardCount - 1)*iHandCardSpace, 0);
addChild(pCardSprite);
}
void DDZHandCardLayer::redrawHandCard(uint8 cbCardData[DDZ_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);
DDZBigCardSprite* pCardSprite = DDZBigCardSprite::create(cbCardData[i], bLordMode);;
CC_ASSERT(pCardSprite!=nullptr);
pCardSprite->setPosition(iStartPosX + i*iHandCardSpace, 0);
this->addChild(pCardSprite);
_vecHandCardArray.push_back(pCardSprite);
}
}
uint16 DDZHandCardLayer::getHandCardSpace(uint8 cbCardCount)
{
if (cbCardCount < 2)
{
return 0;
}
uint16 wHandCardSpace = (_csParentSize.width - DDZ_BIG_CARD_WIDTH*DDZ_HAND_CARD_SCALE) / (cbCardCount - 1);
wHandCardSpace = __min(wHandCardSpace, DDZ_HAND_CARD_SPACE);
return wHandCardSpace;
}
uint16 DDZHandCardLayer::getHandCardWidth(uint8 cbCardCount)
{
if (cbCardCount == 0)
{
return 0;
}
if (cbCardCount == 1)
{
return DDZ_BIG_CARD_WIDTH*DDZ_HAND_CARD_SCALE;
}
uint16 wHandCardWidth = (cbCardCount - 1)*getHandCardSpace(cbCardCount) + DDZ_BIG_CARD_WIDTH*DDZ_HAND_CARD_SCALE;
return wHandCardWidth;
}
uint8 DDZHandCardLayer::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++)
{
DDZBigCardSprite *pCardSprite = _vecHandCardArray[i];
if ((i + 1) == unCardCount)
{
iCardSpace = DDZ_BIG_CARD_WIDTH*DDZ_HAND_CARD_SCALE;
}
Rect rcCardSprite = Rect(pCardSprite->getPositionX(), pCardSprite->getPositionY(), iCardSpace, DDZ_BIG_CARD_HEIGHT*DDZ_HAND_CARD_SCALE);
if (rcCardSprite.containsPoint(ptTouch))
{
return i;
}
}
}
return 0xFF;
}
void DDZHandCardLayer::onSelectCardAction(int iEndSelectIndex)
{
const size_t unCardCount = _vecHandCardArray.size();
for (size_t i = 0; i < unCardCount; i++)
{
DDZBigCardSprite *pCardSprite = _vecHandCardArray[i];
pCardSprite->toNormal();
if (isInSelectCardIndex(i, iEndSelectIndex))
{
pCardSprite->toGray();
}
}
}
bool DDZHandCardLayer::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;
}