-
Notifications
You must be signed in to change notification settings - Fork 2
/
Building.m
477 lines (409 loc) · 15.1 KB
/
Building.m
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//
// Building.m
// CanaClone
//
// Created by Josh Click on 1/1/13.
//
//
#import "Building.h"
static NSString * ImgWall1Left = @"wall1-left.png";
static NSString * ImgWall2Left = @"wall2-left.png";
static NSString * ImgWall3Left = @"wall3-left.png";
static NSString * ImgWall4Left = @"wall4-left.png";
static NSString * ImgWall1Middle = @"wall1-middle.png";
static NSString * ImgWall2Middle = @"wall2-middle.png";
static NSString * ImgWall3Middle = @"wall3-middle.png";
static NSString * ImgWall4Middle = @"wall4-middle.png";
static NSString * ImgWall1Right = @"wall1-right.png";
static NSString * ImgWall2Right = @"wall2-right.png";
static NSString * ImgWall3Right = @"wall3-right.png";
static NSString * ImgWall4Right = @"wall4-right.png";
static NSString * ImgRoof1Left = @"roof1-left.png";
static NSString * ImgRoof2Left = @"roof2-left.png";
static NSString * ImgRoof3Left = @"roof3-left.png";
static NSString * ImgRoof4Left = @"roof4-left.png";
static NSString * ImgRoof5Left = @"roof6-left.png";
static NSString * ImgRoof6Left = @"roof5-left.png";
static NSString * ImgRoof1Middle = @"roof1-middle.png";
static NSString * ImgRoof2Middle = @"roof2-middle.png";
static NSString * ImgRoof3Middle = @"roof3-middle.png";
static NSString * ImgRoof4Middle = @"roof4-middle.png";
static NSString * ImgRoof5Middle = @"roof6-middle.png";
static NSString * ImgRoof6Middle = @"roof5-middle.png";
static NSString * ImgRoof1Right = @"roof1-right.png";
static NSString * ImgRoof2Right = @"roof2-right.png";
static NSString * ImgRoof3Right = @"roof3-right.png";
static NSString * ImgRoof4Right = @"roof4-right.png";
static NSString * ImgRoof5Right = @"roof6-right.png";
static NSString * ImgRoof6Right = @"roof5-right.png";
static NSString * ImgWall1LeftCracked = @"wall1-left-cracked.png";
static NSString * ImgWall2LeftCracked = @"wall2-left-cracked.png";
static NSString * ImgWall3LeftCracked = @"wall3-left-cracked.png";
static NSString * ImgWall4LeftCracked = @"wall4-left-cracked.png";
static NSString * ImgWall1MiddleCracked = @"wall1-middle-cracked.png";
static NSString * ImgWall2MiddleCracked = @"wall2-middle-cracked.png";
static NSString * ImgWall3MiddleCracked = @"wall3-middle-cracked.png";
static NSString * ImgWall4MiddleCracked = @"wall4-middle-cracked.png";
static NSString * ImgWall1RightCracked = @"wall1-right-cracked.png";
static NSString * ImgWall2RightCracked = @"wall2-right-cracked.png";
static NSString * ImgWall3RightCracked = @"wall3-right-cracked.png";
static NSString * ImgWall4RightCracked = @"wall4-right-cracked.png";
static NSString * ImgRoof1LeftCracked = @"roof1-left-cracked.png";
static NSString * ImgRoof2LeftCracked = @"roof2-left-cracked.png";
static NSString * ImgRoof3LeftCracked = @"roof3-left-cracked.png";
static NSString * ImgRoof4LeftCracked = @"roof4-left-cracked.png";
static NSString * ImgRoof5LeftCracked = @"roof6-left-cracked.png";
static NSString * ImgRoof6LeftCracked = @"roof5-left-cracked.png";
static NSString * ImgRoof1MiddleCracked = @"roof1-middle-cracked.png";
static NSString * ImgRoof2MiddleCracked = @"roof2-middle-cracked.png";
static NSString * ImgRoof3MiddleCracked = @"roof3-middle-cracked.png";
static NSString * ImgRoof4MiddleCracked = @"roof4-middle-cracked.png";
static NSString * ImgRoof5MiddleCracked = @"roof6-middle-cracked.png";
static NSString * ImgRoof6MiddleCracked = @"roof5-middle-cracked.png";
static NSString * ImgRoof1RightCracked = @"roof1-right-cracked.png";
static NSString * ImgRoof2RightCracked = @"roof2-right-cracked.png";
static NSString * ImgRoof3RightCracked = @"roof3-right-cracked.png";
static NSString * ImgRoof4RightCracked = @"roof4-right-cracked.png";
static NSString * ImgRoof5RightCracked = @"roof6-right-cracked.png";
static NSString * ImgRoof6RightCracked = @"roof5-right-cracked.png";
static NSString * ImgFloor1Left = @"floor1-left.png";
static NSString * ImgFloor2Left = @"floor2-left.png";
static NSString * ImgFloor1Middle = @"floor1-middle.png";
static NSString * ImgFloor2Middle = @"floor2-middle.png";
static NSString * ImgFloor1Right = @"floor1-right.png";
static NSString * ImgFloor2Right = @"floor2-right.png";
static NSString * ImgWindow1 = @"window1.png";
static NSString * ImgWindow2 = @"window2.png";
static NSString * ImgWindow3 = @"window3.png";
static NSString * ImgWindow4 = @"window4.png";
static NSArray * leftWalls;
static NSArray * rightWalls;
static NSArray * middleWalls;
static NSArray * leftWallsCracked;
static NSArray * rightWallsCracked;
static NSArray * middleWallsCracked;
static NSArray * leftFloors;
static NSArray * middleFloors;
static NSArray * rightFloors;
static NSArray * leftRoofs;
static NSArray * middleRoofs;
static NSArray * rightRoofs;
static NSArray * leftRoofsCracked;
static NSArray * middleRoofsCracked;
static NSArray * rightRoofsCracked;
static NSArray * windowImages;
//----------------------
static NSString * ImgBillboardTopMiddle = @"billboard_top-middle.png";
static NSString * ImgBillboardTopLeft = @"billboard_top-left.png";
static NSString * ImgBillboardTopRight = @"billboard_top-right.png";
static NSString * ImgBillboardMiddleLeft = @"billboard_middle-left.png";
static NSString * ImgBillboardMiddleRight = @"billboard_middle-right.png";
static NSString * ImgBillboardMiddleMiddle = @"billboard_middle-middle.png";
static NSString * ImgBillboardBottomMiddle = @"billboard_bottom-middle.png";
static NSString * ImgBillboardBottomLeft = @"billboard_bottom-left.png";
static NSString * ImgBillboardBottomRight = @"billboard_bottom-right.png";
static NSString * ImgBillboardCatwalkMiddle = @"billboard_catwalk-middle.png";
static NSString * ImgBillboardCatwalkLeft = @"billboard_catwalk-left.png";
static NSString * ImgBillboardCatwalkRight = @"billboard_catwalk-right.png";
static NSString * ImgBillboardPost = @"billboard_post.png";
static NSString * ImgBillboardPost2 = @"billboard_post2.png";
static NSString * ImgBillboardDmg1 = @"billboard_dmg1.png";
static NSString * ImgBillboardDmg2 = @"billboard_dmg2.png";
static NSString * ImgBillboardDmg3 = @"billboard_dmg3.png";
//-----------------
static NSString * ImgAntenna5 = @"antenna5.png";
static NSString * ImgCrane1 = @"crane1.png"; //beam
static NSString * ImgCrane2 = @"crane2.png"; //post
static NSString * ImgCrane3 = @"crane3.png"; //counterweight
static NSString * ImgCrane4 = @"crane4.png"; //cabin
static NSString * ImgCrane5 = @"crane5.png"; //pulley
//------------
@implementation Building
@synthesize buildingWidth;
@synthesize platHeight;
- (void)initSpriteLists
{
leftWalls = @[ImgWall1Left,
ImgWall2Left,
ImgWall3Left,
ImgWall4Left];
rightWalls = @[ImgWall1Right,
ImgWall2Right,
ImgWall3Right,
ImgWall4Right];
middleWalls = @[ImgWall1Middle,
ImgWall2Middle,
ImgWall3Middle,
ImgWall4Middle];
leftWallsCracked = @[ImgWall1LeftCracked,
ImgWall2LeftCracked,
ImgWall3LeftCracked,
ImgWall4LeftCracked];
rightWallsCracked = @[ImgWall1RightCracked,
ImgWall2RightCracked,
ImgWall3RightCracked,
ImgWall4RightCracked];
middleWallsCracked = @[ImgWall1MiddleCracked,
ImgWall2MiddleCracked,
ImgWall3MiddleCracked,
ImgWall4MiddleCracked];
leftFloors = @[ImgFloor1Left,
ImgFloor2Left];
middleFloors = @[ImgFloor1Middle,
ImgFloor2Middle];
rightFloors = @[ImgFloor1Right,
ImgFloor2Right];
leftRoofs = @[ImgRoof1Left,
ImgRoof2Left,
ImgRoof3Left,
ImgRoof4Left,
ImgRoof5Left,
ImgRoof6Left];
middleRoofs = @[ImgRoof1Middle,
ImgRoof2Middle,
ImgRoof3Middle,
ImgRoof4Middle,
ImgRoof5Middle,
ImgRoof6Middle];
rightRoofs = @[ImgRoof1Right,
ImgRoof2Right,
ImgRoof3Right,
ImgRoof4Right,
ImgRoof5Right,
ImgRoof6Right];
leftRoofsCracked = @[ImgRoof1LeftCracked,
ImgRoof2LeftCracked,
ImgRoof3LeftCracked,
ImgRoof4LeftCracked,
ImgRoof5LeftCracked,
ImgRoof6LeftCracked];
middleRoofsCracked = @[ImgRoof1MiddleCracked,
ImgRoof2MiddleCracked,
ImgRoof3MiddleCracked,
ImgRoof4MiddleCracked,
ImgRoof5MiddleCracked,
ImgRoof6MiddleCracked];
rightRoofsCracked = @[ImgRoof1RightCracked,
ImgRoof2RightCracked,
ImgRoof3RightCracked,
ImgRoof4RightCracked,
ImgRoof5RightCracked,
ImgRoof6RightCracked];
windowImages = @[ImgWindow1,
ImgWindow2,
ImgWindow3,
ImgWindow4];
}
- (void)createCrane
{
tileSize = 32;
int maxRow = pixelHeight/tileSize + 1;
int maxCol = BUWidth*4;
buildingWidth = maxCol*tileSize+tileSize;
platHeight = pixelHeight;
int postCol = maxCol/3;
//in future, make crane be able to face both ways
for (int row = 0; row <= maxRow; row++) {
for (int col = 0; col < maxCol; col++) {
CCSprite *sprite;
if (row == 0) { //for the main post
if (col == 0) {
//this is for the weight on the crane
sprite = [CCSprite spriteWithSpriteFrameName:ImgCrane3];
sprite.anchorPoint = ccp(0.0f, 1.0f);
[wallBatch addChild:sprite z:20];
sprite.position = ccp(screenSize.width + col * tileSize+8, pixelHeight - row * tileSize-8);
//this is for antenna
sprite = [CCSprite spriteWithSpriteFrameName:ImgAntenna5];
sprite.anchorPoint = ccp(0.0f, 0.0f);
[wallBatch addChild:sprite z:20];
sprite.position = ccp(screenSize.width + col * tileSize-4, pixelHeight - row * tileSize-1);
}
if (col == postCol) { //for crane controller
sprite = [CCSprite spriteWithSpriteFrameName:ImgCrane4];
sprite.anchorPoint = ccp(0.0f, 0.75f);
[wallBatch addChild:sprite z:20];
sprite.position = ccp(screenSize.width + col * tileSize-8, pixelHeight - row * tileSize);
}
sprite = [CCSprite spriteWithSpriteFrameName:ImgCrane2];
sprite.rotation = 90;
} else if (row > 0 && col == postCol) //for the support post to ground
sprite = [CCSprite spriteWithSpriteFrameName:ImgCrane2];
else
continue;
sprite.anchorPoint = ccp(0.0f, 1.0f);
[wallBatch addChild:sprite z:10];
sprite.position = ccp(screenSize.width + col * tileSize, pixelHeight - row * tileSize);
}
}
}
- (void)createBillboard
{
tileSize = 16;
int maxCol = BUWidth*2+4; //must be even number since some sprites are double sized
if (maxCol<10) maxCol = 16; //set min and max width
else if (maxCol > 24) maxCol = 24;
int maxRow = maxCol*2/5; //make height 2/3 the length
buildingWidth = maxCol*tileSize + tileSize;
platHeight = pixelHeight+tileSize;
for (int row = 0; row <= maxRow; row++) { //draw billboard from bottom up instead
for (int col = 0; col < maxCol; col++) {
CCSprite *sprite;
if (row == 0) { //for the bottom of billboard
if (col == 0) {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardCatwalkLeft];
}
else if (col == maxCol-1) {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardCatwalkRight];
}
else {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardCatwalkMiddle]; //extra stuff since middle piece is 3px shorter
sprite.anchorPoint = ccp(0.0f, 0.0f);
[wallBatch addChild:sprite z:10];
sprite.position = ccp(screenSize.width + col * tileSize, pixelHeight + row * tileSize + 3);
continue;
}
}
else if (row == 1) {
if (col == 0) {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardBottomLeft];
}
else if (col == maxCol-2) {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardBottomRight];
}
else if (col%2==0){
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardBottomMiddle];
}
else
continue;
}
else if (row == maxRow) {
if (col == 0) {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardTopLeft];
}
else if (col == maxCol-2) {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardTopRight];
}
else if (col%2==0){
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardTopMiddle];
}
else
continue;
}
else if ((row+1)%2 == 0) {
if (col == 0) {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardMiddleLeft];
}
else if (col == maxCol-2) {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardMiddleRight];
}
else if (col%2==0){
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardMiddleMiddle];
}
else
continue;
}
else
continue;
sprite.anchorPoint = ccp(0.0f, 0.0f);
[wallBatch addChild:sprite z:10];
sprite.position = ccp(screenSize.width + col * tileSize, pixelHeight + row * tileSize);
}
}
//draw the post
for (int i = 0; i <= platHeight/tileSize; i++) {
CCSprite *sprite;
if (i == 0) {
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardPost2];
}
else if ((i+1)%2 == 0) //this is double height sprite
sprite = [CCSprite spriteWithSpriteFrameName:ImgBillboardPost];
else
continue;
sprite.anchorPoint = ccp(0.5f,1.0f);
[wallBatch addChild:sprite z:1];
sprite.position = ccp(screenSize.width + maxCol/2 * tileSize, pixelHeight - i * tileSize + 8);
}
}
- (void)createBuilding
{
tileSize = 16;
int maxRow = pixelHeight/tileSize + 1;
int maxCol = BUWidth*4 +2; //windows + edges
buildingWidth = maxCol*tileSize+tileSize;
platHeight = pixelHeight;
int roof = arc4random_uniform(6);
int window = arc4random_uniform(4);
int wall = arc4random_uniform(4);
for (int row = 0; row <= maxRow; row++) {
for (int col = 0; col < maxCol; col++) {
CCSprite *sprite;
if (row == 0) { //for the top of the building
if (col == 0) {
sprite = [CCSprite spriteWithSpriteFrameName:leftRoofs[roof]];
}
else if (col == maxCol-1) {
sprite = [CCSprite spriteWithSpriteFrameName:rightRoofs[roof]];
}
else {
sprite = [CCSprite spriteWithSpriteFrameName:middleRoofs[roof]];
}
}
else if (row%2 == 0 && col > 0 && col < maxCol - 1) { //if in an even row, make it a window
if ((col-1)%4 == 0) { //window tex are 4 tiles long
sprite = [CCSprite spriteWithSpriteFrameName:windowImages[window]];
}
else
continue;
}
else {
if (col == 0) {
sprite = [CCSprite spriteWithSpriteFrameName:leftWalls[wall]];
}
else if (col == maxCol-1) {
sprite = [CCSprite spriteWithSpriteFrameName:rightWalls[wall]];
}
else {
sprite = [CCSprite spriteWithSpriteFrameName:middleWalls[wall]];
}
}
sprite.anchorPoint = ccp(0.0f, 1.0f);
[wallBatch addChild:sprite z:10];
sprite.position = ccp(screenSize.width + col * tileSize, pixelHeight - row * tileSize);
}
}
//add props here
BuildingProps *buildingProps = [[BuildingProps alloc] initWithWidth:buildingWidth height:platHeight];
[self addChild:buildingProps];
}
- (id)initWithScrollSpeed:(float)scrollSpeed
{
if (self = [super init]) {
[self initSpriteLists];
screenSize = [[CCDirector sharedDirector] winSize];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Object_Atlas.plist"];
wallBatch = [CCSpriteBatchNode batchNodeWithFile:@"Object_Atlas.png" capacity:750];
[self addChild:wallBatch];
wallBatch.anchorPoint = ccp(0.0f,0.0f);
BUWidth = arc4random_uniform(5) + 2 + (int)scrollSpeed/80;
pixelHeight = arc4random_uniform(70) + 70;
float buildingType = arc4random();
if (scrollSpeed == 150.0f) { //for the first building
pixelHeight = 200;
BUWidth = 15;
buildingType = 1;
}
if (buildingType > 0.25)
[self createBuilding];
else if (buildingType < 0.25) {
if (buildingType < 0.15)
[self createBillboard];
else
[self createCrane];
}
else
CCLOG(@"dude problem with the building type");
}
return self;
}
@end