From 32cb1880f018fc149d1c8a71a83426a8f5a92a6a Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 1 Mar 2016 08:33:27 -0500 Subject: world rewrite --- src/inventory.cpp | 4 +++- src/ui.cpp | 44 +++++++++++++++++++++++++------------------- src/world.cpp | 40 +++++++++++++++++++++++++--------------- 3 files changed, 53 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/inventory.cpp b/src/inventory.cpp index b32b56f..203e707 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -407,6 +407,8 @@ int Inventory::useItem(void){ } bool Inventory::detectCollision(vec2 one, vec2 two){ + (void)one; + (void)two; //float i = 0.0f; /*if(items.empty() || !items[sel].count) @@ -433,6 +435,6 @@ bool Inventory::detectCollision(vec2 one, vec2 two){ i+=HLINE; } }*/ - return !(one.x == two.y); + return false; } diff --git a/src/ui.cpp b/src/ui.cpp index 6e616a7..47110cc 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1039,42 +1039,48 @@ DONE: vec2 oldpos,tmppos; SDL_Event e; - mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2); - mouse.y=(offset.y+SCREEN_HEIGHT/2)-premouse.y; + // update mouse coords + mouse.x = premouse.x + offset.x - ( SCREEN_WIDTH / 2 ); + mouse.y = ( offset.y + SCREEN_HEIGHT / 2 ) - premouse.y; while(SDL_PollEvent(&e)){ switch(e.type){ + + // escape - quit game case SDL_QUIT: gameRunning=false; break; + + // mouse movement - update mouse vector case SDL_MOUSEMOTION: premouse.x=e.motion.x; premouse.y=e.motion.y; break; + + // mouse clicks case SDL_MOUSEBUTTONDOWN: - if((e.button.button & SDL_BUTTON_RIGHT) && dialogBoxExists) + // right click advances dialog + if ( ( e.button.button & SDL_BUTTON_RIGHT ) && dialogBoxExists ) dialogAdvance(); - if((e.button.button & SDL_BUTTON_LEFT) && !dialogBoxExists) + // left click uses item + if ( ( e.button.button & SDL_BUTTON_LEFT ) && !dialogBoxExists ) player->inv->usingi = true; break; - /* - KEYDOWN - */ + + // key presses case SDL_KEYDOWN: - /*if(SDL_KEY == SDLK_ESCAPE){ - //gameRunning = false; - pMenu = true; - return; - }else */if(SDL_KEY == SDLK_SPACE){ - /*if(dialogBoxExists) - dialogAdvance(); - else */if(player->ground){ - player->vel.y=.4; - player->loc.y+=HLINE*2; - player->ground=false; + + // space - make player jump + if ( SDL_KEY == SDLK_SPACE ) { + if ( player->ground ) { + player->loc.y += HLINE * 2; + player->vel.y = .4; + player->ground = false; } break; - }else if(!dialogBoxExists || dialogPassive){ + + // only let other keys be handled if dialog allows it + } else if ( !dialogBoxExists || dialogPassive ) { tmp = currentWorld; switch(SDL_KEY){ case SDLK_a: diff --git a/src/world.cpp b/src/world.cpp index 76dccb8..4fa71d8 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -252,6 +252,7 @@ generate( unsigned int width ) for(wditer = worldData.begin(); wditer != worldData.end(); wditer++){ if ((*wditer).groundHeight) + // wditer + GROUND_HILLINESS can go out of bounds (invalid read) geninc = ( (*(wditer + GROUND_HILLINESS)).groundHeight - (*wditer).groundHeight ) / (float)GROUND_HILLINESS; else (*wditer).groundHeight = (*(wditer - 1)).groundHeight + geninc; @@ -260,7 +261,7 @@ generate( unsigned int width ) (*wditer).grassUnpressed = true; (*wditer).grassHeight[0] = (randGet() % 16) / 3 + 2; (*wditer).grassHeight[1] = (randGet() % 16) / 3 + 2; - + // bound checks if ( (*wditer).groundHeight < GROUND_HEIGHT_MINIMUM ) (*wditer).groundHeight = GROUND_HEIGHT_MINIMUM; @@ -269,6 +270,7 @@ generate( unsigned int width ) if( (*wditer).groundHeight <= 0 ) (*wditer).groundHeight = GROUND_HEIGHT_MINIMUM; + } // define x-coordinate of world's leftmost 'line' @@ -674,11 +676,17 @@ draw( Player *p ) p->draw(); } -/* - * TODO +/** + * Handles physics and such for a single entity. + * + * This function is kept private, as World::detect() should be used instead to + * handle stuffs for all entities at once. */ -void World::singleDetect(Entity *e){ +void World:: +singleDetect( Entity *e ) +{ + std::string killed; unsigned int i,j; int l; @@ -686,12 +694,12 @@ void World::singleDetect(Entity *e){ * Kill any dead entities. */ - if(!e->alive||e->health<=0){ - for(i=0;itype){ + if ( !e->alive || e->health <= 0 ) { + for ( i = 0; i < entity.size(); i++) { + if ( entity[i] == e ){ + switch ( e->type ) { case STRUCTURET: - std::cout<<"Killed a building..."<inside = new char[1]),"\0"); - //strcpy((build.back()->outside = new char[1 + strlen((char *)(currentXML+4))]),(char *)(currentXML+4)); - entity.push_back(build.back()); } @@ -1010,6 +1017,7 @@ World *World::goWorldRight(Player *p){ } std::vector inside; + World *World:: goInsideStructure( Player *p ) { @@ -1035,11 +1043,13 @@ goInsideStructure( Player *p ) tmp = loadWorldFromXML(inside.back().c_str()); for(auto &b : tmp->build){ if(!strcmp(current,b->inside)){ - p->loc.x = b->loc.x + (b->width / 2); inside.pop_back(); ui::toggleBlackFast(); ui::waitForCover(); + + p->loc.x = b->loc.x + (b->width / 2); + ui::toggleBlackFast(); return tmp; -- cgit v1.2.3