Cocos2D: Anti-tower game production Tour (11)
It's time to mix some bad guys!
Open HelloWorldLayer. h and add the following code:
// Add these instance variablesint wave;CCLabelBMFont *ui_wave_lbl;// Add the following property to the properties section@property (nonatomic,strong) NSMutableArray *enemies;
Modify the HelloWorldLayer. m file as follows:
// Synthesize enemies@synthesize enemies;
Now we have created a class that saves Enemy information and manages how they move on the screen. We have created a new class named Enemy, which inherits from CCNode.
Replace Enemy. h with the following content:
#import "cocos2d.h"#import "HelloWorldLayer.h"@class HelloWorldLayer, Waypoint, Tower;@interface Enemy: CCNode { CGPoint myPosition; int maxHp; int currentHp; float walkingSpeed; Waypoint *destinationWaypoint; BOOL active;}@property (nonatomic,assign) HelloWorldLayer *theGame;@property (nonatomic,assign) CCSprite *mySprite;+(id)nodeWithTheGame:(HelloWorldLayer*)_game;-(id)initWithTheGame:(HelloWorldLayer *)_game;-(void)doActivate;-(void)getRemoved;@end
Now replace the Enemy. m file with the following content:
#import "Enemy.h"#import "Tower.h"#import "Waypoint.h"#define HEALTH_BAR_WIDTH 20#define HEALTH_BAR_ORIGIN -10@implementation Enemy@synthesize mySprite, theGame;+(id)nodeWithTheGame:(HelloWorldLayer*)_game { return [[self alloc] initWithTheGame:_game];}-(id)initWithTheGame:(HelloWorldLayer *)_game { if ((self=[super init])) { theGame = _game; maxHp = 40; currentHp = maxHp; active = NO; walkingSpeed = 0.5; mySprite = [CCSprite spriteWithFile:@"enemy.png"]; [self addChild:mySprite]; Waypoint * waypoint = (Waypoint *)[theGame.waypoints objectAtIndex:([theGame.waypoints count]-1)]; destinationWaypoint = waypoint.nextWaypoint; CGPoint pos = waypoint.myPosition; myPosition = pos; [mySprite setPosition:pos]; [theGame addChild:self]; [self scheduleUpdate]; } return self;}-(void)doActivate{ active = YES;}-(void)update:(ccTime)dt{ if(!active)return; if([theGame circle:myPosition withRadius:1 collisionWithCircle:destinationWaypoint.myPosition collisionCircleRadius:1]) { if(destinationWaypoint.nextWaypoint) { destinationWaypoint = destinationWaypoint.nextWaypoint; }else { //Reached the end of the road. Damage the player [theGame getHpDamage]; [self getRemoved]; } } CGPoint targetPoint = destinationWaypoint.myPosition; float movementSpeed = walkingSpeed; CGPoint normalized = ccpNormalize(ccp(targetPoint.x-myPosition.x,targetPoint.y-myPosition.y)); mySprite.rotation = CC_RADIANS_TO_DEGREES(atan2(normalized.y,-normalized.x)); myPosition = ccp(myPosition.x+normalized.x * movementSpeed, myPosition.y+normalized.y * movementSpeed); [mySprite setPosition:myPosition];}-(void)getRemoved{ [self.parent removeChild:self cleanup:YES]; [theGame.enemies removeObject:self]; //Notify the game that we killed an enemy so we can check if we can send another wave [theGame enemyGotKilled];}-(void)draw{ ccDrawSolidRect(ccp(myPosition.x+HEALTH_BAR_ORIGIN, myPosition.y+16), ccp(myPosition.x+HEALTH_BAR_ORIGIN+HEALTH_BAR_WIDTH, myPosition.y+14), ccc4f(1.0, 0, 0, 1.0)); ccDrawSolidRect(ccp(myPosition.x+HEALTH_BAR_ORIGIN, myPosition.y+16), ccp(myPosition.x+HEALTH_BAR_ORIGIN + (float)(currentHp * HEALTH_BAR_WIDTH)/maxHp, myPosition.y+14), ccc4f(0, 1.0, 0, 1.0));}@end