From 0c89c034ffe5fdf79e57c8bdbe9ca07eaf79b382 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 7 Jan 2016 09:28:43 -0500 Subject: xml --- src/gameplay.cpp | 198 ++++++++++--------------------------------------------- 1 file changed, 35 insertions(+), 163 deletions(-) (limited to 'src/gameplay.cpp') diff --git a/src/gameplay.cpp b/src/gameplay.cpp index e8436e7..52ce729 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -3,98 +3,12 @@ #include #include -extern World *currentWorld; -extern Player *player; - -/* - * int (npc*) - * - * dialog - * wait... - * - * switch optchosen - * - * qh.assign - * addAIFunc? - * - * return 1 = repeat - */ - -void story(Mob *callee){ - player->vel.x = 0; - Mix_FadeOutMusic(0); - ui::importantText("It was a dark and stormy night..."); - ui::waitForDialog(); - callee->alive = false; -} - -/* - * Gens - */ - -float gen_worldSpawnHill1(float x){ - return (float)(pow(2,(-x+200)/5) + GEN_MIN); -} - -float gen_worldSpawnHill3(float x){ - float tmp = 60*atan(-(x/30-20))+GEN_MIN*2; - return tmp>GEN_MIN?tmp:GEN_MIN; -} - -/* - * Thing-thangs - */ - -void worldSpawnHill1_hillBlock(Mob *callee){ - player->vel.x = 0; - player->loc.x = callee->loc.x + callee->width; - ui::dialogBox(player->name,NULL,false,"This hill seems to steep to climb up..."); - callee->alive = true; -} - -static Arena *a; -void worldSpawnHill2_infoSprint(Mob *callee){ - - ui::dialogBox(player->name,":Sure:Nah",false,"This page would like to take you somewhere."); - ui::waitForDialog(); - switch(ui::dialogOptChosen){ - case 1: - ui::dialogBox(player->name,NULL,true,"Cool."); - callee->alive = false; - a = new Arena(currentWorld,player); - a->setBackground(BG_FOREST); - a->setBGM("assets/music/embark.wav"); - ui::toggleWhiteFast(); - ui::waitForCover(); - currentWorld = a; - ui::toggleWhiteFast(); - break; - case 2: - default: - ui::dialogBox(player->name,NULL,false,"Okay then."); - break; - } - - //ui::dialogBox("B-) ",NULL,true,"Press \'Shift\' to run!"); -} +#include -int worldSpawnHill2_Quest2(NPC *callee){ - ui::dialogBox(callee->name,NULL,false,"Yo."); - ui::waitForDialog(); - return 0; -} +using namespace tinyxml2; -int worldSpawnHill2_Quest1(NPC *callee){ - ui::dialogBox(callee->name,":Cool.",false,"Did you know that I\'m the coolest NPC in the world?"); - ui::waitForDialog(); - if(ui::dialogOptChosen == 1){ - ui::dialogBox(callee->name,NULL,false,"Yeah, it is."); - currentWorld->getAvailableNPC()->addAIFunc(worldSpawnHill2_Quest2,true); - ui::waitForDialog(); - return 0; - } - return 1; -} +extern World *currentWorld; +extern Player *player; /* * new world @@ -109,85 +23,47 @@ int worldSpawnHill2_Quest1(NPC *callee){ * World definitions */ -static World *worldSpawnHill1; -static World *worldSpawnHill2; -static World *worldSpawnHill3; - -static IndoorWorld *worldSpawnHill2_Building1; - -static World *worldFirstVillage; /* * initEverything() start */ +std::vector earth; + void destroyEverything(void); void initEverything(void){ - //static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary); - - worldSpawnHill1 = new World(); - worldSpawnHill1->setBackground(BG_FOREST); - /*if(!i.fail()){ - worldSpawnHill1->load(&i); - i.close(); - }else{*/ - worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1); - worldSpawnHill1->setBGM("assets/music/embark.wav"); - //} - worldSpawnHill1->addMob(MS_TRIGGER,0,0,worldSpawnHill1_hillBlock); - worldSpawnHill1->addNPC(300,100); - - worldSpawnHill2 = new World(); - worldSpawnHill2->setBackground(BG_FOREST); - worldSpawnHill2->setBGM("assets/music/ozone.wav"); - worldSpawnHill2->generate(700); - worldSpawnHill2->addMob(MS_PAGE,-400,0,worldSpawnHill2_infoSprint); - - worldSpawnHill3 = new World(); - worldSpawnHill3->generateFunc(1000,gen_worldSpawnHill3); - worldSpawnHill3->setBackground(BG_FOREST); - worldSpawnHill3->setBGM("assets/music/ozone.wav"); - - worldFirstVillage = new World(); - worldFirstVillage->setBackground(BG_FOREST); - worldFirstVillage->setBGM("assets/music/embark.wav"); - worldFirstVillage->generate(1000); - - worldSpawnHill1->toRight = worldSpawnHill2; - worldSpawnHill2->toLeft = worldSpawnHill1; - worldSpawnHill2->toRight = worldSpawnHill3; - worldSpawnHill3->toLeft = worldSpawnHill2; - worldSpawnHill3->toRight = worldFirstVillage; - worldFirstVillage->toLeft = worldSpawnHill3; - - /* - * Spawn some entities. - */ - - //playerSpawnHill->addMob(MS_TRIGGER,player->loc.x,0,story); - - //playerSpawnHill->addStructure(STRUCTURET,FOUNTAIN,(rand()%120*HLINE)+100*HLINE,100,test,iw); - //playerSpawnHill->addStructure(STRUCTURET,HOUSE2,(rand()%120*HLINE)+300*HLINE,100,test,iw); - - //playerSpawnHill->addVillage(5,1,4,STRUCTURET,rand()%500+120,(float)200,playerSpawnHill,iw); - //playerSpawnHill->addMob(MS_TRIGGER,-1300,0,CUTSCENEEE);*/ - - - worldSpawnHill2_Building1 = new IndoorWorld(); - worldSpawnHill2_Building1->generate(300); - worldSpawnHill2_Building1->setBackground(BG_WOODHOUSE); - worldSpawnHill2_Building1->setBGM("assets/music/theme_jazz.wav"); - - worldSpawnHill2->addStructure(STRUCTURET,HOUSE,(rand()%120*HLINE),100,worldSpawnHill2_Building1); - worldSpawnHill2->addLight({300,100},{1.0f,1.0f,1.0f}); - worldSpawnHill2->getAvailableNPC()->addAIFunc(worldSpawnHill2_Quest1,false); - - worldFirstVillage->addVillage(5,0,0,STRUCTURET,worldSpawnHill2_Building1); + const char *gentype; + std::vector xmlFiles; + char *file; + XMLDocument xml; + if(getdir("./xml/",xmlFiles)){ + std::cout<<"Error reading XML files!!!1"<setBackground((WORLD_BG_TYPE)atoi(xml.FirstChildElement("World")->FirstChildElement("Background")->GetText())); + earth.back()->setBGM(xml.FirstChildElement("World")->FirstChildElement("BGM")->GetText()); + gentype = xml.FirstChildElement("World")->FirstChildElement("GenerationType")->GetText(); + if(!strcmp(gentype,"Random")){ + std::cout<<"rand\n"; + earth.back()->generate(atoi(xml.FirstChildElement("World")->FirstChildElement("GenerationWidth")->GetText())); + }else{ + abort(); + } + } + } - //worldSpawnHill2->addStructure(STRUCTURET,HOUSE,(rand()%120*HLINE),100,worldSpawnHill1,worldSpawnHill2); player = new Player(); player->spawn(200,100); - currentWorld = worldSpawnHill1; + currentWorld = earth.front(); currentWorld->bgmPlay(NULL); atexit(destroyEverything); } @@ -196,10 +72,6 @@ extern std::vector AIpreload; extern std::vector AIpreaddr; void destroyEverything(void){ - /*static std::ofstream o; - o.open("world.dat",std::ifstream::binary); - worldSpawnHill2->save(&o); - o.close();*/ while(!AIpreload.empty()) AIpreload.pop_back(); -- cgit v1.2.3 From cde980d06325f37d70c1354d1d3b5cc1f3736938 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 8 Jan 2016 08:47:04 -0500 Subject: xml improvements --- Changelog | 16 +++++-- include/entities.h | 1 - src/gameplay.cpp | 111 ++++++++++++++++++++++++++++++++++++++++++----- src/ui.cpp | 5 ++- xml/playerSpawnHill1.xml | 23 ++++++++-- 5 files changed, 133 insertions(+), 23 deletions(-) (limited to 'src/gameplay.cpp') diff --git a/Changelog b/Changelog index d48e16d..e8a016a 100644 --- a/Changelog +++ b/Changelog @@ -470,28 +470,36 @@ - worked on wrapping text for dialog boxes - did more work on GLSL shaders -1/3/2015: +1/3/2016: ========= - finished wrapping text for dialog boxes - began working on world saving/loading again - got some mad GLSL shaders running -1/4/2015: +1/4/2016: ========= - fixed basic world save/load, working on entity saving - GLSL shaders worked? -1/5/2015: +1/5/2016: ========= - can save NPCs and Structures - more shadering -1/6/2015: +1/6/2016: ========= - flashlight! - XML-scripted worlds!!! - !!!!!!!!!!!!!!!!!!!!!!!! + +1/7/2016: +========= + + - xml'd npc and mob spawning + - xml'd npc dialogs + - drafted page xml + diff --git a/include/entities.h b/include/entities.h index 0fcc662..f26863e 100644 --- a/include/entities.h +++ b/include/entities.h @@ -223,7 +223,6 @@ public: void (*hey)(Mob *callee); Mob(int); - Mob(int,unsigned int); ~Mob(); void wander(int); diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 52ce729..6637d23 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -27,36 +27,123 @@ extern Player *player; * initEverything() start */ +typedef struct { + World *ptr; + char *file; +} WorldXML; + +typedef struct { + NPC *npc; + unsigned int index; +} NPCDialog; + +std::vector npcd; +std::vector earthxml; std::vector earth; +int commonAIFunc(NPC *speaker){ + XMLDocument xml; + XMLElement *exml; + unsigned int idx; + + for(auto &n : npcd){ + if(n.npc == speaker){ + idx = n.index; + break; + } + } + + for(auto &e : earthxml){ + if(e.ptr == currentWorld){ + xml.LoadFile(e.file); + exml = xml.FirstChildElement("Dialog")->FirstChildElement(); + + do{ + if(!strcmp(exml->Name(),"text")){ + if(!strcmp(exml->Attribute("name"),speaker->name) && exml->UnsignedAttribute("id") == idx){ + ui::dialogBox(speaker->name,"",false,exml->GetText()); + ui::waitForDialog(); + if(exml->QueryUnsignedAttribute("nextid",&idx) == XML_NO_ERROR){ + for(auto &n : npcd){ + if(n.npc == speaker){ + n.index = idx; + break; + } + } + return 1; + } + return 0; + } + } + exml = exml->NextSiblingElement(); + }while(exml); + } + } + return 0; +} + void destroyEverything(void); void initEverything(void){ - const char *gentype; + const char *name; std::vector xmlFiles; - char *file; + static char *file; + bool dialog; XMLDocument xml; + XMLElement *wxml; + if(getdir("./xml/",xmlFiles)){ std::cout<<"Error reading XML files!!!1"<FirstChildElement(); earth.push_back(new World()); - earth.back()->setBackground((WORLD_BG_TYPE)atoi(xml.FirstChildElement("World")->FirstChildElement("Background")->GetText())); - earth.back()->setBGM(xml.FirstChildElement("World")->FirstChildElement("BGM")->GetText()); - gentype = xml.FirstChildElement("World")->FirstChildElement("GenerationType")->GetText(); - if(!strcmp(gentype,"Random")){ - std::cout<<"rand\n"; - earth.back()->generate(atoi(xml.FirstChildElement("World")->FirstChildElement("GenerationWidth")->GetText())); - }else{ - abort(); - } + + do{ + name = wxml->Name(); + + if(!strcmp(name,"style")){ + earth.back()->setBackground((WORLD_BG_TYPE)wxml->UnsignedAttribute("background")); + earth.back()->setBGM(wxml->Attribute("bgm")); + }else if(!strcmp(name,"generation")){ + if(!strcmp(wxml->Attribute("type"),"Random")){ + earth.back()->generate(wxml->UnsignedAttribute("width")); + } + }else if(!strcmp(name,"mob")){ + earth.back()->addMob(wxml->UnsignedAttribute("type"),wxml->FloatAttribute("x"),wxml->FloatAttribute("y")); + }else if(!strcmp(name,"npc")){ + earth.back()->addNPC(wxml->FloatAttribute("x"),wxml->FloatAttribute("y")); + if(wxml->Attribute("name")){ + delete[] earth.back()->npc.back()->name; + earth.back()->npc.back()->name = new char[strlen(wxml->Attribute("name"))+1]; + strcpy(earth.back()->npc.back()->name,wxml->Attribute("name")); + } + dialog = false; + if(wxml->QueryBoolAttribute("hasDialog",&dialog) == XML_NO_ERROR && dialog){ + for(auto &ex : earthxml){ + if(ex.ptr == earth.back()) + goto SKIP; + } + earthxml.push_back((WorldXML){earth.back(),new char[64]}); + strcpy(earthxml.back().file,file); +SKIP: + earth.back()->npc.back()->addAIFunc(commonAIFunc,false); + npcd.push_back((NPCDialog){earth.back()->npc.back(),0}); + } + } + + wxml = wxml->NextSiblingElement(); + }while(wxml); + + delete[] file; } } diff --git a/src/ui.cpp b/src/ui.cpp index cf5e31e..ead9d8c 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -274,10 +274,11 @@ namespace ui { if(s[i] == ' ') i++; } - if(s[i] == '\n'){ + if(s[i] == '\n' || s[i] == '\r' || s[i] == '\t'){ + /*if(s[i] == '\n'){ yo-=fontSize*1.05; xo=x; - }else if(s[i]==' '){ // Handle spaces + */}else if(s[i]==' '){ // Handle spaces xo+=fontSize/2; }else if(s[i]=='\b'){ // Handle backspaces? xo-=add.x; diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index 3544a7e..c924d7e 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -1,7 +1,22 @@ - 0 - assets/music/embark.wav - Random - 500 +