diff options
-rw-r--r-- | include/inventory.h | 3 | ||||
-rw-r--r-- | main.cpp | 19 | ||||
-rw-r--r-- | src/inventory.cpp | 64 |
3 files changed, 60 insertions, 26 deletions
diff --git a/include/inventory.h b/include/inventory.h index 5f05f3a..31b7d88 100644 --- a/include/inventory.h +++ b/include/inventory.h @@ -73,7 +73,7 @@ public: bool invHover = false; bool selected = false; bool mouseSel = false; - + bool usingi = false; Inventory(unsigned int s); // Creates an inventory of size 's' ~Inventory(void); // Free's allocated memory @@ -81,6 +81,7 @@ public: int addItem(ITEM_ID id,unsigned char count); // Add 'count' items with an id of 'id' to the inventory int takeItem(ITEM_ID id,unsigned char count); // Take 'count' items with an id of 'id' from the inventory int useItem(void); + bool detectCollision(vec2,vec2); void setSelection(unsigned int s); @@ -230,10 +230,10 @@ int main(/*int argc, char *argv[]*/){ // Run SDL_Quit when main returns atexit(SDL_Quit); - /**` + /*! * (Attempt to) Initialize SDL_image libraries with IMG_INIT_PNG so that we can load PNG * textures for the entities and stuff. - **/ + */ if(!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)){ std::cout << "Could not init image libraries! Error: " << IMG_GetError() << std::endl; @@ -243,10 +243,10 @@ int main(/*int argc, char *argv[]*/){ // Run IMG_Quit when main returns atexit(IMG_Quit); - /** + /*! * (Attempt to) Initialize SDL_mixer libraries for loading and playing music/sound files. * - **/ + */ if(Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0){ std::cout << "SDL_mixer could not initialize! Error: " << Mix_GetError() << std::endl; @@ -703,6 +703,11 @@ void render(){ glUseProgramObjectARB(0); } player->inv->draw(); + + if(player->inv->usingi && player->inv->detectCollision(vec2{currentWorld->npc[0]->loc.x, currentWorld->npc[0]->loc.y},vec2{currentWorld->npc[0]->loc.x+currentWorld->npc[0]->width,currentWorld->npc[0]->loc.y+currentWorld->npc[0]->height})){ + currentWorld->npc[0]->alive = false; + } + /* * Here we draw a black overlay if it's been requested. @@ -819,7 +824,10 @@ void logic(){ * click detection is done as well for NPC/player interaction. * */ - if((SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) && !ui::dialogBoxExists)player->inv->useItem(); + if((SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) && !ui::dialogBoxExists)player->inv->usingi = true; + if(player->inv->usingi){ + player->inv->useItem(); + } for(auto &n : currentWorld->npc){ if(n->alive){ @@ -831,7 +839,6 @@ void logic(){ */ if(n->canMove) n->wander((rand() % 120 + 30)); - /* * Don't bother handling the NPC if another has already been handled. */ diff --git a/src/inventory.cpp b/src/inventory.cpp index 8bf8d42..31c414f 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -6,6 +6,9 @@ extern Player *player; extern GLuint invUI; +static float hangle = 0.0f; +static bool up = true; +static float xc,yc; static const Item item[ITEM_COUNT]= { #include "../config/items.h" @@ -260,27 +263,30 @@ void Inventory::draw(void){ } void itemDraw(Player *p,ITEM_ID id,ITEM_TYPE type){ - static float angle = 0.0f; glPushMatrix(); if(!id)return; switch(type){ case SWORD: - angle = 15.0f; + if(hangle < 15){ + hangle = 15.0f; + p->inv->usingi = false; + up = false; + } break; default: - angle = 0.0f; + hangle = 0.0f; } - //glTranslatef(player->loc.x*2,player->loc.y*2,0); - glTranslatef(0-player->loc.x*2,0,0); - glRotatef(angle, 0.0f, 1.0f, 0.0f); + glTranslatef(player->loc.x,player->loc.y+player->height/3,0); + glRotatef(hangle, 0.0f, 0.0f, 1.0f); + glTranslatef(-player->loc.x,-player->loc.y-player->height/3,0); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,itemtex[id]); glColor4ub(255,255,255,255); glBegin(GL_QUADS); - glTexCoord2i(0,1);glVertex2f(p->loc.x, p->loc.y); - glTexCoord2i(1,1);glVertex2f(p->loc.x+item[id].width,p->loc.y); - glTexCoord2i(1,0);glVertex2f(p->loc.x+item[id].width,p->loc.y+item[id].height); - glTexCoord2i(0,0);glVertex2f(p->loc.x, p->loc.y+item[id].height); + glTexCoord2i(0,1);glVertex2f(p->loc.x, p->loc.y+p->height/3); + glTexCoord2i(1,1);glVertex2f(p->loc.x+item[id].width,p->loc.y+p->height/3); + glTexCoord2i(1,0);glVertex2f(p->loc.x+item[id].width,p->loc.y+p->height/3+item[id].height); + glTexCoord2i(0,0);glVertex2f(p->loc.x, p->loc.y+p->height/3+item[id].height); glEnd(); glDisable(GL_TEXTURE_2D); glTranslatef(player->loc.x*2,0,0); @@ -288,24 +294,44 @@ void itemDraw(Player *p,ITEM_ID id,ITEM_TYPE type){ } int Inventory::useItem(void){ - ITEM_ID id = item[inv[sel].id].id; ITEM_TYPE type = item[inv[sel].id].type; if(!invHover){ switch(type){ case SWORD: - - break; - default:break; - } - switch(id){ - case FLASHLIGHT: - player->light ^= true; + if(hangle==15)up=true; + if(up)hangle+=15; + if(hangle>=90)hangle=14; break; default: - //ui::dialogBox(item[id].name,NULL,"You cannot use this item."); break; } } return 0; } +bool Inventory::detectCollision(vec2 one, vec2 two){ + float i = 0.0f; + if(item[inv[sel].id].type == SWORD){ + while(i<item[inv[sel].id].height){ + xc = player->loc.x; yc = player->loc.y+player->height/3; + xc += float(i) * cos((hangle+90)*PI/180); + yc += float(i) * sin((hangle+90)*PI/180); + + /*glColor4f(1.0f,1.0f,1.0f,1.0f); + glBegin(GL_LINES); + glVertex2f(player->loc.x,player->loc.y+player->height/3); + glVertex2f(xc,yc); + glEnd();*/ + + if(xc >= one.x && xc <= two.x){ + if(yc >= one.y && yc <= two.y){ + return true; + } + } + + i+=HLINE; + } + } + return false; +} + |