aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authordrumsetmonkey <abelleisle@roadrunner.com>2016-01-07 08:33:54 -0500
committerdrumsetmonkey <abelleisle@roadrunner.com>2016-01-07 08:33:54 -0500
commite043a2432c4dacce56a308948188482fb230ff33 (patch)
treeb8809e54bc3c516dabfa8eace3b51a92d5c8fbcd /src
parentc7e3d72f0ef08cb9463cd8960bc29dad40e3bdcb (diff)
parent45edad31559852d306d59b50f380cb79c9f27dcc (diff)
Hey, that's pretty good lighting!
Diffstat (limited to 'src')
-rw-r--r--src/common.cpp17
-rw-r--r--src/entities.cpp136
-rw-r--r--src/gameplay.cpp94
-rw-r--r--src/ui.cpp37
-rw-r--r--src/world.cpp86
5 files changed, 295 insertions, 75 deletions
diff --git a/src/common.cpp b/src/common.cpp
index 7449a35..dbcef0b 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -1,4 +1,5 @@
#include <common.h>
+#include <cstring>
#include <cstdio>
#include <chrono>
@@ -11,6 +12,22 @@ unsigned int millis(void){
#endif // __WIN32__
+Condition::Condition(const char *_id,void *val){
+ id = new char[strlen(_id)+1];
+ strcpy(id,_id);
+ value = val;
+}
+Condition::~Condition(){
+ delete[] id;
+}
+
+bool Condition::sameID(const char *s){
+ return !strcmp(id,s);
+}
+void *Condition::getValue(void){
+ return value;
+}
+
void DEBUG_prints(const char* file, int line, const char *s,...){
va_list args;
printf("%s:%d: ",file,line);
diff --git a/src/entities.cpp b/src/entities.cpp
index d12a1ca..7e8d55a 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -1,9 +1,10 @@
#include <entities.h>
#include <ui.h>
-#include <unistd.h>
+#include <istream>
+//#include <unistd.h>
-extern FILE* names;
+extern std::istream *names;
extern unsigned int loops;
extern World *currentWorld;
@@ -12,39 +13,30 @@ extern Player *player;
extern const char *itemName;
-extern
-
void getRandomName(Entity *e){
- int tempNum,max=0;
+ unsigned int tempNum,max=0;
char *bufs;
- rewind(names);
+ names->seekg(0,names->beg);
- bufs = new char[16]; //(char *)malloc(16);
+ bufs = new char[32];
- for(;!feof(names);max++){
- fgets(bufs,16,(FILE*)names);
- }
+ for(;!names->eof();max++)
+ names->getline(bufs,32);
tempNum = rand() % max;
- rewind(names);
+ names->seekg(0,names->beg);
- for(int i=0;i<tempNum;i++){
- fgets(bufs,16,(FILE*)names);
- }
+ for(unsigned int i=0;i<tempNum;i++)
+ names->getline(bufs,32);
- switch(fgetc(names)){
+ switch(bufs[0]){
+ default :
case 'm': e->gender = MALE; break;
case 'f': e->gender = FEMALE;break;
- default : break;
}
- if((fgets(bufs,16,(FILE*)names)) != NULL){
- bufs[strlen(bufs)] = '\0';
- strcpy(e->name,bufs);
- if(e->name[strlen(e->name)-1] == '\n')
- e->name[strlen(e->name)-1] = '\0';
- }
+ strcpy(e->name,bufs+1);
delete[] bufs;
}
@@ -73,7 +65,7 @@ void Entity::spawn(float x, float y){ //spawns the entity you pass to it based o
}
}
- name = new char[16];
+ name = new char[32];
getRandomName(this);
}
@@ -109,7 +101,7 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation
tex = new Texturec(1,"assets/NPC.png");
inv = new Inventory(NPC_INV_SIZE);
- randDialog = rand() % 12 - 1;
+ randDialog = 6;//rand() % 12 - 1;
}
NPC::~NPC(){
while(!aiFunc.empty()){
@@ -154,15 +146,22 @@ Mob::Mob(int sub){
width = HLINE * 8;
height = HLINE * 8;
tex = new Texturec(1, "assets/robin.png");
+ break;
case MS_TRIGGER:
width = HLINE * 20;
height = 2000;
tex = new Texturec(0);
+ break;
case MS_DOOR:
width = HLINE * 12;
height = HLINE * 20;
tex = new Texturec(1,"assets/door.png");
break;
+ case MS_PAGE:
+ width = HLINE * 6;
+ height = HLINE * 4;
+ tex = new Texturec(1,"assets/items/ITEM_PAGE.png");
+ break;
}
inv = new Inventory(NPC_INV_SIZE);
@@ -256,6 +255,7 @@ void Entity::draw(void){ //draws the entities
break;
case MS_BIRD:
case MS_DOOR:
+ case MS_PAGE:
default:
glActiveTexture(GL_TEXTURE0 + 0);
tex->bind(0);
@@ -363,7 +363,7 @@ const char *randomDialog[] = {
"How much wood could a woodchuck chuck if a woodchuck could chuck wood?",
"I don\'t think anyone has ever been able to climb up that hill.",
"If you ever see a hole in the ground, watch out; it could mean the end for you.",
- "Did you know this game has over 4000 lines of code? I didn\'t. I didn't even know I was in a game until now...",
+ "Did you know this game has over 5000 lines of code? I didn\'t. I didn't even know I was in a game until now...",
"HELP MY CAPS LOCK IS STUCK",
"You know, if anyone ever asked me who I wanted to be when I grow up, I would say Abby Ross.",
"I want to have the wallpaper in our house changed. It doesn\'t really fit the environment.",
@@ -493,8 +493,92 @@ void Mob::wander(int timeRun){
}
break;
- case MS_DOOR:
+ case MS_PAGE:
+ if(player->loc.x > loc.x - 100 &&
+ player->loc.x < loc.x + 100 &&
+ ui::mouse.x > loc.x &&
+ ui::mouse.x < loc.x + width &&
+ ui::mouse.y > loc.y - width / 2 &&
+ ui::mouse.y < loc.y + width * 1.5 &&
+ SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)){
+ if(speed != 666){
+ speed = 666;
+ hey(this);
+ speed = 0;
+ }
+ }
+ break;
default:
break;
}
}
+
+char *Entity::baseSave(void){
+ static EntitySavePacket *esp = new EntitySavePacket();
+ memcpy(&esp->isp,inv->save(),sizeof(InventorySavePacket));
+ esp->loc = loc;
+ esp->vel = vel;
+ esp->width = width;
+ esp->height = height;
+ esp->speed = speed;
+ esp->health = health;
+ esp->maxHealth = maxHealth;
+ esp->subtype = subtype;
+ esp->ticksToUse = ticksToUse;
+ esp->randDialog = randDialog;
+ esp->ground = ground;
+ esp->near = near;
+ esp->canMove = canMove;
+ esp->right = right;
+ esp->left = left;
+ esp->alive = alive;
+ esp->hit = hit;
+ esp->type = type;
+ esp->gender = gender;
+ esp->nameSize = strlen(name) + 1;
+ return (char *)esp;
+}
+
+void Entity::baseLoad(char *e){
+ const char *tmpname = "GG\0";
+ EntitySavePacket *esp = (EntitySavePacket *)e;
+ inv->load(&esp->isp);
+ loc = esp->loc;
+ vel = esp->vel;
+ width = esp->width;
+ height = esp->height;
+ speed = esp->speed;
+ health = esp->health;
+ maxHealth = esp->maxHealth;
+ subtype = esp->subtype;
+ ticksToUse = esp->ticksToUse;
+ randDialog = esp->randDialog;
+ ground = esp->ground;
+ near = esp->near;
+ canMove = esp->canMove;
+ right = esp->right;
+ left = esp->left;
+ alive = esp->alive;
+ hit = esp->hit;
+ type = esp->type;
+ gender = esp->gender;
+ name = new char[esp->nameSize+1];
+ strcpy(name,tmpname);
+}
+
+char *NPC::save(unsigned int *size){
+ static char *buf = new char[(*size = sizeof(EntitySavePacket) + aiFunc.size() * sizeof(int(*)(NPC *)))],*esp;
+ memcpy(buf,(esp = baseSave()),sizeof(EntitySavePacket));
+ delete[] esp;
+ memcpy(buf+sizeof(EntitySavePacket),aiFunc.data(),aiFunc.size() * sizeof(int(*)(NPC *)));
+ return buf;
+}
+
+void NPC::load(char *b){
+ unsigned int size = *(unsigned int *)b,size2,i;
+ baseLoad(b + sizeof(unsigned int));
+ size2 = (size - sizeof(unsigned int) - sizeof(EntitySavePacket)) / sizeof(int(*)(NPC *));
+ for(i=0;i<size2;i++){
+ //aiFunc.push_back
+ }
+}
diff --git a/src/gameplay.cpp b/src/gameplay.cpp
index 1c83bd8..431d2cf 100644
--- a/src/gameplay.cpp
+++ b/src/gameplay.cpp
@@ -28,6 +28,10 @@ void story(Mob *callee){
callee->alive = false;
}
+/*
+ * Gens
+ */
+
float gen_worldSpawnHill1(float x){
return (float)(pow(2,(-x+200)/5) + GEN_MIN);
}
@@ -42,7 +46,6 @@ float gen_worldSpawnHill3(float x){
*/
void worldSpawnHill1_hillBlock(Mob *callee){
- std::cout<<"oi";
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...");
@@ -51,30 +54,46 @@ void worldSpawnHill1_hillBlock(Mob *callee){
static Arena *a;
void worldSpawnHill2_infoSprint(Mob *callee){
- 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();
+
+ 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!");
}
-void worldSpawnHill3_itemGet(Mob *callee){
- ui::dialogBox("B-) ",NULL,true,"Right click to pick up items!");
- callee->alive = false;
-}
-
-void worldSpawnHill3_itemSee(Mob *callee){
- ui::dialogBox("B-) ",NULL,true,"Press \'e\' to open your inventory!");
- callee->alive = false;
+int worldSpawnHill2_Quest2(NPC *callee){
+ ui::dialogBox(callee->name,NULL,false,"Yo.");
+ ui::waitForDialog();
+ return 0;
}
-void worldSpawnHill3_leave(Mob *callee){
- ui::dialogBox("B-) ",NULL,true,"Now jump in this hole, and let your journey begin :)");
- callee->alive = false;
+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;
}
/*
@@ -96,39 +115,48 @@ static World *worldSpawnHill3;
static IndoorWorld *worldSpawnHill2_Building1;
+static World *worldFirstVillage;
/*
* initEverything() start
*/
void destroyEverything(void);
void initEverything(void){
-
+ static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary);
+
worldSpawnHill1 = new World();
- worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1);
worldSpawnHill1->setBackground(BG_FOREST);
- worldSpawnHill1->setBGM("assets/music/embark.wav");
+ // 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);
worldSpawnHill2 = new World();
worldSpawnHill2->generate(700);
worldSpawnHill2->setBackground(BG_FOREST);
worldSpawnHill2->setBGM("assets/music/ozone.wav");
- worldSpawnHill2->addMob(MS_TRIGGER,-400,0,worldSpawnHill2_infoSprint);
+ 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");
- worldSpawnHill3->addMob(MS_TRIGGER,-500,0,worldSpawnHill3_itemGet);
- worldSpawnHill3->addMob(MS_TRIGGER,0,0,worldSpawnHill3_itemSee);
- worldSpawnHill3->addObject(FLASHLIGHT,false,"",-200,300);
- worldSpawnHill3->addMob(MS_TRIGGER,650,0,worldSpawnHill3_leave);
- worldSpawnHill3->addHole(800,1000);
+
+ worldFirstVillage = new World();
+ worldFirstVillage->generate(1000);
+ worldFirstVillage->setBackground(BG_FOREST);
+ worldFirstVillage->setBGM("assets/music/embark.wav");
worldSpawnHill1->toRight = worldSpawnHill2;
worldSpawnHill2->toLeft = worldSpawnHill1;
worldSpawnHill2->toRight = worldSpawnHill3;
worldSpawnHill3->toLeft = worldSpawnHill2;
+ worldSpawnHill3->toRight = worldFirstVillage;
+ worldFirstVillage->toLeft = worldSpawnHill3;
/*
* Spawn some entities.
@@ -150,6 +178,9 @@ void initEverything(void){
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);
//worldSpawnHill2->addStructure(STRUCTURET,HOUSE,(rand()%120*HLINE),100,worldSpawnHill1,worldSpawnHill2);
player = new Player();
@@ -164,6 +195,11 @@ extern std::vector<int (*)(NPC *)> AIpreload;
extern std::vector<NPC *> AIpreaddr;
void destroyEverything(void){
+ static std::ofstream o;
+ o.open("world.dat",std::ifstream::binary);
+ worldSpawnHill1->save(&o);
+ o.close();
+
while(!AIpreload.empty())
AIpreload.pop_back();
while(!AIpreaddr.empty())
diff --git a/src/ui.cpp b/src/ui.cpp
index b9a44aa..7979138 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -95,6 +95,8 @@ namespace ui {
bool dialogImportant = false;
unsigned char dialogOptChosen = 0;
+ unsigned int textWrapLimit = 110;
+
/*
* Current font size. Changing this WILL NOT change the font size, see setFontSize() for
* actual font size changing.
@@ -266,7 +268,13 @@ namespace ui {
*/
do{
- if(s[i]=='\n'){ // Handle newlines
+ if(i && ((i / 110.0) == (i / 110))){
+ yo-=fontSize*1.05;
+ xo=x;
+ if(s[i] == ' ')
+ i++;
+ }
+ if(s[i] == '\n'){
yo-=fontSize*1.05;
xo=x;
}else if(s[i]==' '){ // Handle spaces
@@ -300,19 +308,21 @@ namespace ui {
}
}while(s[++i]);
- return putString(x-width/2,y,s);
+ putString(x-width/2,y,s);
+ return width;
}
/*
* Draw a string in a typewriter-esque fashion. Each letter is rendered as calls are made
* to this function. Passing a different string to the function will reset the counters.
*/
-
+
+ static char *ret = NULL;
char *typeOut(char *str){
static unsigned int sinc, // Acts as a delayer for the space between each character.
linc=0, // Contains the number of letters that should be drawn.
size=0; // Contains the full size of the current string.
- static char *ret = NULL;
+ //static char *ret = NULL;
/*
* Create a well-sized buffer if we haven't yet.
@@ -452,6 +462,8 @@ namespace ui {
dialogBoxExists = true;
dialogImportant = false;
+ if(ret)
+ ret[0] = '\0';
}
void waitForDialog(void){
do{
@@ -487,7 +499,7 @@ namespace ui {
}
void draw(void){
unsigned char i;
- float x,y;
+ float x,y,tmp;
char *rtext;
if(dialogBoxExists){
@@ -524,12 +536,12 @@ namespace ui {
for(i=0;i<dialogOptCount;i++){
setFontColor(255,255,255);
- dialogOptLoc[i][1]=y-SCREEN_HEIGHT/4+(fontSize+HLINE)*(i+1);
- dialogOptLoc[i][2]=
- putStringCentered(offset.x,dialogOptLoc[i][1],dialogOptText[i]);
- dialogOptLoc[i][0]=offset.x-dialogOptLoc[i][2]/2;
+ tmp = putStringCentered(offset.x,dialogOptLoc[i][1],dialogOptText[i]);
+ dialogOptLoc[i][2] = offset.x + tmp;
+ dialogOptLoc[i][0] = offset.x - tmp;
+ dialogOptLoc[i][1] = y - SCREEN_HEIGHT / 4 + (fontSize + HLINE) * (i + 1);
if(mouse.x > dialogOptLoc[i][0] &&
- mouse.x < dialogOptLoc[i][0] + dialogOptLoc[i][2] &&
+ mouse.x < dialogOptLoc[i][2] &&
mouse.y > dialogOptLoc[i][1] &&
mouse.y < dialogOptLoc[i][1] + 16 ){ // fontSize
setFontColor(255,255,0);
@@ -666,7 +678,6 @@ DONE:
memcpy(&player->loc,&tmppos,sizeof(vec2));
currentWorld = tmp;
toggleBlackFast();
- dialogBoxExists = false;
}
}
break;
@@ -688,7 +699,6 @@ DONE:
memcpy(&player->loc,&tmppos,sizeof(vec2));
currentWorld = tmp;
toggleBlackFast();
- dialogBoxExists = false;
}
}
break;
@@ -804,6 +814,9 @@ DONE:
case SDLK_RIGHT:
player->inv->sel++;
break;
+ case SDLK_f:
+
+ break;
default:
break;
}
diff --git a/src/world.cpp b/src/world.cpp
index b539a87..c6ab6e1 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -52,6 +52,7 @@ float worldGetYBase(World *w){
}
void World::setBackground(WORLD_BG_TYPE bgt){
+ bgType = bgt;
switch(bgt){
case BG_FOREST:
bgTex = new Texturec(7,bgPaths[0]);
@@ -62,12 +63,69 @@ void World::setBackground(WORLD_BG_TYPE bgt){
}
}
-void World::save(FILE *s){
- fclose(s);
+void World::save(std::ofstream *o){
+ static unsigned int size2;
+ unsigned int size,i;
+ size_t bgms = strlen(bgm) + 1;
+ char *bufptr;
+
+ o->write((char *)&lineCount, sizeof(unsigned int));
+ o->write((char *)line ,lineCount * sizeof(struct line_t));
+ o->write("GG" ,2 * sizeof(char));
+ o->write((char *)star ,100 * sizeof(vec2));
+ o->write((char *)&bgType , sizeof(WORLD_BG_TYPE));
+ o->write((char *)&bgms , sizeof(size_t));
+ o->write(bgm ,strlen(bgm)+1);
+ o->write("NO" ,2 * sizeof(char));
+
+ /*std::vector<NPC *> npc;
+ std::vector<Structures *> build;
+ std::vector<Mob *> mob;
+ std::vector<Entity *> entity;
+ std::vector<Object *> object;*/
+
+ size = npc.size();
+ for(i=0;i<size;i++){
+ bufptr = npc[i]->save(&size2);
+ o->write((char *)&size2,sizeof(unsigned int));
+ o->write(bufptr,size2);
+ }
}
-void World::load(FILE *s){
- fclose(s);
+void World::load(std::ifstream *i){
+ //unsigned int size;
+ size_t bgms;
+ char sig[2];
+
+ i->read((char *)&lineCount,sizeof(unsigned int));
+
+ line = new struct line_t[lineCount];
+ i->read((char *)line,lineCount * sizeof(struct line_t));
+
+ i->read(sig,2 * sizeof(char));
+ if(strncmp(sig,"GG",2)){
+ std::cout<<"world.dat corrupt"<<std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ x_start = 0 - getWidth(this) / 2;
+
+ i->read((char *)star,100 * sizeof(vec2));
+ i->read((char *)&bgType,sizeof(WORLD_BG_TYPE));
+
+ i->read((char *)&bgms,sizeof(size_t));
+ bgm = new char[bgms];
+ i->read(bgm,bgms);
+ setBGM(bgm);
+
+
+ i->read(sig,2 * sizeof(char));
+ if(strncmp(sig,"NO",2)){
+ std::cout<<"world.dat corrupt"<<std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+
}
World::World(void){
@@ -942,16 +1000,20 @@ void World::addStructure(_TYPE t,BUILD_SUB sub, float x,float y,World *inside){
entity.push_back(build.back());
}
-void World::addVillage(int bCount, int npcMin, int npcMax,_TYPE t,float x,float y,World *outside){
+void World::addVillage(int bCount, int npcMin, int npcMax,_TYPE t,World *inside){
std::cout << npcMin << ", " << npcMax << std::endl;
- int xwasd;
+ //int xwasd;
for(int i = 0; i < bCount; i++){
- xwasd = (rand()%(int)x+1000*HLINE);
+ addStructure(t,HOUSE,x_start + (i * 300),100,inside);
+ /*std::cout<<"1\n";
HERE:
+ xwasd = (rand()%(int)x+1000*HLINE);
for(auto &bu : build){
if(xwasd > bu->loc.x && xwasd < bu->loc.x+bu->width)goto HERE;
}
- addStructure(t,HOUSE,xwasd,y,outside);
+ std::cout<<"2\n";
+ addStructure(t,HOUSE,xwasd,y,inside);
+ std::cout<<"3\n";*/
}
}
void World::addMob(int t,float x,float y){
@@ -1015,6 +1077,14 @@ void World::addLayer(unsigned int width){
behind->bgTex=bgTex;
}
+NPC *World::getAvailableNPC(void){
+ for(auto &n : npc){
+ if(n->aiFunc.empty())
+ return n;
+ }
+ return (NPC *)NULL;
+}
+
World *World::goWorldLeft(Player *p){
if(toLeft&&p->loc.x<x_start+HLINE*15){
p->loc.x=toLeft->x_start+getWidth(toLeft)-HLINE*10;