feat: add engine SaveStorageData and LoadStorageData and started saving stats of player
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
// DEFINES//
|
||||
#define TEXT_COLOR_DARK (Color){182, 137, 98, 255}
|
||||
#define TEXT_COLOR_LIGHT (Color){243, 229, 194, 255}
|
||||
#define STORAGE_DATA_FILE "save1.data"
|
||||
|
||||
// ENUMS//
|
||||
typedef enum {
|
||||
@@ -38,6 +39,12 @@ typedef enum { GAMEPLAY_THEME, AMBIENT, SOUNDTRACK_COUNT } SoundtrackID;
|
||||
|
||||
typedef enum View { LOGO = 0, MENU = 1, GAMEPLAY = 2 } View;
|
||||
|
||||
typedef enum {
|
||||
STORAGE_POSITION_GOLD = 0,
|
||||
STORAGE_POSITION_POSX = 1,
|
||||
STORAGE_POSITION_POSY = 2
|
||||
} StorageData;
|
||||
|
||||
// STRUCTS//
|
||||
typedef struct {
|
||||
int frameCounter;
|
||||
@@ -101,6 +108,7 @@ typedef struct {
|
||||
Physics physics;
|
||||
float rotation;
|
||||
Sound soundFlapping;
|
||||
int gold;
|
||||
} Player;
|
||||
|
||||
typedef enum { BOAT, ENEMIES_TEX_COUNT } enemiesTextureID;
|
||||
|
||||
@@ -237,5 +237,8 @@ float Vector2ToRotation(Vector2 vec);
|
||||
Vector2 GetPositionInsideRectangle(Rectangle rec);
|
||||
Vector2 GetPositionOutsideRectangle(Rectangle rec, int min, int max);
|
||||
|
||||
bool SaveStorageValue(const char* fileName, unsigned int position, int value);
|
||||
int LoadStorageValue(const char* fileName, unsigned int position);
|
||||
|
||||
#endif //ENGINE_H
|
||||
|
||||
|
||||
@@ -97,6 +97,9 @@ void RenderGame(App *app) {
|
||||
(Rectangle){0, 0, g->mouse.cursorTexture.width,
|
||||
g->mouse.cursorTexture.height},
|
||||
(Vector2){g->mouse.screenPosition.x, g->mouse.screenPosition.y}, WHITE);
|
||||
|
||||
Player *player = GetPlayer();
|
||||
DrawText(TextFormat("Gold: %d", player->gold), 0.0f, 0.0f, 12.0f, YELLOW);
|
||||
EndTextureMode();
|
||||
|
||||
BeginDrawing();
|
||||
|
||||
+2
-2
@@ -75,9 +75,9 @@ void CreatePlayer(void)
|
||||
.animation = playerAnimation,
|
||||
.physics = playerPhysics,
|
||||
.rotation = rotation,
|
||||
.soundFlapping = LoadSound(playerSoundAssets[FLAPPING].path)
|
||||
.soundFlapping = LoadSound(playerSoundAssets[FLAPPING].path),
|
||||
.gold = LoadStorageValue(STORAGE_DATA_FILE, STORAGE_POSITION_GOLD)
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
void UpdatePlayer(void)
|
||||
|
||||
@@ -73,6 +73,9 @@ void UpdateProjectiles(Projectiles *projectiles, Enemies *enemies) {
|
||||
for (int j = 0; j < enemies->count; j++){
|
||||
if (CheckCollisionCircleRec(projectiles->physics[i].position, projectiles->radius[i], enemies->sprite[j].destRec)){
|
||||
PlaySound(projectiles->soundImpact[i]);
|
||||
Player *player = GetPlayer();
|
||||
player->gold += 1;
|
||||
SaveStorageValue(STORAGE_DATA_FILE, STORAGE_POSITION_GOLD, player->gold);
|
||||
projectiles->active[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#include "engine.h"
|
||||
|
||||
bool SaveStorageValue(const char* fileName, unsigned int position, int value){
|
||||
bool success = false;
|
||||
int dataSize = 0;
|
||||
unsigned int newDataSize = 0;
|
||||
unsigned char *fileData = LoadFileData(fileName, &dataSize);
|
||||
unsigned char *newFileData = NULL;
|
||||
|
||||
if (fileData != NULL)
|
||||
{
|
||||
if (dataSize <= (position*sizeof(int)))
|
||||
{
|
||||
newDataSize = (position + 1)*sizeof(int);
|
||||
newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize);
|
||||
if (newFileData != NULL)
|
||||
{
|
||||
int *dataPtr = (int *)newFileData;
|
||||
dataPtr[position]=value;
|
||||
}
|
||||
else
|
||||
{
|
||||
TraceLog(LOG_WARNING, "FLIEIO: [%s] Failed to realloc data (%u), position in bytes (%u) bigger than actual file size", fileName, dataSize, position*sizeof(int));
|
||||
newFileData = fileData;
|
||||
newDataSize = dataSize;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
newFileData = fileData;
|
||||
newDataSize = dataSize;
|
||||
int *dataPtr = (int *)newFileData;
|
||||
dataPtr[position] = value;
|
||||
}
|
||||
|
||||
success = SaveFileData(fileName, newFileData, newDataSize);
|
||||
RL_FREE(newFileData);
|
||||
TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", fileName, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
TraceLog(LOG_INFO, "FILEIO: [%s] File created successfully", fileName);
|
||||
dataSize = (position + 1)*sizeof(int);
|
||||
fileData = (unsigned char *)RL_MALLOC(dataSize);
|
||||
int *dataPtr = (int *)fileData;
|
||||
dataPtr[position] = value;
|
||||
success = SaveFileData(fileName, fileData, dataSize);
|
||||
UnloadFileData(fileData);
|
||||
TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", fileName, value);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
int LoadStorageValue(const char * fileName, unsigned int position)
|
||||
{
|
||||
int value = 0;
|
||||
int dataSize = 0;
|
||||
unsigned char *fileData = LoadFileData(fileName, &dataSize);
|
||||
if (fileData != NULL)
|
||||
{
|
||||
if (dataSize < ((int)(position*4))) TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to find storage position: %i", fileName, position);
|
||||
else
|
||||
{
|
||||
int *dataPtr = (int *)fileData;
|
||||
value = dataPtr[position];
|
||||
}
|
||||
UnloadFileData(fileData);
|
||||
TraceLog(LOG_INFO, "FILEIO: [%s] Loaded storage value: %i", fileName, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user