puzzel 遊戲的之凹凸拼圖
主要思路參考
http://www.5uflash.com/flashjiaocheng/Flashyingyongkaifa/1250.html
主要算法
//核心算法生成 邊的point 記錄點的順序為從上到下,從左到右
private final function generateSpinSide(rowIndex:int,colIndex:int,sideType:String):PuzzelSplinterSide {
var bitmapW:Number = getFitBitmapDataWidth();
var bitmapH:Number = getFitBitmapDataHeight();
var itemW:Number = getSlinterItemWidth();
var itemH:Number = getSlinterItemHeight();
var stratPoint:Point;
//如果有 凹凸的話除起始之外一定還有 3個點 分别是 端點 控制點 端點
var curvePoint0:Point;
var curveControlPoint:Point;
var curvePoint1:Point;
var radomSpinterGapLength:Number;
var controlCurveLength:Number;
var endPoint:Point;
//是内邊還是外邊
var isRadomInSide:Boolean = Boolean(uint(Math.random()*10)%2);
var result:PuzzelSplinterSide;
//先檢查是否為邊界,若果不是的話它的周邊是肯定有的
if(sideType == TOP_SIDE) {
var isTopBoundary:Boolean = rowIndex == 0;
if(isTopBoundary) {
stratPoint = new Point(colIndex*itemW,rowIndex*itemH);
endPoint = new Point(stratPoint.x + itemW,stratPoint.y);
result = new PuzzelSplinterSide(stratPoint,endPoint);
}
else {//取colIndex row -1 的下邊,隻可以在上左取,不可以往下右取
result = getSplinterSideCache(rowIndex-1,colIndex,BOTTOM_SIDE);;
}
}
else if(sideType == LEFT_SIDE) {
var isLeftBoudary:Boolean = colIndex == 0;
if(isLeftBoudary) {
stratPoint = new Point(colIndex*itemW,rowIndex*itemH);
endPoint = new Point(stratPoint.x,stratPoint.y + itemH);
result = new PuzzelSplinterSide(stratPoint,endPoint);
}
else {//取colIndex -1 row 的右邊
result = getSplinterSideCache(rowIndex,colIndex-1,RIGHT_SIDE);
}
}
else if(sideType == RIGHT_SIDE) {//下右都是未生成的,要新計算
var isRightBoundary:Boolean = colIndex == PUZZEL_GAME_COL_COUNT - 1;
stratPoint = new Point(colIndex*itemW + itemW,rowIndex*itemH);
endPoint = new Point(stratPoint.x,stratPoint.y + itemH);
if(isRightBoundary) {
result = new PuzzelSplinterSide(stratPoint,endPoint);
}
else {
radomSpinterGapLength = getRadomSpinterGapLength(RIGHT_SIDE);
controlCurveLength = getControlCurveLength(RIGHT_SIDE,radomSpinterGapLength);
var rightTopGap:Number = radomSpinterGapLength/Math.tan(Math.PI/180*SPINTER_CUT_ANGLE);
if(isRadomInSide) {//右邊内側
curvePoint0 = new Point(stratPoint.x - radomSpinterGapLength,
stratPoint.y + rightTopGap
);
curveControlPoint = new Point(stratPoint.x - controlCurveLength,
stratPoint.y + itemH/2);
}
else {
curvePoint0 = new Point(stratPoint.x + radomSpinterGapLength,
stratPoint.y + rightTopGap
);
curveControlPoint = new Point(stratPoint.x + controlCurveLength,
stratPoint.y + itemH/2);
}
//curvePoint1 根據 curvePoint0 算是一樣的,故提取出
curvePoint1 = new Point(curvePoint0.x,
curvePoint0.y + itemH - rightTopGap*2);
result = new PuzzelSplinterSide(stratPoint,endPoint,[curvePoint0,curveControlPoint,curvePoint1]);
}
}
else{ //(sideType == BOTTOM_SIDE)
var isBootomBoundary:Boolean = rowIndex == PUZZEL_GAME_ROW_COUNT - 1;
stratPoint = new Point(colIndex*itemW,rowIndex*itemH + itemH);
endPoint = new Point(stratPoint.x + itemW,stratPoint.y);
if(isBootomBoundary) {
result = new PuzzelSplinterSide(stratPoint,endPoint);
}
else {
radomSpinterGapLength = getRadomSpinterGapLength(BOTTOM_SIDE);
controlCurveLength = getControlCurveLength(BOTTOM_SIDE,radomSpinterGapLength);
var bottomLeftGap:Number = radomSpinterGapLength/Math.tan(Math.PI/180*SPINTER_CUT_ANGLE);
if(isRadomInSide) {//是否為内側
curvePoint0 = new Point(stratPoint.x + bottomLeftGap,
stratPoint.y - radomSpinterGapLength);
curveControlPoint = new Point(stratPoint.x + itemW/2,
stratPoint.y - controlCurveLength);
}
else {
curvePoint0 = new Point(stratPoint.x + bottomLeftGap,
stratPoint.y + radomSpinterGapLength);
curveControlPoint = new Point(stratPoint.x + itemW/2,
stratPoint.y + controlCurveLength);
}
//curvePoint1 根據 curvePoint0 算是一樣的,故提取出
curvePoint1 = new Point(curvePoint0.x + itemW - bottomLeftGap*2,
curvePoint0.y);
result = new PuzzelSplinterSide(stratPoint,endPoint,[curvePoint0,curveControlPoint,curvePoint1]);
}
}
//将結果緩存起來
setSplinterSideCache(rowIndex,colIndex,sideType,result);
return result;
}