diff options
author | Hop311 <Hop3114@gmail.com> | 2023-11-17 10:19:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-17 10:19:53 +0100 |
commit | e76336cd92639f4ec71088fc4c80aea4c25528cd (patch) | |
tree | eeed419a7d97ecb58adf63a17eb9184db3e5ed7a /src/openvic-simulation/economy/BuildingInstance.cpp | |
parent | a00b558a53edb40c9e6789790036f0b618e80ec1 (diff) | |
parent | 886b8b8f396438fc2b7da7d2508f2064d14150a8 (diff) |
Merge pull request #75 from OpenVicProject/accumulated-changes
Accumulated changes
Diffstat (limited to 'src/openvic-simulation/economy/BuildingInstance.cpp')
-rw-r--r-- | src/openvic-simulation/economy/BuildingInstance.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/openvic-simulation/economy/BuildingInstance.cpp b/src/openvic-simulation/economy/BuildingInstance.cpp new file mode 100644 index 0000000..417cdda --- /dev/null +++ b/src/openvic-simulation/economy/BuildingInstance.cpp @@ -0,0 +1,52 @@ +#include "BuildingInstance.hpp" + +using namespace OpenVic; + +BuildingInstance::BuildingInstance(BuildingType const& new_building_type, level_t new_level) + : HasIdentifier { building_type.get_identifier() }, building_type { new_building_type }, level { new_level }, + expansion_state { ExpansionState::CannotExpand } {} + +bool BuildingInstance::_can_expand() const { + return level < building_type.get_max_level(); +} + +void BuildingInstance::set_level(BuildingInstance::level_t new_level) { + level = new_level; +} + +bool BuildingInstance::expand() { + if (expansion_state == ExpansionState::CanExpand) { + expansion_state = ExpansionState::Preparing; + expansion_progress = 0.0f; + return true; + } + return false; +} + +/* REQUIREMENTS: + * MAP-71, MAP-74, MAP-77 + */ +void BuildingInstance::update_state(Date today) { + switch (expansion_state) { + case ExpansionState::Preparing: + start_date = today; + end_date = start_date + building_type.get_build_time(); + break; + case ExpansionState::Expanding: + expansion_progress = static_cast<double>(today - start_date) / static_cast<double>(end_date - start_date); + break; + default: expansion_state = _can_expand() ? ExpansionState::CanExpand : ExpansionState::CannotExpand; + } +} + +void BuildingInstance::tick(Date today) { + if (expansion_state == ExpansionState::Preparing) { + expansion_state = ExpansionState::Expanding; + } + if (expansion_state == ExpansionState::Expanding) { + if (end_date <= today) { + level++; + expansion_state = ExpansionState::CannotExpand; + } + } +} |