diff options
author | Starla Insigna <starla4444@gmail.com> | 2011-10-17 20:57:46 -0400 |
---|---|---|
committer | Starla Insigna <starla4444@gmail.com> | 2011-10-17 20:57:46 -0400 |
commit | 08cb72a92328cb0f38947fe5e8c5f23bab8cd0cc (patch) | |
tree | 7f08db53abebda90785398911a6ed2f08c972051 /Classes | |
parent | 4d09a715397de91a66f3fbea0fe2b629f64b6e9a (diff) | |
download | cartcollect-9092687f7e8b6675e31995535ee181cdad20109e.tar.gz cartcollect-9092687f7e8b6675e31995535ee181cdad20109e.tar.bz2 cartcollect-9092687f7e8b6675e31995535ee181cdad20109e.zip |
Added support for gyroscope-based movement v0.4.8
Closes #230
Diffstat (limited to 'Classes')
-rw-r--r-- | Classes/Cart.h | 2 | ||||
-rw-r--r-- | Classes/Cart.m | 13 | ||||
-rw-r--r-- | Classes/GameMode.h | 6 | ||||
-rw-r--r-- | Classes/GameMode.m | 63 | ||||
-rw-r--r-- | Classes/JumpGameMode.m | 14 |
5 files changed, 79 insertions, 19 deletions
diff --git a/Classes/Cart.h b/Classes/Cart.h index 1058025..af463f2 100644 --- a/Classes/Cart.h +++ b/Classes/Cart.h | |||
@@ -26,6 +26,6 @@ | |||
26 | @property (assign) BOOL boundedByScreen; | 26 | @property (assign) BOOL boundedByScreen; |
27 | - (id)initWithSprite:(CCSprite*)sprite; | 27 | - (id)initWithSprite:(CCSprite*)sprite; |
28 | - (void)tick; | 28 | - (void)tick; |
29 | - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration; | 29 | - (void)deviceDidRotate:(double)pitch; |
30 | 30 | ||
31 | @end | 31 | @end |
diff --git a/Classes/Cart.m b/Classes/Cart.m index 979226b..f9da181 100644 --- a/Classes/Cart.m +++ b/Classes/Cart.m | |||
@@ -62,15 +62,14 @@ | |||
62 | } | 62 | } |
63 | } | 63 | } |
64 | 64 | ||
65 | - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration | 65 | #define kFilterFactor 0.05f |
66 | |||
67 | - (void)deviceDidRotate:(double)pitch | ||
66 | { | 68 | { |
67 | static float prevY=0; | 69 | static float prevY=0; |
68 | 70 | float accelY = -((float) pitch * kFilterFactor + (1- kFilterFactor)*prevY); | |
69 | #define kFilterFactor 0.05f | 71 | |
70 | 72 | prevY = accelY; | |
71 | float accelY = -((float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY); | ||
72 | |||
73 | prevY = accelY; | ||
74 | accelX = accelY * 750; | 73 | accelX = accelY * 750; |
75 | } | 74 | } |
76 | 75 | ||
diff --git a/Classes/GameMode.h b/Classes/GameMode.h index e82ad10..2fcf275 100644 --- a/Classes/GameMode.h +++ b/Classes/GameMode.h | |||
@@ -8,6 +8,7 @@ | |||
8 | 8 | ||
9 | #import "CCLayer.h" | 9 | #import "CCLayer.h" |
10 | #import "Cart.h" | 10 | #import "Cart.h" |
11 | #import <CoreMotion/CoreMotion.h> | ||
11 | 12 | ||
12 | #define GAME_SCENE 436 | 13 | #define GAME_SCENE 436 |
13 | #define GAME_LAYER 437 | 14 | #define GAME_LAYER 437 |
@@ -28,6 +29,10 @@ | |||
28 | void (^delayedAction)(void); | 29 | void (^delayedAction)(void); |
29 | 30 | ||
30 | BOOL isPaused; | 31 | BOOL isPaused; |
32 | |||
33 | BOOL hasGyroscope; | ||
34 | double pitch; | ||
35 | CMMotionManager* motionManager; | ||
31 | } | 36 | } |
32 | 37 | ||
33 | @property (readonly) Cart* cart; | 38 | @property (readonly) Cart* cart; |
@@ -43,5 +48,6 @@ | |||
43 | - (void)mainmenu; | 48 | - (void)mainmenu; |
44 | - (void)scheduleDelayedAction:(void(^)(void))delayedAction delay:(float)delay; | 49 | - (void)scheduleDelayedAction:(void(^)(void))delayedAction delay:(float)delay; |
45 | - (void)runDelayedAction; | 50 | - (void)runDelayedAction; |
51 | - (void)setPitch:(double)m_pitch; | ||
46 | 52 | ||
47 | @end | 53 | @end |
diff --git a/Classes/GameMode.m b/Classes/GameMode.m index 1e80237..e45ddfe 100644 --- a/Classes/GameMode.m +++ b/Classes/GameMode.m | |||
@@ -33,8 +33,6 @@ | |||
33 | 33 | ||
34 | if (nil != self) | 34 | if (nil != self) |
35 | { | 35 | { |
36 | isAccelerometerEnabled_ = YES; | ||
37 | |||
38 | objects = [[NSMutableSet alloc] init]; | 36 | objects = [[NSMutableSet alloc] init]; |
39 | 37 | ||
40 | cart = [[Cart alloc] initWithSprite:[CCSprite spriteWithFile:@"cart.png"]]; | 38 | cart = [[Cart alloc] initWithSprite:[CCSprite spriteWithFile:@"cart.png"]]; |
@@ -64,6 +62,26 @@ | |||
64 | } | 62 | } |
65 | 63 | ||
66 | isPaused = NO; | 64 | isPaused = NO; |
65 | |||
66 | Class cmClass = (NSClassFromString(@"CMMotionManager")); | ||
67 | if (cmClass) | ||
68 | { | ||
69 | motionManager = [[CMMotionManager alloc] init]; | ||
70 | |||
71 | if (motionManager.gyroAvailable) | ||
72 | { | ||
73 | [motionManager setDeviceMotionUpdateInterval:1.0f/60.0f]; | ||
74 | isAccelerometerEnabled_ = NO; | ||
75 | hasGyroscope = YES; | ||
76 | } else { | ||
77 | isAccelerometerEnabled_ = YES; | ||
78 | motionManager = nil; | ||
79 | hasGyroscope = NO; | ||
80 | } | ||
81 | } else { | ||
82 | isAccelerometerEnabled_ = YES; | ||
83 | hasGyroscope = NO; | ||
84 | } | ||
67 | } | 85 | } |
68 | 86 | ||
69 | return self; | 87 | return self; |
@@ -71,19 +89,39 @@ | |||
71 | 89 | ||
72 | - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration | 90 | - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration |
73 | { | 91 | { |
74 | [cart accelerometer:accelerometer didAccelerate:acceleration]; | 92 | self.pitch = acceleration.y; |
75 | } | 93 | } |
76 | 94 | ||
77 | - (void)onEnterTransitionDidFinish | 95 | - (void)onEnterTransitionDidFinish |
78 | { | 96 | { |
79 | [super onEnterTransitionDidFinish]; | 97 | [super onEnterTransitionDidFinish]; |
80 | 98 | ||
81 | [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)]; | 99 | if (hasGyroscope) |
100 | { | ||
101 | [motionManager startDeviceMotionUpdates]; | ||
102 | } else { | ||
103 | [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)]; | ||
104 | } | ||
105 | |||
82 | [self schedule:@selector(tick:) interval:1.0f/60.0f]; | 106 | [self schedule:@selector(tick:) interval:1.0f/60.0f]; |
83 | } | 107 | } |
84 | 108 | ||
109 | - (void)onExit | ||
110 | { | ||
111 | if (hasGyroscope) | ||
112 | { | ||
113 | [motionManager stopDeviceMotionUpdates]; | ||
114 | } | ||
115 | } | ||
116 | |||
85 | - (void)tick:(ccTime)dt | 117 | - (void)tick:(ccTime)dt |
86 | { | 118 | { |
119 | if (hasGyroscope) | ||
120 | { | ||
121 | CMDeviceMotion* motion = [motionManager deviceMotion]; | ||
122 | self.pitch = -motion.attitude.pitch; | ||
123 | } | ||
124 | |||
87 | [cart tick]; | 125 | [cart tick]; |
88 | 126 | ||
89 | NSMutableSet* discardedObjects = [NSMutableSet set]; | 127 | NSMutableSet* discardedObjects = [NSMutableSet set]; |
@@ -111,6 +149,11 @@ | |||
111 | 149 | ||
112 | [self pauseSchedulerAndActions]; | 150 | [self pauseSchedulerAndActions]; |
113 | 151 | ||
152 | if (hasGyroscope) | ||
153 | { | ||
154 | [motionManager stopDeviceMotionUpdates]; | ||
155 | } | ||
156 | |||
114 | shadedLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)]; | 157 | shadedLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)]; |
115 | [[[CCDirector sharedDirector] runningScene] addChild:shadedLayer]; | 158 | [[[CCDirector sharedDirector] runningScene] addChild:shadedLayer]; |
116 | 159 | ||
@@ -143,6 +186,11 @@ | |||
143 | shadedLayer = nil; | 186 | shadedLayer = nil; |
144 | pauseLayer = nil; | 187 | pauseLayer = nil; |
145 | 188 | ||
189 | if (hasGyroscope) | ||
190 | { | ||
191 | [motionManager startDeviceMotionUpdates]; | ||
192 | } | ||
193 | |||
146 | [self resumeSchedulerAndActions]; | 194 | [self resumeSchedulerAndActions]; |
147 | 195 | ||
148 | isPaused = NO; | 196 | isPaused = NO; |
@@ -211,6 +259,13 @@ | |||
211 | } | 259 | } |
212 | } | 260 | } |
213 | 261 | ||
262 | - (void)setPitch:(double)m_pitch | ||
263 | { | ||
264 | pitch = m_pitch; | ||
265 | |||
266 | [cart deviceDidRotate:pitch]; | ||
267 | } | ||
268 | |||
214 | - (void)dealloc | 269 | - (void)dealloc |
215 | { | 270 | { |
216 | [objects release]; | 271 | [objects release]; |
diff --git a/Classes/JumpGameMode.m b/Classes/JumpGameMode.m index b192058..eb552a2 100644 --- a/Classes/JumpGameMode.m +++ b/Classes/JumpGameMode.m | |||
@@ -288,13 +288,6 @@ | |||
288 | isGesturing = NO; | 288 | isGesturing = NO; |
289 | } | 289 | } |
290 | 290 | ||
291 | - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration | ||
292 | { | ||
293 | [super accelerometer:accelerometer didAccelerate:acceleration]; | ||
294 | |||
295 | expectedAngle = acceleration.y*M_PI_2; | ||
296 | } | ||
297 | |||
298 | - (int)cartShouldFall:(Cart *)m_cart | 291 | - (int)cartShouldFall:(Cart *)m_cart |
299 | { | 292 | { |
300 | int bottom = 0-m_cart.sprite.boundingBox.size.height/2; | 293 | int bottom = 0-m_cart.sprite.boundingBox.size.height/2; |
@@ -500,6 +493,13 @@ | |||
500 | } | 493 | } |
501 | } | 494 | } |
502 | 495 | ||
496 | - (void)setPitch:(double)m_pitch | ||
497 | { | ||
498 | [super setPitch:m_pitch]; | ||
499 | |||
500 | expectedAngle = pitch*M_PI_2; | ||
501 | } | ||
502 | |||
503 | @end | 503 | @end |
504 | 504 | ||
505 | @implementation LedgeFactory | 505 | @implementation LedgeFactory |