Skip to content

Commit

Permalink
Use Signal for crime events
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRStevens committed Jan 11, 2025
1 parent e22e73d commit b3edae7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
29 changes: 11 additions & 18 deletions OPHD/States/CrimeExecution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ namespace
}


CrimeExecution::CrimeExecution(NotificationArea& notificationArea, const Difficulty& difficulty) :
mDifficulty{difficulty},
mNotificationArea{notificationArea}
CrimeExecution::CrimeExecution(const Difficulty& difficulty) :
mDifficulty{difficulty}
{
}

Expand Down Expand Up @@ -88,13 +87,11 @@ void CrimeExecution::stealFood(FoodProduction& structure)
int foodStolen = calcAmountForStealing(mDifficulty, 5, 15, structure.foodLevel());
structure.foodLevel(structure.foodLevel() - foodStolen);

const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);

mNotificationArea.push({
mCrimeEventSignal.emit(
"Food Stolen",
NAS2D::stringFrom(foodStolen) + " units of food was pilfered from a " + structure.name() + ". " + getReasonForStealing() + ".",
structureTile.xyz(),
NotificationArea::NotificationType::Warning});
structure
);
}
}

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

const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);

mNotificationArea.push({
mCrimeEventSignal.emit(
"Resources Stolen",
NAS2D::stringFrom(amountStolen) + " units of " + resourceNames[indexToStealFrom] + " were stolen from a " + structure.name() + ". " + getReasonForStealing() + ".",
structureTile.xyz(),
NotificationArea::NotificationType::Warning});
structure
);
}


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

const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);

mNotificationArea.push({
mCrimeEventSignal.emit(
"Vandalism",
"A " + structure.name() + " was vandalized.",
structureTile.xyz(),
NotificationArea::NotificationType::Warning});
structure
);
}
10 changes: 7 additions & 3 deletions OPHD/States/CrimeExecution.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "../MapObjects/Structures/FoodProduction.h"
#include "../Common.h"

#include <NAS2D/Signal/Signal.h>

#include <vector>
#include <array>
#include <map>
Expand All @@ -16,11 +18,13 @@ class NotificationArea;
class CrimeExecution
{
public:
CrimeExecution(NotificationArea& notificationArea, const Difficulty& difficulty);
using Signal = NAS2D::Signal<std::string, std::string, const Structure&>;

void executeCrimes(const std::vector<Structure*>& structuresCommittingCrime);
CrimeExecution(const Difficulty& difficulty);

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

protected:
void stealFood(FoodProduction& structure);
Expand All @@ -31,6 +35,6 @@ class CrimeExecution

private:
const Difficulty& mDifficulty;
NotificationArea& mNotificationArea;
std::vector<std::pair<std::string, int>> mMoraleChanges;
Signal mCrimeEventSignal;
};
6 changes: 4 additions & 2 deletions OPHD/States/MapViewState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const std::map<Difficulty, int> MapViewState::ColonyShipDeorbitMoraleLossMultipl

MapViewState::MapViewState(MainReportsUiState& mainReportsState, const std::string& savegame) :
mCrimeRateUpdate{mDifficulty},
mCrimeExecution{mNotificationArea, mDifficulty},
mCrimeExecution{mDifficulty},
mTechnologyReader{"tech0-1.xml"},
mLoadingExisting{true},
mExistingToLoad{savegame},
Expand All @@ -191,14 +191,15 @@ MapViewState::MapViewState(MainReportsUiState& mainReportsState, const std::stri
{
ccLocation() = CcNotPlaced;
NAS2D::Utility<NAS2D::EventHandler>::get().windowResized().connect({this, &MapViewState::onWindowResized});
mCrimeExecution.crimeEventSignal().connect({this, &MapViewState::onCrimeEvent});
}


MapViewState::MapViewState(MainReportsUiState& mainReportsState, const Planet::Attributes& planetAttributes, Difficulty selectedDifficulty) :
mDifficulty{selectedDifficulty},
mTileMap{std::make_unique<TileMap>(planetAttributes.mapImagePath, planetAttributes.maxDepth, planetAttributes.maxMines, HostilityMineYields.at(planetAttributes.hostility))},
mCrimeRateUpdate{mDifficulty},
mCrimeExecution{mNotificationArea, mDifficulty},
mCrimeExecution{mDifficulty},
mTechnologyReader{"tech0-1.xml"},
mPlanetAttributes{planetAttributes},
mMainReportsState{mainReportsState},
Expand All @@ -217,6 +218,7 @@ MapViewState::MapViewState(MainReportsUiState& mainReportsState, const Planet::A
setMeanSolarDistance(mPlanetAttributes.meanSolarDistance);
ccLocation() = CcNotPlaced;
NAS2D::Utility<NAS2D::EventHandler>::get().windowResized().connect({this, &MapViewState::onWindowResized});
mCrimeExecution.crimeEventSignal().connect({this, &MapViewState::onCrimeEvent});
}


Expand Down
2 changes: 2 additions & 0 deletions OPHD/States/MapViewState.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ class MapViewState : public Wrapper

void onMineFacilityExtend(MineFacility* mf);

void onCrimeEvent(std::string title, std::string text, const Structure& structure);

void updatePlayerResources();
void updateResearch();

Expand Down
13 changes: 13 additions & 0 deletions OPHD/States/MapViewStateEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,16 @@ void MapViewState::onMineFacilityExtend(MineFacility* mineFacility)
mineDepthTile.index(TerrainType::Dozed);
mineDepthTile.excavated(true);
}


void MapViewState::onCrimeEvent(std::string title, std::string text, const Structure& structure)
{
const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);

mNotificationArea.push({
std::move(title),
std::move(text),
structureTile.xyz(),
NotificationArea::NotificationType::Warning
});
}

0 comments on commit b3edae7

Please sign in to comment.