summaryrefslogtreecommitdiff
path: root/src/save.c
diff options
context:
space:
mode:
authorzlago2024-10-23 19:17:26 +0200
committerzlago2024-10-23 19:17:26 +0200
commitb12606899c98d7fc7a120c2b79797b5c45283ad2 (patch)
treef210a037fee0f2346bae8a10d1edc1b445b4324f /src/save.c
parentaf6acead62498bc49065ef828e388bcd511ce54d (diff)
hacky save system
Diffstat (limited to 'src/save.c')
-rw-r--r--src/save.c110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/save.c b/src/save.c
new file mode 100644
index 0000000..577ec61
--- /dev/null
+++ b/src/save.c
@@ -0,0 +1,110 @@
+#include "main.h"
+#include "entity.h"
+#include "loader.h"
+#include "tilemap.h"
+#include "input.h"
+#include "util.h"
+#include <string.h>
+#include <stdio.h>
+
+enum {
+ SAVE_A_IDLE,
+ SAVE_A_SPIN,
+ SAVE_A_SPIN2,
+ SAVE_A_SPIN3,
+};
+
+struct anim save_anims[] = {
+ {SAVE_A_IDLE, {0, 0, 16, 16}, 300},
+ {SAVE_A_SPIN2, {16, 0, 16, 16}, 5},
+ {SAVE_A_SPIN3, {32, 0, 16, 16}, 5},
+ {SAVE_A_IDLE, {16, 0, 16, 16}, 5},
+};
+
+static void save_free(struct entity *self) {
+ self->state = 0;
+ free(self->ext), self->ext = NULL;
+}
+
+static int save_update(struct entity *self) {
+ if (self->timer > 0) {
+ self->timer--;
+ }
+ if (
+ self->hitbox.left < entities.player[0].hitbox.right &&
+ self->hitbox.right > entities.player[0].hitbox.left &&
+ self->hitbox.top < entities.player[0].hitbox.bottom &&
+ self->hitbox.bottom > entities.player[0].hitbox.top &&
+ input_s(input_pressed) && self->timer == 0
+ ) {
+ printf("%s %u %u\n", game_level, from_fixed(entities.player[0].x), from_fixed(entities.player[0].y));
+ self->timer = 20;
+ self->anim = save_anims[SAVE_A_SPIN];
+ FILE *file = fopen(save_file_name, "wb");
+ fwrite(game_level, 1, strlen(game_level) + 1, file);
+ char buf[12] = {0};
+ snprintf(buf, sizeof (buf) - 1, "%i", from_fixed(entities.player[0].x));
+ fwrite(buf, 1, strlen(buf) + 1, file);
+ snprintf(buf, sizeof (buf) - 1, "%i", from_fixed(entities.player[0].y));
+ fwrite(buf, 1, strlen(buf) + 1, file);
+ fclose(file);
+ }
+ self->anim.length--;
+ if (self->anim.length == 0) {
+ self->anim = save_anims[self->anim.frame];
+ }
+ return 0;
+}
+
+static int save_hurt(struct entity *self, int damage) {
+ return 0;
+}
+
+static int save_draw(struct entity *self, int camX, int camY) {
+ SDL_Rect rect = self->anim.rect;
+ SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 16, 16, 16});
+ if (self->ext != NULL) {
+ rect.y += 16;
+ struct color const *const color = self->ext;
+ SDL_SetTextureColorMod(self->texture, color->r, color->g, color->b);
+ SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 16, 16, 16});
+ SDL_SetTextureColorMod(self->texture, 255, 255, 255);
+ }
+ return 0;
+}
+
+struct entity *save_new(struct entities *entities) {
+ struct entity *self = entities->enemy + entities->enemies;
+ *self = (struct entity) {
+ .update = save_update,
+ .hurt = save_hurt,
+ .draw = save_draw,
+ .free = save_free,
+ .x = 0, .y = 0,
+ .state = 1,
+ .timer = 0,
+ .ext = NULL,
+ };
+ self->anim = save_anims[SAVE_A_SPIN];
+ self->texture = res_get_texture("save").data;
+ entities->enemies++;
+ return self;
+}
+
+int save_property(struct entity *const restrict self, char const *const restrict property, char const *const restrict value) {
+ if (strcmp(property, "x") == 0) {
+ self->x = to_fixed(atoi(value));
+ } else if (strcmp(property, "y") == 0) {
+ self->y = to_fixed(atoi(value));
+ } else if (strcmp(property, "color") == 0) {
+ free(self->ext);
+ self->ext = malloc(sizeof (struct color));
+ if (util_stringToColor(self->ext, value)) {
+ return 1;
+ }
+ } else {
+ return 1;
+ }
+ self->hitbox = (struct hitbox) {.left = from_fixed(self->x) - 8, .top = from_fixed(self->y) - 16, .right = from_fixed(self->x) + 8, .bottom = from_fixed(self->y)};
+ return 0;
+}