Cocos Creator開發(fā)游戲消滅星星——星星整合

Cocos Creator開發(fā)游戲消滅星星——星星生成

Cocos Creator開發(fā)游戲消滅星星——星星消除

當(dāng)星星全部消除后,需要對地圖進(jìn)行整合。

//matrixCtr.js
bomb (list, count) {
    if (list.length > 0) {
        //TODO: 消除星星
    }
    else {
        //星星消除完的邏輯處理
        if (this._totalCounts == this._currCount) {
            this._tamping = true
            var checkCols = Utils.needCheckCols(this._bombList);
            this.tampRows(checkCols);
            this.scheduleOnce(function () {
                this.tampCols(checkCols);
                this._tamping = false;

                GameData.tampStarData(checkCols);
                //Utils.dumpArray(GameData.starMatrix);

                if (Utils.gameOver(GameData.starMatrix)) {
                    this.deleteRemainSprites();
                }
            }, 0.2);

            // 清除星星消除數(shù)據(jù)
            this._totalCounts = 0;
            this._currCount = 0;
            this._bombList.splice(0, this._bombList.length);
        }
    }
}

整合分兩種:一種是沒有消除的星星掉落下來,即垂直方向;另一種是水平方向。
needCheckCols通過消除的星星檢測地圖中哪些列需要整合。

// Utils.js
function needCheckCols (list) {
    var checkCols = [];
    list.forEach(function (elem, index, arr) {
        if (checkCols.indexOf(elem.y) == -1) {
            checkCols.push(elem.y);
        }
    })
    //從大到小排序
    checkCols.sort(function(a, b) {
        return b - a;
    });
    return checkCols;
};

然后,按順序整合,先垂直方向,最后水平方向

// matrixCtr.js
tampRows (checkCols) {
    // 垂直方向
    checkCols.forEach(function (col, index, arr) {
        var newRow = 0;
        for(var row = 0; row < Config.matrixRow; row++) {
            if (GameData.starMatrix[row][col] >= 0) {
                if (newRow != row) {
                    var index = Utils.indexValue(row, col);
                    var newIndex = Utils.indexValue(newRow, col);

                    var starNode = GameData.starSprite[index];
                    starNode.getComponent("starCtr").updateGrid(newRow, col);

                    GameData.starSprite[newIndex] = starNode;
                    GameData.starSprite[index] = null;

                    var start_y = starNode.y
                    var target_pos = Utils.grid2Pos(newRow, col);

                    starNode.runAction(cc.sequence(cc.moveTo(0.1, target_pos.x, start_y+40),cc.moveTo(0.13, target_pos)));
                }
                newRow++;
            }
        }
    }, this);
},

tampCols (checkCols) {
    checkCols.forEach(function (col, index, arr) {
        //水平方向
        if (GameData.starMatrix[0][col] == -1) {
            var adjust = true;
            for (var row = 1; row < Config.matrixRow; row++) {
                if (GameData.starMatrix[row][col] >= 0) {
                    adjust = false;
                    break;
                }
            }

            if (adjust && col < Config.matrixCol-1) {
                for (var i = col+1; i < Config.matrixCol; i++) {
                    this.setStarNodeByCol(i-1, this.starNodeByCol(i));
                }
                this.clearTheLeftStarNode();
            }
        }
    }, this);
},

地圖整合后,需要判斷是否還有星星能消除

// Utils.js
function gameOver (matrix) {
    for(var row = 0; row < Config.matrixRow; row++) {
        for(var col = 0; col < Config.matrixCol; col++) {
            var tag = matrix[row][col];
            if (tag >= 0) {
                var any = cc.v2(row, col);
                if (any.y - 1 >= 0 && tag == matrix[any.x][any.y-1]) {
                    return false;
                }
                if (any.y + 1 < Config.matrixCol && tag == matrix[any.x][any.y+1]) {
                    return false;
                }
                if (any.x - 1 >= 0 && tag == matrix[any.x-1][any.y]) {
                    return false;
                }
                if (any.x + 1 < Config.matrixRow && tag == matrix[any.x+1][any.y]) {
                    return false;
                }
            }
        }
    }
    return true;
};

如果不能消除,需要刪除剩余的星星

deleteRemainSprites () {
    var remainBombList = GameData.remainStarData();
    this.actCtr.remainNode.active = true;
    this.actCtr.updateRemainTips(Config.extraScore, remainBombList.length);
    this.bombRemain(remainBombList, 1);
    this.deleteRemain = true;
},

bombRemain (list, i) {
    if (list.length > 0) {
        var gridPos = list.shift();
        var index = Utils.indexValue(gridPos.x, gridPos.y);
        this.bombStar(GameData.starSprite[index]);
        GameData.starSprite[index] = null;

        var s = 0;
        if (i < 10) {
            s = Utils.getExtraScore(i);
            this.soundCtr.playEffect("remove_remainstar");
        }
        this.actCtr.updateRemainExtraScroe(s);

        this.scheduleOnce(function () {
            if (i <= 10) {
                I++;
                this.bombRemain(list, i);
            }
            else {
                list.forEach(function (elem, index, arr) {
                this.bombStar(GameData.starSprite[Utils.indexValue(elem.x, elem.y)]);
                }, this);

                this.scheduleOnce(function() {
                    this.toBeOrNotToBe();
                }, 0.3);
            }
        }, 0.3);
    }
    else {
        this.uiCtr.updateExtraScore(i); // 額外加分邏輯
        this.checkIsSuccessed();
        this.toBeOrNotToBe();
    }
},

如果剩余星星數(shù)量小于10個,會有額外的加分。

// uiCtr.js
updateExtraScore (count) {
    if (count < 10) {
        var score = Utils.getExtraScore(count);
        GameData.currScore += score;
        this.updateCurrentScore();
        this.refreshBestScore();
    }
},

最后判斷游戲結(jié)束還是進(jìn)入下一關(guān)?

toBeOrNotToBe () {
    cc.log("***** 檢測 ***** 下一關(guān) or 結(jié)束游戲 *****", GameData.targetScore, GameData.currScore);
    this.actCtr.remainNode.active = false;
    if (GameData.currScore < GameData.targetScore) {
        cc.log("結(jié)束游戲");
        this.actCtr.endAction();
    }
    else {
        cc.log("下一關(guān)");
        this.nextLevel();
    }
    this.deleteRemain = false;
},
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。