-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBattle.cpp
More file actions
149 lines (119 loc) · 3.37 KB
/
Copy pathBattle.cpp
File metadata and controls
149 lines (119 loc) · 3.37 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "Battle.hpp"
#include "BattleRules.hpp"
#include "BattleLog.hpp"
#include "Party.hpp"
#include "Character.hpp"
#include <algorithm>
#include <string>
// =========================
// Battle – Implementation
// =========================
// Constructor
Battle::Battle(std::shared_ptr<BattleRules> battleRules)
: rules(std::move(battleRules)) {
log = std::make_shared<BattleLog>();
}
// Getters
std::shared_ptr<BattleLog> Battle::getLog() const {
return log;
}
BattleRules& Battle::getRules() {
return *rules;
}
const BattleRules& Battle::getRules() const {
return *rules;
}
const std::vector<PartyPtr>& Battle::getParties() const {
return parties;
}
TurnManager& Battle::getTurnManager() {
return turnManager;
}
const TurnManager& Battle::getTurnManager() const {
return turnManager;
}
// Party Management
void Battle::addParty(const PartyPtr& party) {
parties.push_back(party);
}
void Battle::removeParty(const PartyPtr& party) {
parties.erase(
std::remove(parties.begin(), parties.end(), party),
parties.end());
}
// Query Methods
std::vector<CharPtr> Battle::allCharacters() const {
std::vector<CharPtr> all;
for (const auto& party : parties) {
for (const auto& member : party->getMembers()) {
all.push_back(member);
}
}
return all;
}
std::vector<CharPtr> Battle::enemiesOf(const CharPtr& actor) const {
std::vector<CharPtr> enemies;
Party* actorParty = actor->getOwnerParty();
for (const auto& party : parties) {
if (party.get() != actorParty) {
for (const auto& member : party->getMembers()) {
if (member->isAlive()) {
enemies.push_back(member);
}
}
}
}
return enemies;
}
std::vector<CharPtr> Battle::alliesOf(const CharPtr& actor) const {
std::vector<CharPtr> allies;
Party* actorParty = actor->getOwnerParty();
if (actorParty) {
for (const auto& member : actorParty->getMembers()) {
if (member->isAlive() && member != actor) {
allies.push_back(member);
}
}
}
return allies;
}
bool Battle::isOver() const {
int aliveParties = 0;
for (const auto& party : parties) {
if (!party->isDefeated()) {
aliveParties++;
}
}
return aliveParties <= 1;
}
PartyPtr Battle::getWinner() const {
for (const auto& party : parties) {
if (!party->isDefeated()) {
return party;
}
}
return nullptr;
}
// Battle Control
void Battle::start() {
turnManager.buildOrder(parties);
log->add("=== Battle Started! ===");
for (const auto& party : parties) {
log->add(party->getPartyName() + " enters the battle!");
}
}
void Battle::executeRound() {
if (isOver()) return;
log->add("\n--- Round " + std::to_string(turnManager.getRound()) + " ---");
// Rebuild party order at the start of each round
turnManager.buildOrder(parties);
// Execute turn for each party in the current turn order
const auto& queue = turnManager.getPartyQueue();
for (std::size_t i = 0; i < queue.size() && !isOver(); i++) {
PartyPtr party = turnManager.nextParty();
if (party && !party->isDefeated()) {
log->add("\n" + party->getPartyName() + "'s turn:");
party->executeTurn(*this);
}
}
}