aboutsummaryrefslogtreecommitdiff
path: root/source/Player.cpp
blob: 47c3ccc46ff5e84f07a2552c850fdb1e135db010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "Player.hpp"

#include "AppConfig.hpp"
#include "ScopeGuard.hpp"
#include "Utils.hpp"

#include <cstdio>
#include <cstdlib>

// Keep the same number as # of fields in `struct {}` in PlayerKeyBinds
constexpr int kPlayerKeyBindCount = 4;

// Here be dragons: this treats consecutive fiels as an array, technically UB
std::span<int> PlayerKeyBinds::GetKeyArray() {
	return { &keyLeft, kPlayerKeyBindCount };
}
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() {
	if (keybinds.pressedLeft) {
	}
	if (keybinds.pressedRight) {
	}

	// TODO jump controller

	// TODO attack controller
}

void Player::HandleKeyInput(int key, int action) {
	bool pressed;
	if (action == GLFW_PRESS) {
		pressed = true;
	} else if (action == GLFW_REPEAT) {
		return;
	} else /* action == GLFW_RELEASE */ {
		pressed = false;
	}

	for (int i = 0; i < kPlayerKeyBindCount; ++i) {
		int kbKey = keybinds.GetKeyArray()[i];
		bool& kbStatus = keybinds.GetKeyStatusArray()[i];

		if (kbKey == key) {
			kbStatus = pressed;
			break;
		}
	}
}

#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;
	DEFER { fclose(file); };

	// TODO input validation
	PLAYERKEYBINDS_DO_IO(fscanf, &);

	return true;
}

bool Player::SaveToFile() {
	auto file = OpenPlayerConfigFile(this, Utils::WriteTruncate);
	if (!file) return false;
	DEFER { fclose(file); };

	PLAYERKEYBINDS_DO_IO(fprintf, );

	return true;
}
#pragma macro_pop("PLAYERKEYBINDS_DO_IO")