Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added background music and possible fix for crashes (couldn't be test…

…ed because I couldn't reproduce the problem :-( )
  • Loading branch information...
commit 11c5c7b0a0f2b90fa64589524d4e2d238d39da75 1 parent 7a21930
@volkert volkert authored
View
123 PeerPong/MainScene.mm
@@ -17,6 +17,7 @@
#import "QueueLayer.h"
#import "MessageLayer.h"
#import "MyContactListener.h"
+#import "SimpleAudioEngine.h"
@implementation MainScene
@@ -48,6 +49,8 @@ -(id)init {
// Register tick method which is called on every frame rendering
[self schedule:@selector(tick:)];
+
+ [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"peer_pong_intro_128k.mp3"];
}
return self;
@@ -74,6 +77,7 @@ -(void)removePaddleForPlayer:(PeerPongPlayer *)aPlayer {
#pragma mark - Manage layers
-(void)addBallLayer {
+ [[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
[self removeWelcomeLayer];
ballLayer = [BallLayer node];
[self addChild:ballLayer z:9 tag:98];
@@ -113,6 +117,7 @@ -(void)endGame {
gameplay.gameIsRunning = NO;
[self removeBallLayer];
[self addWelcomeLayer];
+ [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"peer_pong_intro_128k.mp3"];
}
#pragma mark - Manage queue items
@@ -129,74 +134,76 @@ -(void)popMessage:(NSString*)aMessage{
#pragma mark - Tick
- (void)tick:(ccTime) dt {
- gameplay.world->Step(dt, 10, 10);
- for(b2Body *b = gameplay.world->GetBodyList(); b; b=b->GetNext()) {
- if (b->GetUserData() != NULL) {
- CCSprite *sprite = (CCSprite *)b->GetUserData();
-
- CGPoint position = ccp(b->GetPosition().x * gameplay.PTM_RATIO,
- b->GetPosition().y * gameplay.PTM_RATIO);
+ @synchronized(self) {
+ gameplay.world->Step(dt, 10, 10);
+ for(b2Body *b = gameplay.world->GetBodyList(); b; b=b->GetNext()) {
+ if (b->GetUserData() != NULL) {
+ CCSprite *sprite = (CCSprite *)b->GetUserData();
+
+ CGPoint position = ccp(b->GetPosition().x * gameplay.PTM_RATIO,
+ b->GetPosition().y * gameplay.PTM_RATIO);
+
+ // Set new calculated position for sprite
+ sprite.position = position;
+ sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
+
+ // Ball sprite specific
+ if(sprite.tag == gameplay.ballTag) {
+ [gameplay ballIsOnPosition:position];
+
+ static int maxSpeed = 30;
+
+ b2Vec2 velocity = b->GetLinearVelocity();
+ float32 speed = velocity.Length();
+
+ if (speed > maxSpeed) {
+ b->SetLinearDamping(0.5);
+ } else if (speed < maxSpeed) {
+ b->SetLinearDamping(0.0);
+ }
+ }
+
+
+ }
+ }
+
+ if(soundDelayFrames > 0) soundDelayFrames--;
+
+ std::vector<MyContact>::iterator pos;
+ BOOL hitsWall = NO;
+ for(pos = contactListener->_contacts.begin(); pos != contactListener->_contacts.end(); ++pos) {
+ MyContact contact = *pos;
- // Set new calculated position for sprite
- sprite.position = position;
- sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
+ if(soundDelayFrames <= 0) {
- // Ball sprite specific
- if(sprite.tag == gameplay.ballTag) {
- [gameplay ballIsOnPosition:position];
+ //
+ if ((contact.fixtureA == ballLayer.fixture && contact.fixtureB == gameBoxLayer.leftEdge) ||
+ (contact.fixtureA == gameBoxLayer.leftEdge && contact.fixtureB == ballLayer.fixture)) {
+ hitsWall = YES;
+ }
- static int maxSpeed = 30;
+ if ((contact.fixtureA == ballLayer.fixture && contact.fixtureB == gameBoxLayer.topEdge) ||
+ (contact.fixtureA == gameBoxLayer.topEdge && contact.fixtureB == ballLayer.fixture)) {
+ hitsWall = YES;
+ }
- b2Vec2 velocity = b->GetLinearVelocity();
- float32 speed = velocity.Length();
+ if ((contact.fixtureA == ballLayer.fixture && contact.fixtureB == gameBoxLayer.rightEdge) ||
+ (contact.fixtureA == gameBoxLayer.rightEdge && contact.fixtureB == ballLayer.fixture)) {
+ hitsWall = YES;
+ }
- if (speed > maxSpeed) {
- b->SetLinearDamping(0.5);
- } else if (speed < maxSpeed) {
- b->SetLinearDamping(0.0);
+ if ((contact.fixtureA == ballLayer.fixture && contact.fixtureB == gameBoxLayer.bottomEdge) ||
+ (contact.fixtureA == gameBoxLayer.bottomEdge && contact.fixtureB == ballLayer.fixture)) {
+ hitsWall = YES;
}
+ if(hitsWall) [gameplay playSound:kSoundBallOnWall];
+
+ soundDelayFrames = 10;
}
- }
- }
-
- if(soundDelayFrames > 0) soundDelayFrames--;
-
- std::vector<MyContact>::iterator pos;
- BOOL hitsWall = NO;
- for(pos = contactListener->_contacts.begin(); pos != contactListener->_contacts.end(); ++pos) {
- MyContact contact = *pos;
-
- if(soundDelayFrames <= 0) {
-
- //
- if ((contact.fixtureA == ballLayer.fixture && contact.fixtureB == gameBoxLayer.leftEdge) ||
- (contact.fixtureA == gameBoxLayer.leftEdge && contact.fixtureB == ballLayer.fixture)) {
- hitsWall = YES;
- }
-
- if ((contact.fixtureA == ballLayer.fixture && contact.fixtureB == gameBoxLayer.topEdge) ||
- (contact.fixtureA == gameBoxLayer.topEdge && contact.fixtureB == ballLayer.fixture)) {
- hitsWall = YES;
- }
-
- if ((contact.fixtureA == ballLayer.fixture && contact.fixtureB == gameBoxLayer.rightEdge) ||
- (contact.fixtureA == gameBoxLayer.rightEdge && contact.fixtureB == ballLayer.fixture)) {
- hitsWall = YES;
- }
-
- if ((contact.fixtureA == ballLayer.fixture && contact.fixtureB == gameBoxLayer.bottomEdge) ||
- (contact.fixtureA == gameBoxLayer.bottomEdge && contact.fixtureB == ballLayer.fixture)) {
- hitsWall = YES;
- }
- if(hitsWall) [gameplay playSound:kSoundBallOnWall];
-
- soundDelayFrames = 10;
- }
-
-
+ }
}
}
View
7 PeerPong/PeerPongGameplay.mm
@@ -199,6 +199,13 @@ -(PeerPongPlayer *)addPlayerWithHello:(Hello *)aHello andSocket:(GCDAsyncSocket*
}
-(PeerPongPlayer *)movePlayerFromQueueToSlot:(NSNumber *)aSlot WithSocket:(GCDAsyncSocket *)aSocket {
+ NSNumber *nextFreeSlot = self.getFreeSlotForPlayer;
+
+ if([nextFreeSlot intValue] != [aSlot intValue]) {
+ NSLog(@"BUG: Multiple players for one slot received! aSlot: %d, NextFreeSlot: %d", [aSlot intValue], [nextFreeSlot intValue]);
+ aSlot = nextFreeSlot;
+ }
+
PeerPongPlayer *player = [self getPlayerSlotBySocket:aSocket];
player.slot = aSlot;
[self.players setObject:player forKey:aSlot];
View
BIN  PeerPong/peer_pong_intro_128k.mp3
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.