diff --git a/Classes/Ball.h b/Classes/Ball.h index d55522f..30c21e6 100644 --- a/Classes/Ball.h +++ b/Classes/Ball.h @@ -12,7 +12,8 @@ @property(readonly,assign) b2Body* Body; @property(readonly,assign) b2Fixture* Fixture; --(id) spawn: (CCLayer*) layer: (b2World*) world: (b2Body*) groundBody: (b2Vec2) force; +-(id) spawn: (CCLayer*) layer: (b2World*) world: (b2Body*) groundBody; +-(void) respawnLeftOrRight; -(void) respawnLeft; -(void) respawnRight; -(void)dealloc; diff --git a/Classes/Ball.mm b/Classes/Ball.mm index 5a2ecc2..45825db 100644 --- a/Classes/Ball.mm +++ b/Classes/Ball.mm @@ -1,11 +1,12 @@ #define PTM_RATIO 32 #import "Ball.h" #import "CCScheduler.h" +#include @implementation Ball @synthesize Sprite, Body, Fixture; --(id) spawn: (CCLayer*) layer: (b2World*) world: (b2Body*) groundBody: (b2Vec2) force { +-(id) spawn: (CCLayer*) layer: (b2World*) world: (b2Body*) groundBody { if ((self=[super init])) { _winSize = [CCDirector sharedDirector].winSize; @@ -23,7 +24,7 @@ -(id) spawn: (CCLayer*) layer: (b2World*) world: (b2Body*) groundBody: (b2Vec2) Body = world->CreateBody(&bodyDef); b2CircleShape shape; - shape.m_radius = (Sprite.size.width/2)/PTM_RATIO; + shape.m_radius = ([Sprite boundingBox].size.width/2)/PTM_RATIO; b2FixtureDef shapeDef; shapeDef.shape = &shape; @@ -32,7 +33,6 @@ -(id) spawn: (CCLayer*) layer: (b2World*) world: (b2Body*) groundBody: (b2Vec2) shapeDef.restitution = 1.0f; Fixture = Body->CreateFixture(&shapeDef); - Body->ApplyLinearImpulse(force, Body->GetPosition()); [[CCScheduler sharedScheduler] scheduleSelector:@selector(tick:) forTarget:self interval:0 paused:FALSE]; } return self; @@ -67,18 +67,43 @@ - (void)tick:(ccTime) dt { } } --(void) respawnLeft { +-(void) respawnLeftOrRight { + int r = arc4random() % 2; + + if(r==1) { + [self respawnLeft]; + } else { + [self respawnRight]; + } +} + +-(void) respawn: (float)withAngle{ [self reset]; - - b2Vec2 force = b2Vec2(-10,10); - Body->ApplyLinearImpulse(force, Body->GetPosition()); + const int speed = 35; + + double x = sin(withAngle * M_PI / 180) * speed; + double y = cos(withAngle * M_PI / 180) * speed; + + b2Vec2 force = b2Vec2(x,y); + Body->ApplyLinearImpulse(force, Body->GetPosition()); +} + +-(void) respawnLeft { + int speed = 35; + int margin = 40; + int r = arc4random() % (180-margin); + r += (180-(margin/2)); + + [self respawn: r]; } -(void) respawnRight{ - [self reset]; + int speed = 35; + int margin = 40; + int r = arc4random() % (180-margin); + r += (margin/2); - b2Vec2 force = b2Vec2(10,0); - Body->ApplyLinearImpulse(force, Body->GetPosition()); + [self respawn: r]; } -(void)dealloc{ diff --git a/Classes/iPongLayer.mm b/Classes/iPongLayer.mm index e554522..2c371de 100755 --- a/Classes/iPongLayer.mm +++ b/Classes/iPongLayer.mm @@ -53,6 +53,8 @@ - (id)init { return self; } + +#ifdef DRAW_DEBUG_INFO -(void)draw{ [super draw]; @@ -69,6 +71,7 @@ -(void)draw{ glEnableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); } +#endif -(void)addBackground{ CCSprite* lineSprite = [CCSprite spriteWithFile:@"line.png" rect:CGRectMake(0,0, 5, winSize.height*2)]; @@ -140,7 +143,6 @@ -(void)higherRightScore{ } - (void)spawnPaddles{ - _leftPaddle = [[Paddle alloc] initWithWorld: _world: _groundBody: ccp(50,50): CGRectMake(0,0, winSize.width/2,winSize.height)]; [self addChild:_leftPaddle.Sprite]; @@ -149,7 +151,8 @@ - (void)spawnPaddles{ } - (void) setupBall{ - _ball = [[Ball alloc] spawn:self :_world : _groundBody : b2Vec2(10,10)]; + _ball = [[Ball alloc] spawn:self :_world : _groundBody]; + [_ball respawnLeftOrRight]; } - (void)tick:(ccTime) dt {