aboutsummaryrefslogtreecommitdiffstats
path: root/src/world.cpp
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-04-26 15:25:03 -0400
committerClyne Sullivan <tullivan99@gmail.com>2016-04-26 15:25:03 -0400
commit88c21e763178856aef0155595d49085919205951 (patch)
treee3bcd6caa28bf0014eeccd3a3c8582ec2b1b7e7c /src/world.cpp
parent202285e89814929e551f9568b84212282b5883fe (diff)
mobs are a-okay
Diffstat (limited to 'src/world.cpp')
-rw-r--r--src/world.cpp191
1 files changed, 112 insertions, 79 deletions
diff --git a/src/world.cpp b/src/world.cpp
index a1d0fc5..785b2b4 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -17,17 +17,6 @@
using namespace tinyxml2;
/* ----------------------------------------------------------------------------
-** Macros section
-** --------------------------------------------------------------------------*/
-
-// defines grass height in HLINEs
-#define GRASS_HEIGHT 4
-
-// indoor world constants
-#define INDOOR_FLOOR_THICKNESS 50
-#define INDOOR_FLOOR_HEIGHTT 400
-
-/* ----------------------------------------------------------------------------
** Variables section
** --------------------------------------------------------------------------*/
@@ -39,7 +28,6 @@ extern World *currentWorldToRight; // main.cpp
extern bool inBattle; // ui.cpp?
extern std::string xmlFolder;
-
// externally referenced in main.cpp
const unsigned int DAY_CYCLE = 12000;
int worldShade = 0;
@@ -54,6 +42,13 @@ static const float GROUND_HEIGHT_MINIMUM = 60.0f;
static const float GROUND_HEIGHT_MAXIMUM = 110.0f;
static const float GROUND_HILLINESS = 10.0f;
+// defines grass height in HLINEs
+static const unsigned int GRASS_HEIGHT = 4;
+
+// indoor world constants
+static const unsigned int INDOOR_FLOOR_THICKNESS = 50;
+static const unsigned int INDOOR_FLOOR_HEIGHTT = 400;
+
// the path of the currently loaded XML file, externally referenced in places
std::string currentXML;
@@ -61,14 +56,15 @@ std::string currentXML;
WorldWeather weather = WorldWeather::Sunny;
// keeps track of pathnames of XML file'd worlds the player has left to enter structures
-std::vector<std::string> inside;
+static std::vector<std::string> inside;
// keeps track of information of worlds the player has left to enter arenas
static std::vector<World *> battleNest;
static std::vector<vec2> battleNestLoc;
// pathnames of images for world themes
-static const std::string bgPaths[][9] = {
+static const unsigned int BG_PATHS_ENTRY_SIZE = 9;
+static const std::string bgPaths[][BG_PATHS_ENTRY_SIZE] = {
{"bg.png", // Daytime background
"bgn.png", // Nighttime background
"bgFarMountain.png", // Furthest layer
@@ -188,13 +184,12 @@ deleteEntities(void)
* object for usage.
*/
void World::
-generate(unsigned int width)
+generate(int width)
{
- // see below for description/usage
float geninc = 0;
// check for valid width
- if ((int)width <= 0)
+ if (width <= 0)
UserError("Invalid world dimensions");
// allocate space for world
@@ -207,20 +202,20 @@ generate(unsigned int width)
// give every GROUND_HILLINESSth entry a groundHeight value
for (; wditer < std::end(worldData); wditer += GROUND_HILLINESS)
- (*(wditer - GROUND_HILLINESS)).groundHeight = (*wditer).groundHeight + (randGet() % 8 - 4);
+ (*(wditer - GROUND_HILLINESS)).groundHeight = (*wditer).groundHeight + (getRand() % 8 - 4);
// create slopes from the points that were just defined, populate the rest of the WorldData structure
for (wditer = std::begin(worldData) + 1; wditer < std::end(worldData); wditer++){
auto w = &*(wditer);
- if (w->groundHeight)
- geninc = ((w + (int)GROUND_HILLINESS)->groundHeight - w->groundHeight) / GROUND_HILLINESS;
+ if (w->groundHeight != 0)
+ geninc = ((w + static_cast<int>(GROUND_HILLINESS))->groundHeight - w->groundHeight) / GROUND_HILLINESS;
w->groundHeight = fmin(fmax((w - 1)->groundHeight + geninc, GROUND_HEIGHT_MINIMUM), GROUND_HEIGHT_MAXIMUM);
- w->groundColor = randGet() % 32 / 8;
+ w->groundColor = getRand() % 32 / 8;
w->grassUnpressed = true;
- w->grassHeight[0] = (randGet() % 16) / 3 + 2;
- w->grassHeight[1] = (randGet() % 16) / 3 + 2;
+ w->grassHeight[0] = (getRand() % 16) / 3 + 2;
+ w->grassHeight[1] = (getRand() % 16) / 3 + 2;
}
// define x-coordinate of world's leftmost 'line'
@@ -230,7 +225,7 @@ generate(unsigned int width)
star = std::vector<vec2> (100, vec2 { 0, 400 });
for (auto &s : star) {
s.x = (getRand() % (-worldStart * 2)) + worldStart;
- s.y = (getRand() % SCREEN_HEIGHT) + 100.0f;
+ s.y = (getRand() % SCREEN_HEIGHT) + 100;
}
}
@@ -243,11 +238,11 @@ void World::
draw(Player *p)
{
const ivec2 backgroundOffset = ivec2 {
- (int)(SCREEN_WIDTH / 2), (int)(SCREEN_HEIGHT / 2)
+ static_cast<int>(SCREEN_WIDTH) / 2, static_cast<int>(SCREEN_HEIGHT) / 2
};
- // iterators
- int i, iStart, iEnd;
+ // iterators ranges
+ int iStart, iEnd;
// shade value for draws -- may be unnecessary
int shadeBackground = -worldShade;
@@ -262,10 +257,10 @@ draw(Player *p)
int alpha;
// shade value for GLSL
- float shadeAmbient = fmax(0, -worldShade / 50.0f + 0.5f); // 0 to 1.5f
+ float shadeAmbient = std::max(0.0f, static_cast<float>(-worldShade) / 50 + 0.5f); // 0 to 1.5f
if (shadeAmbient > 0.9f)
- shadeAmbient = 1.0f;
+ shadeAmbient = 1;
// draw background images.
glEnable(GL_TEXTURE_2D);
@@ -273,8 +268,12 @@ draw(Player *p)
// the sunny wallpaper is faded with the night depending on tickCount
bgTex->bind(0);
switch (weather) {
- case WorldWeather::Snowy : alpha = 150; break;
- case WorldWeather::Rain : alpha = 0; break;
+ case WorldWeather::Snowy:
+ alpha = 150;
+ break;
+ case WorldWeather::Rain:
+ alpha = 0;
+ break;
default:
alpha = 255 - worldShade * 4;
break;
@@ -315,7 +314,7 @@ draw(Player *p)
safeSetColorA(150 + shadeBackground * 2, 150 + shadeBackground * 2, 150 + shadeBackground * 2, 255);
glBegin(GL_QUADS); {
auto xcoord = width / 2 * -1 + offset.x * 0.85f;
- for (i = 0; i <= (int)(worldData.size() * HLINE / 1920); i++) {
+ for (unsigned int i = 0; i <= worldData.size() * HLINE / 1920; i++) {
glTexCoord2i(0, 1); glVertex2i(1920 * i + xcoord, GROUND_HEIGHT_MINIMUM);
glTexCoord2i(1, 1); glVertex2i(1920 * (i + 1) + xcoord, GROUND_HEIGHT_MINIMUM);
glTexCoord2i(1, 0); glVertex2i(1920 * (i + 1) + xcoord, GROUND_HEIGHT_MINIMUM + 1080);
@@ -323,7 +322,7 @@ draw(Player *p)
}
} glEnd();
- for (i = 0; i < 4; i++) {
+ for (unsigned int i = 0; i < 4; i++) {
bgTex->bindNext();
safeSetColorA(bgDraw[i][0] + shadeBackground * 2,
bgDraw[i][0] + shadeBackground * 2,
@@ -353,17 +352,17 @@ draw(Player *p)
glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
glUseProgram(shaderProgram);
- std::for_each(std::begin(particles), std::end(particles), [](Particles &p) {
+ for (auto &p : particles) {
if (p.behind)
p.draw();
- });
+ }
glUseProgram(0);
for (auto &b : build) {
if (b->bsubtype == STALL_MARKET) {
for (auto &n : npc) {
- if (n->type == MERCHT && ((Merchant *)n)->inside == b) {
+ if (n->type == MERCHT && static_cast<Merchant *>(n)->inside == b) {
n->draw();
break;
}
@@ -374,15 +373,15 @@ draw(Player *p)
for (auto &l : light) {
if (l.belongsTo) {
- l.loc.x = l.following->loc.x + SCREEN_WIDTH/2;
+ l.loc.x = l.following->loc.x + SCREEN_WIDTH / 2;
l.loc.y = l.following->loc.y;
}
if (l.flame) {
- l.fireFlicker = .9+((rand()%2)/10.0f);
- l.fireLoc.x = l.loc.x + (rand()%2-1)*3;
- l.fireLoc.y = l.loc.y + (rand()%2-1)*3;
+ l.fireFlicker = 0.9f + ((rand()% 2 ) / 10.0f);
+ l.fireLoc.x = l.loc.x + (rand() % 2 - 1) * 3;
+ l.fireLoc.y = l.loc.y + (rand() % 2 - 1) * 3;
} else {
- l.fireFlicker = 1.0f;
+ l.fireFlicker = 1;
}
}
@@ -395,7 +394,7 @@ draw(Player *p)
auto pointArray = pointArrayBuf.get();
GLfloat flameArray[64];
- for (uint i = 0; i < light.size(); i++) {
+ for (unsigned int i = 0; i < light.size(); i++) {
if (light[i].flame) {
pointArray[2 * i ] = light[i].fireLoc.x - offset.x;
pointArray[2 * i + 1] = light[i].fireLoc.y;
@@ -405,7 +404,7 @@ draw(Player *p)
}
}
- for (uint i = 0; i < light.size(); i++)
+ for (unsigned int i = 0; i < light.size(); i++)
flameArray[i] = light[i].fireFlicker;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@@ -415,7 +414,7 @@ draw(Player *p)
glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
glUniform1f(glGetUniformLocation(shaderProgram, "amb"), shadeAmbient);
- if (light.size() == 0)
+ if (light.empty())
glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), 0);
else {
glUniform1i (glGetUniformLocation(shaderProgram, "numLight"), light.size());
@@ -428,14 +427,14 @@ draw(Player *p)
pOffset = (offset.x + p->width / 2 - worldStart) / HLINE;
// only draw world within player vision
- iStart = (int)fmax(pOffset - (SCREEN_WIDTH / 2 / HLINE) - GROUND_HILLINESS, 0);
- iEnd = (int)fmin(pOffset + (SCREEN_WIDTH / 2 / HLINE) + GROUND_HILLINESS + HLINE, worldData.size());
- iEnd = (int)fmax(iEnd, GROUND_HILLINESS);
+ iStart = static_cast<int>(fmax(pOffset - (SCREEN_WIDTH / 2 / HLINE) - GROUND_HILLINESS, 0));
+ iEnd = static_cast<int>(fmin(pOffset + (SCREEN_WIDTH / 2 / HLINE) + GROUND_HILLINESS + HLINE, worldData.size()));
+ iEnd = static_cast<int>(fmax(iEnd, GROUND_HILLINESS));
// draw the dirt
glBegin(GL_QUADS);
//std::for_each(std::begin(worldData) + iStart, std::begin(worldData) + iEnd, [&](WorldData wd) {
- for (i = iStart; i < iEnd; i++) {
+ for (int i = iStart; i < iEnd; i++) {
if (worldData[i].groundHeight <= 0) {
worldData[i].groundHeight = GROUND_HEIGHT_MINIMUM - 1;
glColor4ub(0, 0, 0, 255);
@@ -465,12 +464,12 @@ draw(Player *p)
glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
safeSetColorA(255, 255, 255, 255);
- for (i = iStart; i < iEnd; i++) {
+ for (int i = iStart; i < iEnd; i++) {
auto wd = worldData[i];
auto gh = wd.grassHeight;
// flatten the grass if the player is standing on it.
- if (!worldData[i].grassUnpressed) {
+ if (!wd.grassUnpressed) {
gh[0] /= 4;
gh[1] /= 4;
}
@@ -520,11 +519,11 @@ draw(Player *p)
// flatten grass under the player if the player is on the ground
if (p->ground) {
- for (i = 0; i < (int)worldData.size(); i++)
- worldData[i].grassUnpressed = !(i < pOffset + 6 && i > pOffset - 6);
+ for (unsigned int i = 0; i < worldData.size(); i++)
+ worldData[i].grassUnpressed = !(i < static_cast<unsigned int>(pOffset + 6) && i > static_cast<unsigned int>(pOffset - 6));
} else {
- for (i = 0; i < (int)worldData.size(); i++)
- worldData[i].grassUnpressed = true;
+ for (auto &wd : worldData)
+ wd.grassUnpressed = true;
}
// draw the player
@@ -576,14 +575,14 @@ singleDetect(Entity *e)
break;
}
- std::cout << "Killed a " << killed << "..." << std::endl;
+ std::cout << "Killed a " << killed << "...\n";
entity.erase(entity.begin() + i);
return;
}
}
// exit on player death
- std::cout << "RIP " << e->name << "." << std::endl;
+ std::cout << "RIP " << e->name << ".\n";
exit(0);
}
@@ -593,17 +592,15 @@ singleDetect(Entity *e)
// forced movement gravity (sword hits)
e->handleHits();
- if (e->subtype == MS_TRIGGER)
- return;
-
// calculate the line that this entity is currently standing on
- l = (int)fmax((e->loc.x + e->width / 2 - worldStart) / HLINE, 0);
- l = (int)fmin(l, lineCount - 1);
+ l = static_cast<int>(fmax((e->loc.x + e->width / 2 - worldStart) / HLINE, 0));
+ l = static_cast<int>(fmin(l, lineCount - 1));
// if the entity is under the world/line, pop it back to the surface
if (e->loc.y < worldData[l].groundHeight) {
int dir = e->vel.x < 0 ? -1 : 1;
- if (l + (dir * 2) < (int)worldData.size() && worldData[l + (dir * 2)].groundHeight - 30 > worldData[l + dir].groundHeight) {
+ if (l + (dir * 2) < static_cast<int>(worldData.size()) &&
+ worldData[l + (dir * 2)].groundHeight - 30 > worldData[l + dir].groundHeight) {
e->loc.x -= (PLAYER_SPEED_CONSTANT + 2.7f) * e->speed * 2 * dir;
e->vel.x = 0;
} else {
@@ -1161,16 +1158,24 @@ addVillage(std::string name, World *world)
return &village.back();
}
-void World::
+/*void World::
addMob(int t, float x, float y)
{
- mob.push_back(new Mob(t));
+ mob.push_back(new new(auto *&t));
mob.back()->spawn(x, y);
entity.push_back(mob.back());
+}*/
+
+void World::addMob(Mob *m, vec2 coord)
+{
+ mob.push_back(m);
+ mob.back()->spawn(coord.x, coord.y);
+
+ entity.push_back(mob.back());
}
-void World::
+/*void World::
addMob(int t, float x, float y, void (*hey)(Mob *))
{
mob.push_back(new Mob(t));
@@ -1178,7 +1183,7 @@ addMob(int t, float x, float y, void (*hey)(Mob *))
mob.back()->hey = hey;
entity.push_back(mob.back());
-}
+}*/
void World::
addNPC(float x, float y)
@@ -1343,8 +1348,6 @@ singleDetect(Entity *e)
if (!e->isAlive())
return;
- if (e->type == MOBT && e->subtype == MS_TRIGGER)
- return;
for (; floornum < floor.size(); floornum++) {
if (floor[floornum][0] + INDOOR_FLOOR_HEIGHTT - 100 > e->loc.y) {
@@ -1491,7 +1494,7 @@ draw(Player *p)
Arena::Arena(World *leave,Player *p,Mob *m) {
generate(800);
- addMob(MS_DOOR,100,100);
+ addMob(new Door(), vec2 {100, 100});
inBattle = true;
mmob = m;
@@ -1578,7 +1581,6 @@ World *loadWorldFromPtr(World *ptr)
/**
* Loads a world from the given XML file.
*/
-
World *
loadWorldFromXMLNoSave(std::string path) {
XMLDocument xml;
@@ -1688,8 +1690,45 @@ loadWorldFromXMLNoSave(std::string path) {
}
}
- // mob creation
- else if (name == "mob") {
+ /**
+ * MOBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
+ * BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
+ * BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
+
+ else if (name == "trigger") {
+ tmp->addMob(MS_TRIGGER, wxml->FloatAttribute("x"), 0, commonTriggerFunc);
+ tmp->getLastMob()->heyid = wxml->Attribute("id");
+ } else if (name == "page") {
+ tmp->addMob(MS_PAGE, wxml->FloatAttribute("x"), 0, commonPageFunc);
+ tmp->getLastMob()->heyid = wxml->Attribute("id");
+ }
+
+ */
+
+ else if (name == "rabbit") {
+ tmp->addMob(new Rabbit(), vec2 {0, 0});
+ tmp->getLastMob()->createFromXML(wxml);
+ } else if (name == "bird") {
+ tmp->addMob(new Bird(), vec2 {0, 0});
+ tmp->getLastMob()->createFromXML(wxml);
+ } else if (name == "trigger") {
+ tmp->addMob(new Trigger(), vec2 {0, 0});
+ tmp->getLastMob()->createFromXML(wxml);
+ } else if (name == "door") {
+ tmp->addMob(new Door(), vec2 {0, 0});
+ tmp->getLastMob()->createFromXML(wxml);
+ } else if (name == "page") {
+ tmp->addMob(new Page(), vec2 {0, 0});
+ tmp->getLastMob()->createFromXML(wxml);
+ }
+
+
+
+
+ /*else if (name == "mob") {
+
+
+
// type info
if (wxml->QueryUnsignedAttribute("type", &flooor) != XML_NO_ERROR)
UserError("XML Error: Invalid type value in <mob> in " + currentXML + "!");
@@ -1711,7 +1750,7 @@ loadWorldFromXMLNoSave(std::string path) {
// custom health value
if (wxml->QueryFloatAttribute("health", &spawnx) == XML_NO_ERROR)
tmp->getLastMob()->health = tmp->getLastMob()->maxHealth = spawnx;
- }
+ }*/
// npc creation
else if (name == "npc") {
@@ -1754,12 +1793,6 @@ loadWorldFromXMLNoSave(std::string path) {
wxml->StrAttribute("texture"),
wxml->StrAttribute("inside")
);
- } else if (name == "trigger") {
- tmp->addMob(MS_TRIGGER, wxml->FloatAttribute("x"), 0, commonTriggerFunc);
- tmp->getLastMob()->heyid = wxml->Attribute("id");
- } else if (name == "page") {
- tmp->addMob(MS_PAGE, wxml->FloatAttribute("x"), 0, commonPageFunc);
- tmp->getLastMob()->heyid = wxml->Attribute("id");
} else if (name == "hill") {
tmp->addHill(ivec2 { wxml->IntAttribute("peakx"), wxml->IntAttribute("peaky") }, wxml->UnsignedAttribute("width"));
} else if (name == "time") {