All Data Structures Files Functions Variables Enumerations Enumerator Properties Defines
/Projects/Cogito/src/Layers/TerrainLayer.m
Go to the documentation of this file.
00001 //
00002 //  TerrainLayer.m
00003 //  Author: Thomas Taylor
00004 //
00005 //  A generic 'terrain' layer.
00006 //  each level inherits from this class
00007 //
00008 //  24/12/2011: Created class
00009 //
00010 
00011 #import "TerrainLayer.h"
00012 
00013 @interface TerrainLayer()
00014 
00015 -(void)initTerrainFromPlist;
00016 
00017 @end
00018 
00019 @implementation TerrainLayer
00020 
00021 #pragma mark -
00022 #pragma mark Memory Allocation
00023 
00024 -(void)dealloc
00025 {
00026     [super dealloc];
00027     [terrain release];
00028     [obstacles release];
00029 }
00030 
00031 #pragma mark -
00032 #pragma mark Initialisation
00033 
00039 -(id)init:(NSString*)_plist 
00040 {    
00041  self = [super init];
00042     
00043  if (self != nil) 
00044  {
00045         plistFilename = _plist;
00046         
00047         terrain = [[CCArray alloc] init];
00048         obstacles = [[CCArray alloc] init];
00049   
00050         [self initTerrainFromPlist];
00051  }
00052     
00053  return self;
00054 }
00055 
00059 -(void)initTerrainFromPlist
00060 {
00061     // load plist file
00062     NSDictionary *plistDictionary = [Utils loadPlistFromFile:plistFilename];
00063     // if plistDictionary is empty, display error message
00064     if(plistDictionary == nil) { CCLOG(@"TerrainLayer.initTerrainFromPlist: Error loading %@.plist", plistFilename); return; }
00065     
00066     for(NSDictionary *object in plistDictionary)
00067     {        
00068         // the individual object
00069         NSDictionary *objectDictionary = [plistDictionary objectForKey:object];
00070         
00071         // type refers to terrain/obstacle, objectType refers to stamper, exit, trapdoor etc.
00072         NSString* type = [objectDictionary objectForKey:@"type"];
00073         NSString *objectType = [objectDictionary objectForKey:@"objectType"];
00074         GameObjectType gameObjectType;
00075         
00076         // the filename of the image
00077         NSString* filename = [objectDictionary objectForKey:@"filename"];
00078 
00079         // screen coordinates for the object
00080         float x = [[objectDictionary objectForKey:@"x"] floatValue];
00081         float y = [[objectDictionary objectForKey:@"y"] floatValue];
00082 
00083         // used in collision detection
00084         BOOL isWall = ([objectDictionary objectForKey:@"isWall"] == nil) ? NO : [[objectDictionary objectForKey:@"isWall"] boolValue];
00085         BOOL isCollideable = ([objectDictionary objectForKey:@"isCollideable"] == nil) ? YES : [[objectDictionary objectForKey:@"isCollideable"] boolValue];
00086         
00091         if([type isEqualToString:@"terrain"])
00092         {
00093             // set the correct object type
00094             if([objectType isEqualToString:@"exit"]) gameObjectType = kObjectExit;
00095             else if([objectType isEqualToString:@"trapdoor"]) gameObjectType = kObjectTrapdoor;
00096             else if([objectType isEqualToString:@"terrainEnd"]) gameObjectType = kObjectTerrainEnd;
00097             else gameObjectType = kObjectTerrain;
00098             
00099             // initialise the object, and add it to the layer
00100             Terrain *terrainObject = [[Terrain alloc] initObjectType:(GameObjectType)gameObjectType withPosition:ccp(x,y) andFilename:filename isWall:isWall];
00101             terrainObject.isCollideable = isCollideable;
00102             [self addChild:terrainObject z:kTerrainZValue];
00103             [terrain addObject:terrainObject];
00104             
00105             // set the spawn point
00106             if([objectType isEqualToString:@"entrance"]) [GameManager sharedGameManager].currentLevel.spawnPoint = ccp(x,y);
00107         }
00108         else if([type isEqualToString:@"obstacle"])
00109         {            
00110             // set the correct obstacle type
00111             if([objectType isEqualToString:@"spikes"]) gameObjectType = kObstaclePit;
00112             else if([objectType isEqualToString:@"cage"]) gameObjectType = kObstacleCage;
00113             else if([objectType isEqualToString:@"water"]) gameObjectType = kObstacleWater;
00114             else if([objectType isEqualToString:@"stamper"]) gameObjectType = kObstacleStamper;
00115             
00116             // initialise the obstacle from the plist info
00117             Obstacle *obstacleObject = [[Obstacle alloc] initObstacleType:gameObjectType withPosition:ccp(x,y) andFilename:filename];
00118             
00119             // add the obstacle to the layer
00120             [obstacles addObject:obstacleObject];
00121             [self addChild:obstacleObject z:kObstacleZValue];
00122         }
00123     }
00124 }
00125 
00126 
00131 #pragma mark -
00132 #pragma mark Getters/Setters
00133 
00138 -(CCArray*)terrain
00139 {
00140     return terrain;
00141 }
00142 
00143 
00148 -(CCArray*)obstacles
00149 {
00150     return obstacles;
00151 }
00152 
00153 @end