aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile2
-rw-r--r--src/entities.cpp38
-rw-r--r--src/ui.cpp2
-rw-r--r--src/world.cpp125
4 files changed, 112 insertions, 55 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 092ab7b..7a4da65 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();
}
@@ -36,9 +36,6 @@ Player::Player(){ //sets all of the player specific traits on object creation
alive = true;
ground = false;
near = true;
- texture[0] = 0;
- texture[1] = 0;
- texture[2] = 0;
inv = new Inventory(PLAYER_INV_SIZE);
tex = new Texturec(3, "assets/player.png", "assets/player1.png", "assets/player2.png");
}
@@ -299,24 +296,7 @@ unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure
* 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
-
- //A new npc is created right here so the new entity can control it
- npc.push_back(NPC()); //create new NPC
-
- /*
- * This is where the spawning gets sketchy...
- *
- * We want to take the newest entity we spawned and set it equal to the new NPC
- * This seg faults, so for some odd reason we have to use the element of size
- * which shouldn't exist since we haven't created it. This entity is then set to the
- * same traits as the newest NPC, which we can access the last element in.
- *
- * Then somehow we can set the same entity with size()-1 to spawn. But it works, so we're
- * not complaining. Although fixing would be nice. Now that it is mentioned...
- * TODO: FIX THIS BORK.
- */
- 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();
}
diff --git a/src/ui.cpp b/src/ui.cpp
index 256f5d7..f36edeb 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -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]);
+
}
}
}