Skip to content

Commit

Permalink
Ball is now responsible for spawning and respawning. It also respawn …
Browse files Browse the repository at this point in the history
…with an random angle now.
  • Loading branch information
pjvds committed Apr 5, 2011
1 parent 8696273 commit 47fb5e1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Classes/Ball.h
Expand Up @@ -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;
Expand Down
45 changes: 35 additions & 10 deletions Classes/Ball.mm
@@ -1,11 +1,12 @@
#define PTM_RATIO 32
#import "Ball.h"
#import "CCScheduler.h"
#include <stdlib.h>

@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;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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{
Expand Down
7 changes: 5 additions & 2 deletions Classes/iPongLayer.mm
Expand Up @@ -53,6 +53,8 @@ - (id)init {
return self;
}


#ifdef DRAW_DEBUG_INFO
-(void)draw{
[super draw];

Expand All @@ -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)];
Expand Down Expand Up @@ -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];

Expand All @@ -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 {
Expand Down

0 comments on commit 47fb5e1

Please sign in to comment.