#include "LevelManager.h" #include "WorldManager.h" #include #include #include static Boolean ReadLn(short Ref, char * TheData) { int i = 0; long DataSize; OSErr Err = noErr; do { DataSize = 1; FSRead(Ref, &DataSize, &TheData[i]); i++; } while(TheData[i-1] != '\n' && TheData[i-1] != '\r' && !Err && DataSize); TheData[i] = '\0'; if(Err || !DataSize) return false; return true; } void DrawLevel(VLevel * l) { int i; int i2; int i3; for(i = 0; i < 5; i++) for(i2 = 0; i2 < 5; i2++) { for(i3 = 0; i3 < 4; i3++) if(l->Rooms[i][i2].IsWall[i3]) DrawObject(&l->Rooms[i][i2].Walls[i3]); DrawObject(&l->Rooms[i][i2].Ground); } } void CleanUpLevel(VLevel * l) { int i; int i2; int i3; //VObjectWrapper * theWObject; for(i = 0; i < 5; i++) for(i2 = 0; i2 < 5; i2++) { for(i3 = 0; i3 < 4; i3++) CleanUpObject(&l->Rooms[i][i2].Walls[i3]); CleanUpObject(&l->Rooms[i][i2].Ground); } /*while(l->Objects != NULL) { theWObject = l->Objects; l->Objects = theWObject->next; CleanUpWObject(theWObject); DisposePtr((Ptr) theWObject); }*/ } static void CreateRooms(VLevel * l) { int i; int i2; int i3; for(i = 0; i < 5; i++) for(i2 = 0; i2 < 5; i2++) { InitObject(&l->Rooms[i][i2].Ground); l->Rooms[i][i2].Ground.Position.x = 40*i; l->Rooms[i][i2].Ground.Position.z = 40*i2; if(l->Cells[i][i2]) /*BuildGrassModel(&l->Rooms[i][i2].Ground.Model); Slow */; for(i3 = 0; i3 < 4; i3++) { InitObject(&l->Rooms[i][i2].Walls[i3]); l->Rooms[i][i2].Walls[i3].Position.x = 40*i; l->Rooms[i][i2].Walls[i3].Position.z = 40*i2; l->Rooms[i][i2].IsWall[i3] = false; } if(l->Cells[i][i2]) { if(i > 0) { if(!l->Cells[i-1][i2]) { BuildLeftWallModel(&l->Rooms[i][i2].Walls[0].Model); l->Rooms[i][i2].IsWall[0] = true; } } else { BuildLeftWallModel(&l->Rooms[i][i2].Walls[0].Model); l->Rooms[i][i2].IsWall[0] = true; } if(i < 5) { if(!l->Cells[i+1][i2]) { BuildRightWallModel(&l->Rooms[i][i2].Walls[1].Model); l->Rooms[i][i2].IsWall[1] = true; } } else { BuildRightWallModel(&l->Rooms[i][i2].Walls[1].Model); l->Rooms[i][i2].IsWall[1] = true; } if(i2 > 0) { if(!l->Cells[i][i2-1]) { BuildFrontWallModel(&l->Rooms[i][i2].Walls[2].Model); l->Rooms[i][i2].IsWall[2] = true; } } else { BuildFrontWallModel(&l->Rooms[i][i2].Walls[2].Model); l->Rooms[i][i2].IsWall[2] = true; } if(i2 < 5) { if(!l->Cells[i][i2+1]) { BuildBackWallModel(&l->Rooms[i][i2].Walls[3].Model); l->Rooms[i][i2].IsWall[3] = true; } } else { BuildBackWallModel(&l->Rooms[i][i2].Walls[3].Model); l->Rooms[i][i2].IsWall[3] = true; } } } } void LoadLevel(VLevel * l, char * name, void * world) { char path[64]; ProcessSerialNumber PID; ProcessInfoRec Info; FSRef FileRef; CFURLRef URL; CFBundleRef Bundle; CFStringRef String; FSSpec Spec; short Ref; int i, i2; long DataSize; char TheData[1024]; char ObjectType[1024]; Boolean added; VObjectWrapper * o; VEnemy * e; VWorld * w; VObject * ob; w = (VWorld *)world; TheData[1] = '\0'; l->Objects = NULL; #ifdef __MWERKS__ String = CFStringCreateWithCString(NULL, name, NULL); #pragma unused (path, PID, Info, Spec) Bundle = CFBundleGetMainBundle(); Bundle = (CFBundleRef)CFRetain((CFTypeRef)Bundle); URL = CFBundleCopyResourceURL(Bundle, String, NULL, NULL); CFURLGetFSRef(URL, &FileRef); FSGetCatalogInfo(&FileRef, kFSCatInfoNone, NULL, NULL, &Spec, NULL); CFRelease((CFTypeRef)Bundle); CFRelease((CFTypeRef)URL); #else sprintf(&path[1], "Levels:%s", name); path[0] = strlen(&path[1]); GetCurrentProcess(&PID); Info.processName = NULL; Info.processAppSpec = &Spec; GetProcessInformation(&PID, &Info); memcpy(&Spec.name, path, 64); #endif FSpOpenDF(&Spec, fsRdPerm, &Ref); for(i2 = 0; i2 < 5; i2++) for(i = 0; i < 5; i++) { DataSize = 1; FSRead(Ref, &DataSize, &TheData[0]); if(TheData[0] == '\r' || TheData[0] == '\n') { DataSize = 1; FSRead(Ref, &DataSize, &TheData[0]); } sscanf(TheData, "%hd", &l->Cells[i][i2]); } CreateRooms(l); while(ReadLn(Ref, TheData)) { added = false; if(!strcmp(TheData, "Enemy\r")) { added = true; e = (VEnemy *)NewPtrClear(sizeof(VEnemy)); InitEnemy(e, TestAI, w); AllocateWObject(&o); o->e = e; o->Type = VENEMY; o->next = l->Objects; do { ReadLn(Ref, TheData); if(!memcmp(TheData, "PosX", 4)) sscanf(TheData, "PosX: %f", &e->Object.Position.x); if(!memcmp(TheData, "PosY", 4)) sscanf(TheData, "PosY: %f", &e->Object.Position.y); if(!memcmp(TheData, "PosZ", 4)) sscanf(TheData, "PosZ: %f", &e->Object.Position.z); if(!memcmp(TheData, "Shields", 7)) sscanf(TheData, "Shields: %d", &e->Shields); } while(strcmp(TheData, "End\r")); } if(added) { if(l->Objects) { l->Objects->previous = o; } l->Objects = o; } added = false; if(!strcmp(TheData, "Object\r")) { added = true; ob = (VObject *)NewPtrClear(sizeof(VObject)); InitObject(ob); AllocateWObject(&o); o->o = ob; o->Type = VOBJECT; o->next = l->Objects; do { ReadLn(Ref, TheData); if(!memcmp(TheData, "PosX", 4)) sscanf(TheData, "PosX: %f", &ob->Position.x); if(!memcmp(TheData, "PosY", 4)) sscanf(TheData, "PosY: %f", &ob->Position.y); if(!memcmp(TheData, "PosZ", 4)) sscanf(TheData, "PosZ: %f", &ob->Position.z); if(!memcmp(TheData, "Type:", 5)) { sscanf(TheData, "Type: %s", ObjectType); if(!strcmp(ObjectType, "Target")) BuildTargetModel(&ob->Model); } } while(strcmp(TheData, "End\r")); CalculateRadius(ob); } if(added) { if(l->Objects) { l->Objects->previous = o; } l->Objects = o; } } FSClose(Ref); }