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..3fb9465 --- /dev/null +++ b/external/engine/array.h @@ -0,0 +1,96 @@ +#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 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; + 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..1446db5 100644 --- a/include/flowers.h +++ b/include/flowers.h @@ -1,30 +1,87 @@ #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; + float ageTimer; +} Flower; + +Flower DEFAULT_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}); + 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 = 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; + 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; + if (!f) return; + RenderSprite(&(f->sprite)); +} + + + + #endif // FLOWERS_H diff --git a/include/game.h b/include/game.h index 897f4cc..05bbb73 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); @@ -99,15 +101,16 @@ 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); - UpdateBee(&g->bee1, &g->flowers, &g->hive); + UpdatePlayer(&g->player, g->flowers); + UpdateArray(g->flowers, UpdateFlower); + // 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); } @@ -127,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) { @@ -155,7 +158,7 @@ void CleanupGame(GameState *g) UnloadMusicStream(g->soundtracks[i].music); } UnloadRenderTexture(g->target); - free(g->flowers.data); + FreeArray(&g->flowers); } #endif // GAME_H 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}, 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);