aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/common.h6
-rw-r--r--include/entities.h22
-rw-r--r--src/entities.cpp55
-rw-r--r--src/main.cpp7
4 files changed, 50 insertions, 40 deletions
diff --git a/include/common.h b/include/common.h
index 922dd59..1210adb 100644
--- a/include/common.h
+++ b/include/common.h
@@ -11,7 +11,7 @@
#include <SDL2/SDL_opengl.h>
typedef struct { float x; float y; } vec2;
-enum _TYPE {STRUCTURET = -1, PLAYERT = 0, NPCT = 1};
+enum _TYPE {STRUCTURET = -1, PLAYERT = 0, NPCT = 1}; //these are the main types of entities
#include <entities.h>
@@ -19,12 +19,12 @@ enum _TYPE {STRUCTURET = -1, PLAYERT = 0, NPCT = 1};
#define SCREEN_HEIGHT 720
//#define FULLSCREEN
-#define HLINE 3
+#define HLINE 3 //base unit of the world
#define initRand(s) srand(s)
#define getRand() rand()
-template<typename T, size_t N>
+template<typename T, size_t N> //this fuction returns the size of any array
int eAmt(T (&)[N]){return N;}
//SDL VARIABLES
diff --git a/include/entities.h b/include/entities.h
index df81ad2..53a8057 100644
--- a/include/entities.h
+++ b/include/entities.h
@@ -6,25 +6,31 @@
class Entity{
public:
void *inWorld;
- float width;
+ float width; //width and height of the player
float height;
- float speed;
+ float speed; //speed of the play
+ //type and subtype
int subtype;
_TYPE type;
- vec2 loc;
+ //example:
+ //type 1(NPC)
+ // |(subtype)
+ // |-> 0 Base NPC
+ // |-> 1 Merchant
+ vec2 loc; //location and velocity of the entity
vec2 vel;
- bool right,left, canMove;
- bool alive;
- unsigned char ground;
+ bool right,left, canMove; //movement variables
+ bool alive; //the flag for whether or not the entity is alive
+ unsigned char ground; //variable for testing what ground the entity is on to apply certain traits
- unsigned int texture[];
+ unsigned int texture[]; //TODO: ADD TEXTURES
void spawn(float, float);
void draw(void);
void wander(int, vec2*);
virtual void interact(){}
private:
- int ticksToUse;
+ int ticksToUse; //The variable for deciding how long an entity should do a certain task
};
class Player : public Entity{
diff --git a/src/entities.cpp b/src/entities.cpp
index c341dba..a7737b3 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -4,7 +4,7 @@ extern std::vector<Entity*>entity;
extern std::vector<NPC>npc;
extern std::vector<Structures>build;
-void Entity::spawn(float x, float y){
+void Entity::spawn(float x, float y){ //spawns the entity you pass to it based off of coords and global entity settings
loc.x = x;
loc.y = y;
vel.x = 0;
@@ -16,78 +16,79 @@ void Entity::spawn(float x, float y){
ground = false;
}
-void Entity::draw(void){
- glColor3ub(0,0,100);
+void Entity::draw(void){ //draws the entities
+ if(type==NPCT)
+ glColor3ub(0,0,100);
+ else if(type==STRUCTURET)
+ glColor3ub(100,0,100);
glRectf(loc.x,loc.y,loc.x+width,loc.y+height);
}
-void Entity::wander(int timeRun, vec2 *v){
- static int direction;
+void Entity::wander(int timeRun, vec2 *v){ //this makes the entites wander about
+ static int direction; //variable to decide what direction the entity moves
if(ticksToUse == 0){
ticksToUse = timeRun;
- v->x = .01*HLINE;
- direction = (getRand() % 3 - 1);
- v->x *= direction;
+ v->x = .008*HLINE; //sets the inital velocity of the entity
+ direction = (getRand() % 3 - 1); //sets the direction to either -1, 0, 1
+ //this lets the entity move left, right, or stay still
+ v->x *= direction; //changes the velocity based off of the direction
}
- ticksToUse--;
+ ticksToUse--; //removes one off of the entities timer
}
-Player::Player(){
+Player::Player(){ //sets all of the player specific traits on object creation
width = HLINE * 8;
height = HLINE * 12;
speed = 1;
- type = PLAYERT;
+ type = PLAYERT; //set type to player
subtype = 5;
alive = true;
ground = false;
}
-void Player::interact(){
+void Player::interact(){ //the function that will cause the player to search for things to interact with
}
-NPC::NPC(){
+NPC::NPC(){ //sets all of the NPC specific traits on object creation
width = HLINE * 8;
height = HLINE * 12;
speed = 1;
- type = NPCT;
+ type = NPCT; //sets type to npc
subtype = 0;
alive = true;
canMove = true;
}
-void NPC::interact(){
+void NPC::interact(){ //have the npc's interact back to the player
//loc.y += .01;
}
-Structures::Structures(){
+Structures::Structures(){ //sets the structure type
type = STRUCTURET;
speed = 0;
alive = true;
}
-unsigned int Structures::spawn(_TYPE t, float x, float y){
+unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure based off of type and coords
loc.x = x;
loc.y = y;
type = t;
/*VILLAGE*/
+ //spawns a village
+ //spawns between 1 and 5 villagers around the village
if(type == STRUCTURET){
loc.y=100;
width = 20 * HLINE;
height = 16 * HLINE;
- //int tempN = (getRand() % 5 + 1);
- int tempN = 2;
+ int tempN = (getRand() % 5 + 1); //amount of villagers that will spawn
for(int i=0;i<tempN;i++){
- entity.push_back(new NPC());
- npc.push_back(NPC());
- //std::cout<<"NPC:"<<npc.size()<<std::endl;
- //std::cout<<"Entity:"<<entity.size()<<std::endl;
- entity[entity.size()] = &npc[npc.size()-1];
- entity[entity.size()-1]->spawn(loc.x + (float)(i - 5) / 8,100);
- //std::cout<<"Entity Type["<<entity.size()<<"]: "<<entity[entity.size()]->type<<std::endl;
- //std::cout<<"Entity Life["<<entity.size()<<"]: "<<entity[entity.size()]->alive<<std::endl;
+ entity.push_back(new NPC()); //create a new entity of NPC type
+ npc.push_back(NPC()); //create new NPC
+ entity[entity.size()] = &npc[npc.size()-1]; //set the new entity to have the same traits as an NPC
+ entity[entity.size()-1]->spawn(loc.x + (float)(i - 5),100); //sets the position of the villager around the village
}
return entity.size();
}
diff --git a/src/main.cpp b/src/main.cpp
index a7a8991..2dc5112 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -161,17 +161,20 @@ void render(){
**************************/
currentWorld->draw(&player->loc); // Draw the world around the player
+ glColor3ub(0,0,0);
player->draw(); // Draw the player
if(ui::debug){
static unsigned int debugDiv=0;
static int fps,d;
+ static float rndy; //variable to round the player y-coord so it is easier to read
if(++debugDiv==20){
fps=1000/deltaTime;
d=deltaTime;
debugDiv=0;
+ rndy = player->loc.y;
}
- ui::putText(player->loc.x-SCREEN_WIDTH/2,SCREEN_HEIGHT-ui::fontSize,"FPS: %d\nD: %d G:%d\nRes: %ux%u",fps,d,player->ground,SCREEN_WIDTH,SCREEN_HEIGHT);
+ ui::putText(player->loc.x-SCREEN_WIDTH/2,SCREEN_HEIGHT-ui::fontSize,"FPS: %d\nD: %d G:%d\nRes: %ux%u\nE: %d\nPOS: (x)%.2f\n (y)%.2f",fps,d,player->ground,SCREEN_WIDTH,SCREEN_HEIGHT,entity.size(),player->loc.x,rndy);
}
ui::draw(); // Draw any UI elements if they need to be
@@ -195,7 +198,7 @@ void logic(){
currentWorld->detect(player);
for(int i=0;i<=entity.size();i++){
if(entity[i]->alive&&entity[i]->type == NPCT){
- entity[i]->wander(90, &entity[i]->vel);
+ entity[i]->wander((rand()%120 + 30), &entity[i]->vel);
//std::cout<<"works"<<i<<std::endl;
}
}