From e7ef3f208c109357538b1f68af10bcd78db95c95 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 8 Apr 2022 22:30:12 -0700 Subject: Changeset: 3 More work --- source/Player.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'source/Player.cpp') diff --git a/source/Player.cpp b/source/Player.cpp index 6575820..2fa8d0d 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -1,5 +1,11 @@ #include "Player.hpp" +#include "AppConfig.hpp" +#include "Utils.hpp" + +#include +#include + // Keep the same number as # of fields in `struct {}` in PlayerKeyBinds constexpr int kPlayerKeyBindCount = 4; @@ -11,10 +17,17 @@ std::span PlayerKeyBinds::GetKeyStatusArray() { return { &pressedLeft, kPlayerKeyBindCount }; } +Player::Player(GameWorld* world, int id) + : GameObject(world) + , mId{ id } { +} + void Player::Awaken() { + LoadFromFile(); } void Player::Resleep() { + SaveToFile(); } void Player::Update() { @@ -48,3 +61,40 @@ void Player::HandleKeyInput(int key, int action) { } } } + +#pragma macro_push("PLAYERKEYBINDS_DO_IO") +#undef PLAYERKEYBINDS_DO_IO +#define PLAYERKEYBINDS_DO_IO(function, fieldPrefix) \ + function(file, "left=%d\n", fieldPrefix keybinds.keyLeft); \ + function(file, "right=%d\n", fieldPrefix keybinds.keyRight); \ + function(file, "jump=%d\n", fieldPrefix keybinds.keyJump); \ + function(file, "attack=%d\n", fieldPrefix keybinds.keyAttack); + +static FILE* OpenPlayerConfigFile(Player* player, Utils::IoMode mode) { + char path[512]; + snprintf(path, sizeof(path), "%s/player%d.txt", AppConfig::dataDir.c_str(), player->GetId()); + + return Utils::OpenCstdioFile(path, mode); +} + +bool Player::LoadFromFile() { + auto file = OpenPlayerConfigFile(this, Utils::Read); + if (!file) return false; + + // TODO input validation + PLAYERKEYBINDS_DO_IO(fscanf, &); + + fclose(file); + return true; +} + +bool Player::SaveToFile() { + auto file = OpenPlayerConfigFile(this, Utils::WriteTruncate); + if (!file) return false; + + PLAYERKEYBINDS_DO_IO(fprintf, ); + + fclose(file); + return true; +} +#pragma macro_pop("PLAYERKEYBINDS_DO_IO") -- cgit v1.2.3-70-g09d2