From cd6afe32f0ee578d6fcc9d9f56f6f9b6d03796c4 Mon Sep 17 00:00:00 2001 From: huaqo-games Date: Thu, 6 Nov 2025 14:27:27 +0100 Subject: [PATCH] refactor: rectangle functions --- include/engine.h | 3 +++ src/appGameBirds.c | 41 ----------------------------------------- src/engineRectangle.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 41 deletions(-) create mode 100644 src/engineRectangle.c diff --git a/include/engine.h b/include/engine.h index c028201..a1ea2bf 100644 --- a/include/engine.h +++ b/include/engine.h @@ -232,5 +232,8 @@ Vector2 GetRandomVector(const Rectangle bounds); Vector2 RotationToVector2(float rotation); float Vector2ToRotation(Vector2 vec); +Vector2 GetPositionInsideRectangle(Rectangle rec); +Vector2 GetPositionOutsideRectangle(Rectangle rec, int min, int max); + #endif //ENGINE_H diff --git a/src/appGameBirds.c b/src/appGameBirds.c index e137598..0260987 100644 --- a/src/appGameBirds.c +++ b/src/appGameBirds.c @@ -1,47 +1,6 @@ #include "appTypes.h" - - -Vector2 GetPositionInsideRectangle(Rectangle rec) -{ - int randX = GetRandomValue(rec.x, rec.width); - int randY = GetRandomValue(rec.y, rec.height); - Vector2 randVector = {randX, randY}; - return randVector; -} - -Vector2 GetPositionOutsideRectangle(Rectangle rec, int min, int max) -{ - int side = GetRandomValue(0, 3); - Vector2 pos = {0}; - - switch (side) - { - case 0: // left - pos.x = rec.x - GetRandomValue(min, max); - pos.y = GetRandomValue((int)rec.y, (int)(rec.y + rec.height)); - break; - - case 1: // right - pos.x = rec.x + rec.width + GetRandomValue(min, max); - pos.y = GetRandomValue((int)rec.y, (int)(rec.y + rec.height)); - break; - - case 2: // top - pos.x = GetRandomValue((int)rec.x, (int)(rec.x + rec.width)); - pos.y = rec.y - GetRandomValue(min, max); - break; - - case 3: // bottom - pos.x = GetRandomValue((int)rec.x, (int)(rec.x + rec.width)); - pos.y = rec.y + rec.height + GetRandomValue(min, max); - break; - } - - return pos; -} - const TextureAsset birdsTextureAssets[BIRDS_TEX_COUNT] = { {"assets/birds.png", 16.0f, 16.0f, 45.0f} }; diff --git a/src/engineRectangle.c b/src/engineRectangle.c new file mode 100644 index 0000000..d581cb7 --- /dev/null +++ b/src/engineRectangle.c @@ -0,0 +1,40 @@ +#include "engine.h" + +Vector2 GetPositionInsideRectangle(Rectangle rec) +{ + int randX = GetRandomValue(rec.x, rec.width); + int randY = GetRandomValue(rec.y, rec.height); + Vector2 randVector = {randX, randY}; + return randVector; +} + +Vector2 GetPositionOutsideRectangle(Rectangle rec, int min, int max) +{ + int side = GetRandomValue(0, 3); + Vector2 pos = {0}; + + switch (side) + { + case 0: // left + pos.x = rec.x - GetRandomValue(min, max); + pos.y = GetRandomValue((int)rec.y, (int)(rec.y + rec.height)); + break; + + case 1: // right + pos.x = rec.x + rec.width + GetRandomValue(min, max); + pos.y = GetRandomValue((int)rec.y, (int)(rec.y + rec.height)); + break; + + case 2: // top + pos.x = GetRandomValue((int)rec.x, (int)(rec.x + rec.width)); + pos.y = rec.y - GetRandomValue(min, max); + break; + + case 3: // bottom + pos.x = GetRandomValue((int)rec.x, (int)(rec.x + rec.width)); + pos.y = rec.y + rec.height + GetRandomValue(min, max); + break; + } + + return pos; +}