美文网首页
Cocos Creator开发游戏消灭星星——星星整合

Cocos Creator开发游戏消灭星星——星星整合

作者: 吃菜小怪兽 | 来源:发表于2018-12-29 11:26 被阅读0次

    Cocos Creator开发游戏消灭星星——星星生成

    Cocos Creator开发游戏消灭星星——星星消除

    当星星全部消除后,需要对地图进行整合。

    //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);
    
                // 清除星星消除数据
                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();
        }
    },
    

    如果剩余星星数量小于10个,会有额外的加分。

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

    最后判断游戏结束还是进入下一关?

    toBeOrNotToBe () {
        cc.log("***** 检测 ***** 下一关 or 结束游戏 *****", GameData.targetScore, GameData.currScore);
        this.actCtr.remainNode.active = false;
        if (GameData.currScore < GameData.targetScore) {
            cc.log("结束游戏");
            this.actCtr.endAction();
        }
        else {
            cc.log("下一关");
            this.nextLevel();
        }
        this.deleteRemain = false;
    },
    

    相关文章

      网友评论

          本文标题:Cocos Creator开发游戏消灭星星——星星整合

          本文链接:https://www.haomeiwen.com/subject/dyeulqtx.html