aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authordrumsetmonkey <abelleisle@roadrunner.com>2016-02-23 07:29:25 -0500
committerdrumsetmonkey <abelleisle@roadrunner.com>2016-02-23 07:29:25 -0500
commitf67648a124655305c3e8947fe312b97ccf74e2fd (patch)
treee2c2a006698836070b3ce651ac0fe89d192da6bc /src
parent0075f9e7997d01fa2105bff38e40a3d5b8b421e6 (diff)
parent0d77d11b708e57546d8267e957230c30bc5d048c (diff)
Villages!
Diffstat (limited to 'src')
-rw-r--r--src/config.cpp39
-rw-r--r--src/entities.cpp77
-rw-r--r--src/gameplay.cpp6
-rw-r--r--src/ui.cpp52
-rw-r--r--src/world.cpp108
5 files changed, 195 insertions, 87 deletions
diff --git a/src/config.cpp b/src/config.cpp
index ca6db1b..72a071d 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -9,9 +9,9 @@ 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 float VOLUME_MASTER;
+extern float VOLUME_MUSIC;
+extern float VOLUME_SFX;
XMLDocument xml;
XMLElement *scr;
@@ -19,6 +19,7 @@ XMLElement *vol;
void readConfig(){
unsigned int uval;
+ float fval;
bool bval;
xml.LoadFile("config/settings.xml");
@@ -36,32 +37,34 @@ void readConfig(){
if(xml.FirstChildElement("hline")->QueryUnsignedAttribute("size",&uval) == XML_NO_ERROR)
HLINE = uval;
else HLINE = 3;
-
- /*SCREEN_WIDTH = scr->UnsignedAttribute("width");
- SCREEN_HEIGHT = scr->UnsignedAttribute("height");
- FULLSCREEN = scr->BoolAttribute("fullscreen");
- HLINE = xml.FirstChildElement("hline")->UnsignedAttribute("size");*/
vol = xml.FirstChildElement("volume");
- VOLUME_MASTER = vol->FirstChildElement("master")->FloatAttribute("volume");
- VOLUME_MUSIC = vol->FirstChildElement("music")->FloatAttribute("volume");
- VOLUME_SFX = vol->FirstChildElement("sfx")->FloatAttribute("volume");
- std::cout<<"Loading font through settings.xml..."<<std::endl;
+ 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;
+
ui::initFonts();
ui::setFontFace(xml.FirstChildElement("font")->Attribute("path"));
+ updateConfig();
}
void updateConfig(){
- vol->FirstChildElement("master")->SetAttribute("volume",VOLUME_MASTER);
- vol->FirstChildElement("music")->SetAttribute("volume",VOLUME_MUSIC);
- vol->FirstChildElement("sfx")->SetAttribute("volume", VOLUME_SFX);
-
Mix_Volume(0,VOLUME_MASTER);
- Mix_Volume(1,VOLUME_SFX);
- Mix_VolumeMusic(VOLUME_MUSIC);
+ Mix_Volume(1,VOLUME_SFX * (VOLUME_MASTER/100.0f));
+ Mix_VolumeMusic(VOLUME_MUSIC * (VOLUME_MASTER/100.0f));
}
void saveConfig(){
+ 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 e020f8b..922a07c 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -62,7 +62,7 @@ void Entity::spawn(float x, float y){ //spawns the entity you pass to it based o
right = true;
left = false;
near = false;
- canMove = true;
+ //canMove = true;
ground = false;
hit = false;
@@ -88,6 +88,8 @@ Player::Player(){ //sets all of the player specific traits on object creation
subtype = 0;
health = maxHealth = 100;
speed = 1;
+ canMove = true;
+
tex = new Texturec(9, "assets/player/playerk.png",
"assets/player/playerk1.png",
"assets/player/playerk2.png",
@@ -115,6 +117,7 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation
health = maxHealth = 100;
maxHealth = health = 100;
+ canMove = true;
tex = new Texturec(1,"assets/NPC.png");
inv = new Inventory(NPC_INV_SIZE);
@@ -141,6 +144,7 @@ Structures::Structures(){ //sets the structure type
name = NULL;
inv = NULL;
+ canMove = false;
}
Structures::~Structures(){
delete tex;
@@ -155,6 +159,7 @@ Mob::Mob(int sub){
aggressive = false;
maxHealth = health = 50;
+ canMove = true;
switch((subtype = sub)){
case MS_RABBIT:
@@ -198,6 +203,7 @@ Object::Object(){
near = false;
width = 0;
height = 0;
+ canMove = false;
maxHealth = health = 1;
@@ -267,7 +273,7 @@ void Entity::draw(void){ //draws the entities
switch(type){
case PLAYERT:
static int texState = 0;
- if(speed && !(loops % (int)(2.0f/(float)speed))){
+ if(speed && !(loops % ((2.0f/speed) < 1 ? 1 : (int)((float)2.0f/(float)speed)))){
if(++texState==9)texState=1;
glActiveTexture(GL_TEXTURE0);
tex->bind(texState);
@@ -332,9 +338,6 @@ NOPE:
if(near)ui::putStringCentered(loc.x+width/2,loc.y-ui::fontSize-HLINE/2,name);
}
-void Player::interact(){
-}
-
/*
* NPC::wander, this makes the npcs wander around the near area
*
@@ -546,3 +549,67 @@ void Mob::wander(int timeRun){
break;
}
}
+
+void Player::save(void){
+ std::string data;
+ std::ofstream out ("xml/main.dat",std::ios::out | std::ios::binary);
+ std::cout<<"Saving player data..."<<std::endl;
+ data.append(std::to_string((int)loc.x) + "\n");
+ 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)inv->items.size()) + "\n");
+ for(auto &i : inv->items)
+ data.append(std::to_string((int)i.count) + "\n" + std::to_string((int)i.id) + "\n");
+
+ data.append((std::string)(currentXML+4) + "\n");
+
+ data.append("dOnE\0");
+ out.write(data.c_str(),data.size());
+ out.close();
+}
+
+void Player::sspawn(float x,float y){
+ unsigned int i;
+ uint count;
+ std::ifstream in ("xml/main.dat",std::ios::in | std::ios::binary);
+ spawn(x,y);
+
+ if(in.good()){
+ std::istringstream data;
+ std::string ddata;
+ std::streampos len;
+
+ in.seekg(0,std::ios::end);
+ len = in.tellg();
+ in.seekg(0,std::ios::beg);
+
+ std::vector<char> buf (len,'\0');
+ in.read(buf.data(),buf.size());
+
+ data.rdbuf()->pubsetbuf(buf.data(),buf.size());
+
+ std::getline(data,ddata);
+ loc.x = std::stoi(ddata);
+ std::getline(data,ddata);
+ loc.y = std::stoi(ddata);
+ std::getline(data,ddata);
+ health = std::stoi(ddata);
+ std::getline(data,ddata);
+ maxHealth = std::stoi(ddata);
+
+ std::getline(data,ddata);
+ for(i = std::stoi(ddata);i;i--){
+ std::getline(data,ddata);
+ count = std::stoi(ddata);
+ std::getline(data,ddata);
+ inv->items.push_back((item_t){count,(uint)std::stoi(ddata)});
+ }
+
+ std::getline(data,ddata);
+ currentWorld = loadWorldFromXMLNoSave(ddata.c_str());
+
+ in.close();
+ }
+}
diff --git a/src/gameplay.cpp b/src/gameplay.cpp
index e252c26..4bbc672 100644
--- a/src/gameplay.cpp
+++ b/src/gameplay.cpp
@@ -278,7 +278,7 @@ void initEverything(void){
*/
for(unsigned int i=0;i<xmlFiles.size();i++){
- if(strncmp(xmlFiles[i].c_str(),".",1) && strncmp(xmlFiles[i].c_str(),"..",2)){
+ if(xmlFiles[i] != "." && xmlFiles[i] != ".." && strcmp(xmlFiles[i].c_str()+xmlFiles[i].size()-3,"dat")){
/*
* Read in the XML file.
@@ -307,9 +307,9 @@ void initEverything(void){
/*
* Spawn the player and begin the game.
*/
-
+
player = new Player();
- player->spawn(200,100);
+ player->sspawn(0,100);
currentWorld->bgmPlay(NULL);
atexit(destroyEverything);
diff --git a/src/ui.cpp b/src/ui.cpp
index 43706e7..6e616a7 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -58,7 +58,7 @@ extern Menu* currentMenu;
extern Menu pauseMenu;
-Mix_Chunk *dialogClick;
+static Mix_Chunk *dialogClick;
extern void mainLoop(void);
@@ -114,6 +114,7 @@ namespace ui {
bool debug=false;
bool posFlag=false;
bool dialogPassive = false;
+ int dialogPassiveTime = 0;
/*
@@ -545,6 +546,24 @@ namespace ui {
dialogImportant = true;
//toggleBlack();
}
+ void passiveImportantText(int duration, const char *text,...){
+ va_list textArgs;
+
+ //if(!player->ground)return;
+
+ memset(dialogBoxText,0,512);
+
+ va_start(textArgs,text);
+ vsnprintf(dialogBoxText,512,text,textArgs);
+ va_end(textArgs);
+
+ dialogBoxExists = true;
+ dialogImportant = true;
+ dialogPassive = true;
+ dialogPassiveTime = duration;
+ }
+
+
void draw(void){
unsigned char i;
float x,y,tmp;
@@ -556,6 +575,14 @@ namespace ui {
if(dialogImportant){
setFontColor(255,255,255);
+ if(dialogPassive){
+ dialogPassiveTime -= deltaTime;
+ if(dialogPassiveTime < 0){
+ dialogPassive = false;
+ dialogImportant = false;
+ dialogBoxExists = false;
+ }
+ }
if(fadeIntensity == 255 || dialogPassive){
setFontSize(24);
putStringCentered(offset.x,offset.y,rtext);
@@ -646,15 +673,14 @@ namespace ui {
}
void quitGame(){
+ dialogBoxExists = false;
+ currentMenu = NULL;
+ delete[] currentMenu;
gameRunning = false;
updateConfig();
saveConfig();
}
-
- void quitMenu(){
- currentMenu = NULL;
- }
-
+
menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f){
menuItem temp;
temp.member = 0;
@@ -719,20 +745,13 @@ namespace ui {
return temp;
}
- char* stradd(const char* a, const char* b){
- size_t len = strlen(a) + strlen(b);
- char *ret = (char*)malloc(len * sizeof(char) + 1);
- *ret = '\0';
-
- return strcat(strcat(ret,a),b);
- }
-
/*
* Draws the menu
*/
void drawMenu(Menu *menu){
setFontSize(24);
+ updateConfig();
SDL_Event e;
mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2);
@@ -1181,13 +1200,10 @@ DONE:
if(SDL_KEY == SDLK_ESCAPE){
//gameRunning = false;
currentMenu = &pauseMenu;
+ player->save();
return;
}
switch(SDL_KEY){
- case SDLK_w:
- dialogPassive = true;
- importantText("Welcome to Meme-Town");
- break;
case SDLK_a:
left = false;
break;
diff --git a/src/world.cpp b/src/world.cpp
index d039bca..6e72328 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -82,18 +82,18 @@ void World::setStyle(const char* pre){
for(uint i = 0; i < arrAmt(buildPaths);i++){
sTexLoc.push_back(prefix);
sTexLoc.back() += buildPaths[i];
- std::cout << sTexLoc.back() << std::endl;
+ //std::cout << sTexLoc.back() << std::endl;
}
prefix += "bg/";
for(uint i = 0; i < arrAmt(bgPaths[0]);i++){
bgFiles.push_back(prefix);
bgFiles.back() += bgPaths[0][i];
- std::cout << bgFiles.back() << std::endl;
+ //std::cout << bgFiles.back() << std::endl;
}
for(uint i = 0; i < arrAmt(bgPaths[1]);i++){
bgFilesIndoors.push_back(prefix);
bgFilesIndoors.back() += bgPaths[1][i];
- std::cout << bgFilesIndoors.back() << std::endl;
+ //std::cout << bgFilesIndoors.back() << std::endl;
}
}
@@ -316,10 +316,7 @@ void World::update(Player *p,unsigned int delta){
p->loc.y += p->vel.y * delta;
p->loc.x +=(p->vel.x * p->speed) * delta;
- /*if(p->inv->usingi){
- p->inv->useItem();
- }*/
-
+
/*
* Update coordinates of all entities except for structures.
*/
@@ -361,22 +358,22 @@ void World::update(Player *p,unsigned int delta){
}
}
-void World::setBGM(const char *path){
- if(!bgm) delete[] bgm;
- if(path != NULL){
- bgm = new char[strlen(path) + 1];
- strcpy(bgm,path);
- bgmObj = Mix_LoadMUS(bgm);
- }else{
- bgm = new char[1];
- bgm[0] = '\0';
- }
+void World::setBGM(std::string path){
+ bgm = new char[path.size()];
+ strcpy(bgm,path.c_str());
+ bgmObj = Mix_LoadMUS(bgm);
}
void World::bgmPlay(World *prev){
- if(prev && strcmp(bgm,prev->bgm)){
+ if(prev){
+ if(bgm != prev->bgm){
+ Mix_FadeOutMusic(800);
+ //Mix_VolumeMusic(50);
+ Mix_PlayMusic(bgmObj,-1); // Loop infinitely
+ }
+ }else{
Mix_FadeOutMusic(800);
- Mix_VolumeMusic(50);
+ //Mix_VolumeMusic(50);
Mix_PlayMusic(bgmObj,-1); // Loop infinitely
}
}
@@ -940,17 +937,13 @@ void World::singleDetect(Entity *e){
}
void World::detect(Player *p){
- //static std::thread villageThread;
- World *hey = this;
/*
* Handle the player.
*/
- singleDetect(p);
- //villageLogic(this);
- //villageThread = std::thread(villageLogic, this);
-
+ //auto pl = std::async(&World::singleDetect,this,p);
+ std::thread(&World::singleDetect,this, p).detach();
/*
* Handle all remaining entities in this world.
@@ -958,8 +951,9 @@ void World::detect(Player *p){
//LOOOOP:
static int what = 0;
- for(auto &e : hey->entity)
- hey->singleDetect(e);
+ for(auto &e : entity)
+ std::thread(&World::singleDetect,this,e).detach();
+ //hey->singleDetect(e);
for(auto &part : particles){
int l;
unsigned int i;
@@ -1004,12 +998,23 @@ void World::detect(Player *p){
}
}
+ for(auto &v : village){
+ 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;
+ }
+ }
+
/*if(hey->infront){
hey = hey->infront;
goto LOOOOP;
}*/
- //villageThread.join();
}
+
void World::addStructure(BUILD_SUB sub, float x,float y, char *tex, const char *inside){
build.push_back(new Structures());
build.back()->inWorld = this;
@@ -1494,23 +1499,27 @@ char *currentXML = NULL;
extern World *currentWorld;
World *loadWorldFromXML(const char *path){
+ if(currentXML){
+ currentWorld->save();
+ delete[] currentXML;
+ }
+
+ return loadWorldFromXMLNoSave(path);
+}
+
+World *loadWorldFromXMLNoSave(const char *path){
XMLDocument xml;
XMLElement *wxml;
XMLElement *vil;
World *tmp;
- float spawnx;
+ float spawnx, randx;
bool dialog,Indoor;
const char *ptr,*name;
unsigned int size = 5 + strlen(path);
- if(currentXML){
- currentWorld->save();
- delete[] currentXML;
- }
-
memset((currentXML = new char[size]),0,size);
strcpy(currentXML,"xml/");
strcat(currentXML,path);
@@ -1523,11 +1532,6 @@ World *loadWorldFromXML(const char *path){
wxml = wxml->FirstChildElement();
Indoor = false;
tmp = new World();
- if(vil){
- vil = vil->FirstChildElement();
- //tmp->village.push_back(vil->Attribute("name"));
-
- }
}else if((wxml = xml.FirstChildElement("IndoorWorld"))){
wxml = wxml->FirstChildElement();
Indoor = true;
@@ -1604,20 +1608,38 @@ World *loadWorldFromXML(const char *path){
wxml = wxml->NextSiblingElement();
}
+ if(vil){
+ tmp->village.push_back(vil->Attribute("name"));
+
+ vil = vil->FirstChildElement();
+ }
+
while(vil){
name = vil->Name();
+ randx = getRand() % tmp->getTheWidth() - (tmp->getTheWidth() / 2.0f);
+
+ /**
+ * READS DATA ABOUT STRUCTURE CONTAINED IN VILLAGE
+ */
if(!strcmp(name,"structure")){
ptr = vil->Attribute("inside");
tmp->addStructure((BUILD_SUB)vil->UnsignedAttribute("type"),
- vil->QueryFloatAttribute("x",&spawnx) != XML_NO_ERROR ?
- getRand() % tmp->getTheWidth() / 2.0f :
- spawnx,
+ vil->QueryFloatAttribute("x", &spawnx) != XML_NO_ERROR ?
+ randx : spawnx,
100,
(char*)vil->Attribute("texture"),
ptr);
- //tmp->village.back().build.push_back(tmp->build.back());
+ tmp->village.back().build.push_back(tmp->build.back());
+ }
+ if(tmp->village.back().build.back()->loc.x < tmp->village.back().start.x){
+ tmp->village.back().start.x = tmp->village.back().build.back()->loc.x;
}
+ if(tmp->village.back().build.back()->loc.x + tmp->village.back().build.back()->width > tmp->village.back().end.x){
+ tmp->village.back().end.x = tmp->village.back().build.back()->loc.x + tmp->village.back().build.back()->width;
+ }
+
+ //go to the next element in the village block
vil = vil->NextSiblingElement();
}