前段时间忙,又没有写博客了,总结一下这段时间学到的东西。今天做的这个是在一个三消界面(类似candy crash)的地形描边 。。。(图片资源在本人的文件下需要的可以下载)
有内角和外角的区别,方法是观察规律根据规律把线和角度画出来。如下边这个就是一个判断单元,把每四个这种结构都遍历出来就可以了。 下边贴出实现。(我的遍历顺序是从左下角为0,0,右上角为,7,7)这么遍历出来的。
1 | 0
——————
0 | 1
#ifndef __FILL_EDGE_H__
#define __FILL_EDGE_H__
#include "GlobalHead.h"
typedef enum ELINE_TYPE
{
ELINE_TYPE_NONE = -1,
ELINE_TYPE_HORIZONTAL_0_1 = 0,
ELINE_TYPE_HORIZONTAL_1_0,
ELINE_TYPE_VERTITAL_0_1,
ELINE_TYPE_VERTITAL_1_0,
}LINE_TYPE;
//顺时针从第1象限到第4象限
typedef enum EANGLE_TYPE
{
EANGLE_TYPE_NONE = -1,
EANGLE_TYPE_1_0_0_0 ,
EANGLE_TYPE_0_1_0_0,
EANGLE_TYPE_0_1_1_1,
EANGLE_TYPE_1_0_1_1,
EANGLE_TYPE_1_1_1_0,
EANGLE_TYPE_0_0_0_1,
EANGLE_TYPE_1_1_0_1,
EANGLE_TYPE_0_0_1_0
}ANGLE_TYPE;
typedef enum EOPPOSITION_TYPE
{
EOPPOSITION_TYPE_NONE = -1,
EOPPOSITION_TYPE_0_1_0_1,
EOPPOSITION_TYPE_1_0_1_0
}OPPOSITION_TYPE;
typedefenum EORGIN
{
EORGIN_DOWN_TO_UP,
EORGIN_LEFT_TO_RIGHT
}ORGIN;
class CCFillEdge
{
public:
CCFillEdge();
void drawLine(CCNode* pParent,int tollgateId);
string lineType(LINE_TYPE type);
string angleType(ANGLE_TYPE type);
string oppositionAngleType(OPPOSITION_TYPE type);
CCPoint travelLineAnchorPosition(LINE_TYPE type);
CCPoint travelAngleAnchorPosition(ANGLE_TYPE type);
CCPoint travelOppsitionAnchorPosition(OPPOSITION_TYPE type);
bool travelHoriontal(int x,int y); // true 不用画
bool travelVetical(int x,int y); // true 不用画
bool travelAngle(int x,int y); // true 画
bool travelOppsitionAngle(int x,int y); //对角为(1,1) (0,0)的情况
int travelAngleSum(int x,int y);
int convert(int x ,int y);
LINE_TYPE getLineType(ORGIN orgin,int x,int y);
ANGLE_TYPE getAngleType(int row,int col);
OPPOSITION_TYPE getOppsitionType(int row,int col);
void convertArray(int mapData[8][8]);
void covertTollgateArray(int tollgateId);
private:
int m_tollArray[8][8];
int m_newArray[10][10];
};
#endif
#include "CCFillEdge.h"
#include "CCTollgate.h"
#define ELEMENT_SIZE 70
#define ROW 8
#define COL 8
CCFillEdge::CCFillEdge()
{
CCLOG("FillEdge::FillEdge()");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
m_newArray[i][j] = 0;
}
}
}
void CCFillEdge::drawLine(CCNode* pParent,int tollgateId)
{
covertTollgateArray(tollgateId); // 将地形转换成0,1形式
convertArray(m_tollArray); // 四周加一圈0
for (int row = 0;row < ROW+1 ;row++)
{
for (int col = 0; col < COL+1; col++)
{
//行
LINE_TYPE h_type =getLineType(EORGIN_DOWN_TO_UP,row,col);
string h_line = lineType(h_type);
CCPoint h_hanchro =travelLineAnchorPosition(h_type);
if (h_type != -1)
{
CCSprite* pSprite1 = CCSprite::create(h_line.c_str());
pSprite1->setAnchorPoint(h_hanchro);
pSprite1->setPosition(ccp(col * ELEMENT_SIZE,row * ELEMENT_SIZE + ELEMENT_SIZE));
pSprite1->setZOrder(0);
pParent->addChild(pSprite1);
}
//列
LINE_TYPE v_type = getLineType(EORGIN_LEFT_TO_RIGHT,row,col);
string v_line = lineType(v_type);
CCPoint v_anchro = travelLineAnchorPosition(v_type);
if (v_type != -1)
{
CCSprite* pSprite2 = CCSprite::create(v_line.c_str());
pSprite2->setAnchorPoint(v_anchro);
pSprite2->setPosition(ccp(col * ELEMENT_SIZE + ELEMENT_SIZE,row * ELEMENT_SIZE));
pSprite2->setZOrder(0);
pParent->addChild(pSprite2);
}
//角度(不包括对角的情况)
ANGLE_TYPE angle_type = getAngleType(row,col);
string angle_line = angleType(angle_type);
CCPoint angle_anchro = travelAngleAnchorPosition(angle_type);
if (angle_type != -1)
{
CCSprite* pAngleSprite = CCSprite::create(angle_line.c_str());
pAngleSprite->setAnchorPoint(angle_anchro);
pAngleSprite->setPosition(ccp(col * ELEMENT_SIZE + ELEMENT_SIZE,row * ELEMENT_SIZE + ELEMENT_SIZE));
pAngleSprite->setZOrder(2);
pParent->addChild(pAngleSprite);
}
}
}
//对角的情况()
for (int row = 0;row < ROW+1 ;row++)
{
for (int col = 0; col < COL+1; col++)
{
if(travelOppsitionAngle(row,col))
{
EOPPOSITION_TYPE oppsition_type = getOppsitionType(row, col);
string oppsition_line = oppositionAngleType(oppsition_type);
CCPoint oppsition_anchro = travelOppsitionAnchorPosition(oppsition_type);
if (oppsition_type != -1)
{
CCSprite* pOppsitionSprite = CCSprite::create(oppsition_line.c_str());
pOppsitionSprite->setAnchorPoint(oppsition_anchro);
pOppsitionSprite->setPosition(ccp(col* ELEMENT_SIZE + ELEMENT_SIZE ,row * ELEMENT_SIZE + ELEMENT_SIZE));
pOppsitionSprite->setZOrder(3);
pParent->addChild(pOppsitionSprite);
}
}
}
}
}
string CCFillEdge::lineType(LINE_TYPE type)
{
string line;
if (type == ELINE_TYPE_HORIZONTAL_0_1)
{
line = MAP_TILE_PATH"gride5.png"; //13
}
else if (type == ELINE_TYPE_HORIZONTAL_1_0)
{
line = MAP_TILE_PATH"gride1.png"; //25
}
else if (type == ELINE_TYPE_VERTITAL_0_1)
{
line = MAP_TILE_PATH"gride7.png"; //17
}
else if (type == ELINE_TYPE_VERTITAL_1_0)
{
line = MAP_TILE_PATH"gride3.png"; //19
}
return line;
}
string CCFillEdge::angleType(ANGLE_TYPE type)
{
string angle;
if (type == EANGLE_TYPE_1_0_0_0)
{
angle = MAP_TILE_PATH"gride6.png";
}
else if (type == EANGLE_TYPE_0_1_0_0)
{
angle = MAP_TILE_PATH"gride4.png";
}
else if (type == EANGLE_TYPE_0_1_1_1)
{
angle = MAP_TILE_PATH"gride10.png";
}
else if (type == EANGLE_TYPE_1_0_1_1)
{
angle = MAP_TILE_PATH"gride11.png";
}
else if (type == EANGLE_TYPE_1_1_1_0)
{
angle = MAP_TILE_PATH"gride8.png";
}
else if (type == EANGLE_TYPE_0_0_0_1)
{
angle = MAP_TILE_PATH"gride0.png";
}
else if (type == EANGLE_TYPE_1_1_0_1)
{
angle = MAP_TILE_PATH"gride9.png";
}
else if (type == EANGLE_TYPE_0_0_1_0)
{
angle = MAP_TILE_PATH"gride2.png";
}
return angle;
}
string CCFillEdge::oppositionAngleType(OPPOSITION_TYPE type)
{
string oppsition;
if (type == EOPPOSITION_TYPE_0_1_0_1)
{
oppsition = MAP_TILE_PATH"gride12.png";
}
else if (type == EOPPOSITION_TYPE_1_0_1_0)
{
oppsition = MAP_TILE_PATH"gride13.png";
}
return oppsition;
}
CCPoint CCFillEdge::travelLineAnchorPosition(LINE_TYPE type)
{
CCPoint anchor ;
if (type == ELINE_TYPE_HORIZONTAL_0_1)
{
anchor = ccp(0,1);
}
else if (type == ELINE_TYPE_HORIZONTAL_1_0)
{
anchor = ccp(0,0);
}
else if (type == ELINE_TYPE_VERTITAL_0_1)
{
anchor = ccp(1,0);
}
else if (type == ELINE_TYPE_VERTITAL_1_0)
{
anchor = ccp(0,0);
}
return anchor;
}
CCPoint CCFillEdge::travelAngleAnchorPosition(ANGLE_TYPE type)
{
CCPoint anchor ;
if (type == EANGLE_TYPE_1_0_0_0)
{
anchor = ccp(1,1);
}
else if (type == EANGLE_TYPE_0_1_0_0)
{
anchor = ccp(0,1);
}
else if (type == EANGLE_TYPE_0_1_1_1)
{
anchor = ccp(0,0);
}
else if (type == EANGLE_TYPE_1_0_1_1)
{
anchor = ccp(1,0);
}
else if (type == EANGLE_TYPE_1_1_1_0)
{
anchor = ccp(0,1);
}
else if (type == EANGLE_TYPE_0_0_0_1)
{
anchor = ccp(1,0);
}
else if (type == EANGLE_TYPE_1_1_0_1)
{
anchor = ccp(1,1);
}
else if (type == EANGLE_TYPE_0_0_1_0)
{
anchor = ccp(0,0);
}
return anchor;
}
CCPoint CCFillEdge::travelOppsitionAnchorPosition(OPPOSITION_TYPE type)
{
CCPoint anchor ;
if (type == EOPPOSITION_TYPE_0_1_0_1)
{
anchor = ccp(0.5,0.5);
}
else if (type == EOPPOSITION_TYPE_1_0_1_0)
{
anchor = ccp(0.5,0.5);
}
return anchor;
}
bool CCFillEdge::travelHoriontal(int x,int y)
{
return (convert(x,y)==convert(x+1, y));
}
bool CCFillEdge::travelVetical(int x,int y)
{
return (convert(x,y)==convert(x, y+1));
}
bool CCFillEdge::travelAngle(int x,int y)
{
int orgin = convert(x,y);
int orginR = convert(x+1,y);
int orginU = convert(x,y+1);
int oringO = convert(x+1,y+1);
return (orgin + orginR + orginU + oringO) % 2 ;
}
bool CCFillEdge::travelOppsitionAngle(int x,int y)
{
int orgin = convert(x,y);
int orginR = convert(x+1,y);
int orginU = convert(x,y+1);
int oringO = convert(x+1,y+1);
if ( (orgin + orginR + orginU + oringO) == 2)
return true;
else
returnfalse;
}
int CCFillEdge::travelAngleSum(int x,int y)
{
int orgin = convert(x,y);
int orginR = convert(x+1,y);
int orginU = convert(x,y+1);
int oringO = convert(x+1,y+1);
return orgin + orginR + orginU + oringO;
}
int CCFillEdge::convert(int x ,int y)
{
// CCAssert(x >= 0 && x <= 6,"" );
// CCAssert(y >= 0 && y <= 6,"" );
return m_newArray[x][y];
}
LINE_TYPE CCFillEdge::getLineType(ORGIN orgin,int x,int y)
{
if (orgin == EORGIN_DOWN_TO_UP)
{
if (!travelHoriontal(x,y))
{
if (convert(x,y)) // 1
{
return ELINE_TYPE_HORIZONTAL_1_0;
}
else // 0
{
return ELINE_TYPE_HORIZONTAL_0_1;
}
}
else
{
return ELINE_TYPE_NONE;
}
}
else
{
if (!travelVetical(x,y))
{
if (convert(x,y)) // 1
{
return ELINE_TYPE_VERTITAL_1_0;
}
else // 0
{
return ELINE_TYPE_VERTITAL_0_1;
}
}
else
{
return ELINE_TYPE_NONE;
}
}
}
ANGLE_TYPE CCFillEdge::getAngleType(int row,int col)
{
if (travelAngle(row,col))
{
int sum = travelAngleSum(row,col); // 1,3
if (sum == 1)
{
if (convert(row+1,col+1))
{
return EANGLE_TYPE_1_0_0_0;
}
else if (convert(row+1,col))
{
return EANGLE_TYPE_0_1_0_0;
}
else if (convert(row,col+1))
{
return EANGLE_TYPE_0_0_0_1;
}
else if (convert(row,col))
{
return EANGLE_TYPE_0_0_1_0;
}
}
else if (sum == 3)
{
if (!convert(row+1,col+1))
{
return EANGLE_TYPE_0_1_1_1;
}
else if (!convert(row+1,col))
{
return EANGLE_TYPE_1_0_1_1;
}
else if (!convert(row,col+1))
{
return EANGLE_TYPE_1_1_1_0;
}
else if (!convert(row,col))
{
return EANGLE_TYPE_1_1_0_1;
}
}
}
else
{
return EANGLE_TYPE_NONE;
}
}
OPPOSITION_TYPE CCFillEdge::getOppsitionType(int row,int col)
{
if (travelOppsitionAngle(row,col))
{
if (convert(row,col) && (convert(row+1, col+1)))
{
return EOPPOSITION_TYPE_1_0_1_0;
}
else if (convert(row,col+1) && (convert(row+1, col)))
{
return EOPPOSITION_TYPE_0_1_0_1;
}
else
{
return EOPPOSITION_TYPE_NONE;
}
}
else
{
return EOPPOSITION_TYPE_NONE;
}
}
void CCFillEdge::convertArray(int array[8][8])
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
m_newArray[i+1][j+1] = array[i][j];
}
}
}
void CCFillEdge::covertTollgateArray(int tollgateId)
{
Tollgate* pTollgate = GetTollgate(tollgateId);
for (int row = 0; row < ROW; row++)
{
for (int col = 0; col < COL; col++)
{
if (pTollgate->cells[ROW - row - 1][col].landId != 0)
{
m_tollArray[row][col] = 1;
}
else
{
m_tollArray[row][col] = 0;
}
printf(" %d ",m_tollArray[row][col]);
}
CCLOG("\n");
}
}