From 419d06f319c631c9c4860aababbfc49ee26aeefc Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <tullivan99@gmail.com>
Date: Fri, 25 Sep 2015 16:15:35 -0400
Subject: updates ;)

---
 Changelog          |  7 +++++++
 include/entities.h |  5 +++--
 include/world.h    |  7 +------
 src/entities.cpp   |  3 ++-
 src/main.cpp       | 37 +++++++++++++++++++++++++++++--------
 src/world.cpp      | 27 +++++++++------------------
 6 files changed, 51 insertions(+), 35 deletions(-)

diff --git a/Changelog b/Changelog
index a781c55..0e6d0a8 100644
--- a/Changelog
+++ b/Changelog
@@ -15,3 +15,10 @@
 	- added dialog boxes and a key binding to acknoledge them (make them disappear)
 	- added a togglable debug overlay thing (F3)
 	- added villages
+
+9/24/2015:
+==========
+
+	- improved entity binding
+	- added structures, villagers, and a basic villager AI
+
diff --git a/include/entities.h b/include/entities.h
index e7389dc..2687c24 100644
--- a/include/entities.h
+++ b/include/entities.h
@@ -5,6 +5,7 @@
 
 class Entity{
 public:
+	void *inWorld;
 	float width;
 	float height;
 	float speed;
@@ -15,7 +16,7 @@ public:
 	bool alive;
 
 	unsigned int texture[];
-
+	
 	void spawn(float, float);
 	void draw(void);
 	void wander(int, vec2*);
@@ -38,7 +39,7 @@ public:
 class Structures : public Entity{
 public:
 	Structures();
-	void spawn(int, float, float);
+	unsigned int spawn(int, float, float);
 };
 
 #endif // ENTITIES_H
diff --git a/include/world.h b/include/world.h
index 2a1d3c3..ff1a3ef 100644
--- a/include/world.h
+++ b/include/world.h
@@ -3,8 +3,6 @@
 
 #include <common.h> // For HLINE, vec2, OpenGL utilities, etc.
 
-//#define WORLD_ENTITY_MAX 64 // Maximum number of entities that can be bound to a world
-
 /*
  *	World - creates and handles an area of land
 */
@@ -29,8 +27,6 @@ private:
 	int x_start;			// Worlds are centered on the x axis (0,n), this contains
 							// where to start drawing the world to have it centered properly.
 	World *behind,*infront;	// Pointers to other areas of land that are behind or in front of this one, respectively.
-	//Entity **peeps;			// Stores pointers to entities that are bound to the world
-	//unsigned int peepCount; // Number of entities bound to the world
 	void singleDetect(Entity *e);
 public:
 	World *toLeft,*toRight;		// Pointers to areas to the left and right of this world. These are made public
@@ -59,8 +55,7 @@ public:
 														// world is drawn the world has to appear directly behind the player)
 	World *goWorldFront(Player *p);						// Functions the same as goWorldBack(), but checks/returns the world in front of
 														// this one.
-														
-	//void addEntity(Entity *e);							// Binds an entity to this world (this world will then handle its drawing and detection)
+	
 };
 
 #endif // WORLD_H
diff --git a/src/entities.cpp b/src/entities.cpp
index 9c18313..5da39dd 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -64,7 +64,7 @@ Structures::Structures(){
 	speed = 0;
 }
 
-void Structures::spawn(int t, float x, float y){
+unsigned int Structures::spawn(int t, float x, float y){
 	loc.x = x;
 	loc.y = y;
 	type = t;
@@ -86,5 +86,6 @@ void Structures::spawn(int t, float x, float y){
 			entity[entity.size()]->type = 1;
 			entity[entity.size()]->spawn(loc.x + (float)(i - 5) / 8,0);
 		}
+		return entity.size();
 	}
 }
