aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/entities/oldMint.c23
-rw-r--r--src/entities/oldMint.h2
-rw-r--r--src/entities/stickyNickel.c24
-rw-r--r--src/entities/stickyNickel.h2
-rw-r--r--src/entity.c4
-rw-r--r--src/entity.h5
-rw-r--r--src/game.c4
-rw-r--r--src/game.h1
-rw-r--r--src/player.c2
-rw-r--r--src/settings.c5
-rw-r--r--src/settings.h7
-rw-r--r--src/ui.c98
-rw-r--r--src/ui.h10
-rw-r--r--src/world.c59
-rw-r--r--src/world.h4
15 files changed, 229 insertions, 21 deletions
diff --git a/src/entities/oldMint.c b/src/entities/oldMint.c
index 17c751e..be90ed6 100644
--- a/src/entities/oldMint.c
+++ b/src/entities/oldMint.c
@@ -10,3 +10,26 @@ void updateOldMint(Entity* entity, Game* game)
DrawBillboard(game->player.camera, game->assets.textures[MINT_TEXTURE],
entity->position, 1.0, WHITE);
}
+
+InteractionCommand interactWithOldMint(Entity* entity, Game* game,
+ Selection selection)
+{
+ InventoryItem item = (InventoryItem){
+ .id = OLD_MINT,
+ .textureId = MINT_TEXTURE,
+ .count = 1
+ };
+
+ switch (selection)
+ {
+ case SELECTION_INTERACT:
+ setInteractionChat(
+ &game->interactionChat,
+ "Oh goodie it's an old mint!\nThough I advice you don't taste it\n...(assuming you want to live)");
+ addItemToInventory(&game->inventory, item);
+ return INTERACTION_TALK;
+ default:
+ entity->id = ENTITY_NONE;
+ return INTERACTION_END;
+ }
+}
diff --git a/src/entities/oldMint.h b/src/entities/oldMint.h
index 433b4a6..fa4ce9f 100644
--- a/src/entities/oldMint.h
+++ b/src/entities/oldMint.h
@@ -9,5 +9,7 @@
void initOldMint(Entity* entity);
void updateOldMint(Entity* entity, Game* game);
+InteractionCommand interactWithOldMint(Entity* entity, Game* game,
+ Selection selection);
#endif
diff --git a/src/entities/stickyNickel.c b/src/entities/stickyNickel.c
index 94755c5..87d3687 100644
--- a/src/entities/stickyNickel.c
+++ b/src/entities/stickyNickel.c
@@ -1,4 +1,5 @@
#include "stickyNickel.h"
+#include "ui.h"
void initStickyNickel(Entity* entity)
{
@@ -11,3 +12,26 @@ void updateStickyNickel(Entity* entity, Game* game)
DrawBillboard(game->player.camera, game->assets.textures[NICKEL_TEXTURE],
entity->position, 1.0, WHITE);
}
+
+InteractionCommand interactWithStickyNickel(Entity* entity, Game* game,
+ Selection selection)
+{
+ InventoryItem item = (InventoryItem){
+ .id = STICKY_NICKEL,
+ .textureId = NICKEL_TEXTURE,
+ .count = 1
+ };
+
+ switch (selection)
+ {
+ case SELECTION_INTERACT:
+ setInteractionChat(
+ &game->interactionChat,
+ "Luck you, its a sticky nickel :D\nBetter wash your hands...");
+ addItemToInventory(&game->inventory, item);
+ return INTERACTION_TALK;
+ default:
+ entity->id = ENTITY_NONE;
+ return INTERACTION_END;
+ }
+}
diff --git a/src/entities/stickyNickel.h b/src/entities/stickyNickel.h
index b7c938e..f291825 100644
--- a/src/entities/stickyNickel.h
+++ b/src/entities/stickyNickel.h
@@ -9,5 +9,7 @@
void initStickyNickel(Entity* entity);
void updateStickyNickel(Entity* entity, Game* game);
+InteractionCommand interactWithStickyNickel(Entity* entity, Game* game,
+ Selection selection);
#endif
diff --git a/src/entity.c b/src/entity.c
index c295289..2565d3b 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -8,7 +8,7 @@ const EntityEntry entityEntries[ENTITY_COUNT] = {
.initCallback = initOldMint,
.updateCallback = updateOldMint,
.closeCallback = NULL,
- .interactionCallback = NULL,
+ .interactionCallback = interactWithOldMint,
.isPlace = false,
.isBuilding = false,
.canBeSelected = true
@@ -18,7 +18,7 @@ const EntityEntry entityEntries[ENTITY_COUNT] = {
.initCallback = initStickyNickel,
.updateCallback = updateStickyNickel,
.closeCallback = NULL,
- .interactionCallback = NULL,
+ .interactionCallback = interactWithStickyNickel,
.isPlace = false,
.isBuilding = false,
.canBeSelected = true
diff --git a/src/entity.h b/src/entity.h
index d0e9f9f..45036d4 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.
@@ -23,11 +24,11 @@ typedef enum Selection Selection;
typedef struct Entity Entity;
-typedef InteractionCommand (*InteractionCallback)(Entity* entity, Game* game,
- Selection selection);
typedef void (*InitEntityCallback)(Entity* entity);
typedef void (*UpdateEntityCallback)(Entity* entity, Game* game);
typedef void (*CloseEntityCallback)(Entity* entity);
+typedef InteractionCommand (*InteractionCallback)(Entity* entity, Game* game,
+ Selection selection);
enum {
ENTITY_NONE = -1,
diff --git a/src/game.c b/src/game.c
index fb7e01d..32a2153 100644
--- a/src/game.c
+++ b/src/game.c
@@ -95,6 +95,9 @@ void initGame(Game* game)
initInteractionChat(&game->interactionChat, &game->settings);
initInteractionMenu(&game->interactionMenu, &game->settings);
+ // Inventory.
+ initInventory(&game->inventory, &game->settings);
+
disableGameCursor(game);
}
@@ -324,6 +327,7 @@ void updateGameScene(Game* game)
updateGameStatus(game);
updateInteractionChat(&game->interactionChat, game);
updateInteractionMenu(&game->interactionMenu, game);
+ updateInventory(&game->inventory, game);
}
void handleGameResize(Game* game)
diff --git a/src/game.h b/src/game.h
index 971dde3..c59d5e9 100644
--- a/src/game.h
+++ b/src/game.h
@@ -28,6 +28,7 @@ struct Game {
InteractionChat interactionChat;
InteractionMenu interactionMenu;
+ Inventory inventory;
SceneId sceneId;
bool isCursorEnabled;
diff --git a/src/player.c b/src/player.c
index ed3fd10..3720762 100644
--- a/src/player.c
+++ b/src/player.c
@@ -433,7 +433,7 @@ void playerUpdateSelectedEntity(Player* player, WorldUID uid, Game* game)
int keyPressed = GetKeyPressed();
// Handle key presses.
- if (IsKeyPressed(game->settings.interactKey))
+ if (IsKeyPressed(game->settings.interactKey) && !player->isInteracting)
{
playerInteractWithEntity(player, uid, game, SELECTION_INTERACT);
}
diff --git a/src/settings.c b/src/settings.c
index ee126b4..14b9257 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -38,9 +38,9 @@ Settings defaultSettings()
.mapZoomSpeed = 0.2,
.entityInfoFontSize = 20,
.statusAndInfoAlpha = (unsigned char)255.0 * 0.8,
+ .uiAlpha = (unsigned char)255.0 * 0.9,
+ .uiOutlineSize = 2.0,
.interactionFontSize = 20,
- .interactionAlpha = (unsigned char)255.0 * 0.9,
- .interactionOutlineSize = 2.0,
.interactionChatHeight = 300.0,
.interactionMenuWidth = 500.0,
.interactionChatAnimationSpeed = 0.03,
@@ -54,6 +54,7 @@ Settings defaultSettings()
.toggleCrossHairKey = KEY_C,
.toggleFPSKey = KEY_F,
.toggleMapPreviewKey = KEY_P,
+ .toggleInventoryKey = KEY_TAB,
.defaultMapZoomKey = KEY_Z,
.interactKey = KEY_E,
.nextMessageKey = KEY_ENTER,
diff --git a/src/settings.h b/src/settings.h
index b64c631..5d1b9c1 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -59,10 +59,10 @@ typedef struct {
int entityInfoFontSize;
unsigned char statusAndInfoAlpha;
- // Interaction chat and menu.
+ // Interaction chat and menu, and also UI.
+ unsigned char uiAlpha;
+ float uiOutlineSize;
int interactionFontSize;
- unsigned char interactionAlpha;
- float interactionOutlineSize;
float interactionChatHeight;
float interactionMenuWidth;
float interactionChatAnimationSpeed;
@@ -78,6 +78,7 @@ typedef struct {
KeyboardKey toggleCrossHairKey;
KeyboardKey toggleFPSKey;
KeyboardKey toggleMapPreviewKey;
+ KeyboardKey toggleInventoryKey;
KeyboardKey defaultMapZoomKey;
KeyboardKey interactKey;
KeyboardKey nextMessageKey;
diff --git a/src/ui.c b/src/ui.c
index 66560bb..089b9d2 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -83,10 +83,10 @@ void updateInteractionChat(InteractionChat* chat, Game* game)
}
Color background = DARKGRAY;
- background.a = game->settings.interactionAlpha;
+ background.a = game->settings.uiAlpha;
DrawRectangleRec(chat->rect, background);
- float lineThickness = game->settings.interactionOutlineSize;
+ float lineThickness = game->settings.uiOutlineSize;
float border = lineThickness + 1.0;
int fontSize = game->settings.interactionFontSize;
@@ -134,7 +134,7 @@ void resizeInteractionMenu(InteractionMenu* menu, const Settings* settings)
settings->interactionFontSize;
menu->rect.x = 0.0;
menu->rect.y = GetRenderHeight() - settings->interactionChatHeight -
- menu->rect.height - settings->interactionOutlineSize;
+ menu->rect.height - settings->uiOutlineSize;
}
void initInteractionMenu(InteractionMenu* menu, const Settings* settings)
@@ -198,10 +198,10 @@ void updateInteractionMenu(InteractionMenu* menu, Game* game)
// Draw background.
Color background = DARKGRAY;
- background.a = game->settings.interactionAlpha;
+ background.a = game->settings.uiAlpha;
DrawRectangleRec(menu->rect, background);
- float lineThickness = game->settings.interactionOutlineSize;
+ float lineThickness = game->settings.uiOutlineSize;
float border = lineThickness + 1.0;
int fontSize = game->settings.interactionFontSize;
@@ -228,8 +228,18 @@ void updateInteractionMenu(InteractionMenu* menu, Game* game)
}
}
-void initInventory(Inventory* inventory)
+void resizeInventory(Inventory* inventory, const Settings* settings)
{
+ float outlineThickness = settings->uiOutlineSize * 2.0;
+ float spriteSize = INVENTORY_SPRITE_SIZE * INVENTORY_SCALE;
+ inventory->rect.width = INVENTORY_COLUMNS * spriteSize + outlineThickness;
+ inventory->rect.height = INVENTORY_ROWS * spriteSize + outlineThickness;
+ 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, const Settings* settings)
+{
+ resizeInventory(inventory, settings);
inventory->visable = false;
inventory->itemCount = 0;
}
@@ -246,8 +256,82 @@ void hideInventory(Inventory* inventory)
void addItemToInventory(Inventory* inventory, InventoryItem 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)
+void updateInventory(Inventory* inventory, Game* game)
{
+ if (IsWindowResized())
+ {
+ resizeInventory(inventory, &game->settings);
+ }
+
+ // Handle toggle key.
+ if (IsKeyPressed(game->settings.toggleInventoryKey))
+ {
+ inventory->visable = !inventory->visable;
+ }
+
+ if (!inventory->visable)
+ {
+ return;
+ }
+
+ // Draw background.
+ float outlineThickness = game->settings.uiOutlineSize;
+ Color backgroundColor = PINK;
+ backgroundColor.a = game->settings.uiAlpha;
+ DrawRectangleRec(inventory->rect, backgroundColor);
+ DrawRectangleLinesEx(inventory->rect, outlineThickness, BLACK);
+
+ // Draw items.
+ float spriteSize = INVENTORY_SPRITE_SIZE * INVENTORY_SCALE;
+ int startX = inventory->rect.x + outlineThickness;
+ int startY = inventory->rect.y + outlineThickness;
+ int maxX = inventory->rect.width + inventory->rect.x - spriteSize;
+ int maxY = inventory->rect.height + inventory->rect.y - spriteSize;
+ int x = startX;
+ int y = startY;
+ int fontSize = spriteSize / 3.0;
+
+ for (int index = 0; index < inventory->itemCount; ++index)
+ {
+ InventoryItem* item = &inventory->items[index];
+
+ DrawTextureEx(game->assets.textures[item->textureId], (Vector2){x, y}, 0.0,
+ INVENTORY_SCALE, WHITE);
+
+ // Draw count.
+ if (item->count > 1)
+ {
+ DrawText(TextFormat("%d", item->count), x, y, fontSize, BLACK);
+ }
+
+ x += spriteSize;
+
+ if (x >= maxX)
+ {
+ x = startX;
+ y += spriteSize;
+ }
+ }
}
diff --git a/src/ui.h b/src/ui.h
index b5089a3..14845d4 100644
--- a/src/ui.h
+++ b/src/ui.h
@@ -8,6 +8,10 @@
#define INTERACTION_COLUMN_MAX 80
#define INVENTORY_MAX 10
+#define INVENTORY_SPRITE_SIZE 32
+#define INVENTORY_ROWS 8
+#define INVENTORY_COLUMNS 8
+#define INVENTORY_SCALE 2.0
typedef char InteractionItems[INTERACTION_MENU_MAX][INTERACTION_LABEL_MAX];
@@ -32,7 +36,7 @@ typedef struct {
typedef struct {
EntityId id;
- AssetId texture;
+ AssetId textureId;
int count;
} InventoryItem;
@@ -65,10 +69,10 @@ void hideInteractionMenu(InteractionMenu* menu);
void updateInteractionMenu(InteractionMenu* menu, Game* game);
// Inventory
-void initInventory(Inventory* inventory);
+void initInventory(Inventory* inventory, const Settings* settings);
void showInventory(Inventory* inventory);
void hideInventory(Inventory* inventory);
void addItemToInventory(Inventory* inventory, InventoryItem item);
-void updateInventory(Inventory* inventory);
+void updateInventory(Inventory* inventory, Game* game);
#endif
diff --git a/src/world.c b/src/world.c
index 0096dbe..b53fb57 100644
--- a/src/world.c
+++ b/src/world.c
@@ -1065,7 +1065,7 @@ void castRayBVH(const World* world, BVHNode node, Ray ray, bool allowAll,
{
for (int index = 0; index < BVH_MAX; ++index)
{
- if (node.entities[index] != -1 &&
+ if (node.entities[index] != ENTITY_NONE &&
GetRayCollisionBox(
ray,
world->entities[node.entities[index]].box).hit)
@@ -1080,6 +1080,10 @@ void castRayBVH(const World* world, BVHNode node, Ray ray, bool allowAll,
{
continue;
}
+ else if (world->entities[node.entities[index]].id == ENTITY_NONE)
+ {
+ continue;
+ }
if (distance < *closestDistance)
{
@@ -1108,4 +1112,57 @@ WorldUID castRayAtWorld(const World* world, Ray ray, bool allowAll,
return uid;
}
+// This turned out to not be needed lol.
+WorldUID checkBoxCollisionBVH(const World* world, BVHNode node,
+ BoundingBox box, bool allowAll)
+{
+ if (!CheckCollisionBoxes(box, node.box))
+ {
+ return ENTITY_NONE;
+ }
+
+ // Leaf node.
+ if (node.branchCount == 0)
+ {
+ for (int index = 0; index < BVH_MAX; ++index)
+ {
+ if (node.entities[index] != ENTITY_NONE &&
+ CheckCollisionBoxes(
+ box,
+ world->entities[node.entities[index]].box))
+ {
+ // Don't include things that can't be selected.
+ if (!allowAll &&
+ !entityCanBeSelected(world->entities[node.entities[index]].id))
+ {
+ continue;
+ }
+
+ return node.entities[index];
+ }
+ }
+ }
+ else // Subtree.
+ {
+ for (int index = 0; index < node.branchCount; ++index)
+ {
+ WorldUID uid = checkBoxCollisionBVH(world, *node.branches[index], box,
+ allowAll);
+
+ if (uid != ENTITY_NONE)
+ {
+ return uid;
+ }
+ }
+ }
+
+ return ENTITY_NONE;
+}
+
+WorldUID checkBoxCollisionWithWorld(const World* world, BoundingBox box,
+ bool allowAll)
+{
+ return checkBoxCollisionBVH(world, world->bvh, box, allowAll);
+}
+
// Abortions are good. Get more abortions.
diff --git a/src/world.h b/src/world.h
index 2b51485..d714b29 100644
--- a/src/world.h
+++ b/src/world.h
@@ -91,7 +91,11 @@ void freeWorld(World world);
// When dealing with the world in 2d use y in place of z.
float getWorldHeightAtLocation(const World* world, float x, float y);
Vector2 worldPositionToPixel(const World* world, float x, float y);
+
+// "allowAll" allows non-selectable entities.
WorldUID castRayAtWorld(const World* world, Ray ray, bool allowAll,
int* tests);
+WorldUID checkBoxCollisionWithWorld(const World* world, BoundingBox box,
+ bool allowAll);
#endif