diff options
| -rw-r--r-- | src/entity.h | 7 | ||||
| -rw-r--r-- | src/game.c | 4 | ||||
| -rw-r--r-- | src/game.h | 1 | ||||
| -rw-r--r-- | src/ui.c | 72 | ||||
| -rw-r--r-- | src/ui.h | 11 |
5 files changed, 84 insertions, 11 deletions
diff --git a/src/entity.h b/src/entity.h index d0e9f9f..b7c458e 100644 --- a/src/entity.h +++ b/src/entity.h @@ -1,4 +1,5 @@ #include "utils.h" +#include "assets.h" // Pretty much any object in the game. @@ -71,6 +72,12 @@ struct Entity { void* data; }; +typedef struct { + EntityId id; + AssetId texture; + int count; +} EntityItem; + // Cubemap based building. typedef struct { Model model; @@ -95,6 +95,9 @@ void initGame(Game* game) initInteractionChat(&game->interactionChat, &game->settings); initInteractionMenu(&game->interactionMenu, &game->settings); + // Inventory. + initInventory(&game->inventory); + disableGameCursor(game); } @@ -324,6 +327,7 @@ void updateGameScene(Game* game) updateGameStatus(game); updateInteractionChat(&game->interactionChat, game); updateInteractionMenu(&game->interactionMenu, game); + updateInventory(&game->inventory); } void handleGameResize(Game* game) @@ -28,6 +28,7 @@ struct Game { InteractionChat interactionChat; InteractionMenu interactionMenu; + Inventory inventory; SceneId sceneId; bool isCursorEnabled; @@ -228,10 +228,19 @@ void updateInteractionMenu(InteractionMenu* menu, Game* game) } } +void resizeInventory(Inventory* inventory) +{ + inventory->rect.width = 300.0; + inventory->rect.height = 300.0; + inventory->rect.x = GetRenderWidth() / 2.0 - inventory->rect.width / 2.0; + inventory->rect.y = GetRenderHeight() / 2.0 - inventory->rect.height / 2.0; +} + void initInventory(Inventory* inventory) { - inventory->visable = false; - inventory->itemCount = 0; + resizeInventory(inventory); + inventory->visable = true; + inventory->itemCount = 40; } void showInventory(Inventory* inventory) @@ -244,10 +253,67 @@ void hideInventory(Inventory* inventory) inventory->visable = false; } -void addItemToInventory(Inventory* inventory, InventoryItem item) +void addItemToInventory(Inventory* inventory, EntityItem item) { + // If item is already in the inventory. + for (int index = 0; index < inventory->itemCount; ++index) + { + if (inventory->items[index].id == item.id) + { + inventory->items[index].count += item.count; + return; + } + } + + // Add item to inventory. + if (inventory->itemCount >= INVENTORY_MAX) + { + TraceLog(LOG_ERROR, "inventory->itemCount >= INVENTORY_MAX"); + } + else + { + inventory->items[inventory->itemCount] = item; + ++inventory->itemCount; + } } void updateInventory(Inventory* inventory) { + if (IsWindowResized()) + { + resizeInventory(inventory); + } + + if (!inventory->visable) + { + return; + } + + // Draw background. + int outlineThickness = 3; + DrawRectangleRec(inventory->rect, PINK); + DrawRectangleLinesEx(inventory->rect, outlineThickness, BLACK); + + // Draw items. + int startX = inventory->rect.x + outlineThickness; + int startY = inventory->rect.y + outlineThickness; + int maxX = inventory->rect.width + inventory->rect.x - INVENTORY_SPRITE_SIZE; + int maxY = inventory->rect.height + inventory->rect.y - + INVENTORY_SPRITE_SIZE; + int x = startX; + int y = startY; + + for (int index = 0; index < inventory->itemCount; ++index) + { + DrawRectangle(x, y, INVENTORY_SPRITE_SIZE, INVENTORY_SPRITE_SIZE, GRAY); + DrawText(TextFormat("%d\n", index), x, y, 16, BLACK); + + x += INVENTORY_SPRITE_SIZE; + + if (x >= maxX) + { + x = startX; + y += INVENTORY_SPRITE_SIZE; + } + } } @@ -8,6 +8,7 @@ #define INTERACTION_COLUMN_MAX 80 #define INVENTORY_MAX 10 +#define INVENTORY_SPRITE_SIZE 42 typedef char InteractionItems[INTERACTION_MENU_MAX][INTERACTION_LABEL_MAX]; @@ -31,15 +32,9 @@ typedef struct { } InteractionMenu; typedef struct { - EntityId id; - AssetId texture; - int count; -} InventoryItem; - -typedef struct { Rectangle rect; bool visable; - InventoryItem items[INVENTORY_MAX]; + EntityItem items[INVENTORY_MAX]; int itemCount; } Inventory; @@ -68,7 +63,7 @@ void updateInteractionMenu(InteractionMenu* menu, Game* game); void initInventory(Inventory* inventory); void showInventory(Inventory* inventory); void hideInventory(Inventory* inventory); -void addItemToInventory(Inventory* inventory, InventoryItem item); +void addItemToInventory(Inventory* inventory, EntityItem item); void updateInventory(Inventory* inventory); #endif |
