用cocos2d3.0寫一個srpg遊戲-移動部分的實現

來源:互聯網
上載者:User

標籤:手遊   移動開發   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遊戲-移動部分的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.