I was playing with the AgentCatalog Sample code. I added a border to AAPLFleeScene.
First I wrote the method based on what apple did for Circular Obstacles.
- (GKObstacle *)addRectObstacleAtPoint:(CGRect)rect {
CGFloat x = rect.origin.x;
CGFloat y = rect.origin.y;
CGFloat w = rect.size.width;
CGFloat h = rect.size.height;
SKShapeNode *rectNode = [SKShapeNode shapeNodeWithRect:rect];
rectNode.lineWidth = 2.5;
rectNode.fillColor = [SKColor grayColor];
rectNode.strokeColor = [SKColor redColor];
rectNode.zPosition = 1;
[self addChild:rectNode];
vector_float2 f[] = {
(vector_float2){x, y},
(vector_float2){x+w, y},
(vector_float2){x+w, y+h},
(vector_float2){x, y+h}
};
GKPolygonObstacle *obstacle = [GKPolygonObstacle obstacleWithPoints:f count:4];
return obstacle;
}
//The circular obstacles added by the method below works - It is by apple
- (GKObstacle *)addObstacleAtPoint:(CGPoint)point {
SKShapeNode *circleShape = [SKShapeNode shapeNodeWithCircleOfRadius:AAPLDefaultAgentRadius];
circleShape.lineWidth = 2.5;
circleShape.fillColor = [SKColor grayColor];
circleShape.strokeColor = [SKColor redColor];
circleShape.zPosition = 1;
circleShape.position = point;
[self addChild:circleShape];
GKCircleObstacle *obstacle = [GKCircleObstacle obstacleWithRadius:AAPLDefaultAgentRadius];
obstacle.position = (vector_float2){point.x, point.y};
return obstacle;
}
Then I wrote a method which uses it
-(void)addObstacles{
CGFloat w = self.frame.size.width;
CGFloat h = self.frame.size.height;
CGFloat borderWidth = 50;
NSArray<GKObstacle *> *obstacles = @[
[self addObstacleAtPoint:CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame) + 150)],
[self addObstacleAtPoint:CGPointMake(CGRectGetMidX(self.frame) - 200, CGRectGetMidY(self.frame) - 150)],
[self addObstacleAtPoint:CGPointMake(CGRectGetMidX(self.frame) + 200, CGRectGetMidY(self.frame) - 150)],
[self addRectObstacleAtPoint:CGRectMake(0, 0, borderWidth, h)],
[self addRectObstacleAtPoint:CGRectMake(0, h-borderWidth, w, borderWidth)],
[self addRectObstacleAtPoint:CGRectMake(w-borderWidth, 0, borderWidth, h)],
[self addRectObstacleAtPoint:CGRectMake(0, 0, w, borderWidth)]
];
[self.enemy.agent.behavior setWeight:100 forGoal:[GKGoal goalToAvoidObstacles:obstacles maxPredictionTime:1]];
}
Then I call that in the method above in didMoveToView in the AAPLFleeScene.
- (void)didMoveToView:(nonnull SKView *)view {
[super didMoveToView:view];
//-----------apple's code
//-----------apple's code
//After Apple's code
[self addObstacles];
}
The result is Screenshot
Any insights on what I am doing wrong? I believe the way I wrote the rectangle is wrong. Could you please help?