Time for action – getting the enemy ship to move around
In order for the enemy ship to move around, we need to use the following steps:
- Open our Xcode project if it's not already open.
- Open the
Battlefield.h
file. - Define all AI states as
enum
, as shown in the following code:typedef NS_ENUM(NSInteger, AIState) { StateWanderAround, StateMoveToPlayer, StateAttack, StateRecuperate };
- Inside the
Battlefield
scene, add a new instance variable called_aiState
, which is of theAIState
type. - Open the
Ship.h
file. - Add a callback block type, as shown in the following line of code:
typedef void(^ShipCallback)(void);
- Declare three new methods for the
Ship
class, as shown in the following code:-(void) moveToX:(float)x andY:(float)y withBlock:(ShipCallback) block; -(float) checkDistanceToShip:(Ship *)ship; -(void) moveToShip:(Ship *)ship withBlock:(ShipCallback) block;
- Open the
Ship.m
file. - Move the contents of the
-(void) moveToX:(float) x andY:(float) y
method into the-(void) moveToX...