diff options
Diffstat (limited to 'Classes/GameMode.m')
-rw-r--r-- | Classes/GameMode.m | 63 |
1 files changed, 59 insertions, 4 deletions
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]; |