Hi everyone,
I've been playing with the Geek & Dad tutorial part 3 of TileGame and I've been trying to extend it with a generic NPC class to replace the existing enemies. What I've done and am trying to do is, subclass CCSprite as NPC and editing the collision code in HelloWorldScene.m to call the isDamaged method on the NPC and record the time it is damaged(so that I can set the enemy object to be invulnerable for an interval).
Right now, the problem I'm facing is I can't seem to get my NPC's to remember when they're hit using ccTime.
I've replaced the CCSprite datatype to be my NPC class and edited the testCollision method to
if (CGRectIntersectsRect(projectileRect, targetRect)) {
// enemy intersects with projectile we can damage/kill mobs here
NSLog(@"Enemy HP: %d",target.hp);
if(target.hp==0)
{
[targetsToDelete addObject:target];
NSLog(@"Enemy HP: %d Enemy Dies",target.hp);
[self removeChild:projectile cleanup:YES];
}
else
{
NSLog(@"collision");
[target isDamaged:1 damagedAt:dt];
}
}
}
My NPC class's methods are as followed
- (void)isDamaged:(int)damageTaken damagedAt:(ccTime)theTime
{
if (![self isInvulnerable:theTime]) {
NSLog(@"NPC Damage Taken!");
[self setHp: [self hp]-damageTaken];
[self setLastDamagedAt:theTime];
}
}
- (bool)isInvulnerable: (ccTime) theTime
{
ccTime duration;
duration = theTime-[self lastDamagedAt];
NSLog(@"Duration: %f",duration);
if (duration<[self invulnerablePeriod]) {
NSLog(@"Invulnerable!");
return YES;
}
return NO;
}
Currently the collisionTest detects the collision as many times as the scheduler tics so thats about 60fps and that would mean my collision is detected at that speed that many times. My aim is to detect the first collision, record the time it collided and based on the duration it has been last hit- grant invulnerability. Sort of like a "blinking invulnerability" in megaman(rockman) to the NPC... if you know what I mean.
I'm unsure of how to use ccTime and the scheduler to do this but I think I'm on my way there. Hope someone can give me push in the right direction!