added sound to pong and fixed some bugs
This commit is contained in:
parent
c7919309e9
commit
ac4036dfc8
15 changed files with 1373 additions and 111 deletions
|
@ -11,7 +11,7 @@
|
|||
|
||||
@implementation GGSNetwork
|
||||
|
||||
#define GGS_HOST @"localhost"
|
||||
#define GGS_HOST @"home.jeena.net"
|
||||
#define GGS_PORT 9000
|
||||
#define NO_TIMEOUT -1
|
||||
|
||||
|
@ -37,6 +37,7 @@
|
|||
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
|
||||
|
||||
[asyncSocket connectToHost:GGS_HOST onPort:GGS_PORT error:nil];
|
||||
|
||||
[asyncSocket readDataToData:HEADER_DELIMITER withTimeout:NO_TIMEOUT tag:HEAD];
|
||||
}
|
||||
|
||||
|
@ -61,12 +62,11 @@
|
|||
}
|
||||
|
||||
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
|
||||
|
||||
|
||||
}
|
||||
|
||||
- (void)onSocket:(AsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag {
|
||||
|
||||
|
||||
if (tag == HEAD) {
|
||||
[self parseAndSetHeader:data];
|
||||
|
||||
|
|
|
@ -57,6 +57,7 @@
|
|||
/*
|
||||
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
|
||||
*/
|
||||
[(PongViewController *)viewController restart];
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#import <UIKit/UIKit.h>
|
||||
#import "GGSDelegate.h"
|
||||
#import "GGSNetwork.h"
|
||||
#import <AVFoundation/AVAudioPlayer.h>
|
||||
|
||||
enum GameType {
|
||||
kGameTypeSinglePlayer = 0,
|
||||
|
@ -28,6 +29,11 @@ enum GameType {
|
|||
IBOutlet UILabel *pointsP2;
|
||||
|
||||
GGSNetwork *ggsNetwork;
|
||||
|
||||
AVAudioPlayer *pingSound;
|
||||
AVAudioPlayer *pongSound;
|
||||
AVAudioPlayer *lostSound;
|
||||
|
||||
}
|
||||
|
||||
@property (nonatomic, retain) IBOutlet UIView *ballView;
|
||||
|
@ -40,6 +46,8 @@ enum GameType {
|
|||
|
||||
@property (nonatomic, retain) GGSNetwork *ggsNetwork;
|
||||
|
||||
- (void)restart;
|
||||
|
||||
- (void)startPositions;
|
||||
- (void)zeroPoints;
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
//
|
||||
|
||||
#import "PongViewController.h"
|
||||
#import "GGSNetwork.h"
|
||||
|
||||
@implementation PongViewController
|
||||
|
||||
|
@ -18,8 +17,8 @@
|
|||
#define WIDTH 480
|
||||
#define HEIGHT 320
|
||||
|
||||
#define TOX(x) ( WIDTH / 100 * (x))
|
||||
#define TOY(y) ( HEIGHT / 100 * (y))
|
||||
#define TOX(x) ( 4.8 * x )
|
||||
#define TOY(y) ( 3.2 * y )
|
||||
|
||||
@synthesize ballView, player1View, player2View, tapToBegin, pointsP1, pointsP2, ggsNetwork;
|
||||
|
||||
|
@ -49,32 +48,90 @@
|
|||
#pragma mark GGSNetwork Delegate
|
||||
|
||||
- (void)GGSNetwork:(GGSNetwork *)_ggsNetwork ready:(BOOL)ready {
|
||||
[ggsNetwork define:@"function playerCommand(user, command, args) { user.sendCommand(command, args); }"];
|
||||
[ggsNetwork sendCommand:@"ready" withArgs:@""];
|
||||
}
|
||||
|
||||
- (void)GGSNetwork:(GGSNetwork *)_ggsNetwork defined:(BOOL)defined {
|
||||
if (defined) {
|
||||
[ggsNetwork sendCommand:@"ready" withArgs:@""];
|
||||
} else {
|
||||
NSLog(@"Not defined");
|
||||
}
|
||||
|
||||
// do nothing.
|
||||
}
|
||||
|
||||
- (void)GGSNetwork:(GGSNetwork *)_ggsNetwork receivedCommand:(NSString *)command withArgs:(NSString *)args {
|
||||
NSLog(@"Command: %@; Args: %@", command, args);
|
||||
|
||||
if ([command isEqualToString:@"ball"]) {
|
||||
NSArray *ball = [args componentsSeparatedByString:@","];
|
||||
ballView.center = CGPointMake([[ball objectAtIndex:0] intValue], [[ball objectAtIndex:1] intValue]);
|
||||
[UIView beginAnimations:NULL context:NULL];
|
||||
CGFloat x = [[ball objectAtIndex:0] floatValue];
|
||||
CGFloat y = [[ball objectAtIndex:1] floatValue];
|
||||
ballView.center = CGPointMake(TOX(x), TOY(y));
|
||||
[UIView commitAnimations];
|
||||
|
||||
} else if ([command isEqualToString:@"player1_y"]) {
|
||||
player1View.center = CGPointMake(20, TOY([args intValue]));
|
||||
|
||||
[UIView beginAnimations:NULL context:NULL];
|
||||
player1View.center = CGPointMake(25, TOY([args floatValue]));
|
||||
[UIView commitAnimations];
|
||||
|
||||
} else if ([command isEqualToString:@"player2_y"]) {
|
||||
player2View.center = CGPointMake(WIDTH - 40, TOY([args intValue]));
|
||||
|
||||
[UIView beginAnimations:NULL context:NULL];
|
||||
player2View.center = CGPointMake(WIDTH - 35, TOY([args floatValue]));
|
||||
[UIView commitAnimations];
|
||||
|
||||
} else if ([command isEqualToString:@"player1_points"]) {
|
||||
|
||||
pointsP1.text = args;
|
||||
gamePaused = YES;
|
||||
[lostSound play];
|
||||
|
||||
} else if ([command isEqualToString:@"player2_points"]) {
|
||||
|
||||
pointsP2.text = args;
|
||||
gamePaused = YES;
|
||||
[lostSound play];
|
||||
|
||||
} else if ([command isEqualToString:@"game"]) {
|
||||
|
||||
if ([args isEqualToString:@"wait"]) {
|
||||
|
||||
NSLog(@"Other ready");
|
||||
|
||||
} else if ([args isEqualToString:@"start"]) {
|
||||
|
||||
gamePaused = NO;
|
||||
|
||||
}
|
||||
} else if ([command isEqualToString:@"welcome"]) {
|
||||
if ([args isEqualToString:@"1"]) {
|
||||
player1View.backgroundColor = [UIColor redColor];
|
||||
} else {
|
||||
player2View.backgroundColor = [UIColor redColor];
|
||||
}
|
||||
|
||||
} else if ([command isEqualToString:@"sound"]) {
|
||||
if ([args isEqualToString:@"ping"]) {
|
||||
[pingSound play];
|
||||
} else {
|
||||
[pongSound play];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Input
|
||||
|
||||
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
|
||||
if (gamePaused) {
|
||||
[ggsNetwork sendCommand:@"start" withArgs:@""];
|
||||
tapToBegin.hidden = YES;
|
||||
} else {
|
||||
CGPoint point = [[[touches allObjects] objectAtIndex:0] locationInView:self.view];
|
||||
if (point.y > (HEIGHT / 2)) {
|
||||
[ggsNetwork sendCommand:@"down" withArgs:@""];
|
||||
} else {
|
||||
[ggsNetwork sendCommand:@"up" withArgs:@""];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,12 +139,35 @@
|
|||
#pragma mark -
|
||||
#pragma mark View
|
||||
|
||||
- (void)restart {
|
||||
player1View.backgroundColor = [UIColor whiteColor];
|
||||
player2View.backgroundColor = [UIColor whiteColor];
|
||||
pointsP1.text = @"0";
|
||||
pointsP2.text = @"0";
|
||||
self.ggsNetwork = [[GGSNetwork alloc] initWithDelegate:self];
|
||||
gamePaused = YES;
|
||||
tapToBegin.hidden = NO;
|
||||
}
|
||||
|
||||
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
|
||||
ggsNetwork = [[GGSNetwork alloc] initWithDelegate:self];
|
||||
NSString *path = [[NSBundle mainBundle] pathForResource:@"ping" ofType:@"wav"];
|
||||
pingSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
|
||||
[pingSound play];
|
||||
|
||||
path = [[NSBundle mainBundle] pathForResource:@"pong" ofType:@"wav"];
|
||||
pongSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
|
||||
[pongSound play];
|
||||
|
||||
path = [[NSBundle mainBundle] pathForResource:@"lost" ofType:@"wav"];
|
||||
lostSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
|
||||
[lostSound play];
|
||||
|
||||
|
||||
//ggsNetwork = [[GGSNetwork alloc] initWithDelegate:self];
|
||||
|
||||
gamePaused = YES;
|
||||
//[self startPositions];
|
||||
//[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moveBall) userInfo:nil repeats:YES];
|
||||
|
@ -254,6 +334,10 @@
|
|||
[pointsP2 release];
|
||||
[ggsNetwork release];
|
||||
|
||||
[pingSound release];
|
||||
[pongSound release];
|
||||
[lostSound release];
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
|
|
|
@ -326,19 +326,19 @@
|
|||
<key>_historyCapacity</key>
|
||||
<integer>0</integer>
|
||||
<key>bookmark</key>
|
||||
<string>1F369E7B1323081C004E7A99</string>
|
||||
<string>1F36A0221323417E004E7A99</string>
|
||||
<key>history</key>
|
||||
<array>
|
||||
<string>1FBEBF151319F5C1006D5497</string>
|
||||
<string>1FBEC001131AA71C006D5497</string>
|
||||
<string>1FBEC002131AA71C006D5497</string>
|
||||
<string>1FBEC05C131B085D006D5497</string>
|
||||
<string>1FBEC05D131B085D006D5497</string>
|
||||
<string>1F369E761323081C004E7A99</string>
|
||||
<string>1F369E771323081C004E7A99</string>
|
||||
<string>1F369E781323081C004E7A99</string>
|
||||
<string>1F369E791323081C004E7A99</string>
|
||||
<string>1F369E7A1323081C004E7A99</string>
|
||||
<string>1F369ED81323101D004E7A99</string>
|
||||
<string>1F369F33132317A8004E7A99</string>
|
||||
<string>1F369F6C13232750004E7A99</string>
|
||||
<string>1F369FE61323395B004E7A99</string>
|
||||
<string>1F36A0201323417E004E7A99</string>
|
||||
<string>1F36A0211323417E004E7A99</string>
|
||||
</array>
|
||||
</dict>
|
||||
<key>SplitCount</key>
|
||||
|
@ -536,7 +536,7 @@
|
|||
<integer>5</integer>
|
||||
<key>WindowOrderList</key>
|
||||
<array>
|
||||
<string>1F369E861323081C004E7A99</string>
|
||||
<string>1F369E8E13230953004E7A99</string>
|
||||
<string>1F369E871323081C004E7A99</string>
|
||||
<string>1CD10A99069EF8BA00B06720</string>
|
||||
<string>1FA056A412F0B540003F1373</string>
|
||||
|
@ -665,8 +665,8 @@
|
|||
<string>yes</string>
|
||||
<key>sizes</key>
|
||||
<array>
|
||||
<string>{{0, 0}, {316, 201}}</string>
|
||||
<string>{{316, 0}, {378, 201}}</string>
|
||||
<string>{{0, 0}, {357, 449}}</string>
|
||||
<string>{{357, 0}, {428, 449}}</string>
|
||||
</array>
|
||||
</dict>
|
||||
<key>VerticalSplitView</key>
|
||||
|
@ -681,8 +681,8 @@
|
|||
<string>yes</string>
|
||||
<key>sizes</key>
|
||||
<array>
|
||||
<string>{{0, 0}, {694, 201}}</string>
|
||||
<string>{{0, 201}, {694, 180}}</string>
|
||||
<string>{{0, 0}, {785, 449}}</string>
|
||||
<string>{{0, 449}, {785, 403}}</string>
|
||||
</array>
|
||||
</dict>
|
||||
</dict>
|
||||
|
@ -702,7 +702,7 @@
|
|||
<key>DebugSTDIOWindowFrame</key>
|
||||
<string>{{200, 200}, {500, 300}}</string>
|
||||
<key>Frame</key>
|
||||
<string>{{0, 0}, {694, 381}}</string>
|
||||
<string>{{0, 0}, {785, 852}}</string>
|
||||
<key>PBXDebugSessionStackFrameViewKey</key>
|
||||
<dict>
|
||||
<key>DebugVariablesTableConfiguration</key>
|
||||
|
@ -712,24 +712,24 @@
|
|||
<string>Value</string>
|
||||
<real>85</real>
|
||||
<string>Summary</string>
|
||||
<real>148</real>
|
||||
<real>198</real>
|
||||
</array>
|
||||
<key>Frame</key>
|
||||
<string>{{316, 0}, {378, 201}}</string>
|
||||
<string>{{357, 0}, {428, 449}}</string>
|
||||
<key>RubberWindowFrame</key>
|
||||
<string>108 977 694 422 0 0 2560 1418 </string>
|
||||
<string>108 506 785 893 0 0 2560 1418 </string>
|
||||
</dict>
|
||||
<key>RubberWindowFrame</key>
|
||||
<string>108 977 694 422 0 0 2560 1418 </string>
|
||||
<string>108 506 785 893 0 0 2560 1418 </string>
|
||||
</dict>
|
||||
<key>Module</key>
|
||||
<string>PBXDebugSessionModule</string>
|
||||
<key>Proportion</key>
|
||||
<string>381pt</string>
|
||||
<string>852pt</string>
|
||||
</dict>
|
||||
</array>
|
||||
<key>Proportion</key>
|
||||
<string>381pt</string>
|
||||
<string>852pt</string>
|
||||
</dict>
|
||||
</array>
|
||||
<key>Name</key>
|
||||
|
@ -754,7 +754,7 @@
|
|||
<key>ToolbarConfiguration</key>
|
||||
<string>xcode.toolbar.config.debugV3</string>
|
||||
<key>WindowString</key>
|
||||
<string>108 977 694 422 0 0 2560 1418 </string>
|
||||
<string>108 506 785 893 0 0 2560 1418 </string>
|
||||
<key>WindowToolGUID</key>
|
||||
<string>1CD10A99069EF8BA00B06720</string>
|
||||
<key>WindowToolIsVisible</key>
|
||||
|
@ -880,18 +880,18 @@
|
|||
<key>GeometryConfiguration</key>
|
||||
<dict>
|
||||
<key>Frame</key>
|
||||
<string>{{0, 0}, {673, 481}}</string>
|
||||
<string>{{0, 0}, {724, 358}}</string>
|
||||
<key>RubberWindowFrame</key>
|
||||
<string>3600 653 673 522 2560 0 1920 1200 </string>
|
||||
<string>3598 801 724 399 2560 0 1920 1200 </string>
|
||||
</dict>
|
||||
<key>Module</key>
|
||||
<string>PBXDebugCLIModule</string>
|
||||
<key>Proportion</key>
|
||||
<string>481pt</string>
|
||||
<string>358pt</string>
|
||||
</dict>
|
||||
</array>
|
||||
<key>Proportion</key>
|
||||
<string>481pt</string>
|
||||
<string>358pt</string>
|
||||
</dict>
|
||||
</array>
|
||||
<key>Name</key>
|
||||
|
@ -911,7 +911,7 @@
|
|||
<key>ToolbarConfiguration</key>
|
||||
<string>xcode.toolbar.config.consoleV3</string>
|
||||
<key>WindowString</key>
|
||||
<string>3600 653 673 522 2560 0 1920 1200 </string>
|
||||
<string>3598 801 724 399 2560 0 1920 1200 </string>
|
||||
<key>WindowToolGUID</key>
|
||||
<string>1C78EAAD065D492600B07095</string>
|
||||
<key>WindowToolIsVisible</key>
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -11,6 +11,10 @@
|
|||
1D60589B0D05DD56006BFB54 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; };
|
||||
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
|
||||
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; };
|
||||
1F369FF813233C1E004E7A99 /* lost.wav in Resources */ = {isa = PBXBuildFile; fileRef = 1F369FF513233C1E004E7A99 /* lost.wav */; };
|
||||
1F369FF913233C1E004E7A99 /* ping.wav in Resources */ = {isa = PBXBuildFile; fileRef = 1F369FF613233C1E004E7A99 /* ping.wav */; };
|
||||
1F369FFA13233C1E004E7A99 /* pong.wav in Resources */ = {isa = PBXBuildFile; fileRef = 1F369FF713233C1E004E7A99 /* pong.wav */; };
|
||||
1F36A00713233CCC004E7A99 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1F36A00613233CCC004E7A99 /* AVFoundation.framework */; };
|
||||
1FBEBF481319FC56006D5497 /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FBEBF471319FC56006D5497 /* CFNetwork.framework */; };
|
||||
1FBEBF4D1319FCDE006D5497 /* AsyncSocket.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FBEBF4C1319FCDE006D5497 /* AsyncSocket.m */; };
|
||||
1FBEBFEF131A97F8006D5497 /* GGSNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FBEBFEE131A97F8006D5497 /* GGSNetwork.m */; };
|
||||
|
@ -26,6 +30,10 @@
|
|||
1D3623250D0F684500981E51 /* PongAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PongAppDelegate.m; sourceTree = "<group>"; };
|
||||
1D6058910D05DD3D006BFB54 /* Pong.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Pong.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
1DF5F4DF0D08C38300B7A737 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
|
||||
1F369FF513233C1E004E7A99 /* lost.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = lost.wav; sourceTree = "<group>"; };
|
||||
1F369FF613233C1E004E7A99 /* ping.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = ping.wav; sourceTree = "<group>"; };
|
||||
1F369FF713233C1E004E7A99 /* pong.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = pong.wav; sourceTree = "<group>"; };
|
||||
1F36A00613233CCC004E7A99 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
|
||||
1FBEBF471319FC56006D5497 /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; };
|
||||
1FBEBF4B1319FCDE006D5497 /* AsyncSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AsyncSocket.h; sourceTree = "<group>"; };
|
||||
1FBEBF4C1319FCDE006D5497 /* AsyncSocket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AsyncSocket.m; sourceTree = "<group>"; };
|
||||
|
@ -51,6 +59,7 @@
|
|||
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */,
|
||||
288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */,
|
||||
1FBEBF481319FC56006D5497 /* CFNetwork.framework in Frameworks */,
|
||||
1F36A00713233CCC004E7A99 /* AVFoundation.framework in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
@ -115,6 +124,9 @@
|
|||
children = (
|
||||
2899E5210DE3E06400AC0155 /* PongViewController.xib */,
|
||||
28AD733E0D9D9553002E5188 /* MainWindow.xib */,
|
||||
1F369FF513233C1E004E7A99 /* lost.wav */,
|
||||
1F369FF613233C1E004E7A99 /* ping.wav */,
|
||||
1F369FF713233C1E004E7A99 /* pong.wav */,
|
||||
8D1107310486CEB800E47090 /* Pong-Info.plist */,
|
||||
);
|
||||
name = Resources;
|
||||
|
@ -127,6 +139,7 @@
|
|||
1D30AB110D05D00D00671497 /* Foundation.framework */,
|
||||
288765A40DF7441C002DB57D /* CoreGraphics.framework */,
|
||||
1FBEBF471319FC56006D5497 /* CFNetwork.framework */,
|
||||
1F36A00613233CCC004E7A99 /* AVFoundation.framework */,
|
||||
);
|
||||
name = Frameworks;
|
||||
sourceTree = "<group>";
|
||||
|
@ -182,6 +195,9 @@
|
|||
files = (
|
||||
28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */,
|
||||
2899E5220DE3E06400AC0155 /* PongViewController.xib in Resources */,
|
||||
1F369FF813233C1E004E7A99 /* lost.wav in Resources */,
|
||||
1F369FF913233C1E004E7A99 /* ping.wav in Resources */,
|
||||
1F369FFA13233C1E004E7A99 /* pong.wav in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
BIN
games/Pong/lost.wav
Normal file
BIN
games/Pong/lost.wav
Normal file
Binary file not shown.
BIN
games/Pong/ping.wav
Normal file
BIN
games/Pong/ping.wav
Normal file
Binary file not shown.
BIN
games/Pong/pong.wav
Normal file
BIN
games/Pong/pong.wav
Normal file
Binary file not shown.
Reference in a new issue