-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLayer.mm
344 lines (273 loc) · 9.48 KB
/
GameLayer.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
// GameLayer.mm
// brendan game
//
// Created by Francis Tseng on 12/1/13.
// Copyright 2013 Francis Tseng. All rights reserved.
//
#import "GameLayer.h"
#include <stdlib.h>
enum {
kTagParentNode = 1,
ground = 2,
player = 3,
obstacle = 4
};
@implementation GameLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
GameLayer *layer = [GameLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init])) {
// enable events
self.touchEnabled = YES;
self.accelerometerEnabled = YES;
size = [CCDirector sharedDirector].winSize;
// add background
CCSprite* background = [CCSprite spriteWithFile:@"background.png"];
background.position = ccp(size.width/2, size.height/2);
[background setScaleX:size.width/background.contentSize.width];
[background setScaleY:size.height/background.contentSize.height];
[self addChild:background z:0];
// Scoring
score = 0;
scoreLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%i", score] fontName:@"Helvetica Neue" fontSize:32];
[scoreLabel setColor:ccc3(255,255,255)];
scoreLabel.position = ccp(size.width/2, size.height/2);
[self addChild:scoreLabel z:0];
// init physics
[self initPhysics];
// create player sprite (with physics!)
playerSprite = [CCPhysicsSprite spriteWithFile:@"player.png"];
float playerScale = 0.1;
CGPoint playerPos = ccp(size.width/2, playerSprite.contentSize.height * playerScale);
[playerSprite setScale:playerScale];
[self addChild:playerSprite];
playerSprite.tag = player;
[playerSprite setPTMRatio:PTM_RATIO];
[playerSprite setB2Body:[self setupBody:playerPos withSprite:playerSprite]];
playerSprite.position = playerPos; // have to set position up after the body
// collision listening
_contactListener = new MyContactListener();
world->SetContactListener(_contactListener);
//Set up sprite
#if 1
// Use batch node. Faster
CCSpriteBatchNode *parent = [CCSpriteBatchNode batchNodeWithFile:@"blocks.png" capacity:100];
spriteTexture_ = [parent texture];
#else
// doesn't use batch node. Slower
spriteTexture_ = [[CCTextureCache sharedTextureCache] addImage:@"blocks.png"];
CCNode *parent = [CCNode node];
#endif
[self addChild:parent z:0 tag:kTagParentNode];
[self scheduleUpdate];
// Spawn blocks regularly.
[self schedule:@selector(updateTime:) interval:1.0f];
}
return self;
}
-(void) dealloc
{
delete world;
world = NULL;
delete m_debugDraw;
m_debugDraw = NULL;
[super dealloc];
}
-(b2Body*) setupBody:(CGPoint)p withSprite:(CCSprite*)sprite
{
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
// body->SetGravityScale(0);
return body;
}
-(void) initPhysics
{
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
world = new b2World(gravity);
// Do we want to let bodies sleep?
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);
m_debugDraw = new GLESDebugDraw( PTM_RATIO );
world->SetDebugDraw(m_debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
// flags += b2Draw::e_jointBit;
// flags += b2Draw::e_aabbBit;
// flags += b2Draw::e_pairBit;
// flags += b2Draw::e_centerOfMassBit;
m_debugDraw->SetFlags(flags);
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody = world->CreateBody(&groundBodyDef);
// Define the ground box shape.
b2EdgeShape groundBox;
// bottom
groundBox.Set(b2Vec2(0,0), b2Vec2(size.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
// top
// groundBox.Set(b2Vec2(0,size.height/PTM_RATIO), b2Vec2(size.width/PTM_RATIO,size.height/PTM_RATIO));
// groundBody->CreateFixture(&groundBox,0);
// left
groundBox.Set(b2Vec2(0,size.height/PTM_RATIO), b2Vec2(0,0));
groundBody->CreateFixture(&groundBox,0);
// right
groundBox.Set(b2Vec2(size.width/PTM_RATIO,size.height/PTM_RATIO), b2Vec2(size.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
}
//-(void) draw
//{
// //
// // IMPORTANT:
// // This is only for debug purposes
// // It is recommend to disable it
// //
// [super draw];
//
// ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
//
// kmGLPushMatrix();
//
// world->DrawDebugData();
//
// kmGLPopMatrix();
//}
-(void) addNewSpriteAtPosition:(CGPoint)p
{
CCNode *parent = [self getChildByTag:kTagParentNode];
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
//just randomly picking one of the images
// int idx = (CCRANDOM_0_1() > .5 ? 0:1);
// int idy = (CCRANDOM_0_1() > .5 ? 0:1);
// CCPhysicsSprite *sprite = [CCPhysicsSprite spriteWithTexture:spriteTexture_ rect:CGRectMake(32 * idx,32 * idy,32,32)];
CCPhysicsSprite *sprite = [CCPhysicsSprite spriteWithTexture:spriteTexture_ rect:CGRectMake(0,0,32,32)];
sprite.tag = obstacle;
[parent addChild:sprite];
[sprite setPTMRatio:PTM_RATIO];
[sprite setB2Body:[self setupBody:p withSprite:sprite]];
[sprite setPosition: ccp( p.x, p.y)];
}
-(void) update: (ccTime) dt
{
//It is recommended that a fixed time step is used with Box2D for stability
//of the simulation, however, we are using a variable time step here.
//You need to make an informed choice, the following URL is useful
//http://gafferongames.com/game-physics/fix-your-timestep/
int32 velocityIterations = 8;
int32 positionIterations = 1;
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world->Step(dt, velocityIterations, positionIterations);
// do stuff on collisions
std::vector<MyContact>::iterator pos;
std::vector<b2Body *>toDestroy;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
bool playerHit = false;
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
// Detect when player is hit
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite* bodyASprite = (CCSprite*) bodyA->GetUserData();
CCSprite* bodyBSprite = (CCSprite*) bodyB->GetUserData();
// Player was hit!
if (bodyASprite.tag == player || bodyBSprite.tag == player) {
playerHit = true;
[self updateScoreBy:-10000];
}
// Detect when obstacle hits ground
} else {
CCSprite *sprite;
b2Body *body;
if (bodyA->GetUserData() != NULL) {
sprite = (CCSprite*) bodyA->GetUserData();
body = bodyA;
} else if (bodyB->GetUserData() != NULL) {
sprite = (CCSprite*) bodyB->GetUserData();
body = bodyB;
}
if (sprite.tag == obstacle) {
toDestroy.push_back(body);
}
}
if (!playerHit) {
[self updateScoreBy:10];
}
}
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) body->GetUserData();
if (sprite) {
[sprite removeFromParentAndCleanup:YES];
}
}
world->DestroyBody(body);
}
}
// Spawn blocks regularly.
-(void) updateTime:(ccTime)dt
{
runningTime += 1;
for (int i=0; i < runningTime/10 + 1; i++) {
[self addNewSpriteAtPosition:ccp(arc4random_uniform(size.width), size.height)];
}
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
right = location.x > size.width/2;
[self schedule:@selector(movePlayer:) interval:0.01f];
}
-(void)movePlayer:(ccTime)dt {
CGPoint playerPos = playerSprite.position;
if (right) {
if (playerPos.x + 5 < size.width) {
playerSprite.position = ccp(playerPos.x + 5, playerPos.y);
}
} else {
if (playerPos.x - 5 > 0) {
playerSprite.position = ccp(playerPos.x - 5, playerPos.y);
}
}
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self unschedule:@selector(movePlayer:)];
}
-(void)updateScoreBy:(int)delta {
score += delta;
[scoreLabel setString:[NSString stringWithFormat:@"%i", score]];
}
@end