349 lines
8.1 KiB
C++
349 lines
8.1 KiB
C++
|
|
|
|||
|
|
#include <algorithm>
|
|||
|
|
#include <iostream>
|
|||
|
|
#include "SimpleAudioEngine.h"
|
|||
|
|
#include "13S_OperateCardPanel.h"
|
|||
|
|
using namespace CocosDenshion;
|
|||
|
|
|
|||
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|||
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|||
|
|
SSSOperateCardPanel::SSSOperateCardPanel() : _fnChooseCard(nullptr)
|
|||
|
|
{
|
|||
|
|
initUIData();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
SSSOperateCardPanel::~SSSOperateCardPanel()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
SSSOperateCardPanel* SSSOperateCardPanel::create(Size csParentSize, const std::function<void(uint8*, uint8)>& fnCallback)
|
|||
|
|
{
|
|||
|
|
SSSOperateCardPanel *pRet = new SSSOperateCardPanel();
|
|||
|
|
if (pRet && pRet->init(csParentSize, fnCallback))
|
|||
|
|
{
|
|||
|
|
pRet->autorelease();
|
|||
|
|
return pRet;
|
|||
|
|
}
|
|||
|
|
CC_SAFE_DELETE(pRet);
|
|||
|
|
return nullptr;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool SSSOperateCardPanel::init(Size csParentSize, const std::function<void(uint8*, uint8)>& fnCallback)
|
|||
|
|
{
|
|||
|
|
if (!LayerColor::init())
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_csParentSize = csParentSize;
|
|||
|
|
_fnChooseCard = 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(SSSOperateCardPanel::onTouchBegan, this);
|
|||
|
|
mListener->onTouchMoved = CC_CALLBACK_2(SSSOperateCardPanel::onTouchMoved, this);
|
|||
|
|
mListener->onTouchEnded = CC_CALLBACK_2(SSSOperateCardPanel::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 SSSOperateCardPanel::onEnter()
|
|||
|
|
{
|
|||
|
|
LayerColor::onEnter();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SSSOperateCardPanel::onExit()
|
|||
|
|
{
|
|||
|
|
this->unscheduleAllCallbacks();
|
|||
|
|
LayerColor::onExit();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool SSSOperateCardPanel::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 SSSOperateCardPanel::onTouchMoved(Touch *pTouch, Event *pEvent)
|
|||
|
|
{
|
|||
|
|
Vec2 ptTouch = convertTouchToNodeSpace(pTouch);
|
|||
|
|
|
|||
|
|
int iSelectCardIndex = isInCardSpriteRect(ptTouch);
|
|||
|
|
if (iSelectCardIndex != 0xFF)
|
|||
|
|
{
|
|||
|
|
onSelectCardAction(iSelectCardIndex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SSSOperateCardPanel::onTouchEnded(Touch *pTouch, Event *pEvent)
|
|||
|
|
{
|
|||
|
|
Vec2 ptTouch = convertTouchToNodeSpace(pTouch);
|
|||
|
|
bool bShootCard = true;
|
|||
|
|
uint8 aryShootCard[SSS_MAX_COUNT] = { 0 };
|
|||
|
|
uint8 cbShootCardCount = 0;
|
|||
|
|
|
|||
|
|
const size_t unCardCount = _vecHandCardArray.size();
|
|||
|
|
for (size_t i = 0; i < unCardCount; i++)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite *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 (_fnChooseCard != nullptr)
|
|||
|
|
{
|
|||
|
|
_fnChooseCard(aryShootCard, cbShootCardCount);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SSSOperateCardPanel::initUIData()
|
|||
|
|
{
|
|||
|
|
_iBeginSelectIndex = 0;
|
|||
|
|
|
|||
|
|
_vecHandCardArray.clear();
|
|||
|
|
this->removeAllChildren();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SSSOperateCardPanel::downAllCard()
|
|||
|
|
{
|
|||
|
|
const size_t unCardCount = _vecHandCardArray.size();
|
|||
|
|
for (size_t i = 0; i < unCardCount; i++)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite *pCardSprite = _vecHandCardArray[i];
|
|||
|
|
if (pCardSprite->isShoot())
|
|||
|
|
{
|
|||
|
|
pCardSprite->downCard();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SSSOperateCardPanel::shootCardData(uint8 cbCardData[SSS_MAX_COUNT], uint8 cbCardCount)
|
|||
|
|
{
|
|||
|
|
downAllCard();
|
|||
|
|
|
|||
|
|
const size_t unAllCardCount = _vecHandCardArray.size();
|
|||
|
|
for (uint8 i = 0; i < cbCardCount; i++)
|
|||
|
|
{
|
|||
|
|
for (size_t j = 0; j < unAllCardCount; j++)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite *pCardSprite = _vecHandCardArray[j];
|
|||
|
|
if (pCardSprite->getCardData() == cbCardData[i])
|
|||
|
|
{
|
|||
|
|
pCardSprite->shootCard();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
|
|||
|
|
void SSSOperateCardPanel::shootCardIndex(uint8 cbCardIndex[SSS_MAX_COUNT], uint8 cbCardCount)
|
|||
|
|
{
|
|||
|
|
downAllCard();
|
|||
|
|
|
|||
|
|
const size_t unAllCardCount = _vecHandCardArray.size();
|
|||
|
|
for (uint8 i = 0; i < cbCardCount; i++)
|
|||
|
|
{
|
|||
|
|
uint8 cbTempIndex = cbCardIndex[i];
|
|||
|
|
if (cbTempIndex<unAllCardCount)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite *pCardSprite = _vecHandCardArray[cbTempIndex];
|
|||
|
|
CC_ASSERT(pCardSprite!=nullptr);
|
|||
|
|
if (pCardSprite != nullptr)
|
|||
|
|
{
|
|||
|
|
pCardSprite->shootCard();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SSSOperateCardPanel::getShootCardData(uint8 aryOutCardData[SSS_MAX_COUNT], uint8 &cbOutCardCount, uint8 cbMaxOutCardCount)
|
|||
|
|
{
|
|||
|
|
cbOutCardCount = 0;
|
|||
|
|
const size_t unCardCount = _vecHandCardArray.size();
|
|||
|
|
for (size_t i = 0; i < unCardCount; i++)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite *pCardSprite = _vecHandCardArray[i];
|
|||
|
|
if ( pCardSprite->isShoot() )
|
|||
|
|
{
|
|||
|
|
aryOutCardData[cbOutCardCount++] = pCardSprite->getCardData();
|
|||
|
|
if (cbOutCardCount >= cbMaxOutCardCount)
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><>ȡδ<C8A1><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
|
|||
|
|
void SSSOperateCardPanel::getDownCardData(uint8 aryOutCardData[SSS_MAX_COUNT], uint8 &cbOutCardCount, uint8 cbMaxOutCardCount)
|
|||
|
|
{
|
|||
|
|
cbOutCardCount = 0;
|
|||
|
|
const size_t unCardCount = _vecHandCardArray.size();
|
|||
|
|
for (size_t i = 0; i < unCardCount; i++)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite *pCardSprite = _vecHandCardArray[i];
|
|||
|
|
if (!pCardSprite->isShoot())
|
|||
|
|
{
|
|||
|
|
aryOutCardData[cbOutCardCount++] = pCardSprite->getCardData();
|
|||
|
|
if (cbOutCardCount >= cbMaxOutCardCount)
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SSSOperateCardPanel::showHandCard(uint8 cbCardData[SSS_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++)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite* pCardSprite = SSSBigCardSprite::create(cbCardData[i]);
|
|||
|
|
|
|||
|
|
CC_ASSERT(pCardSprite != nullptr);
|
|||
|
|
pCardSprite->setPosition(iStartPosX + i*iHandCardSpace, 0);
|
|||
|
|
this->addChild(pCardSprite);
|
|||
|
|
|
|||
|
|
_vecHandCardArray.push_back(pCardSprite);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint8 SSSOperateCardPanel::getHandCardData(uint8 cbCardData[SSS_MAX_COUNT], uint8 cbCardCount)
|
|||
|
|
{
|
|||
|
|
uint8 cbTempCardCount = 0;
|
|||
|
|
const size_t unCardCount = _vecHandCardArray.size();
|
|||
|
|
for (size_t i = 0; i < unCardCount; i++)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite *pCardSprite = _vecHandCardArray[i];
|
|||
|
|
cbCardData[cbTempCardCount++] = pCardSprite->getCardData();
|
|||
|
|
if (cbTempCardCount>=cbCardCount)
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return cbTempCardCount;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint16 SSSOperateCardPanel::getHandCardSpace(uint8 cbCardCount)
|
|||
|
|
{
|
|||
|
|
if (cbCardCount < 2)
|
|||
|
|
{
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint16 wHandCardSpace = (_csParentSize.width - SSS_BIG_CARD_WIDTH) / (cbCardCount - 1);
|
|||
|
|
wHandCardSpace = __min(wHandCardSpace, SSS_HAND_CARD_SPACE);
|
|||
|
|
return wHandCardSpace;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint16 SSSOperateCardPanel::getHandCardWidth(uint8 cbCardCount)
|
|||
|
|
{
|
|||
|
|
if (cbCardCount == 0)
|
|||
|
|
{
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (cbCardCount == 1)
|
|||
|
|
{
|
|||
|
|
return SSS_BIG_CARD_WIDTH;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint16 wHandCardWidth = (cbCardCount - 1)*getHandCardSpace(cbCardCount) + SSS_BIG_CARD_WIDTH;
|
|||
|
|
|
|||
|
|
return wHandCardWidth;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint8 SSSOperateCardPanel::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++)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite *pCardSprite = _vecHandCardArray[i];
|
|||
|
|
if ((i + 1) == unCardCount)
|
|||
|
|
{
|
|||
|
|
iCardSpace = SSS_BIG_CARD_WIDTH;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Rect rcCardSprite = Rect(pCardSprite->getPositionX(), pCardSprite->getPositionY(), iCardSpace, SSS_BIG_CARD_HEIGHT);
|
|||
|
|
if (rcCardSprite.containsPoint(ptTouch))
|
|||
|
|
{
|
|||
|
|
return i;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return 0xFF;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SSSOperateCardPanel::onSelectCardAction(int iEndSelectIndex)
|
|||
|
|
{
|
|||
|
|
const size_t unCardCount = _vecHandCardArray.size();
|
|||
|
|
for (size_t i = 0; i < unCardCount; i++)
|
|||
|
|
{
|
|||
|
|
SSSBigCardSprite *pCardSprite = _vecHandCardArray[i];
|
|||
|
|
pCardSprite->toNormal();
|
|||
|
|
|
|||
|
|
if (isInSelectCardIndex(i, iEndSelectIndex))
|
|||
|
|
{
|
|||
|
|
pCardSprite->toGray();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool SSSOperateCardPanel::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;
|
|||
|
|
}
|