aboutsummaryrefslogtreecommitdiff
path: root/source/Player.cpp
diff options
context:
space:
mode:
authorhnOsmium0001 <[email protected]>2022-04-08 22:30:12 -0700
committerhnOsmium0001 <[email protected]>2022-04-08 22:30:12 -0700
commite47a98793e58a5dbbe76bfed27e59408e43538e4 (patch)
tree250eb4c4200efb63c493ac6f4adb83c89be10ea1 /source/Player.cpp
parent3fdc6eb4f2cbeffce9b250beec4d3a2d52a3f534 (diff)
More work
Diffstat (limited to 'source/Player.cpp')
-rw-r--r--source/Player.cpp50
1 files changed, 50 insertions, 0 deletions
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 <cstdio>
+#include <cstdlib>
+
// Keep the same number as # of fields in `struct {}` in PlayerKeyBinds
constexpr int kPlayerKeyBindCount = 4;
@@ -11,10 +17,17 @@ std::span<bool> 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")