#include "SparrowCard.h" #include "cocos2d.h" #include "ui/CocosGUI.h" #include "cocostudio/CocoStudio.h" #include "YSAudioEngine.h" SparrowCard::SparrowCard() { m_pRootImage = nullptr; // 根节点 m_ValueData = 0x0; // 麻将数值 m_CardType = SP_NULL; // 麻将类型 m_IsTop = false; // 弹起标识 m_bTouch = false; m_cloneCardImg = nullptr; } // 初始化 bool SparrowCard::init(BYTE _value, EN_SPARROW_TYPE _cardType) { // 初始化父类 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) //万字 { strPng = StringUtils::format("Games/Sparrow/card/wancard%d.png", cbCardValue); } else if (0x1 == cbCardColor) //索子 { strPng = StringUtils::format("Games/Sparrow/card/tiaocard%d.png", cbCardValue); } else if (0x2 == cbCardColor) //同子 { strPng = StringUtils::format("Games/Sparrow/card/bingcard%d.png", cbCardValue); } else if (0x3 == cbCardColor) //番子 { strPng = StringUtils::format("Games/Sparrow/card/fengcard%d.png", cbCardValue); } else if (0x4 == cbCardColor) //花牌 { strPng = StringUtils::format("Games/Sparrow/card/huacard%d.png", cbCardValue); } return strPng; } void SparrowCard::SetCardValuePng(BYTE _value, EN_SPARROW_TYPE _cardType) { // 明牌显示 bool isValid = IsValidCard(_value); if (isValid) { m_ValueData = _value; string strPng = GetCardValuePng(_value); auto carValue = (ImageView*)m_pRootImage->getChildByName("value"); // ASSERT(carValue != nullptr); // 录像模式明牌,这里注释掉不做断言; if (carValue) { carValue->loadTexture(strPng); carValue->setVisible(true); } } } void SparrowCard::SetTingCardStatus(bool isTing) { // 必须是自己手牌 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; } // 设置当前牌标识 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); //在2.0秒内,先跳起 屏幕尺寸的1/2 再落下0px,该动作重复1次 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; } // 获取新的麻将牌 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(event->getCurrentTarget()); CC_ASSERT(target == this); //auto location = touch->getLocation(); Node* parent = this->getParent(); if (parent) { // 相对父坐标; Vec2 locationInNode = parent->convertToNodeSpace(touch->getLocation()); Rect boundBox = target->getBoundingBox(); if (!boundBox.containsPoint(locationInNode)) // 是否牌范围内 { 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(event->getCurrentTarget()); CC_ASSERT(target == this); // 父节点; Node* parent = this->getParent(); if (parent) { // 相对父触摸位置 Vec2 touchPos = parent->convertToNodeSpace(touch->getLocation()); //偏移超过麻将长度 if (touchPos.y >= m_pRootImage->getContentSize().height + this->getPosition().y) { if (m_cloneCardImg == nullptr) { //创建一点节点; m_cloneCardImg = (ImageView*)m_pRootImage->clone(); m_cloneCardImg->setAnchorPoint(Vec2(0.5f, 0.5f)); //放入制定场景; parent->addChild(m_cloneCardImg, 9999); } // 设置位置 m_cloneCardImg->setPosition(touchPos); //顔色設置 m_pRootImage->setColor(Color3B(150, 150, 150)); } } } } void SparrowCard::TouchEnd(Touch* touch, Event* event) { CC_ASSERT(getTouchState()); //正常顔色 if (m_cloneCardImg != nullptr) { m_pRootImage->setColor(Color3B(255, 255, 255)); m_cloneCardImg->removeFromParent(); m_cloneCardImg = nullptr; } if (getTouchState()) { auto target = static_cast(event->getCurrentTarget()); CC_ASSERT(target == this); //回调出牌 if (_SendCardCallBack != nullptr) { _SendCardCallBack(this, touch->getLocation()); } this->setTouchState(false); } } void SparrowCard::addClickEventListener(const std::function callback) { //m_pRootImage->addClickEventListener(callback); if (m_CardType == SP_SELFSTAND) { _SendCardCallBack = callback; m_bTouch = true; m_startZorder = this->getLocalZOrder(); this->setTouchState(false); this->BindingTouchEvent(); } }