bool PathArithmetic::isCheck(Vec2 point,vector<Vector<Grid*>> _gridArray){ if (point.x < 0 || point.y < 0) { return false; } PointDelegate* g = PointDelegate::create(point.x,point.y); for (int i = 0; i < _invalidPoints.size(); i++) { PointDelegate* pp = _invalidPoints.at(i); Vec2 t = Point(pp->getX(), pp->getY()); if (point.equals(t)) { return false; } } if (point.x >= mapWidth || point.y >= mapHeight) { return false; } Vector<Grid*> tempX = _gridArray.at((int)g->getX()); Grid* grid = tempX.at((int)g->getY()); if (point.x >= 0 && point.y >= 0 && grid->isPass()) { return true; } return false; }
void Game::monsterMoveWithWayPoints(Vector<PointDelegate*> pathVector,SpriteBase* monster){ Vector<FiniteTimeAction*> actionVector; for(int i = 0; i<pathVector.size();i++){ PointDelegate* pd = pathVector.at(i); Vec2 p = Point(pd->getX(),pd->getY()); Vec2 glPoint = locationForTilePos(p); MoveTo* moveTo = MoveTo::create(1.5f,glPoint); actionVector.pushBack(moveTo); } auto animate = getAnimationByName("eagle_move_",0.5f,4); auto repeatanimate = RepeatForever::create(animate); monster->runAction(repeatanimate); auto callfunc = CallFunc::create([=]{ monster->stopAction(repeatanimate); monster->setMove(false); monster->setMoveCount(monster->getMoveCount()+1); }); actionVector.pushBack(callfunc); auto sequence = Sequence::create(actionVector); monster->runAction(sequence); }
void Game::playerMoveWithWayPoints(Vec2 position,Vector<PointDelegate*> path){ auto animate = getAnimationByName("player_move_",0.4f,4); auto repeatanimate = RepeatForever::create(animate); player->runAction(repeatanimate); Vector<FiniteTimeAction*> actionVector; for(int i=0;i<path.size();i++){ PointDelegate* pd = path.at(i); Vec2 p = Point(pd->getX(),pd->getY()); Vec2 glPoint = locationForTilePos(p); MoveTo* moveTo = MoveTo::create(0.4f,glPoint); actionVector.pushBack(moveTo); } auto callfunc = CallFunc::create([=]{ player->stopAction(repeatanimate); player->setTexture("player_stand_1.png"); }); actionVector.pushBack(callfunc); auto sequence = Sequence::create(actionVector); player->runAction(sequence); }
bool PathArithmetic::findValidGrid(Vec2 from,Vec2 to,vector<Vector<Grid*>> _gridArray){ PointDelegate* fromDelegate = PointDelegate::create(from.x, from.y); _invalidPoints.pushBack(fromDelegate); Vector<PointDelegate*> points; points.pushBack(PointDelegate::create(from.x, from.y-1)); points.pushBack(PointDelegate::create(from.x, from.y+1)); points.pushBack(PointDelegate::create(from.x-1, from.y)); points.pushBack(PointDelegate::create(from.x+1, from.y)); points.pushBack(PointDelegate::create(from.x-1, from.y-1)); points.pushBack(PointDelegate::create(from.x+1, from.y+1)); points.pushBack(PointDelegate::create(from.x-1, from.y+1)); points.pushBack(PointDelegate::create(from.x+1, from.y-1)); Vector<PointDelegate*> temp; for (int i = 0; i < points.size(); i++) { PointDelegate* pd = points.at(i); Vec2 p = Vec2(pd->getX(), pd->getY()); if (p.equals(to)) { _pathPoints.pushBack(pd); return true; } if (isCheck(p, _gridArray)) { temp.pushBack(pd); } } std::sort(temp.begin(), temp.end(), [=](const Ref* obj1, const Ref* obj2){ PointDelegate* p1 = (PointDelegate*)obj1; PointDelegate* p2 = (PointDelegate*)obj2; double r1 = sqrt((p1->getX() - to.x) * (p1->getX() - to.x) + (p1->getY() - to.y) * (p1->getY() - to.y)); double r2 = sqrt((p2->getX() - to.x) * (p2->getX() - to.x) + (p2->getY() - to.y) * (p2->getY() - to.y)); return r1 < r2 ? -1 : 0; }); for (int i = 0; i < temp.size(); i++) { PointDelegate* pd = temp.at(i); Vec2 p = Vec2(pd->getX(), pd->getY()); bool flag = findValidGrid(p, to, _gridArray); if (flag) { _pathPoints.pushBack(pd); return true; } } return false; }