From 52ab86e5f55b189aea4198fe4d15a29c4d3c9d57 Mon Sep 17 00:00:00 2001 From: Physick <96335032+DegustatorPonos@users.noreply.github.com> Date: Thu, 27 Aug 2026 19:34:49 +0500 Subject: distraction scene init --- src/scenes/distractionScene.c | 116 ++++++++++++++++++++++++++++++++++++++++++ src/scenes/puzzleScene.c | 60 ++++++++++++---------- src/scenes/resultView.c | 1 - src/scenes/scenes.h | 1 + 4 files changed, 149 insertions(+), 29 deletions(-) create mode 100644 src/scenes/distractionScene.c (limited to 'src/scenes') diff --git a/src/scenes/distractionScene.c b/src/scenes/distractionScene.c new file mode 100644 index 0000000..1f12aae --- /dev/null +++ b/src/scenes/distractionScene.c @@ -0,0 +1,116 @@ +#include "scenes.h" +#include +#include +#include "../ui_elements/UI.h" +#include "../resources/resources.h" + +static const float BODY_PADDING = 0.05f; +static const int BORDER = 2; +static const int EDITOR_PADDING = 3; + +typedef struct { + DistractionPuzzle *puzzle; + TextArea *EditorInfo; + void (*addTime_cb)(Scene *, size_t); + int scroll; +} distractionSceneData; + +Rectangle getBodyRect() { + float screen_h = GetScreenHeight(); + float screen_w = GetScreenWidth(); + return (Rectangle) { + .x = screen_w * BODY_PADDING, + .y = screen_h * BODY_PADDING, + .height = screen_h * (1 - BODY_PADDING * 2), + .width = screen_w * (1 - BODY_PADDING * 2), + }; +} + +bool updateDisctractionScene(void *self) { + distractionSceneData *data = ((Scene *)self)->data; + Rectangle pane1, pane2; + GetPanesSplit(&pane1, &pane2, getBodyRect(), 0.35, 0.4); + UpdateTextArea(data->EditorInfo, pane2); + + return true; +} + +bool drawDisctractionScene(void *self) { + distractionSceneData *data = ((Scene *)self)->data; + + DrawRectangle(0, 0, + GetScreenWidth(), GetScreenHeight(), + (Color) {0, 0, 0, 127 }); + Rectangle body = getBodyRect(); + Rectangle pane1, pane2; + GetPanesSplit(&pane1, &pane2, body, 0.35, 0.4); + DrawRecBordered(pane1, WHITE, COLORSCHEME_ORANGE, BORDER); + DrawRecBordered(pane2, WHITE, COLORSCHEME_ORANGE, BORDER); + + if (data->puzzle == NULL) { + return true; + } + DrawTextInRec(pane1, data->puzzle->puzzle->Description, + GetMainFont(), 20, COLORSCHEME_BROWN, 3, &data->scroll); + Rectangle editor_rect = { + .x = pane2.x + EDITOR_PADDING, + .y = pane2.y + EDITOR_PADDING, + .width = pane2.width - EDITOR_PADDING * 2, + .height = pane2.height - EDITOR_PADDING * 2, + }; + DrawTextArea(data->EditorInfo, editor_rect); + + return true; +} + +void deinitDisctractionScene(void *self) { + distractionSceneData *data = ((Scene *)self)->data; + if (data->puzzle != NULL) { + FreeDistrationPuzzle(data->puzzle); + } +} + +Scene *CreateDistractionScene(DistractionPuzzle *puzzle, void (*addTime)(Scene *, size_t)) { + if (puzzle == NULL) { + TraceLog(LOG_ERROR, "Failed to create a distraction scene: puzzle struct is NULL"); + return NULL; + } + + Scene *outp = malloc(sizeof(Scene)); + if (outp == NULL) { + TraceLog(LOG_ERROR, "Failed to create a distraction scene: scene allocation failed"); + return NULL; + } + outp->Update = updateDisctractionScene; + outp->Draw = drawDisctractionScene; + outp->Deint = deinitDisctractionScene; + outp->IsTransparent = true; + + distractionSceneData *data = malloc(sizeof(distractionSceneData)); + if (data == NULL) { + free(outp); + TraceLog(LOG_ERROR, "Failed to create a distraction scene: data allocation failed"); + return NULL; + } + data->puzzle = puzzle; + data->addTime_cb = addTime; + + TextAreaDisplayParams *EditorDisplayParams = malloc(sizeof(TextAreaDisplayParams)); + if (EditorDisplayParams != NULL) { + EditorDisplayParams->font = GetMonoFont(); + EditorDisplayParams->spacing = 1; + EditorDisplayParams->font_size = 20; // this will change + EditorDisplayParams->scroll_h = 0; + EditorDisplayParams->scroll_v = 0; + } + + if (puzzle->puzzle->InitialCode != NULL) { + data->EditorInfo = CreateTextAreaFromText(EditorDisplayParams, puzzle->puzzle->InitialCode); + } else { + data->EditorInfo = CreateTextArea(EditorDisplayParams); + } + + outp->data = data; + + return outp; +} diff --git a/src/scenes/puzzleScene.c b/src/scenes/puzzleScene.c index 48e1238..c3e48c1 100644 --- a/src/scenes/puzzleScene.c +++ b/src/scenes/puzzleScene.c @@ -1,5 +1,7 @@ #include +#include #include +#include #include "scenes.h" #include "../resources/resources.h" #include "../ui_elements/UI.h" @@ -9,14 +11,20 @@ typedef struct { TextArea *EditorInfo; Button *submitButton; int scroll; + // Time until the next distraction puzzle in seconds + size_t distraction_timer; + // Miliseconds elapsed + float time_acc; } puzzleSceneData; const int HEADER_FONT_SIZE = 50; -const int PADDING = 10; const int EDITOR_PADDING = 3; const int BORDER = 2; const int FOOTER_HEIGHT = 30; +// Time in seconds +const size_t BASE_TIME_LEFT = 1; // 5 * 60; + void DrawPuzzleName(char *name) { Vector2 width = MeasureTextEx(*GetMainFont(), name, 50, 1); int screen_width = GetScreenWidth(); @@ -60,33 +68,6 @@ Rectangle GetBodyRect() { return outp; } -void GetPanesSplit(Rectangle *pane1, Rectangle *pane2, Rectangle body, float proportion_h, float proportion_v) { - if (body.height >= body.width) { - // vertical split - pane1->x = body.x; - pane1->y = body.y; - pane1->width = body.width; - pane1->height = (body.height - PADDING) * proportion_v; - - pane2->x = body.x; - pane2->y = body.y + pane1->height + PADDING; - pane2->width = body.width; - pane2->height = (body.height - PADDING) * (1 - proportion_v); - return; - } - - // horizontal split - pane1->x = body.x; - pane1->y = body.y; - pane1->height = body.height; - pane1->width = (body.width - PADDING) * proportion_h; - - pane2->y = body.y; - pane2->x = body.x + pane1->width + PADDING; - pane2->height = body.height; - pane2->width = (body.width - PADDING) * (1 - proportion_h); -} - bool drawPuzzleScene(void *self) { puzzleSceneData *data = ((Scene *)self)->data; DrawBackground(); @@ -133,6 +114,15 @@ void SubmitPuzzle(Scene *self) { AddScene(CreateResultView(res)); } +void AddTime(Scene *self, size_t additionalTime) { + puzzleSceneData *data = ((Scene *)self)->data; + data->distraction_timer += additionalTime; +} + +void createDistraction() { + AddScene(CreateDistractionScene(GetRandomDistraction(), AddTime)); +} + bool updatePuzzleScene(void *self) { puzzleSceneData *data = ((Scene *)self)->data; @@ -147,6 +137,17 @@ bool updatePuzzleScene(void *self) { if (data->submitButton != NULL) UpdateButton(data->submitButton); + data->time_acc += GetFrameTime(); + while (data->time_acc >= 1) { + data->distraction_timer -= 1; + data->time_acc -= 1; + // TraceLog(LOG_INFO, "Time left: %u", data->distraction_timer); + if (data->distraction_timer == 0) { + createDistraction(); + return true; + } + } + return true; } @@ -193,6 +194,9 @@ Scene *CreatePuzzleScene(Puzzle *puzzle) { } data->submitButton = submitButton; + data->distraction_timer = BASE_TIME_LEFT; + data->time_acc = 0; + if (puzzle->InitialCode != NULL) { data->EditorInfo = CreateTextAreaFromText(EditorDisplayParams, puzzle->InitialCode); } else { diff --git a/src/scenes/resultView.c b/src/scenes/resultView.c index 567cb02..1cbc182 100644 --- a/src/scenes/resultView.c +++ b/src/scenes/resultView.c @@ -7,7 +7,6 @@ #include "../ui_elements/UI.h" #include "../resources/resources.h" -static const int PADDING = 20; static const int BORDER = 2; static const int SCROLL_SPEED = 10; static const uint8_t MAX_TINT = 96; diff --git a/src/scenes/scenes.h b/src/scenes/scenes.h index b7fdf02..335a139 100644 --- a/src/scenes/scenes.h +++ b/src/scenes/scenes.h @@ -30,6 +30,7 @@ bool UpdateScene(); Scene *CreateMainMenu(); Scene *CreatePuzzleScene(Puzzle *puzzle); +Scene *CreateDistractionScene(DistractionPuzzle *puzzle, void (*addTime)(Scene *, size_t)); Scene *CreateResultView(SubmitionResult *result); #endif // SCENES_H -- cgit v1.3