2024-05-17 13:04:36 +00:00
|
|
|
#include "statemachine.h"
|
|
|
|
#include "station.h"
|
2024-05-17 13:37:03 +00:00
|
|
|
#include "context.h"
|
2024-05-17 13:04:36 +00:00
|
|
|
|
|
|
|
StateMachine::StateMachine(State* initialState)
|
|
|
|
: currentState(initialState) {}
|
|
|
|
|
|
|
|
StateMachine::~StateMachine() {
|
|
|
|
delete currentState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateMachine::updateState(State* newState) {
|
|
|
|
if (newState != currentState) {
|
|
|
|
delete currentState;
|
|
|
|
currentState = newState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateMachine::pickedUp() {
|
|
|
|
State* newState = currentState->pickedUp();
|
|
|
|
updateState(newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateMachine::putDown(Station* newStation) {
|
|
|
|
State* newState = currentState->putDown(newStation);
|
|
|
|
updateState(newState);
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:37:03 +00:00
|
|
|
void StateMachine::update(Context context) {
|
|
|
|
State* newState = currentState->update(context);
|
2024-05-17 13:04:36 +00:00
|
|
|
updateState(newState);
|
|
|
|
}
|