diff options
author | nathansmith117 <thenathansmithsmith@gmail.com> | 2024-02-22 19:19:43 +0000 |
---|---|---|
committer | nathansmith117 <thenathansmithsmith@gmail.com> | 2024-02-22 19:19:43 +0000 |
commit | d6c58243b7b9d90e5e23af2f3c866ddf316cb0fa (patch) | |
tree | 0ee4b8533b28eb3e3d4be034c0eb356d83b98734 /src | |
parent | 1ce1a705c61064e44f83c772b70ee9b208a4c289 (diff) | |
download | PenguinYippies-d6c58243b7b9d90e5e23af2f3c866ddf316cb0fa.tar.gz PenguinYippies-d6c58243b7b9d90e5e23af2f3c866ddf316cb0fa.tar.bz2 PenguinYippies-d6c58243b7b9d90e5e23af2f3c866ddf316cb0fa.zip |
Can now use pixel data for collision
Diffstat (limited to 'src')
-rw-r--r-- | src/clicky.c | 14 | ||||
-rw-r--r-- | src/clicky.h | 3 | ||||
-rw-r--r-- | src/gameScreen.c | 2 | ||||
-rw-r--r-- | src/util.c | 31 | ||||
-rw-r--r-- | src/util.h | 3 |
5 files changed, 30 insertions, 23 deletions
diff --git a/src/clicky.c b/src/clicky.c index 88b23ce..7790715 100644 --- a/src/clicky.c +++ b/src/clicky.c @@ -12,13 +12,8 @@ void updatePenguinLol(Game* game, Clicky* clicky) { if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { - Rectangle clickRect = clicky->rect; - clickRect.width /= 2.0; - clickRect.height /= 2.0; - clickRect.x += clickRect.width / 2.0; - clickRect.y += clickRect.height / 2.0; - - if (CheckCollisionPointRec(getScaledMousePosition(), clickRect)) + if (doesCollideWithAnimationData(clicky->rect, clicky->animation.asset->image.data, + clicky->animation.width, clicky->animation.height, clicky->animation.currentFrame, getScaledMousePosition())) { replayAnimation(&clicky->animation); } @@ -37,11 +32,13 @@ Clicky createPenguinLolClicky(Game* game) Clicky clicky; clicky.animation = createAnimation(&game->assets.animations[PENGUIN_LOL_ANIMATION], ANIMATION_DEFAULT_DELAY); - setAnimationFrame(&clicky.animation, clicky.animation.frameCount - 1); + setAnimationFrame(&clicky.animation, 0); clicky.animation.repeat = false; clicky.texture = NULL; clicky.rect = (Rectangle){0.0, 0.0, 512, 512}; + clicky.colors = LoadImageColors(game->assets.animations[PENGUIN_LOL_ANIMATION].image); + clicky.data = NULL; clicky.updateCB = updatePenguinLol; @@ -51,4 +48,5 @@ Clicky createPenguinLolClicky(Game* game) void freePenginLolClicky(Clicky clicky) { closeAnimation(&clicky.animation); + UnloadImageColors(clicky.colors); } diff --git a/src/clicky.h b/src/clicky.h index 764df00..12b3b12 100644 --- a/src/clicky.h +++ b/src/clicky.h @@ -13,6 +13,9 @@ typedef struct Clicky { Texture* texture; Rectangle rect; + // Used for color collision stuff. + Color* colors; + void* data; ClickyUpdateCB updateCB; } Clicky; diff --git a/src/gameScreen.c b/src/gameScreen.c index 179eabd..7331a22 100644 --- a/src/gameScreen.c +++ b/src/gameScreen.c @@ -101,6 +101,8 @@ void updateGameScreen(GameScreen* gameScreen, Game* game) // update clickies. updateClicky(game, &gameScreen->penguinLol); + + DrawFPS(0, 0); } void closeGameScreen(GameScreen* gameScreen) @@ -10,24 +10,29 @@ Vector2 getScaledMousePosition() return mousePosition; } -bool doesCollideWithAnimation(Rectangle rect, Animation* animation, Vector2 point) +bool doesCollideWithAnimationData(Rectangle rect, void* data, int width, int height, int frame, Vector2 point) { - float xScale = (float)rect.width / animation->width; - float yScale = (float)rect.height / animation->height; + // Doesn't collide with rect. + if (!CheckCollisionPointRec(point, rect)) + { + return false; + } - unsigned int frameOffset = animation->width * animation->height * 4 * animation->currentFrame; + float xScale = (float)width / rect.width; + float yScale = (float)height / rect.height; - // Check each pixal. - for (int row = 0; row < rect.height; ++row) - { - for (int col = 0; col < rect.width; ++col) - { - int scaledRow = row * yScale; - int scaledCol = col * xScale; + int row = (point.y - rect.y) * yScale; + int col = (point.x - rect.x) * xScale; - int pixalPosition = frameOffset + (animation->width * scaledRow + scaledCol); + unsigned int frameOffset = width * height * frame; - } + // Position of the apha byte in the color. + unsigned int position = (frameOffset + (width * row + col)) * 4 + 3; + + // Check apha at position. + if (*(((unsigned char*)data) + position) != 0x00) + { + return true; } return false; @@ -1,6 +1,5 @@ #include "gameCommon.h" #include "animation.h" -#include <raylib.h> #ifndef UTIL_H #define UTIL_H @@ -9,6 +8,6 @@ Vector2 getScaledMousePosition(); // Check pixals and that fun stuff. -bool doesCollideWithAnimation(Rectangle rect, Animation* animation, Vector2 point); +bool doesCollideWithAnimationData(Rectangle rect, void* data, int width, int height, int frame, Vector2 point); #endif |