📄 ttrapp.m
字号:
NSLog(@"Playing song %@, tapfile %@", s, t); [self playGameWithSong: s tapFile: t]; } if ([trig hasPrefix: @"recordsong "]) { NSString * s = [trig substringFromIndex: 11]; NSString * t = [NSString stringWithFormat: @"/var/root/Media/TTR/Taps/%@.tap", [[s substringToIndex: [s length] - 4] lastPathComponent]]; NSLog(@"Recording song %@, tapfile %@", s, t); [self recordSong: s toFile: t]; } break; case msDownloading: if ([trig isEqualToString: @"songdownloaded"]) { [self displayTracksMenu]; } break; }}- (void) downloadSong: (NSString *) songName { NSLog(@"Gonna download %@", songName); NSString * s = [songName stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]; NSString * su = [NSString stringWithFormat: @"http://ibrickr.com/ttr/get.php?song=%@&type=song", s]; NSString * tu = [NSString stringWithFormat: @"http://ibrickr.com/ttr/get.php?song=%@&type=tap", s]; NSString * sf = [NSString stringWithFormat: @"/var/root/Media/TTR/Music/%@.mp3", songName]; NSString * tf = [NSString stringWithFormat: @"/var/root/Media/TTR/Taps/%@.tap", songName]; songDownload = [Downloader downloadURL: su to: sf app: self trigger: @"songdownloaded"]; tapDownload = [Downloader downloadURL: tu to: tf app: self trigger: @"tapdownloaded"]; [self displayDownloader];}- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(unsigned)length { NSLog(@"Download object received %d bytes", length);}- (void)downloadDidFinish:(NSURLDownload *)download { NSLog(@"Download finished");}- (void) clearButtons { while ([buttons count]) { LabelButton * lb = [buttons objectAtIndex: 0]; [lb removeFromSuperview]; [buttons removeObjectAtIndex: 0]; } [self fireWorksBurstAtX: 91 Y: 260]; [self fireWorksBurstAtX: 252 Y: 334];}- (void) addButton: (LabelButton *)lb { [menuView addSubview: lb]; [buttons addObject: lb];}- (void) gameInterface { [menuView removeFromSuperview]; [mainView addSubview: gameView]; [mainView bringSubviewToFront: effectsView];}- (void) menuInterface { [gameView removeFromSuperview]; [mainView addSubview: menuView]; [mainView bringSubviewToFront: effectsView];}- (void) playGameWithSong: (NSString *)filename tapFile: (NSString *) tapfile { [self fireWorksBurstAtX: 160 Y: 240]; songStarted = NO; songFinished = NO; recording = NO; tapindex = 0; badindex = 0; score = 0; [self gameInterface]; [self loadTapsFromFile: tapfile]; [self startPlayingSong: filename];}- (void) recordSong: (NSString *)filename toFile: (NSString *) tapfile { [self fireWorksBurstAtX: 160 Y: 240]; songStarted = NO; songFinished = NO; recording = YES; tapindex = 0; badindex = 0; tapcount = 0; score = 0; recordFile = [tapfile retain]; [self gameInterface]; [self startPlayingSong: filename];}- (void) startPlayingSong: (NSString *)filename { avc = [[AVController alloc] init];
avq = [[AVQueue alloc] init];
avi = [[AVItem alloc] initWithPath:filename error:&error];
[avq appendItem:avi error:&error];
[avc setQueue:avq];
[avc play:nil]; songStarted = YES; }- (int) columnForX: (float) x { if (x < 112) return 0; if (x > 212) return 2; return 1;}- (int) xForColumn: (int) x { if (x == 0) return 65; if (x == 1) return 160; if (x == 2) return 258; return 0;}- (void) loadTapsFromFile: (NSString *) filename { NSData * contents; contents = [NSData dataWithContentsOfFile: filename]; char * data = [contents bytes]; int len = [contents length]; char * c; tapcount = 0; while (c = strchr(data, '\n')) { [self prepareForAnotherTap]; *c = 0; if (sscanf(data, "%f, %f, %f", &taps[tapcount].x, &taps[tapcount].y, &taps[tapcount].t) == 3) { taps[tapcount].y = 400; taps[tapcount].column = [self columnForX: taps[tapcount].x]; taps[tapcount].x = [self xForColumn: taps[tapcount].column]; taps[tapcount].tapped = NO; tapcount++; } else { break; } len -= ((unsigned int)c - (unsigned int)data) + 1; data = c + 1; }}- (void) prepareForAnotherTap { if (tapcount == tapcapacity) { tapcapacity *= 2; songTap * na = (songTap *) malloc(sizeof(songTap) * tapcapacity); memcpy(na, taps, sizeof(songTap) * tapcount); free(taps); taps = na; }}- (void) recordTapAtX: (float) x Y: (float) y { double t = [avc currentTime]; [self prepareForAnotherTap]; taps[tapcount].t = t; taps[tapcount].x = x; taps[tapcount].y = y; tapcount++; NSLog(@"tap: %f, %f, %f, total: %d", x, y, (float) t, tapcount); }- (void) mouseClickAtX: (float) x Y: (float) y { if (recording) [self recordTapAtX: x Y: y]; else if (songStarted) { int column = [self columnForX: x]; double t = [avc currentTime]; //Search for the nearest matching spot int searchstart, searchend, i; float diff; songTap * s; int best_score = wrong_score, thisscore, best_index = -1; searchstart = searchend = tapindex; while (searchstart > 0 && (t - taps[searchstart].t) < bad_time) searchstart--; while (searchend < tapcount && (taps[searchend].t - t) < bad_time) searchend++; for (i = searchstart; i <= searchend; i++) { s = taps + i; if (s->column != column) continue; if (s->tapped) continue; diff = s->t - t; if (diff < 0) diff = -diff; thisscore = wrong_score; if (diff < bad_time) thisscore = bad_score; if (diff < ok_time) thisscore = ok_score; if (diff < good_time) thisscore = good_score; if (diff < perfect_time) thisscore = perfect_score; if (thisscore > best_score) { best_score = thisscore; best_index = i; } } [self scoreChange: best_score X: x Y: y]; if (best_index >= 0) taps[best_index].tapped = YES; } else { [self fireWorksBurstAtX: x Y: y]; }}- (void) scoreChange: (int) ds X: (float) x Y: (float) y { score += ds; [scoreLabel setText: [NSString stringWithFormat: @"Score: %d", score]]; NSLog(@"Score %d\t%d", ds, score); if (score > 0 && score % 5000 < ds) [self fireWorksBurstAtX: x Y: y]; UIImage * ri = nil; if (ds == wrong_score) ri = wrongImage; if (ds >= good_score) ri = goodImage; if (ds >= perfect_score) ri = perfectImage; if (ri != nil) { [self rewardWithImage: ri atX: x Y: y]; }}- (void) rewardWithImage: (UIImage *)img atX: (float)x Y: (float)y { [animatables addObject: [[Reward alloc] initWithImage: img X: x Y: y view: effectsView]]; }- (void) fireWorksBurstAtX: (float)x Y: (float) y { int i; for (i = 0; i < 20 && [animatables count] < 40; i++) { [animatables addObject: [[Particle alloc] initWithX: x Y: y view: effectsView]]; }}- (void) applicationWillSuspend{ }- (void)fireTimer:(NSTimer*)theTimer{ int i; if (!songStarted) {
} else if (!songFinished) { double d = [avc currentTime]; if (d == 0 && lastplayingtime > 0) { songFinished = YES; [self songHasFinished]; } lastplayingtime = d; if (!recording) [self animationTickAtTime: d]; //NSLog(@"Time: %f", (float)d); } //Animatables //NSLog(@"About to tick Animatables %d", [animatables count]); Animatable * an; for (i = 0; i < [animatables count]; i++) { an = [animatables objectAtIndex: i]; //NSLog(@"About to tick an Animatable %d", an); if (NO == [an tick]) { //NSLog(@"About to remove an Animatable"); [animatables removeObject: an]; i--; //NSLog(@"Done"); } } //NSLog(@"Done ticking Animatables %d", animatables); }- (void) animationTickAtTime: (double) d { int i; float posx, posy, scale, rotation; songTap * s; //Start at current time while (tapindex < tapcount && taps[tapindex].t < d) { glowlevels[taps[tapindex].column] = 1; glowlevels[3] = 1; tapindex++; } while (badindex < tapcount && taps[badindex].t < d - bad_time) { if (NO == taps[badindex].tapped) { [self scoreChange: wrong_score X: taps[badindex].x Y: taps[badindex].y]; } badindex++; } //Move back X many taps or X much time int backdex = tapindex + show_count; if (backdex > tapcount-1) backdex = tapcount - 1; //Display those taps for (i = 0; i < show_count; i++) { UIImageView * uiv = [sparkles objectAtIndex: i]; if (i + tapindex < tapcount) { s = taps + tapindex + i; posx = s->x; posy = s->y - ((s->t - d) * 480 / slide_time); scale = ((s->t - d) < 0.1 ? 2 - (s->t - d) * 10 : 1); rotation = d; [uiv setAlpha: ((s->t - d) < 0.1 ? (s->t - d) * 10 : 1)]; } else { posx = posy = -1000; scale = 1; rotation = 0; } [uiv setTransform: CGAffineTransformTranslate( CGAffineTransformRotate( CGAffineTransformScale( CGAffineTransformMakeTranslation(posx-45, posy-45), scale, scale) , rotation), 0, 0)]; } //Glows for (i = 0; i < 4; i++) { UIImageView * uiv = [glows objectAtIndex: i]; if (glowlevels[i] > 0.1) { posx = 0; posy = 0; if (i <= 2) posx = i * 100; else posy = 360; [uiv setTransform: CGAffineTransformMakeTranslation(posx, posy)]; [uiv setAlpha: glowlevels[i]]; } else { [uiv setTransform: CGAffineTransformMakeTranslation(-1000, -1000)]; } glowlevels[i] *= 0.9; } }- (void)songHasFinished { NSLog(@"Song finished"); if (recording) { int i; NSMutableString * s = [NSMutableString stringWithCapacity: 1024]; for (i = 0; i < tapcount; i++) { NSLog(@"Saving %d (%f, %f, %f)", i, taps[i].x, taps[i].y, taps[i].t); [s appendFormat: @"%f, %f, %f\n", taps[i].x, taps[i].y, taps[i].t]; } [s writeToFile: recordFile atomically: YES encoding: NSASCIIStringEncoding error: &error]; NSLog(@"Saved"); } songFinished = NO; songStarted = NO; [self menuInterface]; [self displayMainMenu];}@end double objc_msgSend_fpret(id self, SEL op, ...)
{
struct objc_method * method = class_getInstanceMethod(self->isa, op);
int numArgs = method_getNumberOfArguments(method);
if(numArgs == 2) {
double (*imp)(id, SEL);
imp = (double (*)(id, SEL))method->method_imp;
return imp(self, op);
} else if(numArgs == 3) {
va_list ap;
va_start(ap, op);
double (*imp)(id, SEL, void *);
imp = (double (*)(id, SEL, void *))method->method_imp;
return imp(self, op, va_arg(ap, void *));
}
fprintf(stderr,
"ERROR: objc_msgSend_fpret called on <%s %p> with selector %s had to return 0.0\n", object_getClassName(self),
self, sel_getName(op));
return 0.0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -