Files
wnmj/Classes/Games/SparrowBase/SparrowCard.cpp

334 lines
7.5 KiB
C++
Raw Normal View History

2026-02-13 14:34:15 +08:00
#include "SparrowCard.h"
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"
#include "YSAudioEngine.h"
SparrowCard::SparrowCard()
{
m_pRootImage = nullptr; // <20><><EFBFBD>ڵ<EFBFBD>
m_ValueData = 0x0; // <20><EFBFBD><E9BDAB>ֵ
m_CardType = SP_NULL; // <20><EFBFBD><E9BDAB><EFBFBD><EFBFBD>
m_IsTop = false; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ
m_bTouch = false;
m_cloneCardImg = nullptr;
}
// <20><>ʼ<EFBFBD><CABC>
bool SparrowCard::init(BYTE _value, EN_SPARROW_TYPE _cardType)
{
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (!Sprite::init()) return false;
m_pRootImage = SparrowCardBase::getInstance()->GetSparrowCard(_cardType);
if (nullptr == m_pRootImage) return false;
this->addChild(m_pRootImage);
this->setContentSize(m_pRootImage->getContentSize());
this->setAnchorPoint(Point(0, 0));
m_pRootImage->setAnchorPoint(Point(0, 0));
m_pRootImage->setPosition(Point(0, 0));
m_CardType = _cardType;
SetCardValuePng(_value, _cardType);
return true;
}
void SparrowCard::onExit()
{
Sprite::onExit();
this->removeAllChildren();
if (m_cloneCardImg != nullptr)
{
m_cloneCardImg->removeFromParent();
m_cloneCardImg = nullptr;
}
}
string SparrowCard::GetCardValuePng(BYTE _value)
{
BYTE cbCardValue = GetCardValue(_value);
BYTE cbCardColor = GetCardColor(_value);
string strPng = "";
if (0x0 == cbCardColor) //<2F><><EFBFBD><EFBFBD>
{
strPng = StringUtils::format("Games/Sparrow/card/wancard%d.png", cbCardValue);
}
else if (0x1 == cbCardColor) //<2F><><EFBFBD><EFBFBD>
{
strPng = StringUtils::format("Games/Sparrow/card/tiaocard%d.png", cbCardValue);
}
else if (0x2 == cbCardColor) //ͬ<><CDAC>
{
strPng = StringUtils::format("Games/Sparrow/card/bingcard%d.png", cbCardValue);
}
else if (0x3 == cbCardColor) //<2F><><EFBFBD><EFBFBD>
{
strPng = StringUtils::format("Games/Sparrow/card/fengcard%d.png", cbCardValue);
}
else if (0x4 == cbCardColor) //<2F><><EFBFBD><EFBFBD>
{
strPng = StringUtils::format("Games/Sparrow/card/huacard%d.png", cbCardValue);
}
return strPng;
}
void SparrowCard::SetCardValuePng(BYTE _value, EN_SPARROW_TYPE _cardType)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
bool isValid = IsValidCard(_value);
if (isValid)
{
m_ValueData = _value;
string strPng = GetCardValuePng(_value);
auto carValue = (ImageView*)m_pRootImage->getChildByName("value");
// ASSERT(carValue != nullptr); // ¼<><C2BC>ģʽ<C4A3><CABD><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
if (carValue)
{
carValue->loadTexture(strPng);
carValue->setVisible(true);
}
}
}
void SparrowCard::SetTingCardStatus(bool isTing)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD>
if (m_CardType == SP_SELFSTAND)
{
auto cardStatus = (ImageView*)m_pRootImage->getChildByName("status");
ASSERT(cardStatus != nullptr);
cardStatus->setVisible(true);
if (isTing)
{
m_pRootImage->setEnabled(false);
}
else
{
m_pRootImage->setEnabled(true);
}
}
}
void SparrowCard::SetKingStatus(bool isKing, std::string strKing)
{
if (m_CardType == SP_SELFSTAND || m_CardType == SP_O_OUT || m_CardType == SP_L_OUT || m_CardType == SP_S_OUT || m_CardType == SP_R_OUT)
{
auto king = (ImageView*)m_pRootImage->getChildByName("king");
ASSERT(king != nullptr);
if (isKing)
{
king->loadTexture(strKing);
}
king->setVisible(isKing);
}
}
void SparrowCard::SetTop(bool istop)
{
m_IsTop = istop;
}
// <20><><EFBFBD>õ<EFBFBD>ǰ<EFBFBD>Ʊ<EFBFBD>ʶ
void SparrowCard::SetCurrentOutCard(bool isCurrent)
{
if (isCurrent)
{
Sprite* pCurrIcon = (Sprite*)m_pRootImage->getChildByName("outCardIcon");
if (pCurrIcon == nullptr)
{
Sprite* sp = Sprite::create("Games/Sparrow/card/outCardIcon.png");
ASSERT(sp != nullptr);
sp->setScale(0.5);
sp->setAnchorPoint(Point(0.5, 0));
Point sppos;
sppos.x = m_pRootImage->getContentSize().width / 2;
sppos.y = m_pRootImage->getContentSize().height / 3 * 2;
sp->setPosition(sppos);
sp->setName("outCardIcon");
m_pRootImage->addChild(sp);
//<2F><>2.0<EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>Ļ<EFBFBD>ߴ<EFBFBD><DFB4><EFBFBD>1/2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0px<70><78><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD>ظ<EFBFBD>1<EFBFBD><31>
JumpTo* jump = JumpTo::create(1.0f, sppos, 12, 1);
auto seq = RepeatForever::create(jump);
sp->runAction(seq);
}
}
else
{
Sprite* pCurrIcon = (Sprite*)m_pRootImage->getChildByName("outCardIcon");
if (pCurrIcon)
{
pCurrIcon->stopAllActions();
pCurrIcon->removeFromParentAndCleanup(true);
pCurrIcon = nullptr;
}
}
}
bool SparrowCard::ShowCardValue(BYTE _value)
{
if (m_pRootImage)
{
m_pRootImage->removeFromParent();
m_pRootImage = nullptr;
}
// <20><>ȡ<EFBFBD>µ<EFBFBD><C2B5><EFBFBD><E9BDAB>
if (SP_SELFSTAND == m_CardType)
{
m_pRootImage = SparrowCardBase::getInstance()->GetSparrowCard(SP_SELF_SHOW);
}
else
{
m_CardType = (EN_SPARROW_TYPE)(m_CardType + SP_O_OUT);
m_pRootImage = SparrowCardBase::getInstance()->GetSparrowCard(m_CardType);
}
if (nullptr == m_pRootImage) return false;
this->addChild(m_pRootImage);
this->setContentSize(m_pRootImage->getContentSize());
this->setAnchorPoint(Point(0, 0));
m_pRootImage->setAnchorPoint(Point(0, 0));
m_pRootImage->setPosition(Point(0, 0));
SetCardValuePng(_value, m_CardType);
return true;
}
void SparrowCard::BindingTouchEvent()
{
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(SparrowCard::TouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(SparrowCard::TouchMove, this);
listener->onTouchEnded = CC_CALLBACK_2(SparrowCard::TouchEnd, this);
EventDispatcher * eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
bool SparrowCard::TouchBegan(Touch* touch, Event* event)
{
auto target = static_cast<SparrowCard*>(event->getCurrentTarget());
CC_ASSERT(target == this);
//auto location = touch->getLocation();
Node* parent = this->getParent();
if (parent)
{
// <20><><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>;
Vec2 locationInNode = parent->convertToNodeSpace(touch->getLocation());
Rect boundBox = target->getBoundingBox();
if (!boundBox.containsPoint(locationInNode)) // <20>Ƿ<EFBFBD><C7B7>Ʒ<EFBFBD>Χ<EFBFBD><CEA7>
{
return false;
}
this->setTouchState(true);
return true;
}
return false;
}
void SparrowCard::TouchMove(Touch* touch, Event* event)
{
CC_ASSERT(getTouchState());
if (getTouchState())
{
auto target = static_cast<SparrowCard*>(event->getCurrentTarget());
CC_ASSERT(target == this);
// <20><><EFBFBD>ڵ<EFBFBD>;
Node* parent = this->getParent();
if (parent)
{
// <20><><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
Vec2 touchPos = parent->convertToNodeSpace(touch->getLocation());
//ƫ<>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><E9BDAB><EFBFBD><EFBFBD>
if (touchPos.y >= m_pRootImage->getContentSize().height + this->getPosition().y)
{
if (m_cloneCardImg == nullptr)
{
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD>;
m_cloneCardImg = (ImageView*)m_pRootImage->clone();
m_cloneCardImg->setAnchorPoint(Vec2(0.5f, 0.5f));
//<2F><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>;
parent->addChild(m_cloneCardImg, 9999);
}
// <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
m_cloneCardImg->setPosition(touchPos);
//<2F><>ɫ<EFBFBD>O<EFBFBD><4F>
m_pRootImage->setColor(Color3B(150, 150, 150));
}
}
}
}
void SparrowCard::TouchEnd(Touch* touch, Event* event)
{
CC_ASSERT(getTouchState());
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
if (m_cloneCardImg != nullptr)
{
m_pRootImage->setColor(Color3B(255, 255, 255));
m_cloneCardImg->removeFromParent();
m_cloneCardImg = nullptr;
}
if (getTouchState())
{
auto target = static_cast<SparrowCard*>(event->getCurrentTarget());
CC_ASSERT(target == this);
//<2F>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
if (_SendCardCallBack != nullptr)
{
_SendCardCallBack(this, touch->getLocation());
}
this->setTouchState(false);
}
}
void SparrowCard::addClickEventListener(const std::function<bool(cocos2d::Ref *, Vec2 pos)> callback)
{
//m_pRootImage->addClickEventListener(callback);
if (m_CardType == SP_SELFSTAND)
{
_SendCardCallBack = callback;
m_bTouch = true;
m_startZorder = this->getLocalZOrder();
this->setTouchState(false);
this->BindingTouchEvent();
}
}