-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMap.m
More file actions
325 lines (255 loc) · 11.7 KB
/
Map.m
File metadata and controls
325 lines (255 loc) · 11.7 KB
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
#import "Map.h"
@implementation Map
@synthesize tileData;
@synthesize tileWidth;
@synthesize tileHeight;
@synthesize coins;
@synthesize tileIDs;
@synthesize platforms;
-(id) initWithCSV:(NSString *)csvName tileSize:(CGSize)mTileSize andTileImage:(NSString*)tileImageName {
if ((self = [super init])) {
tiles = [[NSMutableArray alloc] init];
tileData = [[NSMutableArray alloc] init];
tileIDs = [[NSMutableArray alloc] init];
coins = [[NSMutableArray alloc] init];
platforms = [[NSMutableArray alloc] init];
mapSprite = [[SPCompiledSprite alloc] init];
coinSprite = [[SPSprite alloc] init];
platformSprite = [[SPSprite alloc] init];
juggler = [[SPJuggler alloc] init];
tileWidth = mTileSize.width;
tileHeight = mTileSize.height;
[self makeTilesFromImage:tileImageName];
[self setupMapWithCSV:csvName];
[self drawMap];
[self addChild:mapSprite];
[self addChild:coinSprite];
[self addChild:platformSprite];
[self addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
}
return self;
}
-(void) onEnterFrame:(SPEnterFrameEvent *)event
{
[juggler advanceTime:event.passedTime];
}
-(void) makeTilesFromImage:(NSString *)tileImageName {
UIImage *img = [UIImage imageNamed:tileImageName];
int tilesWide = floor(img.size.width/tileWidth);
int tilesHigh = floor(img.size.height/tileHeight);
//NSLog(@"%i,%i", tilesWide, tilesHigh);
if(tiles.count > 0){
[tiles removeAllObjects];
}
SPTexture *tilesTexture = [[SPTexture alloc] initWithContentsOfImage:img];
for (int yy=0; yy<tilesHigh; yy++) {
for (int xx=0;xx<tilesWide; xx++) {
SPRectangle *rect = [[SPRectangle alloc] initWithX:xx*tileWidth y:yy*tileHeight width:tileWidth height:tileHeight];
SPSubTexture *tileTexture = [[SPSubTexture alloc] initWithRegion:rect ofTexture:tilesTexture];
//SPImage *tileImage = [[SPImage alloc] initWithTexture:tileTexture];
[tiles addObject:tileTexture];
//[tileImage release];
}
}
//NSLog(@"tiles count: %i", [tiles count]);
}
-(void) setupMapWithCSV:(NSString *)csvName {
// TO ACCESS A TILE ID: [[[tileData objectAtIndex:COLUMN] objectAtIndex:ROW] intValue]
//Side to side: ROW
// UP AND DOWN: COLUMN
NSString *theCSVPath = [[NSBundle mainBundle] pathForResource:csvName ofType:@"csv"];
NSString *badcsvData = [[NSString alloc] initWithContentsOfFile:theCSVPath encoding:NSUTF8StringEncoding error:NULL];
NSString *csvData = [badcsvData stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]; // trim whitespace
NSArray *rows = [csvData componentsSeparatedByString:@"\n"];
int count = 0;
for (NSString *row in rows)
{
NSMutableArray *newcol = [[NSMutableArray alloc] init];
NSMutableArray *newblankcol = [[NSMutableArray alloc] init];
NSMutableArray *platformArr = [[NSMutableArray alloc] init];
NSArray *colData = [row componentsSeparatedByString:@","];
count+=1;
//Coin/platform stuff..
for (NSString *col in colData)
{
[newblankcol addObject:[NSNull null]];
[platformArr addObject:[NSNull null]];
int newIDint = [col integerValue]-1;
if (![col length] == 0) {
NSString *newID = [NSString stringWithFormat:@"%i", newIDint];
//printf("%i", newIDint);
if (newIDint > -1) {
[newcol addObject:newID];
}
}
}
[tileData addObject:newcol];
[coins addObject:newblankcol];
[platforms addObject:platformArr];
}
}
-(void) drawMap { //Time to draw the tiles!
int mapWidth = [[tileData objectAtIndex:0] count]; //this is wrong...
int mapHeight = [tileData count];
float xOffset = 0;
int yOffset = 0;
for (int i = 0; i < mapHeight; i++) // i = COLUMN
{
for (int j = 0; j < mapWidth; j++) { // j = ROW
int tileID = [[[tileData objectAtIndex:i] objectAtIndex:j] intValue];
SPImage *tile = [[SPImage alloc] initWithTexture:[tiles objectAtIndex:tileID]];
tile.x = xOffset;
tile.y = yOffset;
if (tileID == 22 || tileID == 23) { //small (7) windmill!
NSMutableArray *walls = [self windmillWithLength:(tileID == 23 ? 7 : 9 )atX:xOffset andY:yOffset];
int midpoint = tileID == 23 ? 3 : 4; // if its 23, then its small so 3; otherwise 4
//Add a giant square (7x7) of all the potential sizes of the angle, using our walls array
for (int col = i-midpoint; col < i+midpoint; col++) { //For each column
for (int row = j-midpoint; row < j+midpoint; row++) { //For each row
[[platforms objectAtIndex:col] replaceObjectAtIndex:row withObject:walls];
}
}
//auto adds blank tile to the sprite.. now replace it in the array
NSString *newID = [NSString stringWithFormat:@"%i", 19];
[[tileData objectAtIndex:i] replaceObjectAtIndex:j withObject:newID];
}
else if (tileID == 20 || tileID == 21) {
NSString *newID = [NSString stringWithFormat:@"%i", 19];
//SPImage *tile = [[SPImage alloc] initWithTexture:[tiles objectAtIndex:12]];
[[tileData objectAtIndex:i] replaceObjectAtIndex:j withObject:newID];
tile.texture = [tiles objectAtIndex:12];
[[platforms objectAtIndex:i] replaceObjectAtIndex:j withObject:tile];
[platformSprite addChild:tile];
// create a tween that moves side to side
SPTween *tween = [SPTween tweenWithTarget:tile time:1.5];
if (tileID == 20) {
[tween animateProperty:@"x" targetValue:(tile.x + 48)]; //Left-Right
} else {
[tween animateProperty:@"y" targetValue:(tile.y + 48)]; //Up-Down
}
tween.loop = SPLoopTypeReverse;
[juggler addObject:tween];
//Add a blank tile to the main layer..
SPImage *blankTile = [[SPImage alloc] initWithTexture:[tiles objectAtIndex:19]];
blankTile.x = xOffset;
blankTile.y = yOffset;
[mapSprite addChild:blankTile];
}
else if (tileID == 16) {
[[coins objectAtIndex:i] replaceObjectAtIndex:j withObject:tile];
[coinSprite addChild:tile];
//Add a blank tile to the main layer..
SPImage *blankTile = [[SPImage alloc] initWithTexture:[tiles objectAtIndex:19]];
blankTile.x = xOffset;
blankTile.y = yOffset;
[mapSprite addChild:blankTile];
} else {
[mapSprite addChild:tile];
}
xOffset +=tileWidth;
}
//Done with that row, move on
xOffset = 0.0f;
yOffset +=tileHeight;
}
[mapSprite compile];
}
-(bool) isPoint:(SPPoint *)point inRotatedObject:(SPDisplayObject *)obj {
SPPoint *rotatedPoint = [point rotateBy:-obj.rotation originX:obj.x originY:obj.y];
SPPoint *topLeft = [SPPoint pointWithX:(obj.x - obj.pivotX) y:(obj.y - obj.pivotY)];
SPPoint *bottomRight = [SPPoint pointWithX:(obj.x + obj.pivotX) y:(obj.y + obj.pivotY)];
return rotatedPoint.x > topLeft.x && rotatedPoint.x < bottomRight.x && rotatedPoint.y > topLeft.y && rotatedPoint.y < bottomRight.y;
}
- (int) getTileID:(SPPoint *)loc
{
int column = floor(loc.x / tileWidth);
int row = floor(loc.y / tileHeight);
int tileID = [[[tileData objectAtIndex:row] objectAtIndex:column] intValue];
return tileID;
}
-(NSMutableArray *) getPlatforms:(SPPoint *)loc
{
int mapWidth = [tileData count];
//NSLog(@"Width: %i", mapWidth);
int elrow = floor(loc.x / tileWidth);
int column = floor(loc.y / tileHeight);
NSMutableArray *returnArr = [NSMutableArray array];
//check everything in the column
for (id obj in [platforms objectAtIndex:column]) {
if (![obj isKindOfClass:[NSNull class]]) {
[returnArr addObject:obj];
}
}
//Loop through all the cols, check le row
for (int c=0; c < mapWidth; c++) {
id obj = [[platforms objectAtIndex:c] objectAtIndex:elrow];
if ([obj isKindOfClass:[SPDisplayObject class]]) {
[returnArr addObject:obj];
}
}
return returnArr;
}
-(void) removeCoinAtColumn:(int)col andRow:(int)row {
//Remove from coins array and sprite
SPImage *_tile = [[coins objectAtIndex:col] objectAtIndex:row];
[coinSprite removeChild:_tile];
//replace tile ID
NSString *newTileString = [NSString stringWithFormat:@"19"];
[[tileData objectAtIndex:col] replaceObjectAtIndex:row withObject:newTileString];
}
-(NSMutableArray *) windmillWithLength:(int)length atX:(float)xOffset andY:(float)yOffset {
//Loop twice - once for each wall; We increment the angleOffset by 270 so it goes from 45 to 315 :)
float angleOffset = SP_D2R(45);
NSMutableArray *walls = [[NSMutableArray alloc] init];
for (int w=0; w<2; w++) { //for each wall
SPCompiledSprite *wall = [[SPCompiledSprite alloc] init];
float tileYOff = 0; // (0 by default)
for (int i=0; i<length; i++) {
int tileID;
switch (i) {
case 0: //top end
tileID = 0;
break;
case 6: //bottom end (length 7)
tileID = length==7 ? 8 : 4; //if its length is 7, then this is the end. Otherwise see 8
break;
case 8: //bottom end (length 9)
tileID = 8;
break;
case 3:
tileID = w == 1 && length == 7 ? 13 : 4; //checking that is the short windmill and that its the top wall
break; // middle one for top
case 4:
tileID = w == 1 && length == 9 ? 13 : 4; //checking that is the long windmill and that its the top wall
break;
default: //reg vertical tile
tileID = 4;
break;
}
SPImage *tile = [[SPImage alloc] initWithTexture:[tiles objectAtIndex:tileID]];
tile.y = tileYOff;
[wall addChild:tile];
tileYOff += 16;
}
[wall compile];
wall.x = xOffset;
wall.y = yOffset;
wall.pivotX = wall.width/2.0f;
wall.pivotY = wall.height/2.0f;
wall.rotation = angleOffset;
SPTween *tween = [SPTween tweenWithTarget:wall time:5.0];
[tween animateProperty:@"rotation" targetValue:SP_D2R(w == 1?314 : 404)];
tween.loop = SPLoopTypeRepeat;
[juggler addObject:tween];
[platformSprite addChild:wall];
[walls addObject:wall];
angleOffset = SP_D2R(315);
}
//Add a blank tile to the main layer + replace it in the array with one too
SPImage *blankTile = [[SPImage alloc] initWithTexture:[tiles objectAtIndex:19]];
blankTile.x = xOffset;
blankTile.y = yOffset;
[mapSprite addChild:blankTile];
return walls;
}
@end