標籤:手遊 移動開發 cocos2d-x
確定SRPG一個人物的移動範圍,
我用的是比較傻的窮舉法,
其實就是把人物可移動長度的每一個點都判斷一下是否可以移動,
然後根據地形對人物影響的障礙等級,
每走一步,就少一個障礙等級的行走能力。
具體看代碼
void SearchPathForSLG::init(TMXTiledMap* map,Point heroIndex,int runLength,bool movePath[][255]){
Point mapSize=Point(map->getMapSize().width,map->getMapSize().height);
//重設可移動範圍
for (int i=0; i<255; i++) {
for (int j=0; j<255; j++) {
movePath[i][j]=false;
}
}
//設定移動的障礙等級
for (int i=0; i<255; i++) {
for (int j=0; j<255; j++) {
obstacleLevel[i][j]=TMXMapHelper::getMapTiledObstacleLevel(TMXMapHelper::getMapTiledType(Point(i,j), map));
}
}
//開始尋找
search(heroIndex,runLength,movePath,mapSize);
}
//尋找可移動範圍-順序為上,下,左,右
bool SearchPathForSLG::search(Point heroIndex,int count,bool movePath[][255],Point mapSize){
searchPath(Point(heroIndex.x,heroIndex.y-1),count,movePath,mapSize);
searchPath(Point(heroIndex.x,heroIndex.y+1),count,movePath,mapSize);
searchPath(Point(heroIndex.x-1,heroIndex.y),count,movePath,mapSize);
searchPath(Point(heroIndex.x+1,heroIndex.y),count,movePath,mapSize);
return false;
}
//尋找對應位置是否可移動
bool SearchPathForSLG::searchPath(Point heroIndex,int count,bool movePath[][255],Point mapSize){
Point thisPoint=heroIndex;
//如果該位置沒有越界,就繼續盤但
if(thisPoint.x>=0&&thisPoint.y>=0&&thisPoint.x<mapSize.x&&thisPoint.y<mapSize.y){
int pointX=(int)thisPoint.x;
int pointY=(int)thisPoint.y;
//擷取該位置的障礙等級
int thisObstacleLevel=obstacleLevel[pointX][pointY];
//如果障礙等級是99說明改位置是不能移動的
if(thisObstacleLevel==99)
return false;
//如果當前可移動能力減掉障礙等級之後還能移動,就繼續尋找
if((count-thisObstacleLevel)>=0){
movePath[pointX][pointY]=true;
search(thisPoint,count-thisObstacleLevel,movePath,mapSize);
}
}
return false;
}
效果可以看圖。
用cocos2d3.0寫一個srpg遊戲-移動部分的實現