From f3693157df0217d9dde1638a46a69b37e997c8fb Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <tullivan99@gmail.com>
Date: Fri, 23 Oct 2015 08:41:08 -0400
Subject: fixed everything

---
 main.cpp         | 79 ++++++++++++++++++++--------------------------
 src/entities.cpp | 95 ++++++++++++++++++++++++++++++--------------------------
 src/gameplay.cpp | 70 +++++++++++++++++++++++++++++------------
 src/ui.cpp       | 16 +++++-----
 src/world.cpp    |  2 +-
 5 files changed, 144 insertions(+), 118 deletions(-)

diff --git a/main.cpp b/main.cpp
index 373dbea..a04cd01 100644
--- a/main.cpp
+++ b/main.cpp
@@ -66,44 +66,29 @@ bool gameRunning = true;
 
 /*
  *	currentWorld 	-	This is a pointer to the current world that the player
- * 				is in. Most drawing/entity handling is done through this
- * 				variable. This should only be changed when layer switch
- * 				buttons are pressed (see src/ui.cpp), or when the player
- * 				enters a Structure/Indoor World (see src/ui.cpp again).
+ * 						is in. Most drawing/entity handling is done through this
+ * 						variable. This should only be changed when layer switch
+ * 						buttons are pressed (see src/ui.cpp), or when the player
+ * 						enters a Structure/Indoor World (see src/ui.cpp again).
  * 
- *	player		-	This points to a Player object, containing everything for
- * 				the player. Most calls made with currentWorld require a
- * 				Player object as an argument, and glOrtho is set based
- * 				off of the player's coordinates. This is probably the one
- * 				Entity-derived object that is not pointed to in the entity
- * 				array.
+ *	player			-	This points to a Player object, containing everything for
+ * 						the player. Most calls made with currentWorld require a
+ * 						Player object as an argument, and glOrtho is set based
+ * 						off of the player's coordinates. This is probably the one
+ * 						Entity-derived object that is not pointed to in the entity
+ * 						array.
  * 
- *	entity		 -	Contains pointers to 'all' entities that have been created in
- * 				the game, including NPCs, Structures, and Mobs. World draws
- * 				and entity handling done by the world cycle through entities
- * 				using this array. Entities made that aren't added to this
- * 				array probably won't be noticable by the game.
- * 
- *	npc		 -	An array of all NPCs in the game. It's not exactly clear how
- * 				NPC initing is done, their constructed in this array, then set
- * 				to be pointed to by entity, then maybe spawned with Entity->spawn().
- * 				See src/entities.cpp for more.
- * 				This variable might be referenced as an extern in other files.
- * 
- *	build		 -	An array of all Structures in the game. Entries in entity point to
- *				these, allowing worlds to handle the drawing and stuff of these.
- * 				See src/entities.cpp for more.
- * 
- *	mob		 -	An array of all Mobs in the game, entity entries should point to these
- * 				so the world can take care of them. See src/entities.cpp for more.
+ *	entity			 -	Contains pointers to 'all' entities that have been created in
+ * 						the game, including NPCs, Structures, and Mobs. World draws
+ * 						and entity handling done by the world cycle through entities
+ * 						using this array. Entities made that aren't added to this
+ * 						array probably won't be noticable by the game.
  * 
 */
 
-World  						*currentWorld=NULL;
-Player 						*player;
-std::vector<Entity *	>	 entity;
-std::vector<Structures *>	 build;
-std::vector<Mob			>	 mob;
+World  							*currentWorld=NULL;
+Player 							*player;
+extern std::vector<Entity *	>	 entity;
 
 /*
  *	tickCount contains the number of ticks generated since main loop entrance.
@@ -445,7 +430,7 @@ void mainLoop(void){
 	prevTime	= currentTime;
 	currentTime = millis();
 	deltaTime	= currentTime - prevTime;
-
+	
 	/*
 	 *	Run the logic handler if MSEC_PER_TICK milliseconds have passed.
 	*/
@@ -454,7 +439,7 @@ void mainLoop(void){
 		logic();
 		prevPrevTime = currentTime;
 	}
-
+	
 	/*
 	 *	Update player and entity coordinates.
 	*/
