Added MachinePool::Update

MachinePool::Update allows a user update the states of a single machine
This commit is contained in:
Erris 2025-04-22 09:12:59 +02:00
parent 7f441153ab
commit c42b0aa5bf
2 changed files with 21 additions and 11 deletions

View File

@ -30,20 +30,25 @@ class StateMachinePool {
void UpdateAll() { void UpdateAll() {
for (const auto &[id, machine] : machines) { for (const auto &[id, machine] : machines) {
auto &owner = machine->GetOwner(); Update(id);
const std::string &current = machine->GetCurrentStateId();
auto it = transitions.find(current);
if (it != transitions.end() && auto_transition == true)
for (const auto &transition : it->second)
if (transition.condition(owner)) {
machine->ChangeState(transition.target_state);
break;
}
machine->Update();
} }
} }
void Update(const std::string &id) {
auto &machine = machines.find(id);
auto &owner = machine->GetOwner();
const std::string &current = machine->GetCurrentStateId();
auto it = transitions.find(current);
if (it != transitions.end() && auto_transition == true)
for (const auto &transition : it->second)
if (transition.condition(owner)) {
machine->ChangeState(transition.target_state);
break;
}
machine->Update();
}
void ChangeState(const std::string &id, const std::string &stateId) { void ChangeState(const std::string &id, const std::string &stateId) {
auto it = machines.find(id); auto it = machines.find(id);
const std::string current = it->second->GetCurrentStateId(); const std::string current = it->second->GetCurrentStateId();

View File

@ -43,6 +43,11 @@ class StateSystem {
GetPool<T>().UpdateAll(); GetPool<T>().UpdateAll();
} }
template <typename T>
void Update(const std::string &id) {
GetPool<T>().Update(id);
}
template <typename T> template <typename T>
json Serialize() const { json Serialize() const {
StateMachinePool<T> &pool = GetPool<T>(); StateMachinePool<T> &pool = GetPool<T>();