aboutsummaryrefslogtreecommitdiffstats
path: root/src/clicky.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/clicky.c')
-rw-r--r--src/clicky.c97
1 files changed, 95 insertions, 2 deletions
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;
+}