@@ -462,13 +447,16 @@ void mainLoop(void){
 	player->loc.y+= player->vel.y				*deltaTime;
 	player->loc.x+=(player->vel.x*player->speed)*deltaTime;
 	
-	for(int i=0;i<=entity.size();i++){
+	for(int i=0;i<entity.size();i++){
 		
-		entity[i]->loc.x += entity[i]->vel.x * deltaTime;
-		entity[i]->loc.y += entity[i]->vel.y * deltaTime;
+		if(entity[i]->type == NPCT ||
+		   entity[i]->type == MOBT ){
+			entity[i]->loc.x += entity[i]->vel.x * deltaTime;
+			entity[i]->loc.y += entity[i]->vel.y * deltaTime;
 		
-			 if(entity[i]->vel.x<0)entity[i]->left=true;
-		else if(entity[i]->vel.x>0)entity[i]->left=false;
+				 if(entity[i]->vel.x < 0)entity[i]->left = true;
+			else if(entity[i]->vel.x > 0)entity[i]->left = false;
+		}
 	}
 	
 	/*
@@ -661,13 +649,14 @@ void logic(){
 	*/
 	 //std::cout << "Game Loop: "<< loops << std::endl;
 	
-	for(int i=0;i<=entity.size();i++){
+	for(unsigned int i=0;i<entity.size();i++){
 		
 		/*
 		 *	Check if the entity is in this world and is alive.
 		*/
 		
-		if(entity[i]->inWorld==currentWorld&&entity[i]->alive){
+		if(entity[i]->inWorld == currentWorld &&
+		   entity[i]->alive){
 			
 			/*
 			 *	Switch on the entity's type and handle them accordingly.
@@ -685,7 +674,7 @@ void logic(){
 				*/
 			
 				if(entity[i]->canMove)
-					entity[i]->wander((rand() % 120 + 30), &entity[i]->vel);
+					NPCp(entity[i])->wander((rand() % 120 + 30), &entity[i]->vel);
 				
 				/*
 				 *	Check if the NPC is under the mouse.
@@ -721,7 +710,7 @@ void logic(){
 						
 						if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)){
 							
-							entity[i]->interact();
+							NPCp(entity[i])->interact();
 							//Mix_PlayChannel( -1, horn, 0);	// Audio feedback
 							
 						}
@@ -741,7 +730,7 @@ void logic(){
 				 *	Run the Mob's AI function.
 				*/
 				
-				entity[i]->wander((rand()%240 + 15),&entity[i]->vel);	// Make the mob wander
+				Mobp(entity[i])->wander((rand()%240 + 15),&entity[i]->vel);	// Make the mob wander
 				
 				break;	// End case MOBT
 				
diff --git a/src/entities.cpp b/src/entities.cpp
index 6d2b58c..0bc9fe7 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -1,9 +1,10 @@
 #include <entities.h>
 #include <ui.h>
 
-extern std::vector<Entity*>entity;
-//extern std::vector<NPC>npc;
-extern std::vector<Structures>build;
+std::vector<Entity		*>	entity;
+std::vector<NPC			*>	npc;
+std::vector<Structures	*>	build;
+std::vector<Mob			*>	mob;
 
 extern FILE* names;
 extern unsigned int loops;
@@ -13,14 +14,18 @@ void Entity::spawn(float x, float y){	//spawns the entity you pass to it based o
 	loc.y = y;
 	vel.x = 0;
 	vel.y = 0;
-	alive = true;
-	right = true;
-	left  = false;
-	near  = false;
-	canMove = true;
-	ground  = false;
+	
+	alive	= true;
+	right	= true;
+	left	= false;
+	near	= false;
+	canMove	= true;
+	ground	= false;
+	
 	ticksToUse = 0;
+	
 	if(!maxHealth)health = maxHealth = 1;
+	
 	name = (char*)malloc(16);
 	getName();
 }
@@ -28,48 +33,44 @@ void Entity::spawn(float x, float y){	//spawns the entity you pass to it based o
 Player::Player(){ //sets all of the player specific traits on object creation
 	width = HLINE * 10;
 	height = HLINE * 16;
-	speed = 1;
+	
 	type = PLAYERT; //set type to player
 	subtype = 0;
-	maxHealth = 100;
-	health = maxHealth;
-	alive = true;
-	ground = false;
-	near = true;
-	inv = new Inventory(PLAYER_INV_SIZE);
+	
+	health = maxHealth = 100;
+	speed = 1;
+	
 	tex = new Texturec(3, "assets/player.png", "assets/player1.png", "assets/player2.png");
+	inv = new Inventory(PLAYER_INV_SIZE);
 }
 
 NPC::NPC(){	//sets all of the NPC specific traits on object creation
 	width = HLINE * 10;
 	height = HLINE * 16;
-	speed = 1;
-	type = NPCT; //sets type to npc
+	
+	type	= NPCT; //sets type to npc
 	subtype = 0;
-	alive = true;
-	canMove = true;
-	near = false;
+	
 	tex = new Texturec(1,"assets/NPC.png");
 	inv = new Inventory(NPC_INV_SIZE);
 }
 
 Structures::Structures(){ //sets the structure type
-	type = STRUCTURET;
-	speed = 0;
-	alive = true;
-	near = false;
+	health = maxHealth = 1;
+	
+	alive = false;
+	near  = false;
+	
 	tex = new Texturec(1,"assets/house1.png");
 }
 
 Mob::Mob(){
 	width = HLINE * 10;
 	height = HLINE * 8;
-	speed = 1;
+	
 	type = MOBT; //sets type to MOB
 	subtype = 1; //SKIRL
-	alive = true;
-	canMove = true;
-	near = false;
+	
 	tex = new Texturec(2, "assets/rabbit.png", "assets/rabbit.png1");
 	inv = new Inventory(NPC_INV_SIZE);
 }
@@ -227,13 +228,11 @@ void NPC::wander(int timeRun, vec2 *v){ //this makes the entites wander about
 }
 
 std::vector<int (*)(NPC *)> AIpreload;	// A dynamic array of AI functions that are being preloaded
-std::vector<void *> AIpreaddr;			// A dynamic array of pointers to the NPC's that are being assigned the preloads
+std::vector<NPC *> AIpreaddr;			// A dynamic array of pointers to the NPC's that are being assigned the preloads
 
 void NPC::addAIFunc(int (*func)(NPC *),bool preload){
 	if(preload){										// Preload AI functions so that they're given after 
-#ifdef DEBUG											// the current dialog box is closed
-		DEBUG_printf("Preloading an AI %x.\n",func);
-#endif // DEBUG
+														// the current dialog box is closed
 		AIpreload.push_back(func);
 		AIpreaddr.push_back(this);
 	}
@@ -267,30 +266,38 @@ unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure
 	loc.x = x;
 	loc.y = y;
 	type = t;
+	
 	alive = true;
-	health = maxHealth = 1;
 
 	/*VILLAGE*/
-	if(type == STRUCTURET){
-		loc.y=100;
+	switch(type){
+	case STRUCTURET:
 		width =  50 * HLINE;
 		height = 40 * HLINE;
 
 		/*
-		 *	tempN is the amount of entities that will be spawned in the village. As of 10/21/2015 the village
-		 *	can spawn bewteen 2 and 7 villagers for the starting hut.
+		 *	tempN is the amount of entities that will be spawned in the village. Currently the village
+		 *	will spawn bewteen 2 and 7 villagers for the starting hut.
 		*/
-		int tempN = (getRand() % 5 + 2); //amount of villagers that will spawn
+		
+		unsigned int tempN = (getRand() % 5 + 2);
+		
 		for(int i=0;i<tempN;i++){
+			
 			/*
 			 *				This is where the entities actually spawn.
 			 *		A new entity is created with type NPC so polymorphism can be used
 			*/
-			entity.push_back(new NPC()); //create a new entity of NPC type
-			NPCp(entity[entity.size()-1])->spawn(loc.x + (float)(i - 5),100); //sets the position of the villager around the village
+			
+			npc.push_back(new NPC());
+			npc.back()->spawn(loc.x+(i-5),100);
+			
+			entity.push_back(npc.back());
+			
 		}
-		return entity.size();
+		break;
 	}
+	return 0;
 }
 
 /*
@@ -299,8 +306,8 @@ unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure
  *	See NPC::wander for the explaination of the arguments variables
 */
 void Mob::wander(int timeRun, vec2* v){
-	switch(subtype){ //SKIRL
-		case 1:
+	switch(subtype){
+		case 1: //SKIRL
 			static int direction;	//variable to decide what direction the entity moves
 			if(ticksToUse == 0){
 				ticksToUse = timeRun;
diff --git a/src/gameplay.cpp b/src/gameplay.cpp
index 3b9b819..8230e56 100644
--- a/src/gameplay.cpp
+++ b/src/gameplay.cpp
@@ -3,12 +3,12 @@
 #include <ui.h>
 #include <entities.h>
 
-extern World *currentWorld;
-extern std::vector<Entity*>entity;
-extern std::vector<NPC>npc;
-extern std::vector<Structures *>build;
-extern Player *player;
-extern std::vector<Mob>mob;
+extern World	*currentWorld;
+extern Player	*player;
+extern std::vector<Entity		*>	entity;
+extern std::vector<Structures	*>	build;
+extern std::vector<Mob			*>	mob;
+extern std::vector<NPC			*>	npc;
 
 
 extern void mainLoop(void);
@@ -29,37 +29,67 @@ int giveTestQuest(NPC *speaker){
 void initEverything(void){
 	unsigned int i;
 	
+	/*
+	 *	Generate a new world. 
+	*/
+	
 	World *test=new World();
 	test->generate(SCREEN_WIDTH/2);
+	
+	/*
+	 *	Add two layers, a platform, and a hole to the world. 
+	*/
+	
 	test->addLayer(400);
 	test->addLayer(100);
+	
 	test->addPlatform(150,100,100,10);
+
 	test->addHole(100,150);
+	
+	/*
+	 *	Setup the current world, making the player initially spawn in `test`.
+	*/
+	
 	currentWorld=test;
 	
-	// Make the player
+	/*
+	 *	Create the player.
+	*/
+	
 	player=new Player();
 	player->spawn(0,100);
 	
-	// Make structures
-	entity.push_back(new Entity());
+	/*
+	 *	Create a structure (this will create villagers when spawned).
+	*/
+	
 	build.push_back(new Structures());
-	entity[0]=build[0];
-
-	build[0]->spawn(STRUCTURET,(rand()%120*HLINE),10);
+	entity.push_back(build.back());
+	build.back()->spawn(STRUCTURET,(rand()%120*HLINE),10);
+	
+	/*
+	 *	Generate an indoor world and link the structure to it. 
+	*/
+	
 	IndoorWorld *iw=new IndoorWorld();
 	iw->generate(200);
-	build[0]->inside=iw;
+	build.back()->inside=iw;
 
-	entity.push_back(new Mob()); //create a new entity of NPC type
-	mob.push_back(Mob()); //create new NPC
-	entity[entity.size()] = &mob[mob.size()-1]; //set the new entity to have the same traits as an NPC
-	entity[entity.size()-1]->spawn(200,100); //sets the position of the villager around the village
-	entity.pop_back();
+	/*
+	 *	Spawn a mob. 
+	*/
 	
+	mob.push_back(new Mob());
+	entity.push_back(mob.back());
+	mob.back()->spawn(200,100);
 	
+	/*
+	 *	Link all the entities that were just created to the initial world, and setup a test AI function. 
+	*/
 	NPCp(entity[1])->addAIFunc(giveTestQuest,false);
-	for(i=0;i<entity.size()+1;i++){
-		entity[i]->inWorld=test;
+	
+	for(i=0;i<entity.size();i++){
+		entity[i]->inWorld=currentWorld;
 	}
 }
diff --git a/src/ui.cpp b/src/ui.cpp
index f36edeb..9d7de8a 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -6,7 +6,7 @@ extern Player *player;			// 'player' should be (must be) defined in main.cpp
 extern World  *currentWorld;	// should/must also be defined in main.cpp
 
 extern std::vector<int (*)(NPC *)> AIpreload;	// see entities.cpp
-extern std::vector<void *> AIpreaddr;			//
+extern std::vector<NPC *> AIpreaddr;			//
 
 extern bool gameRunning;
 
@@ -261,12 +261,12 @@ namespace ui {
 		}
 		
 		unsigned int i;
-		if(!dialogBoxExists&&AIpreaddr.size()){	// Flush preloaded AI functions if necessary
-			for(i=0;i<AIpreaddr.size();i++){
-				NPCp(AIpreaddr.front())->addAIFunc(AIpreload.front(),false);
-				AIpreaddr.erase(AIpreaddr.begin());
-				AIpreload.erase(AIpreload.begin());
-			}
-		}
+		//if(!dialogBoxExists&&AIpreaddr.size()){	// Flush preloaded AI functions if necessary
+			//for(i=0;i<AIpreaddr.size();i++){
+				//AIpreaddr.front()->addAIFunc(AIpreload.front(),false);
+				//AIpreaddr.erase(AIpreaddr.begin());
+				//AIpreload.erase(AIpreload.begin());
+			//}
+		//}
 	}
 }
diff --git a/src/world.cpp b/src/world.cpp
index 98b9aa7..a05c155 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -182,7 +182,7 @@ LOOP2:													// Draw each world
 			if(p->ground==1&&i<ph+6&&i>ph-6)cline[i].gs=false;
 			else cline[i].gs=true;
 		}
-		for(i=0;i<entity.size()+1;i++){
+		for(i=0;i<entity.size();i++){
 			if(entity[i]->inWorld==this){
 				entity[i]->draw();
 			}
-- 
cgit v1.2.3