I'm trying to make a turn based game, and each enemy should find a path to player node. I'm using gameplaykit's pathfinding, and every time enemy has to make a turn, I make sure to remove other enemies from that GKGridGraph (so that enemy won't walk on one another) and latter I add that same nodes.
NSMutableArray <GKGridGraphNode *> *walls = [NSMutableArray array];
for(SKNode *mapNode in enemies)
{
for(SKNode *node in mapNode.children)
{
if((node.position.x != enemy.position.x)||(node.position.y != enemy.position.y))
{
GKGridGraphNode *graphNode = [_NewGraph nodeAtGridPosition:
(vector_int2){(node.position.x-300)/70, (node.position.y-180)/70}];
[walls addObject:graphNode];
}
}
}
[_NewGraph removeNodes:walls];
GKGridGraphNode* GKenemyNode = [_NewGraph nodeAtGridPosition:(vector_int2)
{(enemy.position.x-300)/70, (enemy.position.y-180)/70}];
GKGridGraphNode* GKplayerNode = [_NewGraph nodeAtGridPosition:(vector_int2)
{(player.position.x-300)/70,(player.position.y-180)/70}];
NSArray<GKGridGraphNode *> *path = [_NewGraph findPathFromNode:GKenemyNode toNode:GKplayerNode];
[_NewGraph addNodes:walls];
If I comment out lines removeNodes and addNodes everything works fine.
EDIT: Would it be a good idea to every time create new class object that could return generated GKGridGraph, instead of reasigning the same one?
Okey, so I found the answer and would like to share with all of you ! After removing nodes I did this (no need to add nodes as I did):
for(GKGridGraphNode *node in walls)
{
[NewGraph connectNodeToAdjacentNodes:node];
}
NewGraph = nil;
GoodLuck !