From ab5811a7c519e62820a767f70140b537fec935d0 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sun, 23 Jan 2022 13:52:29 -0700 Subject: [PATCH] everything --- res/map/testing.tmx | 4 ++-- src/impl/game.c | 35 +++++++++++++++++++++++------------ src/impl/hooks.c | 11 ++++------- src/impl/main.c | 10 ++++++++-- src/impl/save.c | 29 +++++++++++++++++++++++++++++ src/include/save.h | 15 +++++++++++++++ 6 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 src/impl/save.c create mode 100644 src/include/save.h diff --git a/res/map/testing.tmx b/res/map/testing.tmx index 9d03806..487e9c8 100644 --- a/res/map/testing.tmx +++ b/res/map/testing.tmx @@ -124,14 +124,14 @@ - + - + diff --git a/src/impl/game.c b/src/impl/game.c index f69d1f8..f587d7d 100644 --- a/src/impl/game.c +++ b/src/impl/game.c @@ -73,18 +73,6 @@ void init_game() { }; } -void save_game() { - // diskw(&game_state, sizeof(struct GameState)); -} - -void load_game() { - // struct GameState temp; - // diskr(&temp, sizeof(struct GameState)); - // if (temp.valid) { - // game_state = temp; - // } -} - int calc_room_id(struct GameState *game) { return game->currentRoom.loc.x + (game->currentRoom.loc.y * (game->overworld->static_map.width / 20)); @@ -117,3 +105,26 @@ void set_default_room_state(struct GameState *game) { game->currentRoom.state = new_state; } + +// void load_world(struct GameState *game) { +// int state_array_loc = 0; +// for (uint8_t i = 0; i < 3; i++) { +// if (game->room_states.id[i] == game->world_id) { +// state_array_loc = i; +// } +// } +// uint16_t id = game->room_states.state[state_array_loc]; +// switch (id) { +// case TILEMAP_TESTING_ID: +// game->overworld = &testing_tilemap; +// break; +// case TILEMAP_TESTING_ID: +// game->overworld = &testing_tilemap; +// break; +// case TILEMAP_TESTING_ID: +// game->overworld = &testing_tilemap; +// break; +// default: +// break; +// } +// } \ No newline at end of file diff --git a/src/impl/hooks.c b/src/impl/hooks.c index 4eacb54..2eda9ca 100644 --- a/src/impl/hooks.c +++ b/src/impl/hooks.c @@ -11,6 +11,7 @@ #include "text.h" #include "collision.h" +#include "save.h" void on_room_enter() { struct Room *new_room = &game_state.currentRoom; @@ -39,6 +40,8 @@ void on_room_enter() { } } game_state.currentRoom.blocks.size = index; + + save(); } void on_room_exit() { @@ -46,17 +49,11 @@ void on_room_exit() { game_state.currentRoom.blocks.size, game_state.player.loc.room.x, game_state.player.loc.room.y); game_state.currentRoom.blocks.size = 0; - save_game(); game_state.currentRoom.loc = game_state.player.loc.room; } -void on_game_launch() { - - load_game(); - - on_room_enter(); -} +void on_game_launch() { on_room_enter(); } struct TerrainMap terrain; diff --git a/src/impl/main.c b/src/impl/main.c index 1bff4b3..ae1a39a 100644 --- a/src/impl/main.c +++ b/src/impl/main.c @@ -4,8 +4,10 @@ #include "../../res/map/dungeon_one.map.h" #include "../../res/map/dungeon_two.map.h" #include "../../res/map/testing.map.h" - #include "block.h" +#include "save.h" + +#include "wasm4.h" void start() { initalize_dungeon_one_tilemap(); @@ -13,7 +15,11 @@ void start() { initalize_testing_tilemap(); init_game(); - on_game_launch(); + if (load_save_game()) { + trace("game loaded"); + } + + // on_game_launch(); } void update() { on_update(); } diff --git a/src/impl/save.c b/src/impl/save.c new file mode 100644 index 0000000..e0ba6fd --- /dev/null +++ b/src/impl/save.c @@ -0,0 +1,29 @@ +#include "save.h" + +void save() { + struct SaveGame g = (struct SaveGame){ + .valid = VALID, + .player = game_state.player, + .inventory = game_state.inventory, + .currentRoom = game_state.currentRoom, + .world_id = game_state.world_id, + }; + + diskw(&g, sizeof(struct SaveGame)); +} + +bool load_save_game() { + struct SaveGame g; + diskr(&g, sizeof(struct SaveGame)); + + if (g.valid == VALID) { + game_state.currentRoom = g.currentRoom; + game_state.player = g.player; + game_state.inventory = g.inventory; + game_state.world_id = g.world_id; + + return true; + } + + return false; +} \ No newline at end of file diff --git a/src/include/save.h b/src/include/save.h new file mode 100644 index 0000000..d24e99c --- /dev/null +++ b/src/include/save.h @@ -0,0 +1,15 @@ +#include "game.h" +#include "wasm4.h" + +#define VALID 0xCAFEBABE + +struct SaveGame { + uint32_t valid; + struct Player player; + struct PlayerInventory inventory; + struct Room currentRoom; + uint16_t world_id; +}; + +void save(); +bool load_save_game(); \ No newline at end of file