Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Last active December 18, 2015 00:19
Show Gist options
  • Save gregtemp/5695743 to your computer and use it in GitHub Desktop.
Save gregtemp/5695743 to your computer and use it in GitHub Desktop.
Simple Match Game Example
//
// C4WorkSpace.m
// Examples
//
// Created by Greg Debicki.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace {
BOOL s0Bool, s1Bool, s2Bool, s3Bool, s4Bool, s5Bool;
NSMutableArray *shapes, *images, *masks;
}
-(void)setup {
shapes = [@[] mutableCopy];
images = [@[] mutableCopy];
masks = [@[] mutableCopy];
[self setupMasks];
[self setupImages];
[self placeImages];
[self setupShapes];
[self setupGestures];
}
-(void) setupMasks {
CGRect rect = CGRectMake(0, 0, 150, 150);
for(int i = 0; i < 6; i ++) {
C4Shape *m = [C4Shape ellipse:rect];
[masks addObject:m];
}
}
-(void) setupImages {
C4Image *i0 = [C4Image imageNamed:@"Photo1.jpg"];
C4Image *i1 = [C4Image imageNamed:@"Photo1.jpg"];
C4Image *i2 = [C4Image imageNamed:@"Photo2.jpg"];
C4Image *i3 = [C4Image imageNamed:@"Photo2.jpg"];
C4Image *i4 = [C4Image imageNamed:@"Photo3.jpg"];
C4Image *i5 = [C4Image imageNamed:@"Photo3.jpg"];
[images addObject:i0];
[images addObject:i1];
[images addObject:i2];
[images addObject:i3];
[images addObject:i4];
[images addObject:i5];
for (int i = 0; i < 6; i++) {
C4Image *img = (C4Image *)images[i];
img.width *= 0.5f;
C4Shape *m = (C4Shape *)masks[i];
m.center = img.center;
img.mask = m;
}
}
-(void) placeImages {
NSInteger count = 0;
NSInteger order[6] = {2,3,5,0,4,1};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++){
CGPoint currentCenter = self.canvas.center;
currentCenter.x -= 200;
currentCenter.x += j * 200;
currentCenter.y += i * 200;
C4Image *img = (C4Image *)images[order[count]];
img.center = currentCenter;
img.userInteractionEnabled = NO;
count++;
}
}
[self.canvas addObjects:images];
}
-(void) setupShapes {
CGRect rect = CGRectMake(0, 0, 150, 150);
for(int i = 0; i < 6; i ++) {
C4Shape *s = [C4Shape ellipse:rect];
C4Image *img = (C4Image *)images[i];
s.center = img.center;
[shapes addObject:s];
}
[self.canvas addObjects:shapes];
}
-(void) setupGestures {
for (int i = 0; i < 6; i++) {
[shapes[i] addGesture:TAP name:@"tap" action:@"tapped"];
}
[self listenFor:@"tapped" fromObject:shapes[0] andRunMethod:@"s0On"];
[self listenFor:@"tapped" fromObject:shapes[1] andRunMethod:@"s1On"];
[self listenFor:@"tapped" fromObject:shapes[2] andRunMethod:@"s2On"];
[self listenFor:@"tapped" fromObject:shapes[3] andRunMethod:@"s3On"];
[self listenFor:@"tapped" fromObject:shapes[4] andRunMethod:@"s4On"];
[self listenFor:@"tapped" fromObject:shapes[5] andRunMethod:@"s5On"];
}
-(void) s0On {
C4Shape *s = (C4Shape *)shapes[0];
s.animationDuration = 0.2f;
s.alpha = 0.0f;
s0Bool = YES;
[self runMethod:@"s0Off" afterDelay:1.0f];
}
-(void) s0Off {
C4Shape *s = (C4Shape *)shapes[0];
if(s1Bool != YES) {
s.alpha = 1.0f;
s0Bool = NO;
}
[self runMethod:@"didWin" afterDelay:2.0f];
}
-(void) s1On {
C4Shape *s = (C4Shape *)shapes[1];
s.animationDuration = 0.2f;
s.alpha = 0.0f;
s1Bool = YES;
[self runMethod:@"s1Off" afterDelay:1.0f];
}
-(void) s1Off {
C4Shape *s = (C4Shape *)shapes[1];
if(s0Bool != YES) {
s.alpha = 1.0f;
s1Bool = NO;
}
[self runMethod:@"didWin" afterDelay:2.0f];
}
-(void) s2On {
C4Shape *s = (C4Shape *)shapes[2];
s.animationDuration = 0.2f;
s.alpha = 0.0f;
s2Bool = YES;
[self runMethod:@"s2Off" afterDelay:1.0f];
}
-(void) s2Off {
C4Shape *s = (C4Shape *)shapes[2];
if(s3Bool != YES) {
s.alpha = 1.0f;
s2Bool = NO;
}
[self runMethod:@"didWin" afterDelay:2.0f];
}
-(void) s3On {
C4Shape *s = (C4Shape *)shapes[3];
s.animationDuration = 0.2f;
s.alpha = 0.0f;
s3Bool = YES;
[self runMethod:@"s3Off" afterDelay:1.0f];
}
-(void) s3Off {
C4Shape *s = (C4Shape *)shapes[3];
if(s2Bool != YES) {
s.alpha = 1.0f;
s3Bool = NO;
}
[self runMethod:@"didWin" afterDelay:2.0f];
}
-(void) s4On {
C4Shape *s = (C4Shape *)shapes[4];
s.animationDuration = 0.2f;
s.alpha = 0.0f;
s4Bool = YES;
[self runMethod:@"s4Off" afterDelay:1.0f];
}
-(void) s4Off {
C4Shape *s = (C4Shape *)shapes[4];
if(s5Bool != YES) {
s.alpha = 1.0f;
s4Bool = NO;
}
[self runMethod:@"didWin" afterDelay:2.0f];
}
-(void) s5On {
C4Shape *s = (C4Shape *)shapes[5];
s.animationDuration = 0.2f;
s.alpha = 0.0f;
s5Bool = YES;
[self runMethod:@"s5Off" afterDelay:1.0f];
}
-(void) s5Off {
C4Shape *s = (C4Shape *)shapes[5];
if(s4Bool != YES) {
s.alpha = 1.0f;
s5Bool = NO;
}
[self runMethod:@"didWin" afterDelay:2.0f];
}
-(void) didWin {
if (s0Bool==YES && s1Bool==YES && s2Bool==YES &&
s3Bool==YES && s4Bool==YES && s5Bool==YES) {
for (int i = 0; i < 6; i ++) {
C4Shape *s = (C4Shape *)shapes[i];
s.animationDuration = 2.0f;
s.alpha = 1.0f;
}
s0Bool = NO;
s1Bool = NO;
s2Bool = NO;
s3Bool = NO;
s4Bool = NO;
s5Bool = NO;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment