Skip to content

Commit 1d9de11

Browse files
authored
Merge pull request #1499 from OutpostUniverse/refactorCrimeEvents
Add `Signal` for `CrimeExecution` events
2 parents f3a2d80 + 0073313 commit 1d9de11

5 files changed

+45
-26
lines changed

OPHD/States/CrimeExecution.cpp

+17-18
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@ namespace
4747
}
4848

4949

50-
CrimeExecution::CrimeExecution(NotificationArea& notificationArea, const Difficulty& difficulty) :
51-
mDifficulty{difficulty},
52-
mNotificationArea{notificationArea}
50+
CrimeExecution::CrimeExecution(const Difficulty& difficulty) :
51+
mDifficulty{difficulty}
5352
{
5453
}
5554

5655

56+
CrimeExecution::CrimeExecution(const Difficulty& difficulty, Signal::DelegateType onCrimeEvent) : CrimeExecution{difficulty}
57+
{
58+
mCrimeEventSignal.connect(onCrimeEvent);
59+
}
60+
61+
5762
void CrimeExecution::executeCrimes(const std::vector<Structure*>& structuresCommittingCrime)
5863
{
5964
mMoraleChanges.clear();
@@ -88,13 +93,11 @@ void CrimeExecution::stealFood(FoodProduction& structure)
8893
int foodStolen = calcAmountForStealing(mDifficulty, 5, 15, structure.foodLevel());
8994
structure.foodLevel(structure.foodLevel() - foodStolen);
9095

91-
const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);
92-
93-
mNotificationArea.push({
96+
mCrimeEventSignal.emit(
9497
"Food Stolen",
9598
NAS2D::stringFrom(foodStolen) + " units of food was pilfered from a " + structure.name() + ". " + getReasonForStealing() + ".",
96-
structureTile.xyz(),
97-
NotificationArea::NotificationType::Warning});
99+
structure
100+
);
98101
}
99102
}
100103

@@ -125,25 +128,21 @@ void CrimeExecution::stealResources(Structure& structure, const std::array<std::
125128
int amountStolen = calcAmountForStealing(mDifficulty, 2, 5, structure.storage().resources[indexToStealFrom]);
126129
structure.storage().resources[indexToStealFrom] -= amountStolen;
127130

128-
const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);
129-
130-
mNotificationArea.push({
131+
mCrimeEventSignal.emit(
131132
"Resources Stolen",
132133
NAS2D::stringFrom(amountStolen) + " units of " + resourceNames[indexToStealFrom] + " were stolen from a " + structure.name() + ". " + getReasonForStealing() + ".",
133-
structureTile.xyz(),
134-
NotificationArea::NotificationType::Warning});
134+
structure
135+
);
135136
}
136137

137138

138139
void CrimeExecution::vandalize(Structure& structure)
139140
{
140141
mMoraleChanges.push_back(std::make_pair("Vandalism", -1));
141142

142-
const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);
143-
144-
mNotificationArea.push({
143+
mCrimeEventSignal.emit(
145144
"Vandalism",
146145
"A " + structure.name() + " was vandalized.",
147-
structureTile.xyz(),
148-
NotificationArea::NotificationType::Warning});
146+
structure
147+
);
149148
}

OPHD/States/CrimeExecution.h

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "../MapObjects/Structures/FoodProduction.h"
44
#include "../Common.h"
55

6+
#include <NAS2D/Signal/Signal.h>
7+
68
#include <vector>
79
#include <array>
810
#include <map>
@@ -16,21 +18,24 @@ class NotificationArea;
1618
class CrimeExecution
1719
{
1820
public:
19-
CrimeExecution(NotificationArea& notificationArea, const Difficulty& difficulty);
21+
using Signal = NAS2D::Signal<std::string, std::string, const Structure&>;
22+
23+
CrimeExecution(const Difficulty& difficulty);
24+
CrimeExecution(const Difficulty& difficulty, Signal::DelegateType onCrimeEvent);
2025

2126
void executeCrimes(const std::vector<Structure*>& structuresCommittingCrime);
27+
std::vector<std::pair<std::string, int>> moraleChanges() const { return mMoraleChanges; }
28+
Signal::Source& crimeEventSignal() { return mCrimeEventSignal; }
2229

30+
protected:
2331
void stealFood(FoodProduction& structure);
2432
void stealRefinedResources(Structure& structure);
2533
void stealRawResources(Structure& structure);
34+
void stealResources(Structure& structure, const std::array<std::string, 4>& resourceNames);
2635
void vandalize(Structure& structure);
2736

28-
std::vector<std::pair<std::string, int>> moraleChanges() const { return mMoraleChanges; }
29-
3037
private:
3138
const Difficulty& mDifficulty;
32-
NotificationArea& mNotificationArea;
3339
std::vector<std::pair<std::string, int>> mMoraleChanges;
34-
35-
void stealResources(Structure& structure, const std::array<std::string, 4>& resourceNames);
40+
Signal mCrimeEventSignal;
3641
};

OPHD/States/MapViewState.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ const std::map<Difficulty, int> MapViewState::ColonyShipDeorbitMoraleLossMultipl
177177

178178
MapViewState::MapViewState(MainReportsUiState& mainReportsState, const std::string& savegame) :
179179
mCrimeRateUpdate{mDifficulty},
180-
mCrimeExecution{mNotificationArea, mDifficulty},
180+
mCrimeExecution{mDifficulty, {this, &MapViewState::onCrimeEvent}},
181181
mTechnologyReader{"tech0-1.xml"},
182182
mLoadingExisting{true},
183183
mExistingToLoad{savegame},
@@ -198,7 +198,7 @@ MapViewState::MapViewState(MainReportsUiState& mainReportsState, const Planet::A
198198
mDifficulty{selectedDifficulty},
199199
mTileMap{std::make_unique<TileMap>(planetAttributes.mapImagePath, planetAttributes.maxDepth, planetAttributes.maxMines, HostilityMineYields.at(planetAttributes.hostility))},
200200
mCrimeRateUpdate{mDifficulty},
201-
mCrimeExecution{mNotificationArea, mDifficulty},
201+
mCrimeExecution{mDifficulty, {this, &MapViewState::onCrimeEvent}},
202202
mTechnologyReader{"tech0-1.xml"},
203203
mPlanetAttributes{planetAttributes},
204204
mMainReportsState{mainReportsState},

OPHD/States/MapViewState.h

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class MapViewState : public Wrapper
205205

206206
void onMineFacilityExtend(MineFacility* mf);
207207

208+
void onCrimeEvent(std::string title, std::string text, const Structure& structure);
209+
208210
void updatePlayerResources();
209211
void updateResearch();
210212

OPHD/States/MapViewStateEvent.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,16 @@ void MapViewState::onMineFacilityExtend(MineFacility* mineFacility)
251251
mineDepthTile.index(TerrainType::Dozed);
252252
mineDepthTile.excavated(true);
253253
}
254+
255+
256+
void MapViewState::onCrimeEvent(std::string title, std::string text, const Structure& structure)
257+
{
258+
const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);
259+
260+
mNotificationArea.push({
261+
std::move(title),
262+
std::move(text),
263+
structureTile.xyz(),
264+
NotificationArea::NotificationType::Warning
265+
});
266+
}

0 commit comments

Comments
 (0)