mardi 13 janvier 2009

minigolf on the iphone

We started a minigolf on the nds, learning a lot about the way C uses arrays. The same process started with Xcode-Objective C on the Iphone, recreating this minigolf game.

The minigolf has a small engine allowing for shapes of the field which are queer, not rectangular.
For this we needed to triangulate the field and have arrays ready of the points, the lines and the triangles involved. The triangle class has an pointarray inside, as well as the three lines in an array.

In the nds code, then the arrays were initialized like:

u16 p3[] = { 61,180, 22,171, 8,102, 8,42, 55,7, 95,13, 105,34, 136,69, 156,84, 164,83, 184,79, 188,74, 183,56, 179,52, 157,45, 151,45, 117,82, 114,87, 119,111, 120,115, 162,147, 160,166, 133,180, 84,144, 76,109, 76,79, 131,9, 196,6, 241,46, 237,74, 220,102, 198,125, 163,133, 127,103, 120,88, 84,53, 77,54, 64,66, 61,74, 60,132, 63,138, 86,166, 0,0,0,0,0};

each three integers were a triangle, the naughts at the end were to stop the making triangle objects, C having no way of telling how long an array is (?).

of course these data should be read in from an external file, so giving the possibility to add fields of your own. In Xcode i decided to do that immediately:

reading in a .txt file: (The triangles are given only with the number of the points from a point array, so 1,2,3 means triangle with point number 1, point number 2, point number 3.)

NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"triangles1" ofType:@"txt"];
if (filePath2) {
NSString *myText = [NSString stringWithContentsOfFile:filePath2];
if (myText) {
NSLog(myText); //see if it worked!
NSArray *arr = [myText componentsSeparatedByString:@","];

and at the end separating the file with the comma's in between the numbers.
Then the points were searched from the point array made earlier.

ok before this code: don't forget to inilialize the array:
triangleArray = [[NSMutableArray alloc] init];

now use a loop to generate the set of three points:
integer_t i;
for(i=0;i
{
int triangleNumber1 = (int)[[arr objectAtIndex:i] intValue ];
int triangleNumber2 = (int)[[arr objectAtIndex:i+1] intValue ];
int triangleNumber3 = (int)[[arr objectAtIndex:i+2] intValue ];

make the triangle object: (using the point array made earlier in the same way)

Triangle *myTriangle = [[Triangle alloc] initWithNumbers3:( MyPoint *)[pointArray objectAtIndex:triangleNumber1]:( MyPoint *)[pointArray objectAtIndex:triangleNumber2]:( MyPoint *)[pointArray objectAtIndex:triangleNumber3] ];

and adding this triangle object to the triangleArray:

[triangleArray addObject:myTriangle ];
}
NSLog(@"made triangleArray");
}
}
else
NSLog(@"no path found");

of course without the actual point, line and triangles classes you cannot do much with this code, but is sketches the principle. (This blog is a diairy.....what is written in 5 minutes....could have costed me a few houres, mainly searching for the right syntax in a for me "new" language, in all kinds of blogs on the web.)

reference link array:
http://www.cocoadev.com/index.pl?NSMutableArray
reference link reading in an external file (scroll down):
http://iphoneincubator.com/blog/page/2
reference link for NSString "Quickies"
http://www.borkware.com/quickies/one?topic=NSString