TapestryEngine

A 2D Platformer Game Engine
Log | Files | Refs

3f59b108f92f8357fe0e533161c328fecd39c416.svn-base (19169B)


      1 #include "Level.h"
      2 
      3 Actor* FindRef(std::vector<ActorName*> refs, char* name)
      4 {
      5 	for (int i = 0; i < (int)refs.size(); i++)
      6 	{
      7 		if (strcmp(refs.at(i)->name, name) == false)
      8 		{
      9 			return refs.at(i)->act;
     10 		}
     11 	}
     12 
     13 	return NULL;
     14 }
     15 
     16 bool Level::LoadWorld(cJSON* level)
     17 {
     18 	cJSON* world = cJSON_GetObjectItem(level, "world");
     19 	mHeight = cJSON_GetObjectItem(world, "world_h")->valueint;
     20 	mWidth = cJSON_GetObjectItem(world, "world_w")->valueint;
     21 	
     22 	
     23 	
     24 	char * ter = cJSON_GetObjectItem(world, "terrain")->valuestring;
     25 	mColMap = LoadSurfaceBMP(ter);
     26 
     27 	cJSON* backs = cJSON_GetObjectItem(world, "backgrounds");
     28 	for (int i = 0; i < cJSON_GetArraySize(backs); i++)
     29 	{
     30 		cJSON* layer = cJSON_GetArrayItem(backs, i);
     31 		char* back = cJSON_GetObjectItem(layer, "back")->valuestring;
     32 
     33 		SDL_Texture* back_tex = SDL_CreateTextureFromSurface(mRen, LoadSurfaceBMP(back));
     34 		int alpha = cJSON_GetObjectItem(layer, "alpha")->valueint;
     35 
     36 		if (alpha != 0)
     37 		{
     38 			SDL_SetTextureAlphaMod(back_tex, alpha);
     39 		}
     40 
     41 		mBack.push_back(back_tex);
     42 
     43 		float p_factor = (float)cJSON_GetObjectItem(layer, "parallax_factor")->valueint;
     44 		mParallax_back.push_back(p_factor);
     45 	}
     46 
     47 	cJSON* fores = cJSON_GetObjectItem(world, "foregrounds");
     48 	for (int i = 0; i < cJSON_GetArraySize(fores); i++)
     49 	{
     50 		cJSON* layer = cJSON_GetArrayItem(fores, i);
     51 		char* fore = cJSON_GetObjectItem(layer, "fore")->valuestring;
     52 
     53 		SDL_Texture* fore_tex = SDL_CreateTextureFromSurface(mRen, LoadSurfaceBMP(fore));
     54 		int alpha = cJSON_GetObjectItem(layer, "alpha")->valueint;
     55 
     56 		if (alpha != 0)
     57 		{
     58 			SDL_SetTextureAlphaMod(fore_tex, alpha);
     59 		}
     60 
     61 		mFore.push_back(fore_tex);
     62 	}
     63 
     64 	cJSON* overs = cJSON_GetObjectItem(world, "overlays");
     65 	for (int i = 0; i < cJSON_GetArraySize(overs); i++)
     66 	{
     67 		cJSON* layer = cJSON_GetArrayItem(overs, i);
     68 		char* overs = cJSON_GetObjectItem(layer, "over")->valuestring;
     69 
     70 		SDL_Texture* over_tex = SDL_CreateTextureFromSurface(mRen, LoadSurfaceBMP(overs));
     71 		int alpha = cJSON_GetObjectItem(layer, "alpha")->valueint;
     72 
     73 		if (alpha != 0)
     74 		{
     75 			SDL_SetTextureAlphaMod(over_tex, alpha);
     76 		}
     77 
     78 		mOver.push_back(over_tex);
     79 
     80 		float p_factor = (float)cJSON_GetObjectItem(layer, "parallax_factor")->valueint;
     81 		mParallax_over.push_back(p_factor);
     82 	}
     83 	return true;
     84 }
     85 
     86 bool Level::LoadCamera(cJSON* level)
     87 {
     88 	cJSON* camera = cJSON_GetObjectItem(level, "camera");
     89 	int camx = cJSON_GetObjectItem(camera, "x")->valueint;
     90 	int camy = cJSON_GetObjectItem(camera, "y")->valueint;
     91 	int camh = cJSON_GetObjectItem(camera, "h")->valueint;
     92 	int camw = cJSON_GetObjectItem(camera, "w")->valueint;
     93 
     94 	mCam = Camera(camh, camw, camx, camy, mHeight, mWidth);
     95 	
     96 	cJSON* blockers = cJSON_GetObjectItem(camera, "blockers");
     97 	
     98 	for (int i = 0; i < cJSON_GetArraySize(blockers); i++)
     99 	{
    100 		cJSON* blocker = cJSON_GetArrayItem(blockers, i);
    101 		SDL_Rect* r = new SDL_Rect;
    102 		r->h = cJSON_GetObjectItem(blocker, "h")->valueint;
    103 		r->w = cJSON_GetObjectItem(blocker, "w")->valueint;
    104 		r->x = cJSON_GetObjectItem(blocker, "x")->valueint;
    105 		r->y = cJSON_GetObjectItem(blocker, "y")->valueint;
    106 
    107 		mCam.LogBlocker(r);
    108 	}
    109 
    110 	return true;
    111 }
    112 
    113 bool Level::LoadAnimations(cJSON* level)
    114 {
    115 	cJSON* animations = cJSON_GetObjectItem(level, "animations");
    116 	mAnim_names = (char**)malloc(cJSON_GetArraySize(animations) * sizeof(char*));
    117 	for (int i = 0; i < cJSON_GetArraySize(animations); i++)
    118 	{
    119 		cJSON* anim_pack = cJSON_GetArrayItem(animations, i);
    120 		mAnim_names[i] = cJSON_GetObjectItem(anim_pack, "pack_name")->valuestring;
    121 		//gCons->ConsPrintf("Animation Pack Name: %s\n", anim_names[i]);
    122 
    123 		cJSON* frame_size = cJSON_GetObjectItem(anim_pack, "frame_size");
    124 		int h = cJSON_GetObjectItem(frame_size, "h")->valueint;
    125 		int w = cJSON_GetObjectItem(frame_size, "w")->valueint;
    126 
    127 		cJSON* col_data = cJSON_GetObjectItem(anim_pack, "col_data");
    128 		int off_x = cJSON_GetObjectItem(col_data, "off_x")->valueint;
    129 		int off_y = cJSON_GetObjectItem(col_data, "off_y")->valueint;
    130 		int col_h = cJSON_GetObjectItem(col_data, "col_h")->valueint;
    131 		int col_w = cJSON_GetObjectItem(col_data, "col_w")->valueint;
    132 
    133 		cJSON* anim_set = cJSON_GetObjectItem(anim_pack, "anim_set");
    134 		cJSON* special_anims = cJSON_GetObjectItem(anim_pack, "special_anims");
    135 
    136 		int anim_count			= cJSON_GetArraySize(anim_set);
    137 		int special_anim_count  = cJSON_GetArraySize(special_anims);
    138 		int total_anims = (anim_count + special_anim_count);
    139 
    140 
    141 		FrameSet* frame_data = (FrameSet*)malloc( (total_anims)* sizeof(FrameSet));
    142 		int* Index = (int*)malloc( (total_anims) * sizeof(int));
    143 
    144 		//int j;
    145 		for (int j = 0; j < anim_count; j++)
    146 		{
    147 			cJSON* anim = cJSON_GetArrayItem(anim_set, j);
    148 			char* filename = cJSON_GetObjectItem(anim, "filename")->valuestring;
    149 			int frame_count = cJSON_GetObjectItem(anim, "frame_count")->valueint;
    150 			int frame_rate = cJSON_GetObjectItem(anim, "frame_rate")->valueint;
    151 			//int h = cJSON_GetObjectItem(anim, "h")->valueint;
    152 			//int w = cJSON_GetObjectItem(anim, "w")->valueint;
    153 			char* id_str = cJSON_GetObjectItem(anim, "id")->valuestring;
    154 
    155 			int id = ConvertStringToSymbol(id_str);
    156 			//gCons->ConsPrintf("image source path: %s\n", filename);
    157 			frame_data[j] = FrameSet(mRen, filename, frame_count, frame_rate, h, w);
    158 			Index[j] = id;
    159 		}
    160 
    161 		for (int k= 0; k < (special_anim_count); k++)
    162 		{
    163 			cJSON* anim = cJSON_GetArrayItem(special_anims, k);
    164 			char* filename = cJSON_GetObjectItem(anim, "filename")->valuestring;
    165 			int frame_count = cJSON_GetObjectItem(anim, "frame_count")->valueint;
    166 			int frame_rate = cJSON_GetObjectItem(anim, "frame_rate")->valueint;
    167 			h = cJSON_GetObjectItem(anim, "custom_h")->valueint;
    168 			w = cJSON_GetObjectItem(anim, "custom_W")->valueint;
    169 			char* id_str = cJSON_GetObjectItem(anim, "id")->valuestring;
    170 
    171 			int id = ConvertStringToSymbol(id_str);
    172 			//gCons->ConsPrintf("image source path: %s\n", filename);
    173 
    174 			frame_data[anim_count + k] = FrameSet(mRen, filename, frame_count, frame_rate, h, w);
    175 
    176 			Index[anim_count + k] = id;
    177 
    178 		}
    179 
    180 		AnimDataPack* pack = new AnimDataPack(total_anims, frame_data, Index);
    181 		pack->SetColData(off_x, off_y, col_h, col_w);
    182 		mAnimData.push_back(pack);
    183 
    184 
    185 		LoadAnimGraph(anim_pack);
    186 	}
    187 
    188 	return true;
    189 }
    190 
    191 bool Level::LoadAnimGraph(cJSON* anim_pack)
    192 {
    193 	AnimGraphData* anim_g = new AnimGraphData( *mAnimData.back() );
    194 
    195 	cJSON* loop_ids = cJSON_GetObjectItem(anim_pack, "loop_ids");
    196 	for (int i = 0; i < (int)cJSON_GetArraySize(loop_ids); i++)
    197 	{
    198 		int id = ConvertStringToSymbol(cJSON_GetArrayItem(loop_ids, i)->valuestring);
    199 		anim_g->DefineLoop(id);
    200 	}
    201 	anim_g->CreateTranisitionMatrix();
    202 
    203 	cJSON* transition_ids = cJSON_GetObjectItem(anim_pack, "transition_ids");
    204 	for (int i = 0; i < (int)cJSON_GetArraySize(transition_ids); i++)
    205 	{
    206 		int id = ConvertStringToSymbol(cJSON_GetArrayItem(transition_ids, i)->valuestring);
    207 		anim_g->DefineTransitions(id);
    208 	}
    209 
    210 	cJSON* fireables_ids = cJSON_GetObjectItem(anim_pack, "fireables_ids");
    211 	for (int i = 0; i < (int)cJSON_GetArraySize(fireables_ids); i++)
    212 	{
    213 		int id = ConvertStringToSymbol(cJSON_GetArrayItem(fireables_ids, i)->valuestring);
    214 		anim_g->DefineFireables(id);
    215 	}
    216 
    217 	cJSON* anim_graph = cJSON_GetObjectItem(anim_pack, "anim_graph");
    218 	for (int i = 0; i < (int)cJSON_GetArraySize(anim_graph); i++)
    219 	{
    220 		cJSON* loop = cJSON_GetArrayItem(anim_graph, i);
    221 		char* loop_name = cJSON_GetObjectItem(loop, "loop_name")->valuestring;
    222 		int init_id = ConvertStringToSymbol(loop_name);
    223 
    224 		cJSON* transitions = cJSON_GetObjectItem(loop, "transitions");
    225 		for(int j = 0; j < (int)cJSON_GetArraySize(transitions); j++)
    226 		{
    227 			char* transition = cJSON_GetArrayItem(transitions, j)->valuestring;
    228 			int trans_id = ConvertStringToSymbol(transition);
    229 			
    230 			int target_id = anim_g->GetLoopIDIndex(j);
    231 
    232 			anim_g->PopulateTransitionMatrix(init_id, target_id, trans_id);
    233 		}
    234 	}
    235 	mAnimGraphs.push_back(anim_g);
    236 	
    237 	return false;
    238 }
    239 
    240 bool Level::LoadActors(cJSON* level)
    241 {
    242 	cJSON* actors = cJSON_GetObjectItem(level, "actors");
    243 	std::vector<ActorName*> ActorRefs;
    244 
    245 	mAHM = ActorHandleManager();
    246 	mSpatMon = SpatialMonitor(mHeight, mWidth, &mAHM);
    247 
    248 	for (int i = 0; i < cJSON_GetArraySize(actors); i++)
    249 	{
    250 		cJSON* act = cJSON_GetArrayItem(actors, i);
    251 		
    252 		char* anim_pack = cJSON_GetObjectItem(act, "anim_pack")->valuestring;
    253 		//gCons->ConsPrintf("sought set name: %s\n", anim_pack);
    254 
    255 		AnimGraph* actor_pack = NULL;
    256 		for (int j = 0; j <  (int)mAnimData.size(); j++)
    257 		{
    258 			if (!strcmp(anim_pack, mAnim_names[j]))
    259 			{
    260 				actor_pack = new AnimGraph(mAnimGraphs.at(j));
    261 			}
    262 		}
    263 #ifdef DEBUG
    264 		assert(actor_pack != NULL);
    265 #endif
    266 
    267 	
    268 		SDL_Rect PosRect;
    269 		PosRect.x = cJSON_GetObjectItem(act, "x")->valueint;
    270 		PosRect.y = cJSON_GetObjectItem(act, "y")->valueint;
    271 		PosRect.h = actor_pack->GetAnimPack()->GetAnimData()->GetCol_h();
    272 		PosRect.w = actor_pack->GetAnimPack()->GetAnimData()->GetCol_w();
    273 		SDL_Rect DrawRect;
    274 		DrawRect.x = actor_pack->GetAnimPack()->GetAnimData()->GetOff_x();
    275 		DrawRect.y = actor_pack->GetAnimPack()->GetAnimData()->GetOff_y();
    276 		DrawRect.h = actor_pack->GetH();
    277 		DrawRect.w = actor_pack->GetW();
    278 
    279 		ActorName* ref = new ActorName;
    280 		ref->name = cJSON_GetObjectItem(act, "name")->valuestring;
    281 
    282 		char* type = cJSON_GetObjectItem(act, "type")->valuestring;
    283 		if (!strcmp(type, "player"))
    284 		{
    285 			Player* actor = new Player(mColMap, actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib); //needs a delete
    286 			ref->act = actor;
    287 			//mSpatMon.LogActor(*actor);
    288 			mInput->mFeed.Subscribe(actor);
    289 
    290 			mCam.SetFollowTarget(actor);
    291 			mCast.push_back(actor);
    292 			mPlayer = actor;
    293 
    294 			AnimGraph* hat_pack = NULL;
    295 			for (int j = 0; j < (int)mAnimData.size(); j++)
    296 			{
    297 				if (!strcmp("sword", mAnim_names[j]))
    298 				{
    299 					hat_pack = new AnimGraph(mAnimGraphs.at(j));
    300 				}
    301 			}
    302 			actor->SetSword(actor->AttachHat(new sword(hat_pack, actor, &mAHM, NULL, &mPrtLib, &mSoundLib)));
    303 		}
    304 		else if (!strcmp(type, "test_blocker"))
    305 		{
    306 			Test_Blocker* actor = new Test_Blocker(actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib);
    307 			ref->act = actor;
    308 			//mSpatMon.LogActor(*actor);
    309 			mCast.push_back(actor);
    310 		}
    311 		else if (!strcmp(type, "gate")) //Gate is a multiactor entity.
    312 		{
    313 			int dir = ConvertStringToSymbol(cJSON_GetObjectItem(act, "dir")->valuestring);
    314 			Gate* actor = new Gate(actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib, dir);
    315 			//mSpatMon.LogActor(*actor);
    316 			mCast.push_back(actor);
    317 			actor_pack->mFeed.Subscribe(actor);
    318 
    319 			AnimGraph* winch_pack = NULL;
    320 			for (int j = 0; j < (int)mAnimData.size(); j++)
    321 			{
    322 				if (!strcmp("winch", mAnim_names[j]))
    323 				{
    324 					winch_pack = new AnimGraph(mAnimGraphs.at(j));
    325 				}
    326 			}
    327 			PosRect.h = winch_pack->GetAnimPack()->GetAnimData()->GetCol_h();
    328 			PosRect.w = winch_pack->GetAnimPack()->GetAnimData()->GetCol_w();
    329 			PosRect.y -= 29;
    330 			if (dir == LEFT)
    331 			{
    332 				PosRect.x += 7;
    333 			}
    334 			else //dir == right
    335 			{
    336 				PosRect.x -= (21);
    337 			}
    338 
    339 			DrawRect.x = winch_pack->GetAnimPack()->GetAnimData()->GetOff_x();
    340 			DrawRect.y = winch_pack->GetAnimPack()->GetAnimData()->GetOff_y();
    341 			DrawRect.h = winch_pack->GetH();
    342 			DrawRect.w = winch_pack->GetW();
    343 			Winch* actor2 = new Winch(winch_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib, actor, dir);
    344 			ref->act = actor2;  //Gate ref refers to the paired winch because its more useful
    345 			actor2->mFeed.Subscribe(actor);
    346 			//mSpatMon.LogActor(*actor2);
    347 			mCast.push_back(actor2);
    348 		}
    349 		else if (!strcmp(type, "gateman")) //Gateman must be attached to a gate. currently hardcoded to actor name "gate0"
    350 		{
    351 			Gateman* actor = new Gateman(actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib, cJSON_GetObjectItem(act, "lines")->valuestring, FindRef(ActorRefs, "gate0")->GetHandle());
    352 			ref->act = actor;
    353 			//mSpatMon.LogActor(*actor);
    354 			mCast.push_back(actor);
    355 		}
    356 		else if (!strcmp(type, "talker"))
    357 		{
    358 			Talker* actor = new Talker(actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib, cJSON_GetObjectItem(act, "lines")->valuestring);
    359 			ref->act = actor;
    360 			//mSpatMon.LogActor(*actor);
    361 			mCast.push_back(actor);
    362 		}
    363 		else if (!strcmp(type, "door"))
    364 		{
    365 			int x_off = cJSON_GetObjectItem(act, "x_off")->valueint;
    366 			int y_off = cJSON_GetObjectItem(act, "y_off")->valueint;
    367 			Door* actor = new Door(actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib, x_off, y_off);
    368 			ref->act = actor;
    369 			PosRect.x = cJSON_GetObjectItem(act, "x2")->valueint;
    370 			PosRect.y = cJSON_GetObjectItem(act, "y2")->valueint;
    371 			Door* actor2 = new Door(actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib, x_off, y_off);
    372 			actor->SetTeleDest(actor2->GetTelePos());
    373 			actor2->SetTeleDest(actor->GetTelePos());
    374 			SDL_Rect* r = new SDL_Rect;
    375 			r->h = 0;
    376 			r->w = 0;
    377 			r->x = 100;
    378 			r->y = 100;
    379 			//actor->SetTeleDest(r);
    380 			//mSpatMon.LogActor(*actor);
    381 			//mSpatMon.LogActor(*actor2);
    382 			mCast.push_back(actor);
    383 			mCast.push_back(actor2);
    384 		}
    385 		else if (!strcmp(type, "sludge_seal"))
    386 		{
    387 			Sludge_Seal* actor = new Sludge_Seal(mColMap, actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib);
    388 			ref->act = actor;
    389 			//mSpatMon.LogActor(*actor);
    390 			mCast.push_back(actor);
    391 		}
    392 		else if (!strcmp(type, "pursuer"))
    393 		{
    394 			Pursuer* actor =  new Pursuer(mColMap, actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib);
    395 			ref->act = actor;
    396 			//mSpatMon.LogActor(*actor);
    397 			mCast.push_back(actor);
    398 		}
    399 		else if (!strcmp(type, "wanderer"))
    400 		{
    401 			Wanderer* actor = new Wanderer(mColMap, actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib);
    402 			ref->act = actor;
    403 			//mSpatMon.LogActor(*actor);
    404 			mCast.push_back(actor);
    405 		}
    406 		else if (!strcmp(type, "rabbit"))
    407 		{
    408 			Rabbit* actor = new Rabbit(mColMap, actor_pack, PosRect, DrawRect, &mAHM, &mSpatMon, &mPrtLib, &mSoundLib);
    409 			ref->act = actor;
    410 			//mSpatMon.LogActor(*actor);
    411 			mCast.push_back(actor);
    412 		}
    413 		else
    414 		{
    415 			gCons->ConsPrintf("Failed to load actor: %s is an unrecognized actor type\n", type);
    416 		}
    417 		ActorRefs.push_back(ref);
    418 	}
    419 	
    420 	return true;
    421 }
    422 
    423 bool Level::LoadParticles(cJSON* level)
    424 {
    425 	cJSON* particles = cJSON_GetObjectItem(level, "particles");
    426 	for (int i = 0; i < cJSON_GetArraySize(particles); i++)
    427 	{
    428 		cJSON* prt = cJSON_GetArrayItem(particles, i);
    429 		
    430 		char* anim_pack = cJSON_GetObjectItem(prt, "anim_pack")->valuestring;
    431 		//gCons->ConsPrintf("sought set name: %s\n", anim_pack);
    432 
    433 		//AnimDataPack* particle_anims = NULL;
    434 		AnimGraph* particle_anims = NULL;
    435 		for (int j = 0; j < (int)mAnimData.size(); j++)
    436 		{
    437 			//gCons->ConsPrintf("stored set name: %s\n", anim_names[j]);
    438 			if (!strcmp(anim_pack, mAnim_names[j]))
    439 			{
    440 				particle_anims = new AnimGraph(mAnimGraphs.at(j));
    441 			}
    442 		}
    443 #ifdef DEBUG
    444 		assert(particle_anims != NULL);
    445 #endif
    446 		int prth = particle_anims->GetH();
    447 		int prtw = particle_anims->GetW();
    448 		
    449 		ParticleData* pd = new ParticleData;
    450 		pd->AnimData = particle_anims;
    451 		pd->Height = prth;
    452 		pd->Width = prtw;
    453 		pd->spat = &mSpatMon;
    454 		
    455 		mPrtLib.LogParticleData(pd, anim_pack);
    456 		return true;
    457 	}
    458 	return false;
    459 }
    460 
    461 bool Level::LoadSounds(cJSON* level)
    462 {
    463 	cJSON* sounds = cJSON_GetObjectItem(level, "sounds");
    464 
    465 	for (int i = 0; i < cJSON_GetArraySize(sounds); i++)
    466 	{
    467 		cJSON* sound = cJSON_GetArrayItem(sounds, i);
    468 		char* fname = cJSON_GetObjectItem(sound, "filename")->valuestring;
    469 		char* name  = cJSON_GetObjectItem(sound, "name"    )->valuestring;
    470 		int vol     = cJSON_GetObjectItem(sound, "vol"     )->valueint;
    471 
    472 		Mix_Chunk* snd = new Mix_Chunk;
    473 		snd = LoadSound(fname);
    474 		Mix_VolumeChunk(snd, vol);
    475 
    476 		mSoundLib.LogSoundData(snd, name);
    477 	}
    478 
    479 	cJSON* sound_regions = cJSON_GetObjectItem(level, "sound_regions");
    480 
    481 	for (int i = 0; i < cJSON_GetArraySize(sound_regions); i++)
    482 	{
    483 		cJSON* region = cJSON_GetArrayItem(sound_regions, i);
    484 		SDL_Rect reg_rect;
    485 		reg_rect.x = cJSON_GetObjectItem(region, "x")->valueint;
    486 		reg_rect.y = cJSON_GetObjectItem(region, "y")->valueint;
    487 		reg_rect.h = cJSON_GetObjectItem(region, "h")->valueint;
    488 		reg_rect.w = cJSON_GetObjectItem(region, "w")->valueint;
    489 
    490 		RegionSound* rs = mSoundLib.SpawnRegionSound(reg_rect);
    491 
    492 		cJSON* tracks = cJSON_GetObjectItem(region, "tracks");
    493 		for (int j = 0; j < cJSON_GetArraySize(tracks); j++)
    494 		{
    495 			char* track = cJSON_GetArrayItem(tracks, j)->valuestring;
    496 			rs->LogTrack(mSoundLib.GetSoundData(track));
    497 		}
    498 	}
    499 
    500 	return false;
    501 }
    502 
    503 bool Level::LoadHud(cJSON* level)
    504 {
    505 	cJSON* texs = cJSON_GetObjectItem(level, "hud_textures");
    506 
    507 	//Load LifeMeter Widget
    508 	char* life_base = cJSON_GetObjectItem(texs, "life_base")->valuestring;
    509 	char* life_bar = cJSON_GetObjectItem(texs, "life_bar" )->valuestring;
    510 
    511 	SDL_Rect LifePos;
    512 	LifePos.h = 15;
    513 	LifePos.w = 250;
    514 	LifePos.x = 25;
    515 	LifePos.y = 25;
    516 	mHUD.LogWidget( new LifeMeter(mPlayer, LifePos, mRen, life_base, life_bar) );
    517 	//
    518 
    519 	//Load Fader widget
    520 	char* black = cJSON_GetObjectItem(texs, "fader_black")->valuestring;
    521 	char* red = cJSON_GetObjectItem(texs, "fader_red")->valuestring;
    522 	Fader* f = new Fader(mRen, black, red);
    523 	mPlayer->mFeed.Subscribe(f);
    524 	mHUD.LogWidget(f);
    525 	//
    526 
    527 	//Load Dialogue Widget
    528 	char* text_box = cJSON_GetObjectItem(texs, "text_box")->valuestring;
    529 
    530 	SDL_Rect DialoguePos;
    531 	DialoguePos.h = 0;
    532 	DialoguePos.w = 0;
    533 	DialoguePos.x = 300;
    534 	DialoguePos.y = 200;
    535 
    536 	SDL_Color fg = {0,0,0};
    537 
    538 	Dialogue* d = new Dialogue(DialoguePos, mRen, "Tapestry.ttf", text_box, fg, mPlayer);
    539 	mPlayer->mFeed.Subscribe(d);
    540 
    541 	mHUD.LogWidget(d);
    542 	//
    543 
    544 	return true;
    545 }
    546 
    547 bool Level::Load(const char* filename)
    548 {
    549 	cJSON* root = LoadJSON(filename);
    550 	//PrintJSON(root);
    551 	cJSON* level = cJSON_GetObjectItem(root, "level");
    552 
    553 	LoadWorld(level);
    554 
    555 	LoadCamera(level);
    556 
    557 	gDiagDraw = new DiagnosticDraw(mRen, mCam);
    558 
    559 	LoadAnimations(level);
    560 
    561 	LoadActors(level);
    562 
    563 	LoadParticles(level);
    564 
    565 	LoadSounds(level);
    566 
    567 	LoadHud(level);
    568 
    569 //	int cells[9];
    570 
    571 //	mSpatMon.GetAdjacentCells(36, cells);
    572 
    573 	return true;
    574 }
    575 
    576 bool Level::Update()
    577 {
    578 	///Update Actors
    579 	for (int i = 0; i < (int)(mCast.size()); i++)
    580 	{
    581 		mCast.at(i)->ActorUpdate();
    582 	}
    583 	///Update Particles
    584 	for (int i = 0; i < (int)(mPrtLib.GetParticles()->size()); i++)
    585 	{
    586 		mPrtLib.GetParticles()->at(i)->ActorUpdate();
    587 	}
    588 
    589 	///Draw HUD
    590 	mHUD.UpdateWidgets();
    591 
    592 	mCam.ActorFollow(mRen, 150, 75);
    593 
    594 	///Draw Backgrounds
    595 	for (int i = 0; i < (int)mBack.size(); i++)
    596 	{
    597 		DrawParallax(mRen, mBack.at(i), mCam, mParallax_back.at(i), mHeight, mWidth);
    598 	}
    599 
    600 	///Draw Foregrounds
    601 	for (int i = 0; i < (int)mFore.size(); i++)
    602 	{
    603 		SDL_RenderCopy(mRen, mFore.at(i), mCam.GetCamView(), NULL);
    604 	}
    605 
    606 	///Draw Actors
    607 	for (int i = 0; i < (int)mCast.size(); i++)
    608 	{
    609 		DrawActor(mRen, *mCast.at(i), mCam);
    610 	}
    611 
    612 	///Draw Particles
    613 	for (int i = 0; i < (int)(mPrtLib.GetParticles()->size()); i++)
    614 	{
    615 		DrawActor(mRen, *mPrtLib.GetParticles()->at(i), mCam);
    616 	}
    617 
    618 	///Draw overlays
    619 	for (int l = 0; l < (int)mOver.size(); l++)
    620 	{
    621 		DrawParallax(mRen, mOver.at(l), mCam, mParallax_over.at(l), mHeight, mWidth);
    622 	}
    623 
    624 	mHUD.DrawWidgets();
    625 
    626 	///Play Sounds
    627 	mSoundLib.UpdateRegionSounds(mPlayer->GetPosition());
    628 	mSoundLib.UpdatePointSounds(mCam.GetCenterX(), mCam.GetCenterY());
    629 	
    630 	//gCons->ConsPrintf("%d channels are playing\n", Mix_Playing(-1));
    631 
    632 	///Draw Diagnositic rectangles
    633 	#ifdef DEBUG
    634 	gDiagDraw->Update();
    635 	#endif
    636 	
    637 	return true;
    638 }
    639 
    640 bool Level::EventProcess(Event eve)
    641 {
    642 	return true;
    643 }