diff options
author | nathansmith117 <thenathansmithsmith@gmail.com> | 2024-03-13 18:07:43 +0000 |
---|---|---|
committer | nathansmith117 <thenathansmithsmith@gmail.com> | 2024-03-13 18:07:43 +0000 |
commit | 8372a776ad2e06b99e4704403504ba74932276ec (patch) | |
tree | 1c688217d8420474ad4c8b68c721d1a27d9df43d /src | |
parent | 694efe00e1e01af8534672b60970a3e0555e5f34 (diff) | |
download | PenguinYippies-8372a776ad2e06b99e4704403504ba74932276ec.tar.gz PenguinYippies-8372a776ad2e06b99e4704403504ba74932276ec.tar.bz2 PenguinYippies-8372a776ad2e06b99e4704403504ba74932276ec.zip |
Made a clicker
Diffstat (limited to 'src')
-rw-r--r-- | src/assets.c | 3 | ||||
-rw-r--r-- | src/assets.h | 5 | ||||
-rw-r--r-- | src/clicky.c | 97 | ||||
-rw-r--r-- | src/clicky.h | 11 | ||||
-rw-r--r-- | src/gameCommon.h | 11 | ||||
-rw-r--r-- | src/shop.c | 20 | ||||
-rw-r--r-- | src/shop.h | 4 |
7 files changed, 144 insertions, 7 deletions
diff --git a/src/assets.c b/src/assets.c index d4352cb..95f90ed 100644 --- a/src/assets.c +++ b/src/assets.c @@ -12,7 +12,8 @@ const char textureAssetsNames[TEXTURE_ASSET_COUNT][ASSETS_NAME_MAX] = { const char animationAssetsNames[ANIMATION_ASSET_COUNT][ASSETS_NAME_MAX] = { "buttonBox.gif", - "penguinLol.gif" + "penguinLol.gif", + "clicker.gif" }; void loadTextures(Assets* assets) diff --git a/src/assets.h b/src/assets.h index 943b409..bf6da36 100644 --- a/src/assets.h +++ b/src/assets.h @@ -4,7 +4,7 @@ #define ASSETS_NAME_MAX 100 #define TEXTURE_ASSET_COUNT 6 -#define ANIMATION_ASSET_COUNT 2 +#define ANIMATION_ASSET_COUNT 3 #ifndef ASSETS_H #define ASSETS_H @@ -26,7 +26,8 @@ enum enum { BUTTON_BOX_ANIMATION, - PENGUIN_LOL_ANIMATION + PENGUIN_LOL_ANIMATION, + CLICKER_ANIMATION }; typedef struct Assets { diff --git a/src/clicky.c b/src/clicky.c index 0e32fb0..f6787b8 100644 --- a/src/clicky.c +++ b/src/clicky.c @@ -2,6 +2,7 @@ #include "game.h" #include "assets.h" #include "util.h" +#include <raylib.h> void initClickies(Clickies* clickies) { @@ -37,6 +38,7 @@ void updateClickies(Game* game, Clickies* clickies) } } +// Penguin lol methods. void updatePenguinLol(Game* game, Clicky* clicky) { // Mouse clicky. @@ -69,7 +71,7 @@ void updatePenguinLol(Game* game, Clicky* clicky) } } - // Run animation and update. + // Run animation and draw. runAnimation(&clicky->animation); Texture texture = clicky->animation.texture; @@ -90,7 +92,7 @@ Clicky createPenguinLolClicky(Game* game) setAnimationFrame(&clicky.animation, clicky.animation.frameCount - 1); clicky.animation.repeat = false; clicky.texture = NULL; - clicky.rect = (Rectangle){0.0, 0.0, 512, 512}; + clicky.rect = (Rectangle){0.0, 0.0, 512.0, 512.0}; clicky.data = NULL; clicky.updateCB = updatePenguinLol; @@ -100,3 +102,94 @@ Clicky createPenguinLolClicky(Game* game) return clicky; } + +// Clicker methods. +void updateClicker(Game* game, Clicky* clicky) +{ + Clicker* clicker = (Clicker*)clicky->data; + + // Drag around. + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) + { + if (IsCursorOnScreen() && doesCollideWithAnimationData(clicky->rect, clicky->animation.asset->image.data, + clicky->animation.width, clicky->animation.height, clicky->animation.currentFrame, getScaledMousePosition())) + { + Vector2 mouseDelta = GetMouseDelta(); + clicky->rect.x += mouseDelta.x; + clicky->rect.y += mouseDelta.y; + } + } + + // Collides with a other clicky. + double currentTime = GetTime(); + + if (currentTime - clicker->timeLastClicked >= clicker->delay) + { + Clickies* clickies = &game->clickies; + + for (int i = 0; i < clickies->clickiesCount; ++i) + { + Clicky* testClicky = &clickies->clickies[i]; + + // Same clicky lmao. + if (testClicky == clicky) + { + continue; + } + + if (CheckCollisionRecs(clicky->rect, testClicky->rect)) + { + testClicky->wasClicked = true; + break; // Clicker only clicks one clicky + } + } + + clicker->timeLastClicked = currentTime; + } + + runAnimation(&clicky->animation); + + Texture texture = clicky->animation.texture; + DrawTexturePro(texture, (Rectangle){0.0, 0.0, texture.width, texture.height}, + clicky->rect, Vector2Zero(), 0.0, WHITE); +} + +void freeClicker(Clicky clicky) +{ + closeAnimation(&clicky.animation); + + if (clicky.data != NULL) + { + YP_FREE(clicky.data); + } +} + +Clicky createClickerClicky(Game* game) +{ + Clicky clicky; + + clicky.animation = createAnimation(&game->assets.animations[CLICKER_ANIMATION], ANIMATION_DEFAULT_DELAY); + playAnimation(&clicky.animation); + clicky.animation.repeat = true; + clicky.texture = NULL; + clicky.rect = (Rectangle){0.0, 0.0, 128.0, 128.0}; + + // Clicker structure thingy thing. + clicky.data = YP_MALLOC(sizeof(Clicker)); + + if (clicky.data == NULL) + { + ALLOCATION_ERROR; + } + + Clicker* clicker = (Clicker*)clicky.data; + clicker->timeLastClicked = 0.0; + clicker->delay = CLICKER_DEFAULT_DELAY; + + clicky.updateCB = updateClicker; + clicky.freeCB = freeClicker; + + clicky.wasClicked = false; + + return clicky; +} diff --git a/src/clicky.h b/src/clicky.h index cb96397..e38ecdf 100644 --- a/src/clicky.h +++ b/src/clicky.h @@ -3,6 +3,8 @@ #define MAX_CLICKIES 256 +#define CLICKER_DEFAULT_DELAY 0.5 + #ifndef CLICKY_H #define CLICKY_H @@ -24,6 +26,12 @@ typedef struct Clicky { bool wasClicked; } Clicky; +// ittle thingy used by the clicker +typedef struct Clicker { + double timeLastClicked; + double delay; +} Clicker; + typedef struct Clickies { Clicky clickies[MAX_CLICKIES]; size_t clickiesCount; @@ -37,5 +45,8 @@ void updateClickies(Game* game, Clickies* clickies); // A silly silly penguin lol. Clicky createPenguinLolClicky(Game* game); +// Create a clicker. +Clicky createClickerClicky(Game* game); + #endif diff --git a/src/gameCommon.h b/src/gameCommon.h index d0e8284..eb5ccf7 100644 --- a/src/gameCommon.h +++ b/src/gameCommon.h @@ -10,6 +10,17 @@ #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 + +// Memory management. +#define YP_MALLOC(size) malloc(size) +#define YP_CALLOC(nmemb, size) calloc(nmemb, size) +#define YP_REALLOC(ptr, size) realloc(ptr, size) +#define YP_REALLOCARRAY(ptr, nmemb, size) reallocarray(ptr, nmemb, size) +//#define YP_REALLOCARRAY(ptr, nmemb, size) realloc(ptr, nmemb * size) +#define YP_FREE(ptr) free(ptr) + +#define ALLOCATION_ERROR TraceLog(LOG_ERROR, "Allocation error in %s:%d", __FILE__, __LINE__) + #ifndef GAME_COMMON_H #define GAME_COMMON_H @@ -3,6 +3,7 @@ #include "assets.h" #include "util.h" #include "clicky.h" +#include <raylib.h> // Callbacks. void createPenguinLolCB(ShopEntry* entry, Game* game) @@ -19,6 +20,18 @@ void createPenguinLolCB(ShopEntry* entry, Game* game) addClickyToClickies(&game->clickies, lol); } +void createClicker(ShopEntry* entry, Game* game) +{ + SetRandomSeed(clock()); + + int randomX = GetRandomValue(200, WINDOW_WIDTH - 200); + int randomY = GetRandomValue(200, WINDOW_HEIGHT - 200); + + Clicky clicker = createClickerClicky(game); + + addClickyToClickies(&game->clickies, clicker); +} + void initShop(Shop* shop, Game* game) { Assets* assets = &game->assets; @@ -26,6 +39,9 @@ void initShop(Shop* shop, Game* game) // Entries. shop->penguinLol = LoadTextureFromImage(assets->animations[PENGUIN_LOL_ANIMATION].image); shop->entries[0] = (ShopEntry){&shop->penguinLol, 10, createPenguinLolCB}; + + shop->clicker = LoadTextureFromImage(assets->animations[CLICKER_ANIMATION].image); + shop->entries[1] = (ShopEntry){&shop->clicker, 20, createClicker}; } void buyThingFromShop(Shop* shop, int id, Game* game) @@ -72,7 +88,8 @@ void updateShop(Shop* shop, Game* game) double height = 100.0; Rectangle rects[SHOP_ENTRY_COUNT] = { - (Rectangle){startX, startY, width, height} + (Rectangle){startX, startY, width, height}, + (Rectangle){startX, startY + height, width, height} }; // Entries. @@ -111,4 +128,5 @@ void updateShop(Shop* shop, Game* game) void closeShop(Shop* shop) { UnloadTexture(shop->penguinLol); + UnloadTexture(shop->clicker); } @@ -1,8 +1,9 @@ #include "gameCommon.h" +#include <raylib.h> // The fullname is waaayyy toooo looonnnng for lazy lazy me -#define SHOP_ENTRY_COUNT 1 +#define SHOP_ENTRY_COUNT 2 #ifndef SHOP_H #define SHOP_H @@ -22,6 +23,7 @@ typedef struct Shop { // Some silly textures. Texture penguinLol; + Texture clicker; } Shop; void initShop(Shop* shop, Game* game); |