功能实现

  1. 完善卡牌绘制
  2. 回合结束
  3. 弃牌

完善卡牌绘制

之前的卡牌绘制位置基本上是固定的,我想每出一张牌位置就更新一下,所以修改下卡牌绘制的函数

void Game::drawCard() {
    int pos = 0;                    //记录绘制位置
    for (int i = 0; i < humanPlayer.handMaxNum; i++) {
        if (humanPlayer.handCards[i]->cardState != humanPlayer.handCards[i]->null) {
            //cout << "is " << i << endl;
            if (humanPlayer.handCards[i]->btnState == humanPlayer.handCards[i]->CLICK) {                //如果为按下的状态
                humanPlayer.handCards[i]->move(Mouse::getPosition(window));                                //卡牌移动事件
            } else {
                humanPlayer.handCards[i]->setPosition((float)(300 + pos * 150), (float)500);                                //默认位置
                humanPlayer.handCards[i]->originPosition = humanPlayer.handCards[i]->getPosition();        //设置默认位置
                humanPlayer.handCards[i]->originMouse = Mouse::getPosition(window);                        //设定按下鼠标前鼠标的位置
            }
            window.draw(*humanPlayer.handCards[i]);
            if (pos < humanPlayer.handCardNums) {
                pos++;
            }
        }
    }
    window.display();                        //展示屏幕
}

回合结束

首先重写一下之前的抽牌函数,今天又出现了许多bug。。

void Player::getCard(Card card[], int getNum, int cardLength) {
    if (handCardNums == handMaxNum) {
        return;
    } else if (getNum + handCardNums > handMaxNum) {
        getNum = handMaxNum - handCardNums;
    }
    srand((unsigned)time(NULL));
    if (handCardNums <= handMaxNum) {
        for (; getNum != 0;) {
            int num = rand() % cardLength;
            for (int i = 0; i < handMaxNum; i++) {
                if (handCards[i] == &nullCard) {
                    if (card[num].cardState == card[num].cardPool) {                                    //如果卡牌在卡池里就抽牌
                        card[num].cardState = card[num].handPool;                                        //卡牌移到手牌区
                        handCardNums += 1;                                                //手牌数加一
                        handCards[i] = &card[num];                            //手牌数组赋值
                        getNum--;
                    }
                }
            }
        }
    }
}

Game.cpp中添加回合结束按钮的事件

void Game::fightInput(Vector2i mousePosition, Event event) {
    switch (turnEnd.checkMouse(mousePosition, event, 0, 0)) {
        case 1:
            hoverSd.play();
            break;
        case 3:
            releaseSd.play();
            humanPlayer.getCard(cards, humanPlayer.getCardNum, sizeof(cards) / sizeof(cards[0]));
            break;
        default:
            break;
    }
}

效果如下

弃牌

原版的弃牌是回合结束时手牌数大于手牌上限才要求弃牌,为了简化,我直接设置一个主动弃牌的功能,正好放在之前被我阉割的技能的位置

所以要准备两张图片,普通的删除和长按图片高亮的删除

在Player.cpp中添加弃牌动作

void Player::disCard(Card* card) {                        //弃牌
    card->cardState = card->disCardPool;            //放入弃牌堆
    handCardNums--;                                //手牌数减一
}

然后在之前出牌的地方改一下

if (enemyCarpente.checkMouse(mousePosition)) {
                        attackSd.play();                                                    //播放攻击音乐
                        humanPlayer.useCardTo(humanPlayer.handCards[i], &enemyCarpente);    //对敌人使用卡牌
                        humanPlayer.handCards[i] = &humanPlayer.nullCard;                    //设定使用后的手牌为null
                    } else {
                        if (humanPlayer.handCards[i]->mouseContainf(disCardBtn.getPosition(), 30, -30)) {
                            humanPlayer.disCard(humanPlayer.handCards[i]);
                            humanPlayer.handCards[i] = &humanPlayer.nullCard;                    //设定使用后的手牌为null
                        } else
                            humanPlayer.handCards[i]->setState(humanPlayer.handCards[i]->NORMAL);        //释放后设定为普通状态
                    }

这样讲鼠标拖拽到删除的按钮上就可以实现弃牌了

但是当按钮比较小时根据鼠标判断就很难受,所以我们根据按钮的坐标是不是进入卡牌坐标的范围来判断

if (humanPlayer.handCards[i]->mouseContain(disCardBtn.getPosition(), 30, -30)) {
                        humanPlayer.disCard(humanPlayer.handCards[i]);
                        humanPlayer.handCards[i] = &humanPlayer.nullCard;                    //设定使用后的手牌为null
                    }

看下效果

Last modification:February 12th, 2021 at 06:15 am