aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common.cpp75
-rw-r--r--src/config.cpp142
-rw-r--r--src/entities.cpp195
-rw-r--r--src/gameplay.cpp220
-rw-r--r--src/gametime.cpp76
-rw-r--r--src/inventory.cpp16
-rw-r--r--src/mob.cpp24
-rw-r--r--src/threadpool.cpp99
-rw-r--r--src/ui.cpp81
-rw-r--r--src/ui_menu.cpp13
-rw-r--r--src/world.cpp126
11 files changed, 432 insertions, 635 deletions
diff --git a/src/common.cpp b/src/common.cpp
index 34b2a61..c86454b 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -1,17 +1,15 @@
+#include <common.hpp>
+
#include <cstring>
#include <cstdio>
#include <chrono>
#ifndef __WIN32__
-# include <sys/types.h>
-# include <dirent.h>
-# include <errno.h>
-# include <vector>
-#endif // __WIN32__
-#include <common.hpp>
-
-#ifndef __WIN32__
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#include <vector>
unsigned int millis(void) {
std::chrono::system_clock::time_point now=std::chrono::system_clock::now();
@@ -20,37 +18,33 @@ unsigned int millis(void) {
#endif // __WIN32__
-void DEBUG_prints(const char* file, int line, const char *s,...) {
+void DEBUG_prints(const char* file, int line, const char *s,...)
+{
va_list args;
- printf("%s:%d: ",file,line);
- va_start(args,s);
- vprintf(s,args);
+ printf("%s:%d: ", file, line);
+ va_start(args, s);
+ vprintf(s, args);
va_end(args);
}
-void safeSetColor(int r,int g,int b) { // safeSetColor() is an alternative to directly using glColor3ub() to set
- if (r>255)r=255; // the color for OpenGL drawing. safeSetColor() checks for values that are
- if (g>255)g=255; // outside the range of an unsigned character and sets them to a safer value.
- if (b>255)b=255;
- if (r<0)r=0;
- if (g<0)g=0;
- if (b<0)b=0;
- glColor3ub(r,g,b);
+void safeSetColor(int r, int g, int b)
+{
+ r = static_cast<int>(fmax(fmin(r, 255), 0));
+ g = static_cast<int>(fmax(fmin(g, 255), 0));
+ b = static_cast<int>(fmax(fmin(b, 255), 0));
+ glColor3ub(r, g, b);
}
void safeSetColorA(int r,int g,int b,int a) {
- if (r>255)r=255;
- if (g>255)g=255;
- if (b>255)b=255;
- if (a>255)a=255;
- if (r<0)r=0;
- if (g<0)g=0;
- if (b<0)b=0;
- if (a<0)a=0;
- glColor4ub(r,g,b,a);
+ r = static_cast<int>(fmax(fmin(r, 255), 0));
+ g = static_cast<int>(fmax(fmin(g, 255), 0));
+ b = static_cast<int>(fmax(fmin(b, 255), 0));
+ a = static_cast<int>(fmax(fmin(a, 255), 0));
+ glColor4ub(r, g, b, a);
}
-int getdir(std::string dir, std::vector<std::string> &files) {
+int getdir(std::string dir, std::vector<std::string> &files)
+{
DIR *dp;
struct dirent *dirp;
if (!(dp = opendir(dir.c_str()))) {
@@ -63,20 +57,22 @@ int getdir(std::string dir, std::vector<std::string> &files) {
return 0;
}
-void strVectorSortAlpha(std::vector<std::string> *v) {
+void strVectorSortAlpha(std::vector<std::string> *v)
+{
static bool change;
- do{
+ do {
change = false;
- for(unsigned int i=0;i<v->size()-1;i++) {
- if (v[0][i] > v[0][i+1]) {
- std::swap(v[0][i],v[0][i+1]);
+ for (unsigned int i=0; i < v->size() - 1; i++) {
+ if (v[0][i] > v[0][i + 1]) {
+ std::swap(v[0][i], v[0][i + 1]);
change = true;
}
}
- }while(change);
+ } while (change);
}
-const char *readFile(const char *path) {
+const char *readFile(const char *path)
+{
std::ifstream in (path,std::ios::in);
unsigned int size;
GLchar *buf;
@@ -94,9 +90,8 @@ const char *readFile(const char *path) {
return buf;
}
-void
-UserError(std::string reason)
+void UserError(std::string reason)
{
- std::cout << "User error: " << reason << "!" << std::endl;
+ std::cout << "User error: " << reason << "!\n";
abort();
}
diff --git a/src/config.cpp b/src/config.cpp
index 752b365..d18016f 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -2,82 +2,74 @@
#include <ui.hpp>
-using namespace tinyxml2;
-
-extern unsigned int HLINE;
-extern unsigned int SCREEN_WIDTH;
-extern unsigned int SCREEN_HEIGHT;
-extern bool FULLSCREEN;
-
-extern float VOLUME_MASTER;
-extern float VOLUME_MUSIC;
-extern float VOLUME_SFX;
-
-extern std::string xmlFolder;
-
-XMLDocument xml;
-XMLElement *scr;
-XMLElement *vol;
-
-namespace config {
-
- void read(void) {
- unsigned int uval;
- float fval;
- bool bval;
-
- xml.LoadFile("config/settings.xml");
- scr = xml.FirstChildElement("screen");
-
- if (scr->QueryUnsignedAttribute("width",&uval) == XML_NO_ERROR)
- SCREEN_WIDTH = uval;
- else SCREEN_WIDTH = 1280;
- if (scr->QueryUnsignedAttribute("height",&uval) == XML_NO_ERROR)
- SCREEN_HEIGHT = uval;
- else SCREEN_HEIGHT = 800;
- if (scr->QueryBoolAttribute("fullscreen",&bval) == XML_NO_ERROR)
- FULLSCREEN = bval;
- else FULLSCREEN = false;
- if (xml.FirstChildElement("hline")->QueryUnsignedAttribute("size",&uval) == XML_NO_ERROR)
- HLINE = uval;
- else HLINE = 3;
-
- vol = xml.FirstChildElement("volume");
-
- if (vol->FirstChildElement("master")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR)
- VOLUME_MASTER = fval;
- else VOLUME_MASTER = 50;
- if (vol->FirstChildElement("music")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR)
- VOLUME_MUSIC = fval;
- else VOLUME_MUSIC = 50;
- if (vol->FirstChildElement("sfx")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR)
- VOLUME_SFX = fval;
- else VOLUME_SFX = 50;
-
- xmlFolder = xml.FirstChildElement("world")->Attribute("start");
- if (xmlFolder=="\0")xmlFolder = "xml/";
- std::cout << "Folder: " << xmlFolder << std::endl;
-
- ui::initFonts();
- ui::setFontFace(xml.FirstChildElement("font")->Attribute("path"));
-
- if (xml.FirstChildElement("debug"))
- ui::debug = ui::posFlag = true;
-
- config::update();
- }
+#include <SDL2/SDL_mixer.h>
- void update(void) {
- Mix_Volume(0,VOLUME_MASTER);
- Mix_Volume(1,VOLUME_SFX * (VOLUME_MASTER/100.0f));
- Mix_VolumeMusic(VOLUME_MUSIC * (VOLUME_MASTER/100.0f));
- }
-
- void save(void) {
- vol->FirstChildElement("master")->SetAttribute("volume",VOLUME_MASTER);
- vol->FirstChildElement("music")->SetAttribute("volume",VOLUME_MUSIC);
- vol->FirstChildElement("sfx")->SetAttribute("volume", VOLUME_SFX);
+#include <tinyxml2.h>
+using namespace tinyxml2;
- xml.SaveFile("config/settings.xml", false);
+namespace game {
+ unsigned int HLINE;
+ unsigned int SCREEN_WIDTH;
+ unsigned int SCREEN_HEIGHT;
+ bool FULLSCREEN;
+
+ namespace config {
+ static XMLDocument xml;
+ static XMLElement *vol;
+
+ float VOLUME_MASTER;
+ float VOLUME_MUSIC;
+ float VOLUME_SFX;
+
+ std::string xmlFolder;
+
+ void read(void) {
+ xml.LoadFile("config/settings.xml");
+ auto exml = xml.FirstChildElement("screen");
+
+ if (exml->QueryUnsignedAttribute("width", &SCREEN_WIDTH) != XML_NO_ERROR)
+ SCREEN_WIDTH = 1024;
+ if (exml->QueryUnsignedAttribute("height", &SCREEN_HEIGHT) != XML_NO_ERROR)
+ SCREEN_HEIGHT = 768;
+ if (exml->QueryBoolAttribute("fullscreen", &FULLSCREEN) != XML_NO_ERROR)
+ FULLSCREEN = false;
+
+ if (xml.FirstChildElement("hline")->QueryUnsignedAttribute("size", &HLINE) != XML_NO_ERROR)
+ HLINE = 3;
+
+ vol = exml = xml.FirstChildElement("volume");
+ if (exml->FirstChildElement("master")->QueryFloatAttribute("volume", &VOLUME_MASTER) != XML_NO_ERROR)
+ VOLUME_MASTER = 50;
+ if (exml->FirstChildElement("music")->QueryFloatAttribute("volume", &VOLUME_MUSIC) != XML_NO_ERROR)
+ VOLUME_MUSIC = 50;
+ if (exml->FirstChildElement("sfx")->QueryFloatAttribute("volume", &VOLUME_SFX) != XML_NO_ERROR)
+ VOLUME_SFX = 50;
+
+ xmlFolder = xml.FirstChildElement("world")->StrAttribute("start");
+ if (xmlFolder.empty())
+ xmlFolder = "xml/";
+
+ ui::initFonts();
+ ui::setFontFace(xml.FirstChildElement("font")->Attribute("path"));
+
+ if (xml.FirstChildElement("debug"))
+ ui::debug = ui::posFlag = true;
+
+ config::update();
+ }
+
+ void update(void) {
+ Mix_Volume(0, VOLUME_MASTER);
+ Mix_Volume(1, VOLUME_SFX * (VOLUME_MASTER / 100.0f));
+ Mix_VolumeMusic(VOLUME_MUSIC * (VOLUME_MASTER / 100.0f));
+ }
+
+ void save(void) {
+ vol->FirstChildElement("master")->SetAttribute("volume", VOLUME_MASTER);
+ vol->FirstChildElement("music")->SetAttribute("volume", VOLUME_MUSIC);
+ vol->FirstChildElement("sfx")->SetAttribute("volume", VOLUME_SFX);
+
+ xml.SaveFile("config/settings.xml", false);
+ }
}
}
diff --git a/src/entities.cpp b/src/entities.cpp
index 6f94bc2..ee26438 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -154,8 +154,8 @@ void Entity::moveTo(float dest_x)
}
Player::Player(){ //sets all of the player specific traits on object creation
- width = HLINE * 10;
- height = HLINE * 16;
+ width = HLINES(10);
+ height = HLINES(16);
type = PLAYERT; //set type to player
subtype = 0;
@@ -181,8 +181,8 @@ Player::~Player() {
}
NPC::NPC() { //sets all of the NPC specific traits on object creation
- width = HLINE * 10;
- height = HLINE * 16;
+ width = HLINES(10);
+ height = HLINES(16);
type = NPCT; //sets type to npc
subtype = 0;
@@ -207,8 +207,8 @@ NPC::~NPC()
}
Merchant::Merchant() { //sets all of the Merchant specific traits on object creation
- width = HLINE * 10;
- height = HLINE * 16;
+ width = HLINES(10);
+ height = HLINES(16);
type = MERCHT; //sets type to merchant
subtype = 0;
@@ -304,7 +304,7 @@ void Object::reloadTexture(void) {
}
bool Entity::isNear(Entity e) {
- return pow(e.loc.x - loc.x, 2) + pow(e.loc.y - loc.y, 2) <= pow(40 * HLINE, 2);
+ return pow(e.loc.x - loc.x, 2) + pow(e.loc.y - loc.y, 2) <= pow(HLINES(40), 2);
}
void NPC::drawThingy(void) const
@@ -342,7 +342,7 @@ void Entity::draw(void)
switch(type) {
case PLAYERT:
static int texState = 0;
- if (speed && !(loops % ((2.0f/speed) < 1 ? 1 : (int)((float)2.0f/(float)speed)))) {
+ if (speed && !(game::time::getTickCount() % ((2.0f/speed) < 1 ? 1 : (int)((float)2.0f/(float)speed)))) {
if (++texState==9)texState=1;
glActiveTexture(GL_TEXTURE0);
tex->bind(texState);
@@ -389,10 +389,10 @@ NOPE:
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
if (near && type != MOBT)
- ui::putStringCentered(loc.x+width/2,loc.y-ui::fontSize-HLINE/2,name);
+ ui::putStringCentered(loc.x + width / 2, loc.y - ui::fontSize - game::HLINE / 2, name);
if (health != maxHealth) {
- glColor3ub(150,0,0); glRectf(loc.x, loc.y + height, loc.x + width, loc.y + height + HLINE * 2);
- glColor3ub(255,0,0); glRectf(loc.x, loc.y + height, loc.x + width * (health / maxHealth), loc.y + height + HLINE * 2);
+ glColor3ub(150,0,0); glRectf(loc.x, loc.y + height, loc.x + width, loc.y + height + HLINES(2));
+ glColor3ub(255,0,0); glRectf(loc.x, loc.y + height, loc.x + width * (health / maxHealth), loc.y + height + HLINES(2));
}
}
@@ -412,16 +412,16 @@ wander(int timeRun)
hitCooldown--;
if (targetx != 0.9112001f) {
- if (loc.x > targetx + HLINE * 5)
- vel.x = -0.018 * HLINE;
- else if (loc.x < targetx - HLINE * 5)
- vel.x = 0.018 * HLINE;
+ if (loc.x > targetx + HLINES(5))
+ vel.x = HLINES(-0.018);
+ else if (loc.x < targetx - HLINES(5))
+ vel.x = HLINES(0.018);
else
targetx = 0.9112001f;
} else if (ticksToUse == 0) {
ticksToUse = timeRun;
- vel.x = .008 * HLINE;
+ vel.x = HLINES(0.008);
direction = (getRand() % 3 - 1);
if (direction == 0)
@@ -448,6 +448,15 @@ extern int commonAIFunc(NPC *speaker);
void NPC::interact() { //have the npc's interact back to the player
std::thread([this]{
+ std::vector<XMLElement *> dopt;
+ XMLDocument xml;
+ XMLElement *exml,*oxml;
+
+ static unsigned int oldidx = 9999;
+ std::string nname;
+ unsigned int idx;
+ bool stop;
+
loc.y += 5;
canMove=false;
@@ -455,14 +464,144 @@ void NPC::interact() { //have the npc's interact back to the player
right = !left;
if (dialogCount && dialogIndex != 9999) {
- if (!commonAIFunc(this))
- dialogCount--;
+ // load the XML file and find the dialog tags
+ xml.LoadFile(currentXML.c_str());
+COMMONAIFUNC:
+ idx = 0;
+ stop = false;
+ exml = xml.FirstChildElement("Dialog");
+
+ // search for the dialog block associated with this npc
+ while (exml->StrAttribute("name") != name)
+ exml = exml->NextSiblingElement();
+
+ // search for the desired text block
+ exml = exml->FirstChildElement();
+ do {
+ if (std::string("text") == exml->Name() && exml->UnsignedAttribute("id") == (unsigned)dialogIndex)
+ break;
+ } while ((exml = exml->NextSiblingElement()));
+
+ // handle quest tags
+ if ((oxml = exml->FirstChildElement("quest"))) {
+ std::string qname;
+
+ // iterate through all quest tags
+ do {
+ // assign quest
+ if (!(qname = oxml->StrAttribute("assign")).empty())
+ player->qh.assign(qname, "None", std::string(oxml->GetText())); // TODO add descriptions
+
+ // check / finish quest
+ else if (!(qname = oxml->StrAttribute("check")).empty()) {
+ if (player->qh.hasQuest(qname) && player->qh.finish(qname)) {
+ // QuestHandler::finish() did all the work..
+ break;
+ } else {
+ // run error dialog
+ oldidx = dialogIndex;
+ dialogIndex = oxml->UnsignedAttribute("fail");
+ goto COMMONAIFUNC;
+ }
+ }
+ } while((oxml = oxml->NextSiblingElement()));
+ }
+
+ // handle give tags
+ if ((oxml = exml->FirstChildElement("give"))) {
+ do player->inv->addItem(oxml->Attribute("id"), oxml->UnsignedAttribute("count"));
+ while ((oxml = oxml->NextSiblingElement()));
+ }
+
+ // handle take tags
+ if ((oxml = exml->FirstChildElement("take"))) {
+ do player->inv->takeItem(oxml->Attribute("id"), oxml->UnsignedAttribute("count"));
+ while ((oxml = oxml->NextSiblingElement()));
+ }
+
+ // handle movement directs
+ if ((oxml = exml->FirstChildElement("gotox")))
+ moveTo(std::stoi(oxml->GetText()));
+
+ // handle dialog options
+ if ((oxml = exml->FirstChildElement("option"))) {
+ std::string optstr;
+
+ // convert option strings to a colon-separated format
+ do {
+ // append the next option
+ optstr.append(std::string(":") + oxml->Attribute("text"));
+
+ // save the associated XMLElement
+ dopt.push_back(oxml);
+ } while ((oxml = oxml->NextSiblingElement()));
+
+ // run the dialog stuff
+ ui::dialogBox(name, optstr, false, exml->GetText() + 1);
+ ui::waitForDialog();
+
+ if (ui::dialogOptChosen)
+ exml = dopt[ui::dialogOptChosen - 1];
+
+ dopt.clear();
+ }
+
+ // optionless dialog
+ else {
+ ui::dialogBox(name, "", false, exml->GetText());
+ ui::waitForDialog();
+ }
+
+ // trigger other npcs if desired
+ if (!(nname = exml->StrAttribute("call")).empty()) {
+ NPC *n = *std::find_if(std::begin(currentWorld->npc), std::end(currentWorld->npc), [nname](NPC *npc) {
+ return (npc->name == nname);
+ });
+
+ if (exml->QueryUnsignedAttribute("callid", &idx) == XML_NO_ERROR) {
+ n->dialogIndex = idx;
+ n->addAIFunc(false);
+ }
+ }
+
+ // handle potential following dialogs
+ if ((idx = exml->UnsignedAttribute("nextid"))) {
+ dialogIndex = idx;
+
+ // stop talking
+ if (exml->QueryBoolAttribute("stop", &stop) == XML_NO_ERROR && stop) {
+ dialogIndex = 9999;
+ dialogCount--;
+ }
+
+ // pause, allow player to click npc to continue
+ else if (exml->QueryBoolAttribute("pause", &stop) == XML_NO_ERROR && stop) {
+ //return 1;
+ }
+
+ // instantly continue
+ else {
+ goto COMMONAIFUNC;
+ }
+ }
+
+ // stop talking
+ else {
+ // error text?
+ if (oldidx != 9999) {
+ dialogIndex = oldidx;
+ oldidx = 9999;
+ } else {
+ dialogIndex = 9999;
+ dialogCount--;
+ }
+ }
} else {
ui::dialogBox(name, "", false, randomDialog[randDialog]);
}
ui::waitForDialog();
- canMove=true;
+ canMove = true;
}).detach();
}
@@ -475,7 +614,7 @@ void Merchant::wander(int timeRun) {
if (ticksToUse == 0) {
ticksToUse = timeRun;
- vel.x = .008 * HLINE;
+ vel.x = HLINES(0.008);
direction = (getRand() % 3 - 1);
if (direction == 0)
@@ -487,12 +626,12 @@ void Merchant::wander(int timeRun) {
if (vel.x < 0)
currentWorld->goWorldLeft(this);
if (inside != nullptr) {
- loc.y = inside->loc.y + HLINE * 2;
+ loc.y = inside->loc.y + HLINES(2);
vel.y = GRAVITY_CONSTANT * 5;
- if (loc.x <= inside->loc.x + HLINE * 5)
- loc.x = inside->loc.x + HLINE * 5;
- else if (loc.x + width >= inside->loc.x + inside->width - HLINE * 5)
- loc.x = inside->loc.x + inside->width - width - HLINE * 5;
+ if (loc.x <= inside->loc.x + HLINES(5))
+ loc.x = inside->loc.x + HLINES(5);
+ else if (loc.x + width >= inside->loc.x + inside->width - HLINES(5))
+ loc.x = inside->loc.x + inside->width - width - HLINES(5);
}
ticksToUse--;
}
@@ -668,7 +807,7 @@ void Particles::update(float _gravity, float ground_y)
// handle gravity
else if (gravity && vel.y > -1.0f) {
- vel.y -= _gravity * gtime::getDeltaTime();
+ vel.y -= _gravity * game::time::getDeltaTime();
}
}
@@ -685,7 +824,7 @@ void Player::save(void) {
data.append(std::to_string((int)loc.y) + "\n");
data.append(std::to_string((int)health) + "\n");
data.append(std::to_string((int)maxHealth) + "\n");
- data.append(std::to_string((int)gtime::getTickCount()) + "\n");
+ data.append(std::to_string((int)game::time::getTickCount()) + "\n");
data.append(std::to_string((int)inv->items.size()) + "\n");
for(auto &i : inv->items)
@@ -727,7 +866,7 @@ void Player::sspawn(float x,float y) {
std::getline(data,ddata);
maxHealth = std::stoi(ddata);
std::getline(data,ddata);
- gtime::tick(std::stoi(ddata));
+ game::time::tick(std::stoi(ddata));
std::getline(data,ddata);
for(i = std::stoi(ddata);i;i--) {
diff --git a/src/gameplay.cpp b/src/gameplay.cpp
deleted file mode 100644
index 7ffb1f2..0000000
--- a/src/gameplay.cpp
+++ /dev/null
@@ -1,220 +0,0 @@
-#include <common.hpp>
-#include <entities.hpp>
-#include <world.hpp>
-#include <ui.hpp>
-
-#include <tinyxml2.h>
-using namespace tinyxml2;
-
-extern Player *player; // main.cpp
-extern World *currentWorld; // main.cpp
-
-extern Menu pauseMenu;
-extern Menu optionsMenu;
-
-extern std::string xmlFolder;
-
-extern std::vector<NPC *> aipreload;
-
-extern void mainLoop(void); // main.cpp
-
-std::vector<XMLElement *> dopt;
-
-void destroyEverything(void);
-
-int commonAIFunc(NPC *speaker)
-{
- XMLDocument xml;
- XMLElement *exml,*oxml;
-
- static unsigned int oldidx = 9999;
-
- std::string name;
- unsigned int idx = 0;
- bool stop = false;
-
- // load the XML file and find the dialog tags
- xml.LoadFile(currentXML.c_str());
- exml = xml.FirstChildElement("Dialog");
-
- // search for the dialog block associated with this npc
- while (exml->StrAttribute("name") != speaker->name)
- exml = exml->NextSiblingElement();
-
- // search for the desired text block
- exml = exml->FirstChildElement();
- std::cout << speaker->dialogIndex << '\n';
- do {
- if (std::string("text") == exml->Name() && exml->UnsignedAttribute("id") == (unsigned)speaker->dialogIndex)
- break;
- } while ((exml = exml->NextSiblingElement()));
-
- // handle quest tags
- if ((oxml = exml->FirstChildElement("quest"))) {
- std::string qname;
-
- // iterate through all quest tags
- do {
- // assign quest
- if (!(qname = oxml->StrAttribute("assign")).empty())
- player->qh.assign(qname, "None", std::string(oxml->GetText())); // TODO add descriptions
-
- // check / finish quest
- else if (!(qname = oxml->StrAttribute("check")).empty()) {
- if (player->qh.hasQuest(qname) && player->qh.finish(qname)) {
- // QuestHandler::finish() did all the work..
- break;
- } else {
- // run error dialog
- oldidx = speaker->dialogIndex;
- speaker->dialogIndex = oxml->UnsignedAttribute("fail");
- return commonAIFunc(speaker);
- }
- }
- } while((oxml = oxml->NextSiblingElement()));
- }
-
- // handle give tags
- if ((oxml = exml->FirstChildElement("give"))) {
- do player->inv->addItem(oxml->Attribute("id"), oxml->UnsignedAttribute("count"));
- while ((oxml = oxml->NextSiblingElement()));
- }
-
- // handle take tags
- if ((oxml = exml->FirstChildElement("take"))) {
- do player->inv->takeItem(oxml->Attribute("id"), oxml->UnsignedAttribute("count"));
- while ((oxml = oxml->NextSiblingElement()));
- }
-
- // handle movement directs
- if ((oxml = exml->FirstChildElement("gotox")))
- speaker->moveTo(std::stoi(oxml->GetText()));
-
- // handle dialog options
- if ((oxml = exml->FirstChildElement("option"))) {
- std::string optstr;
-
- // convert option strings to a colon-separated format
- do {
- // append the next option
- optstr.append(std::string(":") + oxml->Attribute("text"));
-
- // save the associated XMLElement
- dopt.push_back(oxml);
- } while ((oxml = oxml->NextSiblingElement()));
-
- // run the dialog stuff
- ui::dialogBox(speaker->name, optstr, false, exml->GetText() + 1);
- ui::waitForDialog();
-
- if (ui::dialogOptChosen)
- exml = dopt[ui::dialogOptChosen - 1];
-
- dopt.clear();
- }
-
- // optionless dialog
- else {
- ui::dialogBox(speaker->name, "", false, exml->GetText());
- ui::waitForDialog();
- }
-
- // trigger other npcs if desired
- if (!(name = exml->StrAttribute("call")).empty()) {
- NPC *n = *std::find_if(std::begin(currentWorld->npc), std::end(currentWorld->npc), [name](NPC *npc) {
- return (npc->name == name);
- });
-
- if (exml->QueryUnsignedAttribute("callid", &idx) == XML_NO_ERROR) {
- n->dialogIndex = idx;
- n->addAIFunc(false);
- }
- }
-
- // handle potential following dialogs
- if ((idx = exml->UnsignedAttribute("nextid"))) {
- speaker->dialogIndex = idx;
-
- // stop talking
- if (exml->QueryBoolAttribute("stop", &stop) == XML_NO_ERROR && stop) {
- speaker->dialogIndex = 9999;
- return 0;
- }
-
- // pause, allow player to click npc to continue
- else if (exml->QueryBoolAttribute("pause", &stop) == XML_NO_ERROR && stop) {
- return 1;
- }
-
- // instantly continue
- else {
- return commonAIFunc(speaker);
- }
- }
-
- // stop talking
- else {
- // error text?
- if (oldidx != 9999) {
- speaker->dialogIndex = oldidx;
- oldidx = 9999;
- return 1;
- } else {
- speaker->dialogIndex = 9999;
- return 0;
- }
- }
-
- return 0;
-}
-
-void initEverything(void) {
- std::vector<std::string> xmlFiles;
- XMLDocument xml;
-
- /*
- * Read the XML directory into an array.
- */
-
- if (getdir(std::string("./" + xmlFolder).c_str(), xmlFiles))
- UserError("Error reading XML files!!!");
-
- /*
- * Sort the files alphabetically.
- */
-
- strVectorSortAlpha(&xmlFiles);
-
- /*
- * Load the first file found as currentWorld.
- */
-
- for (xf : xmlFiles) { //unsigned int i=0;i<xmlFiles.size();i++){
- if (xf[0] != '.' && strcmp(&xf[xf.size() - 3], "dat")){
- // read the xml file
- std::cout << "File to load: " << xf << std::endl;
- currentWorld = loadWorldFromXML(xf);
- break;
- }
- }
-
- /*
- * Spawn the player and begin the game.
- */
-
- player = new Player();
- player->sspawn(0,100);
-
- ui::menu::init();
-
- currentWorld->bgmPlay(NULL);
- atexit(destroyEverything);
-}
-
-void destroyEverything(void) {
- currentWorld->save();
- //delete currentWorld;
- //delete[] currentXML;
-
- aipreload.clear();
-}
diff --git a/src/gametime.cpp b/src/gametime.cpp
index be63885..598cd4f 100644
--- a/src/gametime.cpp
+++ b/src/gametime.cpp
@@ -9,42 +9,44 @@ static unsigned int deltaTime = 1;
static unsigned int currentTime = 0;
static unsigned int prevTime, prevPrevTime;
-namespace gtime {
- void setTickCount(unsigned int t) {
- tickCount = t;
- }
-
- unsigned int getTickCount(void) {
- return tickCount;
- }
-
- unsigned int getDeltaTime(void) {
- return deltaTime;
- }
-
- void tick(void) {
- tickCount++;
- }
-
- void tick(unsigned int ticks) {
- tickCount += ticks;
- }
-
- void mainLoopHandler(void) {
- if (!currentTime)
- currentTime = prevTime = millis();
-
- currentTime = millis();
- deltaTime = currentTime - prevTime;
- prevTime = currentTime;
- }
-
- bool tickHasPassed(void) {
- if (prevPrevTime + MSEC_PER_TICK <= currentTime) {
- prevPrevTime = currentTime;
- return true;
- }
-
- return false;
+namespace game {
+ namespace time {
+ void setTickCount(unsigned int t) {
+ tickCount = t;
+ }
+
+ unsigned int getTickCount(void) {
+ return tickCount;
+ }
+
+ unsigned int getDeltaTime(void) {
+ return (deltaTime > 0) ? deltaTime : 1;
+ }
+
+ void tick(void) {
+ tickCount++;
+ }
+
+ void tick(unsigned int ticks) {
+ tickCount += ticks;
+ }
+
+ void mainLoopHandler(void) {
+ if (!currentTime)
+ currentTime = prevTime = millis();
+
+ currentTime = millis();
+ deltaTime = currentTime - prevTime;
+ prevTime = currentTime;
+ }
+
+ bool tickHasPassed(void) {
+ if (prevPrevTime + MSEC_PER_TICK <= currentTime) {
+ prevPrevTime = currentTime;
+ return true;
+ }
+
+ return false;
+ }
}
}
diff --git a/src/inventory.cpp b/src/inventory.cpp
index f8b8c3e..07ce377 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -43,8 +43,8 @@ void initInventorySprites(void) {
itemMap.back()->name = exml->StrAttribute("name");
itemMap.back()->type = exml->StrAttribute("type");
itemMap.back()->texloc = exml->StrAttribute("sprite");
- itemMap.back()->width = exml->FloatAttribute("width") * HLINE;
- itemMap.back()->height = exml->FloatAttribute("height") * HLINE;
+ itemMap.back()->width = HLINES(exml->FloatAttribute("width"));
+ itemMap.back()->height = HLINES(exml->FloatAttribute("height"));
itemMap.back()->maxStackSize = exml->UnsignedAttribute("maxstack");
itemMap.back()->attribValue = exml->FloatAttribute("value");
itemMap.back()->tex = new Texturec({ itemMap.back()->texloc });
@@ -219,7 +219,7 @@ void Inventory::draw(void) {
static vec2 mouseStart = {0,0};
C("End define");
- auto deltaTime = gtime::getDeltaTime();
+ auto deltaTime = game::time::getDeltaTime();
for (auto &r : iray) {
r.start.x = player->loc.x + (player->width / 2);
@@ -228,15 +228,15 @@ void Inventory::draw(void) {
} a = 0;
for (auto &cr : curRay) {
- cr.start.x = (offset.x + SCREEN_WIDTH / 2);
+ cr.start.x = (offset.x + game::SCREEN_WIDTH / 2);
cr.start.y = offset.y - (a * itemWide * 1.5f);
curCurCoord[a++] = cr.start;
} a = 0;
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 8; c++) {
- massRay[a].x = ((offset.x - SCREEN_WIDTH / 2) + itemWide) + c * itemWide * 1.5f;
- massRay[a].y = ((offset.y + SCREEN_HEIGHT / 2) - itemWide * 1.5f) - r * itemWide * 1.5f;
+ massRay[a].x = ((offset.x - game::SCREEN_WIDTH / 2) + itemWide) + c * itemWide * 1.5f;
+ massRay[a].y = ((offset.y + game::SCREEN_HEIGHT / 2) - itemWide * 1.5f) - r * itemWide * 1.5f;
a++;
}
} a = 0;
@@ -594,7 +594,7 @@ int Inventory::useItem(void)
}
if (up)
- hangle += 0.325f * dir * gtime::getDeltaTime();
+ hangle += 0.325f * dir * game::time::getDeltaTime();
if (!player->left) {
if (hangle <= -90)
@@ -633,7 +633,7 @@ bool Inventory::detectCollision(vec2 one, vec2 two) {
}
}
- i+=HLINE;
+ i += game::HLINE;
}
}
return false;
diff --git a/src/mob.cpp b/src/mob.cpp
index ea97474..c783209 100644
--- a/src/mob.cpp
+++ b/src/mob.cpp
@@ -16,10 +16,11 @@ void Page::act(void)
{
if (player->loc.x > loc.x - 100 && player->loc.x < loc.x + 100 && isInside(ui::mouse) &&
(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT))) {
- std::cout << "Hey\n";
- ui::drawPage(pageTexPath);
- ui::waitForDialog();
- die();
+ std::thread([this](void){
+ ui::drawPage(pageTexPath);
+ ui::waitForDialog();
+ die();
+ }).detach();
}
}
@@ -132,7 +133,7 @@ Bird::Bird(void)
void Bird::act(void)
{
static bool direction = false;
- auto deltaTime = gtime::getDeltaTime();
+ auto deltaTime = game::time::getDeltaTime();
if (!--actCounter) {
actCounter = actCounterInitial;
direction ^= 1;
@@ -172,13 +173,20 @@ Trigger::Trigger(void)
width = HLINES(20);
height = 2000;
tex = new Texturec(0);
+ triggered = false;
}
void Trigger::act(void)
{
auto c = player->loc.x + player->width / 2;
- if (c > loc.x && c < loc.x + width) {
+ static bool running = false;
+
+ if (triggered) {
+ die();
+ } else if (!running && c > loc.x && c < loc.x + width) {
std::thread([&]{
+ running = true;
+
XMLDocument xml;
XMLElement *exml;
@@ -203,7 +211,9 @@ void Trigger::act(void)
}
ui::toggleBlackFast();
- die();
+
+ triggered = true;
+ running = false;
}).detach();
}
}
diff --git a/src/threadpool.cpp b/src/threadpool.cpp
deleted file mode 100644
index c4f1c4a..0000000
--- a/src/threadpool.cpp
+++ /dev/null
@@ -1,99 +0,0 @@
-#include <threadpool.hpp>
-
-/**
- * Stolen from some guy.
- */
-
-// Constructor.
-ThreadPool::ThreadPool(int threads) :
- terminate(false),
- stopped(false)
-{
- // Create number of required threads and add them to the thread pool vector.
- for(int i = 0; i < threads; i++)
- {
- threadPool.emplace_back(thread(&ThreadPool::Invoke, this));
- }
-}
-
-void ThreadPool::Enqueue(function<void()> f)
-{
- // Scope based locking.
- {
- // Put unique lock on task mutex.
- unique_lock<mutex> lock(tasksMutex);
-
- // Push task into queue.
- tasks.push(f);
- }
-
- // Wake up one thread.
- condition.notify_one();
-}
-
-void ThreadPool::Invoke() {
-
- function<void()> task;
- while(true)
- {
- // Scope based locking.
- {
- // Put unique lock on task mutex.
- unique_lock<mutex> lock(tasksMutex);
-
- // Wait until queue is not empty or termination signal is sent.
- condition.wait(lock, [this]{ return !tasks.empty() || terminate; });
-
- // If termination signal received and queue is empty then exit else continue clearing the queue.
- if (terminate && tasks.empty())
- {
- return;
- }
-
- // Get next task in the queue.
- task = tasks.front();
-
- // Remove it from the queue.
- tasks.pop();
- }
-
- // Execute the task.
- task();
- }
-}
-
-void ThreadPool::ShutDown()
-{
- // Scope based locking.
- {
- // Put unique lock on task mutex.
- unique_lock<mutex> lock(tasksMutex);
-
- // Set termination flag to true.
- terminate = true;
- }
-
- // Wake up all threads.
- condition.notify_all();
-
- // Join all threads.
- for(thread &thread : threadPool)
- {
- thread.join();
- }
-
- // Empty workers vector.
- threadPool.empty();
-
- // Indicate that the pool has been shut down.
- stopped = true;
-}
-
-// Destructor.
-ThreadPool::~ThreadPool()
-{
- if (!stopped)
- {
- ShutDown();
- }
-}
diff --git a/src/ui.cpp b/src/ui.cpp
index 654820d..8e9cd1d 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -411,7 +411,7 @@ namespace ui {
linc=0, // Contains the number of letters that should be drawn.
size=0; // Contains the full size of the current string.
- auto tickCount = gtime::getTickCount();
+ auto tickCount = game::time::getTickCount();
// reset values if a new string is being passed.
if (!linc || ret.substr(0, linc) != str.substr(0, linc)) {
@@ -561,8 +561,7 @@ namespace ui {
}
void waitForCover(void) {
- while (fadeIntensity < 255)
- mainLoop();
+ while (fadeIntensity < 255);
fadeIntensity = 255;
}
@@ -573,33 +572,31 @@ namespace ui {
void importantText(const char *text,...) {
va_list textArgs;
- char *printfbuf;
+ std::unique_ptr<char[]> printfbuf (new char[512]);
dialogBoxText.clear();
- printfbuf = new char[ 512 ];
va_start(textArgs,text);
- vsnprintf(printfbuf,512,text,textArgs);
+ vsnprintf(printfbuf.get(),512,text,textArgs);
va_end(textArgs);
- dialogBoxText = printfbuf;
- delete[] printfbuf;
+ dialogBoxText = printfbuf.get();
dialogBoxExists = true;
dialogImportant = true;
+ dialogPassive = false;
+ dialogPassiveTime = 0;
}
void passiveImportantText(int duration, const char *text, ...) {
va_list textArgs;
- char *printfbuf;
+ std::unique_ptr<char[]> printfbuf (new char[512]);
dialogBoxText.clear();
- printfbuf = new char[ 512 ];
va_start(textArgs,text);
- vsnprintf(printfbuf,512,text,textArgs);
+ vsnprintf(printfbuf.get(),512,text,textArgs);
va_end(textArgs);
- dialogBoxText = printfbuf;
- delete[] printfbuf;
+ dialogBoxText = printfbuf.get();
dialogBoxExists = true;
dialogImportant = true;
@@ -634,8 +631,11 @@ namespace ui {
float x,y,tmp;
std::string rtext;
+ auto SCREEN_WIDTH = game::SCREEN_WIDTH;
+ auto SCREEN_HEIGHT = game::SCREEN_HEIGHT;
+
// will return if not toggled
- action::draw(vec2 {player->loc.x + player->width / 2, player->loc.y + player->height + HLINE});
+ action::draw(vec2 {player->loc.x + player->width / 2, player->loc.y + player->height + game::HLINE});
if (pageTexReady) {
glEnable(GL_TEXTURE_2D);
@@ -649,32 +649,32 @@ namespace ui {
glDisable(GL_TEXTURE_2D);
} else if (dialogBoxExists) {
-
- rtext=typeOut(dialogBoxText);
+ rtext = typeOut(dialogBoxText);
if (dialogImportant) {
setFontColor(255,255,255);
if (dialogPassive) {
- dialogPassiveTime -= gtime::getDeltaTime();
+ dialogPassiveTime -= game::time::getDeltaTime();
if (dialogPassiveTime < 0) {
dialogPassive = false;
dialogImportant = false;
dialogBoxExists = false;
}
}
+
if (fadeIntensity == 255 || dialogPassive) {
setFontSize(24);
putStringCentered(offset.x,offset.y,rtext);
setFontSize(16);
}
}else if (dialogMerchant) {
- x=offset.x-SCREEN_WIDTH/6;
- y=(offset.y+SCREEN_HEIGHT/2)-HLINE*8;
+ x = offset.x - SCREEN_WIDTH / 6;
+ y = (offset.y + SCREEN_HEIGHT / 2) - HLINES(8);
drawBox(vec2 {x, y}, vec2 {x + SCREEN_WIDTH / 3, y - SCREEN_HEIGHT * 0.6f});
// draw typeOut'd text
- putString(x + HLINE, y - fontSize - HLINE, (rtext = typeOut(dialogBoxText)));
+ putString(x + game::HLINE, y - fontSize - game::HLINE, (rtext = typeOut(dialogBoxText)));
std::string itemString1 = std::to_string(merchTrade.quantity[0]) + "x",
itemString2 = std::to_string(merchTrade.quantity[1]) + "x";
@@ -736,7 +736,7 @@ namespace ui {
setFontColor(255, 255, 255);
// draw option
- dialogOptText[i].second.y = y - SCREEN_HEIGHT / 2 - (fontSize + HLINE) * (i + 1);
+ dialogOptText[i].second.y = y - SCREEN_HEIGHT / 2 - (fontSize + game::HLINE) * (i + 1);
tmp = putStringCentered(offset.x, dialogOptText[i].second.y, dialogOptText[i].first);
// get coordinate information on option
@@ -754,21 +754,20 @@ namespace ui {
setFontColor(255, 255, 255);
} else { //normal dialog box
- x = offset.x - SCREEN_WIDTH / 2 + HLINE * 8;
- y = offset.y + SCREEN_HEIGHT / 2 - HLINE * 8;
+ x = offset.x - SCREEN_WIDTH / 2 + HLINES(8);
+ y = offset.y + SCREEN_HEIGHT / 2 - HLINES(8);
- drawBox(vec2 {x, y}, vec2 {x + SCREEN_WIDTH - HLINE * 16, y - SCREEN_HEIGHT / 4});
+ drawBox(vec2 {x, y}, vec2 {x + SCREEN_WIDTH - HLINES(16), y - SCREEN_HEIGHT / 4});
rtext = typeOut(dialogBoxText);
-
- putString(x+HLINE,y-fontSize-HLINE,rtext);
+ putString(x + game::HLINE, y - fontSize - game::HLINE, rtext);
for(i=0;i<dialogOptText.size();i++) {
setFontColor(255,255,255);
tmp = putStringCentered(offset.x,dialogOptText[i].second.y,dialogOptText[i].first);
dialogOptText[i].second.z = offset.x + tmp;
dialogOptText[i].second.x = offset.x - tmp;
- dialogOptText[i].second.y = y - SCREEN_HEIGHT / 4 + (fontSize + HLINE) * (i + 1);
+ dialogOptText[i].second.y = y - SCREEN_HEIGHT / 4 + (fontSize + game::HLINE) * (i + 1);
if (mouse.x > dialogOptText[i].second.x &&
mouse.x < dialogOptText[i].second.z &&
mouse.y > dialogOptText[i].second.y &&
@@ -844,8 +843,8 @@ namespace ui {
dialogBoxExists = false;
currentMenu = NULL;
gameRunning = false;
- config::update();
- config::save();
+ game::config::update();
+ game::config::save();
}
void closeBox() {
@@ -856,6 +855,9 @@ namespace ui {
void dialogAdvance(void) {
unsigned char i;
+ dialogPassive = false;
+ dialogPassiveTime = 0;
+
if (pageTex) {
glDeleteTextures(1, &pageTex);
pageTex = 0;
@@ -898,9 +900,6 @@ EXIT:
//if (!dialogMerchant)closeBox();
dialogBoxExists = false;
dialogMerchant = false;
- dialogPassive = false;
-
- //DONE:
// handle important text
if (dialogImportant) {
@@ -913,6 +912,10 @@ EXIT:
static bool left=true,right=false;
static int heyOhLetsGo = 0;
static int mouseWheelUpCount = 0, mouseWheelDownCount = 0;
+
+ auto SCREEN_WIDTH = game::SCREEN_WIDTH;
+ auto SCREEN_HEIGHT = game::SCREEN_HEIGHT;
+
World *tmp;
vec2 oldpos,tmppos;
SDL_Event e;
@@ -1001,7 +1004,7 @@ EXIT:
// space - make player jump
if (SDL_KEY == SDLK_SPACE) {
if (player->ground) {
- player->loc.y += HLINE * 2;
+ player->loc.y += HLINES(2);
player->vel.y = .4;
player->ground = false;
}
@@ -1012,7 +1015,7 @@ EXIT:
tmp = currentWorld;
switch(SDL_KEY) {
case SDLK_t:
- gtime::tick(50);
+ game::time::tick(50);
break;
case SDLK_a:
if (fadeEnable)break;
@@ -1090,12 +1093,12 @@ EXIT:
// start hover counter?
if (!heyOhLetsGo) {
- heyOhLetsGo = loops;
+ heyOhLetsGo = game::time::getTickCount();
player->inv->mouseSel = false;
}
// run hover thing
- if (loops - heyOhLetsGo >= 2 && !(player->inv->invOpen) && !(player->inv->selected)) {
+ if (game::time::getTickCount() - heyOhLetsGo >= 2 && !(player->inv->invOpen) && !(player->inv->selected)) {
player->inv->invHover = true;
// enable action ui
@@ -1232,6 +1235,9 @@ EXIT:
}
void drawFade(void) {
+ auto SCREEN_WIDTH = game::SCREEN_WIDTH;
+ auto SCREEN_HEIGHT = game::SCREEN_HEIGHT;
+
if (!fadeIntensity) {
if (fontSize != 16)
setFontSize(16);
@@ -1292,6 +1298,9 @@ EXIT:
}
void takeScreenshot(GLubyte* pixels) {
+ auto SCREEN_WIDTH = game::SCREEN_WIDTH;
+ auto SCREEN_HEIGHT = game::SCREEN_HEIGHT;
+
std::vector<GLubyte> bgr (SCREEN_WIDTH * SCREEN_HEIGHT * 3, 0);
for(uint x = 0; x < SCREEN_WIDTH*SCREEN_HEIGHT*3; x+=3) {
diff --git a/src/ui_menu.cpp b/src/ui_menu.cpp
index 0c7a6d8..09b09c8 100644
--- a/src/ui_menu.cpp
+++ b/src/ui_menu.cpp
@@ -11,7 +11,7 @@ void Menu::gotoParent(void)
{
if (!parent) {
currentMenu = nullptr;
- config::update();
+ game::config::update();
} else {
currentMenu = parent;
}
@@ -90,9 +90,9 @@ namespace ui {
pauseMenu.items.push_back(ui::menu::createButton({-256/2,-300},{256,75},{0.0f,0.0f,0.0f}, "Segfault", segFault));
pauseMenu.child = &optionsMenu;
- optionsMenu.items.push_back(ui::menu::createSlider({0-(float)SCREEN_WIDTH/4,0-(512/2)}, {50,512}, {0.0f, 0.0f, 0.0f}, 0, 100, "Master", &VOLUME_MASTER));
- optionsMenu.items.push_back(ui::menu::createSlider({-200,100}, {512,50}, {0.0f, 0.0f, 0.0f}, 0, 100, "Music", &VOLUME_MUSIC));
- optionsMenu.items.push_back(ui::menu::createSlider({-200,000}, {512,50}, {0.0f, 0.0f, 0.0f}, 0, 100, "SFX", &VOLUME_SFX));
+ optionsMenu.items.push_back(ui::menu::createSlider({0-static_cast<float>(game::SCREEN_WIDTH)/4,0-(512/2)}, {50,512}, {0.0f, 0.0f, 0.0f}, 0, 100, "Master", &game::config::VOLUME_MASTER));
+ optionsMenu.items.push_back(ui::menu::createSlider({-200,100}, {512,50}, {0.0f, 0.0f, 0.0f}, 0, 100, "Music", &game::config::VOLUME_MUSIC));
+ optionsMenu.items.push_back(ui::menu::createSlider({-200,000}, {512,50}, {0.0f, 0.0f, 0.0f}, 0, 100, "SFX", &game::config::VOLUME_SFX));
optionsMenu.parent = &pauseMenu;
}
@@ -101,10 +101,13 @@ namespace ui {
}
void draw(void) {
+ auto SCREEN_WIDTH = game::SCREEN_WIDTH;
+ auto SCREEN_HEIGHT = game::SCREEN_HEIGHT;
+
SDL_Event e;
setFontSize(24);
- config::update();
+ game::config::update();
mouse.x = ui::premouse.x+offset.x-(SCREEN_WIDTH/2);
mouse.y = (offset.y+SCREEN_HEIGHT/2)-ui::premouse.y;
diff --git a/src/world.cpp b/src/world.cpp
index 785b2b4..ab2c908 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -219,13 +219,13 @@ generate(int width)
}
// define x-coordinate of world's leftmost 'line'
- worldStart = (width - GROUND_HILLINESS) * HLINE / 2 * -1;
+ worldStart = (width - GROUND_HILLINESS) * game::HLINE / 2 * -1;
// create empty star array, should be filled here as well...
star = std::vector<vec2> (100, vec2 { 0, 400 });
for (auto &s : star) {
s.x = (getRand() % (-worldStart * 2)) + worldStart;
- s.y = (getRand() % SCREEN_HEIGHT) + 100;
+ s.y = (getRand() % game::SCREEN_HEIGHT) + 100;
}
}
@@ -237,6 +237,10 @@ generate(int width)
void World::
draw(Player *p)
{
+ auto SCREEN_WIDTH = game::SCREEN_WIDTH;
+ auto SCREEN_HEIGHT = game::SCREEN_HEIGHT;
+ auto HLINE = game::HLINE;
+
const ivec2 backgroundOffset = ivec2 {
static_cast<int>(SCREEN_WIDTH) / 2, static_cast<int>(SCREEN_HEIGHT) / 2
};
@@ -542,7 +546,7 @@ singleDetect(Entity *e)
unsigned int i;
int l;
- auto deltaTime = gtime::getDeltaTime();
+ auto deltaTime = game::time::getDeltaTime();
// kill dead entities
if (!e->isAlive()) {
@@ -593,7 +597,7 @@ singleDetect(Entity *e)
e->handleHits();
// calculate the line that this entity is currently standing on
- l = static_cast<int>(fmax((e->loc.x + e->width / 2 - worldStart) / HLINE, 0));
+ l = static_cast<int>(fmax((e->loc.x + e->width / 2 - worldStart) / game::HLINE, 0));
l = static_cast<int>(fmin(l, lineCount - 1));
// if the entity is under the world/line, pop it back to the surface
@@ -626,10 +630,10 @@ singleDetect(Entity *e)
// insure that the entity doesn't fall off either edge of the world.
if (e->loc.x < worldStart) {
e->vel.x = 0;
- e->loc.x = worldStart + HLINE / 2;
- } else if (e->loc.x + e->width + HLINE > worldStart + worldStart * -2) {
+ e->loc.x = worldStart + game::HLINE / 2;
+ } else if (e->loc.x + e->width + game::HLINE > worldStart + worldStart * -2) {
e->vel.x = 0;
- e->loc.x = worldStart + worldStart * -2 - e->width - HLINE;
+ e->loc.x = worldStart + worldStart * -2 - e->width - game::HLINE;
}
}
}
@@ -656,7 +660,7 @@ detect(Player *p)
// handle particles
for (auto &part : particles) {
// get particle's current world line
- l = (int)fmax((part.loc.x + part.width / 2 - worldStart) / HLINE, 0);
+ l = (int)fmax((part.loc.x + part.width / 2 - worldStart) / game::HLINE, 0);
l = (int)fmin(lineCount - 1, l);
part.update(GRAVITY_CONSTANT, worldData[l].groundHeight);
}
@@ -666,10 +670,10 @@ detect(Player *p)
switch (b->bsubtype) {
case FOUNTAIN:
for (unsigned int r = (randGet() % 25) + 11; r--;) {
- addParticle(randGet() % HLINE * 3 + b->loc.x + b->width / 2, // x
+ addParticle(randGet() % HLINES(3) + b->loc.x + b->width / 2, // x
b->loc.y + b->height, // y
- HLINE * 1.25, // width
- HLINE * 1.25, // height
+ HLINES(1.25), // width
+ HLINES(1.25), // height
randGet() % 7 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x
(4 + randGet() % 6) * .05, // vel.y
{ 0, 0, 255 }, // RGB color
@@ -681,9 +685,9 @@ detect(Player *p)
case FIRE_PIT:
for(unsigned int r = (randGet() % 20) + 11; r--;) {
addParticle(randGet() % (int)(b->width / 2) + b->loc.x + b->width / 4, // x
- b->loc.y + 3 * HLINE, // y
- HLINE, // width
- HLINE, // height
+ b->loc.y + HLINES(3), // y
+ game::HLINE, // width
+ game::HLINE, // height
randGet() % 3 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x
(4 + randGet() % 6) * .005, // vel.y
{ 255, 0, 0 }, // RGB color
@@ -700,11 +704,13 @@ detect(Player *p)
// draws the village welcome message if the player enters the village bounds
for (auto &v : village) {
- if (p->loc.x > v.start.x && p->loc.x < v.end.x && !v.in) {
- ui::passiveImportantText(5000, "Welcome to %s", v.name.c_str());
- v.in = true;
- } else {
- v.in = false;
+ if (p->loc.x > v.start.x && p->loc.x < v.end.x) {
+ if (!v.in) {
+ ui::passiveImportantText(5000, "Welcome to %s", v.name.c_str());
+ v.in = true;
+ }
+ } else {
+ v.in = false;
}
}
}
@@ -1042,12 +1048,12 @@ goWorldLeft(Player *p)
World *tmp;
// check if player is at world edge
- if (!toLeft.empty() && p->loc.x < worldStart + HLINE * 15.0f) {
+ if (!toLeft.empty() && p->loc.x < worldStart + HLINES(15)) {
// load world (`toLeft` conditional confirms existance)
tmp = loadWorldFromPtr(currentWorldToLeft);
// adjust player location
- p->loc.x = tmp->worldStart + HLINE * 20;
+ p->loc.x = tmp->worldStart + HLINES(20);
p->loc.y = tmp->worldData[tmp->lineCount - 1].groundHeight;
return tmp;
@@ -1064,10 +1070,10 @@ goWorldRight(Player *p)
{
World *tmp;
- if (!toRight.empty() && p->loc.x + p->width > -worldStart - HLINE * 15) {
+ if (!toRight.empty() && p->loc.x + p->width > -worldStart - HLINES(15)) {
tmp = loadWorldFromPtr(currentWorldToRight);
- p->loc.x = tmp->worldStart - HLINE * -15.0f;
+ p->loc.x = tmp->worldStart - HLINES(-15.0);
p->loc.y = GROUND_HEIGHT_MINIMUM;
return tmp;
@@ -1083,7 +1089,7 @@ bool World::
goWorldLeft(NPC *e)
{
// check if entity is at world edge
- if(!toLeft.empty() && e->loc.x < worldStart + HLINE * 15.0f) {
+ if(!toLeft.empty() && e->loc.x < worldStart + HLINES(15)) {
currentWorldToLeft->addNPC(e->loc.x,e->loc.y);
e->die();
@@ -1361,22 +1367,22 @@ singleDetect(Entity *e)
}
if (e->vel.y > -2)
- e->vel.y -= GRAVITY_CONSTANT * gtime::getDeltaTime();
+ e->vel.y -= GRAVITY_CONSTANT * game::time::getDeltaTime();
if (e->ground) {
e->loc.y = ceil(e->loc.y);
e->vel.y = 0;
}
- start = worldStart + fstart[floornum] * HLINE;
- end = start + floor[floornum].size() * HLINE;
+ start = worldStart + HLINES(fstart[floornum]);
+ end = start + HLINES(floor[floornum].size());
if (e->loc.x < start) {
e->vel.x = 0;
- e->loc.x = start + HLINE / 2;
- } else if (e->loc.x + e->width + HLINE > end) {
+ e->loc.x = start + game::HLINE / 2;
+ } else if (e->loc.x + e->width + game::HLINE > end) {
e->vel.x = 0;
- e->loc.x = end - e->width - HLINE;
+ e->loc.x = end - e->width - game::HLINE;
}
}
@@ -1387,6 +1393,10 @@ draw(Player *p)
unsigned int i,f;
int x;
+ auto SCREEN_WIDTH = game::SCREEN_WIDTH;
+ auto SCREEN_HEIGHT = game::SCREEN_HEIGHT;
+ auto HLINE = game::HLINE;
+
// draw lights
for (auto &l : light) {
if (l.belongsTo) {
@@ -1459,7 +1469,7 @@ draw(Player *p)
for (f = 0; f < floor.size(); f++) {
i = 0;
for (h : floor[f]) {
- x = worldStart + fstart[f] * HLINE + (i * HLINE);
+ x = worldStart + fstart[f] * HLINE + HLINES(i);
glVertex2i(x , h );
glVertex2i(x + HLINE, h );
glVertex2i(x + HLINE, h - INDOOR_FLOOR_THICKNESS);
@@ -1515,7 +1525,7 @@ World *Arena::exitArena(Player *p) {
World *tmp;
if (!mmob->isAlive() &&
p->loc.x + p->width / 2 > mob[0]->loc.x &&
- p->loc.x + p->width / 2 < mob[0]->loc.x + HLINE * 12) {
+ p->loc.x + p->width / 2 < mob[0]->loc.x + HLINES(12)) {
tmp = battleNest.front();
battleNest.erase(battleNest.begin());
@@ -1530,9 +1540,9 @@ World *Arena::exitArena(Player *p) {
mmob->die();
return tmp;
- }else{
- return this;
}
+
+ return this;
}
std::string getWorldWeatherStr(WorldWeather ww)
@@ -1690,21 +1700,7 @@ loadWorldFromXMLNoSave(std::string path) {
}
}
- /**
- * 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");
- }
-
- */
-
+ // mob creation
else if (name == "rabbit") {
tmp->addMob(new Rabbit(), vec2 {0, 0});
tmp->getLastMob()->createFromXML(wxml);
@@ -1722,36 +1718,6 @@ loadWorldFromXMLNoSave(std::string path) {
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 + "!");
-
- // spawn at coordinate if desired
- if (wxml->QueryFloatAttribute("x", &spawnx) == XML_NO_ERROR)
- tmp->addMob(flooor, spawnx, wxml->FloatAttribute("y"));
- else
- tmp->addMob(flooor, 0, 100);
-
- // aggressive tag
- if (wxml->QueryBoolAttribute("aggressive", &dialog) == XML_NO_ERROR)
- tmp->getLastMob()->aggressive = dialog;
-
- // indoor spawning floor selection
- if (Indoor && wxml->QueryUnsignedAttribute("floor", &flooor) == XML_NO_ERROR)
- Indoorp(tmp)->moveToFloor(tmp->npc.back(), flooor);
-
- // custom health value
- if (wxml->QueryFloatAttribute("health", &spawnx) == XML_NO_ERROR)
- tmp->getLastMob()->health = tmp->getLastMob()->maxHealth = spawnx;
- }*/
-
// npc creation
else if (name == "npc") {
const char *npcname;
@@ -1796,7 +1762,7 @@ loadWorldFromXMLNoSave(std::string path) {
} else if (name == "hill") {
tmp->addHill(ivec2 { wxml->IntAttribute("peakx"), wxml->IntAttribute("peaky") }, wxml->UnsignedAttribute("width"));
} else if (name == "time") {
- gtime::setTickCount(std::stoi(wxml->GetText()));
+ game::time::setTickCount(std::stoi(wxml->GetText()));
} else if (Indoor && name == "floor") {
if (wxml->QueryFloatAttribute("start",&spawnx) == XML_NO_ERROR)
Indoorp(tmp)->addFloor(wxml->UnsignedAttribute("width"), spawnx);