Skip to content

Commit

Permalink
Inventory system fin
Browse files Browse the repository at this point in the history
  • Loading branch information
arbeiter committed Aug 22, 2022
1 parent 1d5c6f8 commit ceec7d8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
2 changes: 2 additions & 0 deletions code/clonegame/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "vector"
#include "tuple"
using namespace std;
//TODO 1: Render inventory.vector using inventory.render in draw()

Game::Game()
:mWindow(nullptr)
Expand Down Expand Up @@ -225,6 +226,7 @@ void Game::generateOutput()
DrawActor();
displayFont();
inventory.draw();
inventory.drawDropLocations();
SDL_RenderPresent(mRenderer);
}

Expand Down
60 changes: 53 additions & 7 deletions code/clonegame/inventory.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#include "inventory.h"


// DONE: Once drag is on, start tracking the mouse to draw the texture
// Part I: Draw a transparent structure and make it track the mouse
// Render the grid to a texture
// Set blending on and set transparency to 40%, background to red
// (X, Y) co-ordinates are centered at the current mouse location

// Part II: Make the transparent structure overlap the destination grid position
// If the destination (X, Y) co-ordinates are on the map
// Validate the destination inside a region
// If the destination (X, Y) co-ordinates are within a crafting tray
// Snap item to fit
// Find the items in the stats tuple and decrement values there
// TODO Crafting: Part II: Make the transparent structure overlap the destination grid position
// If the destination (X, Y) co-ordinates are on the map
// Validate the destination inside a region
// If the destination (X, Y) co-ordinates are within a crafting tray
// Snap item to fit
// Find the items in the stats tuple and decrement values there

// TODO: Clear texture drawing stuff

Expand Down Expand Up @@ -140,19 +141,64 @@ void Inventory::itemDraggable() {
if(intersect != -1) {
mouseState.selectedIndex = intersect;
printf("Pressed and now selected Index set to 1 %d %d \n", mouseState.sourceX, mouseState.sourceY);
firstLoad = true;
}
}

if(mouseState.selectedIndex != -1 && mouseState.dragMode == false) {
// TODO: Remove firstLoad HACK!
if(mouseState.selectedIndex != -1 && mouseState.dragMode == false && firstLoad == true) {
int prevIndex = mouseState.selectedIndex;
printf("Mouse state selected index after release %d\n", mouseState.selectedIndex);
printf("Released and now selected Index set to 1 %f %f \n", mouseState.destinationX, mouseState.destinationY);
printf("Released and now selected Index set to -1 \n");

int num_items = itemStats.size();
int selected_item_count = itemStats[prevIndex].itemCount;
std::cout << "ITEM COUNT OF SELECTED_ITEM " << selected_item_count << std::endl;

if(selected_item_count > 0 && selected_item_count < num_items) {
string itemName = itemStats[prevIndex].itemName;
ItemDropLocations curr;
curr.drop_x = mouseState.destinationX;
curr.drop_y = mouseState.destinationY;
curr.item_key = mouseState.selectedIndex;
dropLocations.push_back(curr);
itemStats[prevIndex].itemCount -= 1;
}

// Update locations inventory
// Update the inventory
mouseState.selectedIndex = -1;
}
}

void Inventory::drawInventoryItem(int x, int y, int i) {
int start_pos_x = 0;
int start_pos_y = i * 32; // TODO: Get this from the texture and store it somewhere
int abs_w = tex_w;
int abs_h = 32;

SDL_Rect srcrect;
srcrect.x = start_pos_x;
srcrect.y = start_pos_y;
srcrect.w = abs_w;
srcrect.h = abs_h;

SDL_Rect destrect;
destrect.x = x;
destrect.y = y;
destrect.w = abs_w * 2;
destrect.h = abs_h * 2;
SDL_RenderCopy(mRenderer, bitmapTex, &srcrect, &destrect);
}

void Inventory::drawDropLocations() {
for(int i = 0; i < dropLocations.size(); i++) {
ItemDropLocations location = dropLocations[i];
drawInventoryItem(location.drop_x, location.drop_y, location.item_key);
}
}

void Inventory::renderTextureWithTransform(SDL_Rect &dstrect) {
SDL_SetRenderTarget(mRenderer, NULL);
//SDL_RenderClear(mRenderer);
Expand Down
8 changes: 6 additions & 2 deletions code/clonegame/inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,34 @@ struct ItemStats {
struct ItemDropLocations {
float drop_x;
float drop_y;
string item_key;
int item_key;
};

class Inventory {
public:
bool firstLoad;
Inventory() = default;
std::vector<ItemDropLocations> dropLocations;
Inventory(SDL_Renderer *renderer, TTF_Font *mFont)
: mRenderer{renderer}, font(mFont), width(3440), height(1440)
{
loadItemStats();
texManager = new TextureManager(renderer);
bitmapTex = texManager->LoadTexture("./res/final_collectables_transparent.png");
loadItemStats();
int wi, hi;
SDL_QueryTexture(bitmapTex, NULL, NULL, &wi, &hi);
tex_w = wi;
tex_h = hi;
highlighted_index = -1;
//mouseState = { -1, -1, -1, -1, false, -1 };
firstLoad = false;
}

void draw();
void refreshDisplay();
void loadItemStats();
void drawDropLocations();
void drawInventoryItem(int x, int y, int index);
void drawItem(SDL_Rect &srcrect, int width, int item_idx);
void drawItemInner(SDL_Rect &srcrect, int width, int item_idx, bool isTextured);
void renderTextureWithTransform(SDL_Rect &dstrect);
Expand Down

0 comments on commit ceec7d8

Please sign in to comment.