// Use this for initialization void Start() { //读取配置和解析数据 InitData(); //初始化游戏背景 InitGameBg(); //加载资源 InitResources(); //初始化关卡数据,如通过目标,数量,步数 InitPlayLevelMsg(); //初始化方块 InitGrid(); //初始化关卡完成界面 InitFinishPlayLevel(); openEditorButton.transform.SetAsLastSibling();//设置编辑器开关的渲染顺序为最后 //传递数据 GridConn.InitGridUIAttribute(); GridDrop.InitGridUIAttribute(); GridReset.InitGridUIAttribute(); }
// Use this for initialization void Start() { //读取配置和解析数据 initData(); //初始化游戏背景 initGameBg(); //加载资源 initResources(); //初始化关卡数据,如通过目标,数量,步数 initPlayLevelMsg(); //初始化方块 initGrid(); //初始化关卡完成界面 initFinishPlayLevel(); //传递数据 GridConn.initGridUIAttribute(); GridDrop.initGridUIAttribute(); GridReset.initGridUIAttribute(); }
/// <summary> /// 执行方块掉落动画 /// </summary> public static void Drop() { //[0]定义每帧掉落距离和横移距离 dropY = moveX = mInterval * 0.1f; //[1]遍历所有列,完成方块掉落 for (int h = 18; h > 0; h--) { for (int i = 0; i < mGameData.vertical; i++) { if (mGridListManager[i].Count >= h) { checkIndex = h - 1; //[3]判断剩余List的最后一个对象是否需要移动,每帧执行移动并修改后续需要移动的距离 if (mGridListManager[i][checkIndex].dropHeight > 0 || mGridListManager[i][checkIndex].hasMoveWidth > 0 || mGridListManager[i][checkIndex].hasDropHeight > 0 || mGridListManager[i][checkIndex].dropAfterMoveHeight > 0) { //[3.1]先进行垂直掉落 if (mGridListManager[i][checkIndex].dropHeight > dropY) { //执行掉落 mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(0.0f, -dropY, 0.0f); mGridListManager[i][checkIndex].dropHeight -= dropY; //若为补充方块掉落,则掉落距离超过第一个格子一半,则开始显示 if (mGridListManager[i][checkIndex].isTop && mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position.y <= gridBorderY) { mGridListManager[i][checkIndex].gridObject.SetActive(true); mGridListManager[i][checkIndex].isTop = false; } //若为传送门入口方块掉落,则掉落距离超过传送门下方格子,则开始隐藏 if (mDoorDataList != null) { foreach (DoorBean doorBean in mDoorDataList) { if (doorBean.inVertical == i) { gridHideInDoor = mGridBaseListManager[i][doorBean.inHorizontal].gridBase.GetComponent <RectTransform>().position.y; if (mGridListManager[i][checkIndex].isDropInDoor && mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position.y <= gridHideInDoor) { mGridListManager[i][checkIndex].gridObject.SetActive(false); mGridListManager[i][checkIndex].isDropInDoor = false; break; } } } //若为传送门出口方块掉落,则掉落距离超过传送门上方格子,则开始显示 foreach (DoorBean doorBean in mDoorDataList) { if (doorBean.outVertical == i) { gridShowOutDoor = (mGridBaseListManager[i][doorBean.outHorizontal].gridBase.GetComponent <RectTransform>().position + new Vector3(0.0f, mGridSize / 2, 0.0f)).y; if (mGridListManager[i][checkIndex].isDropOutDoor && mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position.y <= gridShowOutDoor) { mGridListManager[i][checkIndex].gridObject.SetActive(true); mGridListManager[i][checkIndex].isDropOutDoor = false; break; } } } } //若为金豆荚篮子位置,则金豆荚方块中心掉落穿过篮子,则开始隐藏,并删除对象 if (mGridOfBeanPodList != null && mBasketDataList != null) { endCheckBeanPod = false; foreach (GridBean gridBean in mGridOfBeanPodList) { foreach (BasketBean basketBean in mBasketDataList) { if (gridBean.vertical == basketBean.basketVertical) { if (gridBean.gridObject.GetComponent <RectTransform>().position.y < basketBean.basket.GetComponent <RectTransform>().position.y + mGridSize * 0.4f / 2) { Destroy(gridBean.gridObject); mGridListManager[i].Remove(gridBean); mGridOfBeanPodList.Remove(gridBean); mDeleteCounts++; //更新目标板数据 GridConn.UpdateTargetCounts(mDeleteCounts); } endCheckBeanPod = true; break; } } if (endCheckBeanPod) { break; } } } continue; } else { mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(0.0f, -mGridListManager[i][checkIndex].dropHeight, 0.0f); mGridListManager[i][checkIndex].dropHeight = 0; mGridListManager[i][checkIndex].dropCounts = 0; //若为传送门入口列下移到最底部,则移除方块 isDelete = false; if (mDoorDataList != null) { foreach (DoorBean doorBean in mDoorDataList) { if (doorBean.inVertical == i) { if (mGridListManager[doorBean.inVertical][checkIndex].isDestroy) { Destroy(mGridListManager[doorBean.inVertical][checkIndex].gridObject); mGridListManager[doorBean.inVertical].RemoveAt(checkIndex); isDelete = true; break; } } } } if (isDelete) { continue; } } //[3.2]垂直掉落完后,进行横移 if (mGridListManager[i][checkIndex].moveCounts >= mGridListManager[i][checkIndex].dropHeightFromOtherCounts) { if (mGridListManager[i][checkIndex].moveCounts > 0) { if (mGridListManager[i][checkIndex].hasMoveWidth > moveX) { //往左边移动 if (mGridListManager[i][checkIndex].moveDirection == -1) { mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(-moveX, 0.0f, 0.0f); } //往右边移动 if (mGridListManager[i][checkIndex].moveDirection == 1) { mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(moveX, 0.0f, 0.0f); } mGridListManager[i][checkIndex].hasMoveWidth -= moveX; continue; } else { //往左边移动 if (mGridListManager[i][checkIndex].moveDirection == -1) { mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(-mGridListManager[i][checkIndex].hasMoveWidth, 0.0f, 0.0f); } //往右边移动 if (mGridListManager[i][checkIndex].moveDirection == 1) { mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(mGridListManager[i][checkIndex].hasMoveWidth, 0.0f, 0.0f); } if (mGridListManager[i][checkIndex].moveCounts > 0) { mGridListManager[i][checkIndex].moveCounts--; if (mGridListManager[i][checkIndex].moveCounts == 0) { mGridListManager[i][checkIndex].hasMoveWidth = 0; mGridListManager[i][checkIndex].moveHorizontal = 9; } else { mGridListManager[i][checkIndex].hasMoveWidth = mInterval; } } } } } //[3.3]横移后的掉落 if (mGridListManager[i][checkIndex].dropHeightFromOtherCounts > 0) { if (mGridListManager[i][checkIndex].hasDropHeight > dropY) { //执行掉落 mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(0.0f, -dropY, 0.0f); mGridListManager[i][checkIndex].hasDropHeight -= dropY; continue; } else { mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(0.0f, -mGridListManager[i][checkIndex].hasDropHeight, 0.0f); if (mGridListManager[i][checkIndex].dropHeightFromOtherCounts > 0) { mGridListManager[i][checkIndex].dropHeightFromOtherCounts--; if (mGridListManager[i][checkIndex].dropHeightFromOtherCounts == 0) { mGridListManager[i][checkIndex].hasDropHeight = 0; } else { mGridListManager[i][checkIndex].hasDropHeight = mInterval; } } } } //[3.4]横移掉落完毕后,若下方有空位执行的掉落 if (mGridListManager[i][checkIndex].dropAfterMoveHeight > dropY) { //执行掉落 mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(0.0f, -dropY, 0.0f); mGridListManager[i][checkIndex].dropAfterMoveHeight -= dropY; } else { mGridListManager[i][checkIndex].gridObject.GetComponent <RectTransform>().position += new Vector3(0.0f, -mGridListManager[i][checkIndex].dropAfterMoveHeight, 0.0f); mGridListManager[i][checkIndex].dropAfterMoveHeight = 0; } } } } } //判断是否为僵局 isCheckReset = true; for (int h = 18; h > 0; h--) { for (int i = 0; i < mGameData.vertical; i++) { if (mGridListManager[i].Count >= h) { checkIndex = h - 1; //如果还有任何需要移动的格子,则不检测僵局 if (mGridListManager[i][checkIndex].dropHeight > 0 || mGridListManager[i][checkIndex].hasMoveWidth > 0 || mGridListManager[i][checkIndex].hasDropHeight > 0 || mGridListManager[i][checkIndex].dropAfterMoveHeight > 0) { isCheckReset = false; break; } if (isCheckReset && h == 1 && i == 8) { //判断当前方块是否为僵局,方法调用位置仍需思考 GridReset.CheckGameisConn(); } } } if (!isCheckReset) { break; } } }
/// <summary> /// 记录方块掉落的信息 /// </summary> public static void RecordGridDropMsg() { recordMsgCounts = 0; while (recordMsgCounts < 8) { //[1]遍历所有列,若数量不等于9,则相应列进行掉落 for (int i = 0; i < mGameData.vertical; i++) { if (mGridListManager[i].Count < 9) { //[2]遍历数组,从后往前检测是否需要移动补充空位 , 检测位置 minEmptyIndex = -1; for (int x = mGridListManager[i].Count - 1, checkIndex = mGameData.horizontal - 1, mGridBaseSpriteIndex; x >= 0; x--, checkIndex--) { //若List的x处是否为挖空or雪块,则也不需掉落 mGridBaseSpriteIndex = mGridBaseListManager[i][mGridListManager[i][x].horizontal].spriteIndex; if (mGridBaseSpriteIndex == -1 || (mGridBaseSpriteIndex >= 15 && mGridBaseSpriteIndex <= 21)) { if (minEmptyIndex == -1) { minEmptyIndex = mGridListManager[i][x].horizontal; } else { if (mGridListManager[i][x].horizontal < minEmptyIndex) { minEmptyIndex = mGridListManager[i][x].horizontal; } } //若中途遇到格子为空,若有传送门出口,则也可进行补充 endRecord = false; if (!mGridBaseListManager[i][checkIndex].isHasGrid && mDoorDataList != null) { foreach (DoorBean doorBean in mDoorDataList) { if (doorBean.outVertical == i && doorBean.outHorizontal <= checkIndex) { DropFromDoor(doorBean, checkIndex - x); endRecord = true; break; } } } if (endRecord) { break; } //若mGridListManager已检测完,则最后判断是从顶部备用行补充,还是传送门补充 if (x == 0) { //获取空格子上方所需补充的个数 if (minEmptyIndex > 0) { addCounts = minEmptyIndex; for (int s = minEmptyIndex; s > 0; s--) { if (mGridBaseListManager[i][s - 1].isHasGrid) { addCounts--; } } } else { addCounts = checkIndex; } //检测该列上方是否有传送门出口,若有,则不从备用行补充方块 hasDoor = false; if (mDoorDataList != null) { foreach (DoorBean doorBean in mDoorDataList) { //需补充方块上方有传送门 if (doorBean.outVertical == i) { DropFromDoor(doorBean, addCounts); hasDoor = true; break; } } } if (!hasDoor) { //若无传送门,则从默认备用行掉落补充 if (addCounts > 0) { DropFromTopList(i, addCounts); } } } continue; } //[3]判断剩余List的最后一个对象是否处于最大的位置,若不是,则记录需要掉落的相关信息 if (mGridListManager[i][x].horizontal == checkIndex) { if (x != 0) { continue; } } else { ////判断可否掉落 dropGridCounts = 0; for (int y = mGridListManager[i][x].horizontal + 1, lowerGridSpriteIndex; y <= checkIndex; y++) { //若下方为挖空格子,则直接不需掉落 lowerGridSpriteIndex = mGridBaseListManager[i][mGridListManager[i][x].horizontal + 1].spriteIndex; if (lowerGridSpriteIndex == -1 || (lowerGridSpriteIndex >= 15 && lowerGridSpriteIndex <= 21)) { break; } //若下方没有方块,则掉落距离+1 if (!mGridBaseListManager[i][y].isHasGrid) { dropGridCounts++; } //若跨了一个方块以上才碰到挖空,则直接跳出,返回dropGridCounts if (y > mGridListManager[i][x].horizontal + 1 && (mGridBaseListManager[i][y].spriteIndex == -1 || (mGridBaseListManager[i][y].spriteIndex >= 15 && mGridBaseListManager[i][y].spriteIndex <= 21))) { break; } } if (dropGridCounts > 0) { //下移的距离 dropHeight = dropGridCounts * mInterval; //TODO 判断是否为当列需要掉落的距离 if (mGridListManager[i][x].horizontal > mGridListManager[i][x].moveHorizontal) { mGridListManager[i][x].dropAfterMoveHeight = dropHeight; } else { //记录方块需要掉落的高度 mGridListManager[i][x].dropHeight = dropHeight; } mGridListManager[i][x].dropCounts = dropGridCounts; //修改GridBean下移后的listHorizontal信息 mGridBaseListManager[i][mGridListManager[i][x].horizontal].gridBean = null; mGridBaseListManager[i][mGridListManager[i][x].horizontal].isHasGrid = false; mGridListManager[i][x].horizontal += mGridListManager[i][x].dropCounts; mGridListManager[i][x].gridObject.name = "grid" + i.ToString() + mGridListManager[i][x].horizontal.ToString(); mGridBaseListManager[i][mGridListManager[i][x].horizontal].isHasGrid = true; mGridBaseListManager[i][mGridListManager[i][x].horizontal].gridBean = mGridListManager[i][x]; mGridBaseListManager[i][mGridListManager[i][x].horizontal].spriteIndex = mGridListManager[i][x].spriteIndex; } } //[4]记录所需补充方块掉落的相关信息,和生成补充方块 if (x == 0) { //[4.1]获取空格子上方所需补充的个数 if (minEmptyIndex > 0) { addCounts = minEmptyIndex; for (int s = minEmptyIndex; s > 0; s--) { if (mGridBaseListManager[i][s - 1].isHasGrid) { addCounts--; } } } else { addCounts = checkIndex; } //[4.2]检测该列上方是否有传送门出口,若有,则不从备用行补充方块 hasDoor = false; if (mDoorDataList != null) { foreach (DoorBean doorBean in mDoorDataList) { //[4.2.1]需补充方块上方有传送门 if (doorBean.outVertical == i && doorBean.outHorizontal <= checkIndex) { DropFromDoor(doorBean, addCounts); hasDoor = true; break; } } } if (!hasDoor) { //[4.2.2]若无传送门,则从默认备用行掉落补充 if (addCounts > 0) { DropFromTopList(i, addCounts); } } } } } } if (recordMsgCounts < 8) { //[5]若当列有挖空阻挡掉落,则从两方进行掉落补充,若隔壁两列上方没有方块,则继续查找再左两列or右两列的方块 for (int h = 8; h >= 1; h--) { for (int v = 0; v < 9; v++) { if (!mGridBaseListManager[v][h].isHasGrid && mGridBaseListManager[v][h - 1].spriteIndex != -1 && !(mGridBaseListManager[v][h - 1].spriteIndex >= 15 && mGridBaseListManager[v][h - 1].spriteIndex <= 21)) { DropFromOtherVertical(v, h); } } } } //[6]检测金豆荚位置,如果金豆荚位置处于篮子上方,则金豆荚位置上方所有方块均需要掉落一行 if (mGridOfBeanPodList != null && mBasketDataList != null) { foreach (GridBean gridBean in mGridOfBeanPodList) { foreach (BasketBean basketBean in mBasketDataList) { if (gridBean.vertical == basketBean.basketVertical && gridBean.horizontal == basketBean.basketHorizontal) { DropOneHeight(basketBean.basketVertical, basketBean.basketHorizontal); } } } //判断通过目标是否为金豆荚,如果是,则检测当前格子内容是否存在金豆荚,如果不存在,则产生一个。 isCreateOneBeanPod = true; for (int v = 0; v < 9; v++) { for (int h = 0; h < 9; h++) { if (mGridListManager[v][h].spriteIndex == 13) { isCreateOneBeanPod = false; break; } } if (isCreateOneBeanPod && v == 8) { GridConn.CreateBeanPod(); } } } recordMsgCounts++; } }