diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/entities.cpp | 31 | ||||
-rw-r--r-- | src/ui.cpp | 2 | ||||
-rw-r--r-- | src/world.cpp | 125 |
4 files changed, 112 insertions, 48 deletions
diff --git a/src/Makefile b/src/Makefile index b88dc53..8021f42 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ LIBS = -lGL -lSDL2_image -lSDL2_mixer -FLAGS = -m32 -std=c++11 -I../include -I../include/freetype2 -lSDL2main -lSDL2 -lfreetype +FLAGS = -std=c++11 -I../include -I../include/freetype2 -lSDL2main -lSDL2 -lfreetype OUT = `echo "" $$(ls -c $(wildcard *.cpp)) | sed s/.cpp/.o/g | sed 's/ / ..\/out\//g'` diff --git a/src/entities.cpp b/src/entities.cpp index 9b87150..ce983e8 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -2,7 +2,7 @@ #include <ui.h> extern std::vector<Entity*>entity; -extern std::vector<NPC>npc; +//extern std::vector<NPC>npc; extern std::vector<Structures>build; extern FILE* names; @@ -13,14 +13,14 @@ 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; - right = false; - left = false; - near = false; - ticksToUse = 0; - canMove = true; - ground = false; alive = true; - if(!maxHealth)health = maxHealth = 50; + right = true; + left = false; + near = false; + canMove = true; + ground = false; + ticksToUse = 0; + if(!maxHealth)health = maxHealth = 1; name = (char*)malloc(16); getName(); } @@ -33,9 +33,6 @@ Player::Player(){ //sets all of the player specific traits on object creation subtype = 0; maxHealth = 100; health = maxHealth; - alive = true; - ground = false; - near = true; texture[0] = loadTexture("assets/player.png"); texture[1] = loadTexture("assets/player1.png"); texture[2] = loadTexture("assets/player2.png"); @@ -48,9 +45,6 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation speed = 1; type = NPCT; //sets type to npc subtype = 0; - alive = true; - canMove = true; - near = false; texture[0] = loadTexture("assets/NPC.png"); texture[1] = 0; texture[2] = 0; @@ -60,8 +54,6 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation Structures::Structures(){ //sets the structure type type = STRUCTURET; speed = 0; - alive = true; - near = false; texture[0] = loadTexture("assets/house1.png"); texture[1] = 0; texture[2] = 0; @@ -73,9 +65,6 @@ Mob::Mob(){ speed = 1; type = MOBT; //sets type to MOB subtype = 1; //SKIRL - alive = true; - canMove = true; - near = false; texture[0] = loadTexture("assets/rabbit.png"); texture[1] = loadTexture("assets/rabbit1.png"); texture[2] = 0; @@ -261,9 +250,7 @@ unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure int tempN = (getRand() % 5 + 2); //amount of villagers that will spawn for(int i=0;i<tempN;i++){ 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 + NPCp(entity[entity.size()-1])->spawn(loc.x + (float)(i - 5),100); //sets the position of the villager around the village } return entity.size(); } @@ -21,9 +21,11 @@ namespace ui { bool debug=false; bool dialogBoxExists=false; unsigned int fontSize; + /* * initFonts(), setFontFace(), and setFontSize() are pretty self-explanatory */ + void initFonts(void){ if(FT_Init_FreeType(&ftl)){ std::cout<<"Error! Couldn't initialize freetype."<<std::endl; diff --git a/src/world.cpp b/src/world.cpp index bd58573..98b9aa7 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -3,10 +3,11 @@ #define getWidth(w) ((w->lineCount-GEN_INC)*HLINE) // Calculates the width of world 'w' #define GEN_INC 10 // Defines at what interval y values should be calculated for the array 'line'. - // As explained in World(), the last few lines in the array 'line' are incorrectly calculated - // or not calculated at all, so GEN_INC is also used to decrease 'lineCount' in functions like draw() - // and detect(). -#define GRASS_HEIGHT 4 // Defines how long the grass layer of a line should be in multiples of HLINE. + // As explained in World(), the last few lines in the array 'line' are incorrectly calculated + // or not calculated at all, so GEN_INC is also used to decrease 'lineCount' in functions like draw() + // and detect(). + +#define GRASS_HEIGHT 4 // Defines how long the grass layer of a line should be in multiples of HLINE. #define DRAW_Y_OFFSET 50 // Defines how many pixels each layer should be offset from each other on the y axis when drawn. @@ -30,8 +31,8 @@ World::World(void){ } void World::generate(unsigned int width){ // Generates the world and sets all variables contained in the World class. - unsigned int i; // Used for 'for' loops - float inc; // See line 40 + unsigned int i; // Used for 'for' loops + float inc; // See line 40 /* * Calculate the world's real width. The current form of generation fails to generate @@ -228,47 +229,121 @@ LOOP2: // Draw each world void World::singleDetect(Entity *e){ unsigned int i; + + /* + * Kill any dead entities. + */ + if(e->health<=0){ + e->alive=false; - }else if(e->alive){ - i=(e->loc.x+e->width/2-x_start)/HLINE; // Calculate what line the player is currently on - if(e->loc.y<line[i].y){ - e->vel.y=0; + + } + + /* + * Handle only living entities. + */ + + if(e->alive){ + + /* + * Calculate the line that this entity is currently standing on. + */ + + i=(e->loc.x + e->width / 2 - x_start) / HLINE; + + /* + * If the entity is under the world/line, pop it back to the surface. + */ + + if(e->loc.y < line[i].y){ + e->ground=true; - e->loc.y=line[i].y-.001*deltaTime; - }else if(e->loc.y>line[i].y-.002*deltaTime){ + + e->vel.y=0; + e->loc.y=line[i].y - .001 * deltaTime; + + /* + * Otherwise, if the entity is above the line... + */ + + }else if(e->loc.y > line[i].y - .002 * deltaTime){ + + /* + * Check for any potential platform collision (i.e. landing on a platform) + */ + for(i=0;i<platform.size();i++){ - if(((e->loc.x+e->width>platform[i].p1.x)&(e->loc.x+e->width<platform[i].p2.x))|| - ((e->loc.x<platform[i].p2.x)&(e->loc.x>platform[i].p1.x))){ - if(e->loc.y>platform[i].p1.y&&e->loc.y<platform[i].p2.y){ - if(e->vel.y<0){ + + if(((e->loc.x + e->width > platform[i].p1.x) & (e->loc.x + e->width < platform[i].p2.x)) || // Check X left bounds + ((e->loc.x < platform[i].p2.x) & (e->loc.x>platform[i].p1.x))){ // Check X right bounds + if(e->loc.y > platform[i].p1.y && e->loc.y < platform[i].p2.y){ // Check Y bounds + + /* + * Check if the entity is falling onto the platform so + * that it doesn't snap to it when attempting to jump + * through it. + * + */ + + if(e->vel.y<=0){ + + e->ground=2; + e->vel.y=0; e->loc.y=platform[i].p2.y; - e->ground=2; - return; + + //return; // May not be necessary } } } } - e->vel.y-=.001*deltaTime; + + /* + * Handle gravity. + */ + + e->vel.y-=.001 * deltaTime; + } - if(e->loc.x<x_start){ // Keep the player inside world bounds (ui.cpp handles world jumping) + + /* + * Insure that the entity doesn't fall off either edge of the world. + */ + + if(e->loc.x<x_start){ // Left bound + e->vel.x=0; - e->loc.x=x_start+HLINE/2; - }else if(e->loc.x+e->width+HLINE>x_start+getWidth(this)){ + e->loc.x=x_start + HLINE / 2; + + }else if(e->loc.x + e->width + HLINE > x_start + getWidth(this)){ // Right bound + e->vel.x=0; - e->loc.x=x_start+getWidth(this)-e->width-HLINE; + e->loc.x=x_start + getWidth(this) - e->width - HLINE; + } } } -extern unsigned int newEntityCount; void World::detect(Player *p){ unsigned int i; + + /* + * Handle the player. + */ + singleDetect(p); - for(i=0;i<entity.size()+1;i++){ + + /* + * Handle all remaining entities in this world. + */ + + for(i=0;i<entity.size();i++){ + if(entity[i]->inWorld==this){ + singleDetect(entity[i]); + } } } |