Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Import the interfaces
- #import "HelloWorldLayer.h"
- #import "SimpleAudioEngine.h"
- #import "GameOverScene.h"
- // HelloWorldLayer implementation
- NSMutableArray *_targets;
- NSMutableArray *_projectiles;
- @implementation HelloWorldLayer
- +(CCScene *) scene
- {
- // 'scene' is an autorelease object.
- CCScene *scene = [CCScene node];
- // 'layer' is an autorelease object.
- HelloWorldLayer *layer = [HelloWorldLayer node];
- // add layer as a child to scene
- [scene addChild: layer];
- // return the scene
- return scene;
- }
- -(void)spriteMoveFinished:(id)sender {
- CCSprite *sprite = (CCSprite *)sender;
- [self removeChild:sprite cleanup:YES];
- if (sprite.tag == 1) { // target
- [_targets removeObject:sprite];
- } else if (sprite.tag == 2) { // projectile
- [_projectiles removeObject:sprite];
- }
- }
- -(void)addTarget {
- CCSprite *target = [CCSprite spriteWithFile:@"sprout.png"
- rect:CGRectMake(0, 0, 40, 40)];
- target.tag = 1;
- [_targets addObject:target];
- // Determine where to spawn the target along the Y axis
- CGSize winSize = [[CCDirector sharedDirector] winSize];
- int minY = target.contentSize.height/2;
- int maxY = winSize.height - target.contentSize.height/2;
- int rangeY = maxY - minY;
- int actualY = (arc4random() % rangeY) + minY;
- // Create the target slightly off-screen along the right edge,
- // and along a random position along the Y axis as calculated above
- target.position = ccp(winSize.width + (target.contentSize.width/2), actualY);
- [self addChild:target];
- // Determine speed of the target
- int minDuration = 1.5;
- int maxDuration = 4.0;
- int rangeDuration = maxDuration - minDuration;
- int actualDuration = (arc4random() % rangeDuration) + minDuration;
- // Create the actions
- id actionMove = [CCMoveTo actionWithDuration:actualDuration
- position:ccp(-target.contentSize.width/2, actualY)];
- id actionMoveDone = [CCCallFuncN actionWithTarget:self
- selector:@selector(spriteMoveFinished:)];
- [target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
- }
- - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- UITouch *touch = [touches anyObject];
- CGPoint location = [touch locationInView:[touch view]];
- location = [[CCDirector sharedDirector] convertToGL:location];
- //
- [[SimpleAudioEngine sharedEngine] playEffect:@"pew.wav"];
- //
- CGSize winSize = [[CCDirector sharedDirector] winSize];
- CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png"
- rect:CGRectMake(0, 0, 20, 5)];
- projectile.position = ccp(20, winSize.height/2);
- //
- int offX = location.x - projectile.position.x;
- int offY = location.y - projectile.position.y;
- //
- if (offX <= 0) return;
- //
- [self addChild:projectile];
- projectile.tag = 2;
- [_projectiles addObject:projectile];
- //
- int realX = winSize.width + (projectile.contentSize.width/2);
- float ratio = (float) offY / (float) offX;
- int realY = (realX * ratio) + projectile.position.y;
- CGPoint realDest = ccp(realX, realY);
- //
- int offRealX = realX - projectile.position.x;
- int offRealY = realY - projectile.position.y;
- float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
- float velocity = 800/1; // 480pixels/1sec
- float realMoveDuration = length/velocity;
- //
- [projectile runAction:[CCSequence actions:
- [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
- [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
- nil]];
- }
- - (void)update:(ccTime)dt {
- NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
- for (CCSprite *projectile in _projectiles) {
- CGRect projectileRect = CGRectMake(
- projectile.position.x - (projectile.contentSize.width/2),
- projectile.position.y - (projectile.contentSize.height/2),
- projectile.contentSize.width,
- projectile.contentSize.height);
- NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
- for (CCSprite *target in _targets) {
- CGRect targetRect = CGRectMake(
- target.position.x - (target.contentSize.width/2),
- target.position.y - (target.contentSize.height/2),
- target.contentSize.width,
- target.contentSize.height);
- if (CGRectIntersectsRect(projectileRect, targetRect)) {
- [targetsToDelete addObject:target];
- }
- }
- for (CCSprite *target in targetsToDelete) {
- [_targets removeObject:target];
- [self removeChild:target cleanup:YES];
- }
- if (targetsToDelete.count > 0) {
- [projectilesToDelete addObject:projectile];
- }
- [targetsToDelete release];
- }
- for (CCSprite *projectile in projectilesToDelete) {
- [_projectiles removeObject:projectile];
- [self removeChild:projectile cleanup:YES];
- }
- [projectilesToDelete release];
- }
- // on "init" you need to initialize your instance
- -(id) init
- {
- if( (self=[super init] )) {
- [self schedule:@selector(update:)];
- CGSize winSize = [[CCDirector sharedDirector] winSize];
- CCSprite *player = [CCSprite spriteWithFile:@"player.png"
- rect:CGRectMake(0, 0, 40, 40)];
- player.position = ccp(player.contentSize.width/2, winSize.height/2);
- [self addChild:player];
- //
- self.isTouchEnabled = YES;
- [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"techno_loop.wav"];
- _targets = [[NSMutableArray alloc] init];
- _projectiles = [[NSMutableArray alloc] init];
- [self schedule:@selector(update:)];
- }
- [self schedule:@selector(gameLogic:) interval:1.0];
- return self;
- }
- -(void)gameLogic:(ccTime)dt {
- [self addTarget];
- }
- // on "dealloc" you need to release all your retained objects
- - (void) dealloc
- {
- // in case you have something to dealloc, do it in this method
- // in this particular example nothing needs to be released.
- // cocos2d will automatically release all the children (Label)
- // don't forget to call "super dealloc"
- [_targets release];
- _targets = nil;
- [_projectiles release];
- _projectiles = nil;
- [super dealloc];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement