-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpponent.cpp
More file actions
89 lines (69 loc) · 2.3 KB
/
Opponent.cpp
File metadata and controls
89 lines (69 loc) · 2.3 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
#include "precomp.h"
#include "Opponent.h"
#include "Army.h"
#include "Commands.h"
#include "Unit.h"
#include "Scene.h"
#include "Terrain.h"
#include "TerrainData.h"
#include "Scope.h"
RTS::Opponent::Opponent(Framework::Scene& scene, Framework::EntityId armyEntityId) :
Entity(scene)
{
mHasFixedTick = true;
mArmy = dynamic_cast<Army*>(scene.mEntityManager->TryGetEntity(armyEntityId).value_or(nullptr));
}
void RTS::Opponent::FixedTick()
{
std::vector<Unit*> units = mArmy->GetUnitsInArmy();
std::vector<Unit*> unitsToCommand;
unitsToCommand.reserve(units.size());
for (Unit* unit : units)
{
unit->SetAggroLevel(AggroLevel::pursuit);
if (Framework::Random::Range(1.0f) <= sGiveCommandChance)
{
unitsToCommand.push_back(unit);
}
}
for (uint i = 0; i < unitsToCommand.size(); i++)
{
const uint desiredGroupSize = RandomGroupSize();
std::vector<Unit*> group = { unitsToCommand[i] };
group.reserve(desiredGroupSize);
for (uint j = i + 1; j < unitsToCommand.size() && group.size() < desiredGroupSize; j++, i++)
{
group.push_back(unitsToCommand[j]);
}
OrderToRandomPosition(std::move(group));
}
}
bool RTS::Opponent::Serialize(Framework::Data::Scope& parentScope) const
{
Entity::Serialize(parentScope);
Framework::Data::Scope& myScope = parentScope.AddChild("Opponent");
myScope.AddVariable("armyEntityId") << mArmy->GetId();
return true;
}
void RTS::Opponent::Deserialize(const Framework::Data::Scope& parentScope)
{
Entity::Deserialize(parentScope);
const Framework::Data::Scope& myScope = parentScope.GetScope("Opponent");
Framework::EntityId id;
myScope.GetVariable("armyEntityId") >> id;
mScene.mEntityManager->DelayedIdRequest(id,
[](Entity& entity, void* me)
{
static_cast<Opponent*>(me)->SetArmy(static_cast<Army*>(&entity));
}, this);
}
void RTS::Opponent::OrderToRandomPosition(std::vector<Unit*>&& group) const
{
const Framework::TerrainData* const terrainData = mScene.mTerrain->GetData();
glm::vec2 position = { Framework::Random::Range(sMinDistFromEdge, terrainData->mWorldSizeX - sMinDistFromEdge), Framework::Random::Range(sMinDistFromEdge, terrainData->mWorldSizeZ - sMinDistFromEdge) };
FormFormation(std::move(group), position);
}
uint RTS::Opponent::RandomGroupSize() const
{
return Framework::Random::Uint() % (sMaxGroupSize - sMinGroupSize) + sMinGroupSize;
}