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;
}