From 77f2c38204e968498b34ea7d2ab4883774b40fcd Mon Sep 17 00:00:00 2001 From: Huaqo Date: Sat, 12 Apr 2025 19:39:21 +0200 Subject: [PATCH 1/3] array.h, still needs refactor of object and objectarray --- .vscode/settings.json | 4 +- external/engine/array.h | 88 +++++++++++++++++++++++++++++++++++++++++ include/flowers.h | 56 ++++++++++++++++++-------- include/game.h | 12 +++--- 4 files changed, 138 insertions(+), 22 deletions(-) create mode 100644 external/engine/array.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 721a7ec..3cd8cb3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,8 @@ "files.associations": { "asset.h": "c", "stdlib.h": "c", - "view.h": "c" + "view.h": "c", + "__node_handle": "c", + "font.h": "c" } } \ No newline at end of file diff --git a/external/engine/array.h b/external/engine/array.h new file mode 100644 index 0000000..6886b5f --- /dev/null +++ b/external/engine/array.h @@ -0,0 +1,88 @@ +#ifndef ARRAY_H +#define ARRAY_H + +typedef struct { + void** elements; + unsigned int count; + unsigned int capacity; +} Array; + +Array* CreateArray(unsigned int initCapacity){ + Array* array = malloc(sizeof(Array)); + if (!array) return NULL; + + array->elements = malloc(sizeof(void* ) * initCapacity); + if (!array->elements) { + free(array); + return NULL; + } + + array->count = 0; + array->capacity = initCapacity; + return array; +} + +int AddElementToArray(Array* array, void* element){ + if(array->count >= array->capacity) { + unsigned int newCapacity = array->capacity * 2; + void** newElements = realloc(array->elements, sizeof(void*) * newCapacity); + if (!newElements) return 0; + + array->elements = newElements; + array->capacity = newCapacity; + } + array->elements[array->count++] = element; + return 1; +} + +// NORMAL REMOVE +// int RemoveElementFromArray(Array* array, unsigned int index){ +// if (index >= array->count) return 0; + +// for (unsigned int i = index; i < array->count - 1; i++) { +// array->elements[i] = array->elements[i + 1]; +// } + +// array->count--; +// return 1; +// } + +// SWAP AND POP REMOVE +int RemoveElementFromArray(Array* array, unsigned int index) { + if (index >= array->count) return 0; + + array->elements[index] = array->elements[array->count - 1]; + + array->count--; + + return 1; +} + +void* GetRandomElementFromArray(Array* array){ + if (array == NULL || array->elements == NULL || array->count == 0) { + return NULL; + } + int randomIndex = GetRandomValue(0, (int)array->count-1); + return &array->elements[randomIndex]; +} + +void RenderArray(Array* array, void (*renderFunc)(void*)) +{ + if (!array || !renderFunc) return; + for (unsigned int i = 0; i < array->count; i++) { + renderFunc(array->elements[i]); + } +} + +void FreeArray(Array** array) { + if (!array || !*array) return; + + free((*array)->elements); + (*array)->elements = NULL; + + free(*array); + *array = NULL; +} + + +#endif // ARRAY_H diff --git a/include/flowers.h b/include/flowers.h index d4e3ecf..9fe4905 100644 --- a/include/flowers.h +++ b/include/flowers.h @@ -1,30 +1,54 @@ #ifndef FLOWERS_H #define FLOWERS_H -#include +#include "array.h" -ObjectArray CreateFlowers(Texture2D texture, float frameWidth, float rotation, float fieldSize) +typedef enum { + CHILD, + TEEN, + ADULT +} AGE; + +typedef struct { + Sprite sprite; + Vector2 position; + AGE age; +} Flower; + +Array* CreateFlowers(Texture2D texture, float frameWidth, float rotation, float fieldSize) { - ObjectArray flowers = CreateObjectArray(&(Object){ - .sprite = { - .texture = texture, - .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, - .destRec = {0.0f, 0.0f, frameWidth, frameWidth}, - .origin = {frameWidth / 2, frameWidth / 2}, - .rotation = rotation, - .color = WHITE}, - .position = {0.0f, 0.0f}}, - 10); + Array* flowers = CreateArray(10); - if (!flowers.data) - { + if(!flowers) { printf("Memory allocation failed.\n"); exit(1); } - SetRandomPositionForObjectArray(&flowers, (Rectangle){-fieldSize, -fieldSize, fieldSize * 2, fieldSize * 2}); - + for (int i = 0; i < 10; i++) { + Flower* flower = malloc(sizeof(Flower)); + *flower = (Flower){ + .sprite = { + .texture = texture, + .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, + .destRec = {(float)GetRandomValue(-fieldSize,fieldSize), (float)GetRandomValue(-fieldSize,fieldSize), frameWidth, frameWidth}, + .origin = {frameWidth / 2, frameWidth / 2}, + .rotation = rotation, + .color = WHITE, + }, + .age = CHILD + }; + AddElementToArray(flowers, flower); + } return flowers; } +void RenderFlower(void* element) +{ + Flower* f = (Flower*)element; + if (!f) return; + RenderSprite(&(f->sprite)); +} + + + #endif // FLOWERS_H diff --git a/include/game.h b/include/game.h index 897f4cc..69949cc 100644 --- a/include/game.h +++ b/include/game.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "gameassets.h" #include "floor.h" @@ -24,7 +26,7 @@ typedef struct RenderTexture2D target; Floor floor; Object hive; - ObjectArray flowers; + Array* flowers; Player player; Bee bee1; Camera2D camera; @@ -65,7 +67,7 @@ void InitGame(GameState *g, Window *window) // Animated Entities g->player = CreatePlayer(IDLE_DOWN, HOE, g->textures[PLAYER], g->assets.textureAssets[PLAYER].frameWidth, 4, 2, g->hive.position, g->assets.textureAssets[PLAYER].rotation, 50.0f); - g->bee1 = CreateBee(FLYING, g->textures[BEE], g->assets.textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive); + // g->bee1 = CreateBee(FLYING, g->textures[BEE], g->assets.textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive); // I/O g->camera = CreateCamera(window->width, window->height, g->player.physics.position, 10.0f); @@ -100,14 +102,14 @@ void UpdateGame(GameState *g, View *currentView, bool *running) UpdateCamera2D(&g->camera, &g->player.physics.position, &g->mouse); UpdateFloor(&g->floor, &g->camera); UpdatePlayer(&g->player); - UpdateBee(&g->bee1, &g->flowers, &g->hive); + // UpdateBee(&g->bee1, &g->flowers, &g->hive); } void RenderComponents(GameState *g) { RenderFloor(&g->floor); RenderSprite(&g->hive.sprite); - RenderObjectArray(&g->flowers); + RenderArray(g->flowers, RenderFlower); RenderSprite(&g->player.sprite); RenderSprite(&g->bee1.sprite); } @@ -155,7 +157,7 @@ void CleanupGame(GameState *g) UnloadMusicStream(g->soundtracks[i].music); } UnloadRenderTexture(g->target); - free(g->flowers.data); + FreeArray(&g->flowers); } #endif // GAME_H From 79c6a058c742a9e1cb48233f3ddecd65f989c9b7 Mon Sep 17 00:00:00 2001 From: Huaqo Date: Sat, 12 Apr 2025 20:19:49 +0200 Subject: [PATCH 2/3] feat: flower growth over time --- external/engine/array.h | 8 ++++++++ include/flowers.h | 23 ++++++++++++++++++++++- include/game.h | 1 + include/gameassets.h | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/external/engine/array.h b/external/engine/array.h index 6886b5f..3fb9465 100644 --- a/external/engine/array.h +++ b/external/engine/array.h @@ -66,6 +66,14 @@ void* GetRandomElementFromArray(Array* array){ return &array->elements[randomIndex]; } +void UpdateArray(Array* array, void (*updateFunc)(void*)) +{ + if (!array || !updateFunc) return; + for (unsigned int i = 0; i < array->count; i++) { + updateFunc(array->elements[i]); + } +} + void RenderArray(Array* array, void (*renderFunc)(void*)) { if (!array || !renderFunc) return; diff --git a/include/flowers.h b/include/flowers.h index 9fe4905..8202571 100644 --- a/include/flowers.h +++ b/include/flowers.h @@ -13,6 +13,7 @@ typedef struct { Sprite sprite; Vector2 position; AGE age; + float ageTimer; } Flower; Array* CreateFlowers(Texture2D texture, float frameWidth, float rotation, float fieldSize) @@ -35,13 +36,32 @@ Array* CreateFlowers(Texture2D texture, float frameWidth, float rotation, float .rotation = rotation, .color = WHITE, }, - .age = CHILD + .age = CHILD, + .ageTimer = 0.0f }; AddElementToArray(flowers, flower); } return flowers; } + +void UpdateFlower(void* element) { + Flower* f = (Flower*)element; + if (!f) return; + + f->ageTimer += 1; + + if (f->age == CHILD && f->ageTimer >= 3000.0f) { + f->age = TEEN; + f->sprite.sourceRec.x = f->sprite.sourceRec.width; + } + else if (f->age == TEEN && f->ageTimer >= 6000.0f) { + f->age = ADULT; + f->sprite.sourceRec.x = 2.0f * f->sprite.sourceRec.width; + } +} + + void RenderFlower(void* element) { Flower* f = (Flower*)element; @@ -51,4 +71,5 @@ void RenderFlower(void* element) + #endif // FLOWERS_H diff --git a/include/game.h b/include/game.h index 69949cc..e7b3f5b 100644 --- a/include/game.h +++ b/include/game.h @@ -102,6 +102,7 @@ void UpdateGame(GameState *g, View *currentView, bool *running) UpdateCamera2D(&g->camera, &g->player.physics.position, &g->mouse); UpdateFloor(&g->floor, &g->camera); UpdatePlayer(&g->player); + UpdateArray(g->flowers, UpdateFlower); // UpdateBee(&g->bee1, &g->flowers, &g->hive); } diff --git a/include/gameassets.h b/include/gameassets.h index 1b5bc43..bf31de6 100644 --- a/include/gameassets.h +++ b/include/gameassets.h @@ -21,7 +21,7 @@ const TextureAsset texAssets[TEX_COUNT] = { {"assets/SproutLandsBasic/Characters/character.png", 48.0f, 48.0f, 0.0f}, {"assets/SproutLandsBasic/Characters/BasicCharakterActions.png", 48.0f, 48.0f, 0.0f}, {"assets/SproutLandsBasic/Objects/tools.png", 48.0f, 16.0f, 0.0f}, - {"assets/flower.png", 16.0f, 16.0f, 45.0f}, + {"assets/carrot_and_tomato.png", 32.0f, 32.0f, 0.0f}, {"assets/character_bee.png", 16.0f, 16.0f, 0.0f}, {"assets/grass.png", 16.0f, 16.0f, 0.0f}, {"assets/hive.png", 32.0f, 32.0f, 0.0f}, From 23f16988b3ad8de7eb08df7a3138a3b00adf0aac Mon Sep 17 00:00:00 2001 From: beyer-rebecca Date: Sat, 12 Apr 2025 21:52:50 +0200 Subject: [PATCH 3/3] feat: flower plant on player location --- include/flowers.h | 36 ++++++++++++++++++++++++------------ include/game.h | 4 ++-- include/inventar.h | 2 +- include/player.h | 11 +++++++---- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/include/flowers.h b/include/flowers.h index 8202571..1446db5 100644 --- a/include/flowers.h +++ b/include/flowers.h @@ -16,6 +16,8 @@ typedef struct { float ageTimer; } Flower; +Flower DEFAULT_FLOWER; + Array* CreateFlowers(Texture2D texture, float frameWidth, float rotation, float fieldSize) { Array* flowers = CreateArray(10); @@ -25,25 +27,35 @@ Array* CreateFlowers(Texture2D texture, float frameWidth, float rotation, float exit(1); } + DEFAULT_FLOWER = (Flower){ + .sprite = { + .texture = texture, + .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, + .destRec = {(float)GetRandomValue(-fieldSize,fieldSize), (float)GetRandomValue(-fieldSize,fieldSize), frameWidth, frameWidth}, + .origin = {frameWidth / 2, frameWidth / 2}, + .rotation = rotation, + .color = WHITE, + }, + .age = CHILD, + .ageTimer = 0.0f + }; + for (int i = 0; i < 10; i++) { Flower* flower = malloc(sizeof(Flower)); - *flower = (Flower){ - .sprite = { - .texture = texture, - .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, - .destRec = {(float)GetRandomValue(-fieldSize,fieldSize), (float)GetRandomValue(-fieldSize,fieldSize), frameWidth, frameWidth}, - .origin = {frameWidth / 2, frameWidth / 2}, - .rotation = rotation, - .color = WHITE, - }, - .age = CHILD, - .ageTimer = 0.0f - }; + *flower = DEFAULT_FLOWER; + flower->sprite.destRec = (Rectangle){(float)GetRandomValue(-fieldSize,fieldSize), (float)GetRandomValue(-fieldSize,fieldSize), frameWidth, frameWidth}; AddElementToArray(flowers, flower); } return flowers; } +void AddFlower(Array* flowers, Rectangle rec){ + Flower* flower = malloc(sizeof(Flower)); + *flower = DEFAULT_FLOWER; + flower->sprite.destRec = rec; + AddElementToArray(flowers, flower); +} + void UpdateFlower(void* element) { Flower* f = (Flower*)element; diff --git a/include/game.h b/include/game.h index e7b3f5b..05bbb73 100644 --- a/include/game.h +++ b/include/game.h @@ -101,7 +101,7 @@ void UpdateGame(GameState *g, View *currentView, bool *running) UpdateMouse(&g->mouse, &g->camera); UpdateCamera2D(&g->camera, &g->player.physics.position, &g->mouse); UpdateFloor(&g->floor, &g->camera); - UpdatePlayer(&g->player); + UpdatePlayer(&g->player, g->flowers); UpdateArray(g->flowers, UpdateFlower); // UpdateBee(&g->bee1, &g->flowers, &g->hive); } @@ -130,7 +130,7 @@ void RenderGame(GameState *g, const State *appState, Flags *flags) { RenderPostFX(&g->postFX[i], &g->target); } - DrawText(TextFormat("%d", g->player.inventar.seedCount), 10, 10, 50, WHITE); + DrawText(TextFormat("Flower Seeds: %d", g->player.inventar.flowerSeedCount), 10, 10, 50, WHITE); if (flags->showFPS) { diff --git a/include/inventar.h b/include/inventar.h index f672b7c..e271dca 100644 --- a/include/inventar.h +++ b/include/inventar.h @@ -3,7 +3,7 @@ typedef struct{ - int seedCount; + int flowerSeedCount; } Inventar; #endif // INVENTAR_H diff --git a/include/player.h b/include/player.h index 0d48220..7bb8912 100644 --- a/include/player.h +++ b/include/player.h @@ -5,7 +5,9 @@ #include #include #include +#include +#include "flowers.h" #include "inventar.h" typedef enum @@ -70,7 +72,7 @@ Player CreatePlayer(PlayerAnimationState animState, SelectedTool selectedTool, T .color = WHITE}, .animation = {.state = animState, .currentFrame = 0, .maxFrame = maxFrame, .framesCounter = 0, .framesSpeed = framesSpeed, .animTimer = 0.0f}, .physics = {.position = startPosition, .direction = (Vector2){0.0f, 0.0f}, .speed = speed}, - .inventar = {.seedCount = 6}}; + .inventar = {.flowerSeedCount = 6}}; } void UpdatePlayerAnimationState(int *state, SelectedTool *selectedTool, Vector2 *dir) @@ -129,11 +131,12 @@ void UpdatePlayerAnimationState(int *state, SelectedTool *selectedTool, Vector2 } } -void UpdatePlayer(Player *player) +void UpdatePlayer(Player *player, Array* flowers) { - if (IsKeyPressed(KEY_SPACE) && player->inventar.seedCount > 0) + if (IsKeyPressed(KEY_SPACE) && player->inventar.flowerSeedCount > 0) { - player->inventar.seedCount--; + AddFlower(flowers, player->sprite.destRec); + player->inventar.flowerSeedCount--; } Vector2 dir = GetDirectionVector(); UpdatePlayerAnimationState(&player->animation.state, &player->selectedTool, &dir);