diff --git a/src/main.cpp b/src/main.cpp
index 327bd66..6049d2c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -81,20 +81,41 @@ int main(int argc, char *argv[]){
 	****     GAMELOOP      ****
 	**************************/
 
-	World *test =new World(SCREEN_WIDTH/2),
-		  *test2=new World(SCREEN_WIDTH*2);
+	//************************************************************************//
+	//	WORLD GENERATION STUFF												  //
+	//************************************************************************//
+
+	// Make a world
+	World *test =new World(SCREEN_WIDTH/2);
 	test->addLayer(400);
-	test->addLayer(100);
-	test->toLeft=test2;
-	test2->toRight=test;
+	test->addLayer(100);	
 	currentWorld=test;
+	
+	// Make the player
 	player=new Player();
 	player->spawn(0,100);
-
-	entity.push_back(new Entity());//create the blank first element for the player; 
+	
+	// Make structures
+	entity.push_back(new Entity());
 	build.push_back(Structures());
 	entity[0]=&build[0];
+	
+	static unsigned int i;
 	build[0].spawn(-1,0,10);
+	for(i=0;i<entity.size()+1;i++){
+		entity[i]->inWorld=test;
+	}
+	for(i=0;i<entity.size();i++){
+		std::cout<<(unsigned)&*entity[i]<<std::endl;
+	}
+	std::cout<<std::endl;
+	for(i=0;i<npc.size();i++){
+		std::cout<<(unsigned)&npc[i]<<std::endl;
+	}
+	
+	//************************************************************************//
+	//	END WORLD GENERATION STUFF											  //
+	//************************************************************************//
 
 	currentTime=millis();
 	while(gameRunning){
@@ -157,7 +178,7 @@ void render(){
 	ui::draw();							// Draw any UI elements if they need to be
 
 	for(int i=0;i<=entity.size();i++){
-		entity[i]->draw();
+		//entity[i]->draw();
 		entity[i]->loc.x += entity[i]->vel.x * deltaTime;
 		entity[i]->loc.y += entity[i]->vel.y * deltaTime;
 	}
diff --git a/src/world.cpp b/src/world.cpp
index b279f2f..fde75bb 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -50,13 +50,10 @@ World::World(unsigned int width){		// Generates the world and sets all variables
 	x_start=0-getWidth(this)/2+GEN_INC/2*HLINE;	// Calculate x_start (explained in world.h)
 	behind=infront=NULL;						// Set pointers to other worlds to NULL
 	toLeft=toRight=NULL;						// to avoid accidental calls to goWorld... functions
-	//peeps=(Entity **)calloc(WORLD_ENTITY_MAX+1,sizeof(Entity *));	// peeps[0] is reserved for the player when detect() is called
-	//peepCount=0;
 }
 
 World::~World(void){
 	free(line);	// Free (de-allocate) the array 'line'
-	//free(peeps); // same for the entity array
 }
 
 void World::draw(vec2 *vec){
@@ -106,11 +103,10 @@ LOOP2:													// Draw each world
 	}else{							// Otherwise reset static values and return
 		yoff=DRAW_Y_OFFSET;
 		shade=0;
-		/*if(peepCount){
-			for(i=1;i<peepCount;i++){
-				peeps[i]->draw();
-			}
-		}*/
+		for(i=0;i<entity.size()+1;i++){
+			if(entity[i]->inWorld==this)
+				entity[i]->draw();
+		}
 	}
 }
 
@@ -133,11 +129,14 @@ void World::singleDetect(Entity *e){
 		}
 	}
 }
+
+extern unsigned int newEntityCount;
 void World::detect(Player *p){
 	unsigned int i;
 	singleDetect(p);
-	for(i=0;i<=entity.size();i++){
-		singleDetect(entity[i]);
+	for(i=0;i<entity.size()+1;i++){
+		if(entity[i]->inWorld==this)
+			singleDetect(entity[i]);
 	}
 }
 
@@ -185,11 +184,3 @@ World *World::goWorldFront(Player *p){
 	}
 	return this;
 }
-
-/*void World::addEntity(Entity *e){
-	if(peepCount!=WORLD_ENTITY_MAX){
-		peeps[1+peepCount++]=e; // See peeps's allocation in World() for explanation of the 1+...
-	}else{
-		std::cout<<"Warning: can't add any more entities"<<std::endl;
-	}
-}*/
-- 
cgit v1.2.3