-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTurnManager.cpp
More file actions
76 lines (58 loc) · 1.92 KB
/
Copy pathTurnManager.cpp
File metadata and controls
76 lines (58 loc) · 1.92 KB
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
#include "TurnManager.hpp"
#include "Party.hpp"
#include "Character.hpp"
#include <algorithm>
// =========================
// TurnManager – Implementation
// =========================
// Constructor
TurnManager::TurnManager()
: currentPartyIndex(0), round(0) {}
// Getters
int TurnManager::getRound() const {
return round;
}
std::size_t TurnManager::getCurrentPartyIndex() const {
return currentPartyIndex;
}
const std::vector<PartyPtr>& TurnManager::getPartyQueue() const {
return partyQueue;
}
// Methods
void TurnManager::buildOrder(const std::vector<PartyPtr>& parties) {
partyQueue.clear();
for (const auto& party : parties) {
if (!party->isDefeated()) {
partyQueue.push_back(party);
}
}
// Sort by average speed of alive members (fastest party acts first)
std::sort(partyQueue.begin(), partyQueue.end(),
[](const PartyPtr& a, const PartyPtr& b) {
int avgSpeedA = 0, avgSpeedB = 0;
auto aliveA = a->getAliveMembers();
auto aliveB = b->getAliveMembers();
for (const auto& m : aliveA) { avgSpeedA += m->getFinalStat(StatType::Speed); }
for (const auto& m : aliveB) { avgSpeedB += m->getFinalStat(StatType::Speed); }
if (!aliveA.empty()) avgSpeedA /= static_cast<int>(aliveA.size());
if (!aliveB.empty()) avgSpeedB /= static_cast<int>(aliveB.size());
return avgSpeedA > avgSpeedB;
});
currentPartyIndex = 0;
round++;
}
PartyPtr TurnManager::nextParty() {
if (partyQueue.empty()) return nullptr;
for (std::size_t tries = 0; tries < partyQueue.size(); tries++) {
auto& party = partyQueue[currentPartyIndex];
currentPartyIndex = (currentPartyIndex + 1) % partyQueue.size();
if (!party->isDefeated()) {
return party;
}
}
return nullptr;
}
void TurnManager::resetRound() {
round = 0;
currentPartyIndex = 0;
}