private NumberMoveObject deleteEmptyMoveObject(NumberMoveObject moveObj) { NumberMoveObject tempObj = moveObj; NumberMoveObject beginObj = null; while (tempObj != null) { if (tempObj.moveNumberObj != null && beginObj == null) { beginObj = tempObj; } if (tempObj.moveNumberObj == null) { NumberMoveObject parent = tempObj.parent; if (parent != null) { parent.next = tempObj.next; } if (tempObj.next != null) { tempObj.next.parent = parent; } } tempObj = tempObj.next; } return(beginObj); }
public void Moving() { speed += 10f * Time.deltaTime; Vector2 pos = moveNumberObj.GetCurrentPos(); pos += moveDir * speed; if (parent == null && Vector2.Dot(endPos - pos, moveDir) <= 0) { pos = endPos; moveFinished = true; } else if (parent != null && parent.moveFinished == true) { Vector2 tempEndPos = parent.moveNumberObj.GetCurrentPos() - distanceWithParent * moveDir; bool needMerge = parent.moveNumberObj.GetNumber() == moveNumberObj.GetNumber() && this.hasMerged == false && parent.hasMerged == false; if (needMerge) { tempEndPos = parent.moveNumberObj.GetCurrentPos(); } if (Vector2.Dot(tempEndPos - pos, moveDir) <= 0) { pos = tempEndPos; moveFinished = true; if (needMerge) { GameFramework.singleton.getGameRender().DestroyObject(this.moveNumberObj); parent.next = this.next; if (this.next != null) { this.next.parent = parent; } parent.hasMerged = true; parent.moveNumberObj.SetNumber(parent.moveNumberObj.GetNumber() * 2); } } } moveNumberObj.SetPosition(pos); if (next != null) { next.Moving(); } }
private void CaculateMoveObjects(InputProtocol moveType) { int count = 0; if (moveType == InputProtocol.MoveRight || moveType == InputProtocol.MoveLeft) { count = Rows; } if (moveType == InputProtocol.MoveUp || moveType == InputProtocol.MoveDown) { count = Cols; } int[] startIndexs = new int[count]; moveObjList.Clear(); for (int c = 0; c < count; ++c) { int i = 0; int j = 0; switch (moveType) { case InputProtocol.MoveUp: { i = Rows - 1; j = c; } break; case InputProtocol.MoveDown: { i = 0; j = c; } break; case InputProtocol.MoveLeft: { i = c; j = 0; } break; case InputProtocol.MoveRight: { i = c; j = Cols - 1; } break; } NumberMoveObject moveObj = getMoveObject(i, j, moveType, null); moveObj = deleteEmptyMoveObject(moveObj); moveObjList.Add(moveObj); } }
private NumberMoveObject getMoveObject(int i, int j, InputProtocol moveType, NumberMoveObject parent) { if (i < 0 || i >= Rows) { return(null); } if (j < 0 || j >= Cols) { return(null); } Vector2 endPos = new Vector2(); switch (moveType) { case InputProtocol.MoveUp: { endPos.x = x_left_bottom + j * (NumberW + NumberObjDistance); endPos.y = y_left_bottom + (Rows - 1) * (NumberH + NumberObjDistance); } break; case InputProtocol.MoveDown: { endPos.x = x_left_bottom + j * (NumberW + NumberObjDistance); endPos.y = y_left_bottom + 0 * (NumberH + NumberObjDistance); } break; case InputProtocol.MoveLeft: { endPos.x = x_left_bottom + 0 * (NumberW + NumberObjDistance); endPos.y = y_left_bottom + i * (NumberH + NumberObjDistance); } break; case InputProtocol.MoveRight: { endPos.x = x_left_bottom + (Cols - 1) * (NumberW + NumberObjDistance); endPos.y = y_left_bottom + i * (NumberH + NumberObjDistance); } break; } NumberMoveObject moveObj = new NumberMoveObject(numberObjectArray[i, j], moveType, endPos, NumberW + NumberObjDistance); switch (moveType) { case InputProtocol.MoveUp: { i--; } break; case InputProtocol.MoveDown: { i++; } break; case InputProtocol.MoveLeft: { j++; } break; case InputProtocol.MoveRight: { j--; } break; } moveObj.next = getMoveObject(i, j, moveType, moveObj); moveObj.parent = parent; return(moveObj); }