Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StateMachine workaround without SubclassOf #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions src/Util/StateMachine/StateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,18 @@ TickType_t StateMachine::getEventScanningTime() const noexcept{
return 0;
}

void StateMachine::setStartingStateType(const SubclassOf<State>& type) noexcept{
if(current.isValid()){
return;
}

if(type == nullptr){
return;
}

startingStateType = type;
}

void StateMachine::tick(float deltaTime) noexcept{
Super::tick(deltaTime);

if(current.isValid()){
const SubclassOf<State> nextStateType = current->transitionTo();

const auto nextStateType = current->transitionTo();
if(nextStateType != nullptr){
const Class* previousType = current->getStaticClass();

current->onTransitionTo(*nextStateType);
current->onTransitionTo(nextStateType);
current->destroy();
current = newObject<State>(*nextStateType, this);
current = newObject<State>(nextStateType, this);
current->onTransitionFrom(previousType);
}
}else if(startingStateType != nullptr) {
Expand Down
9 changes: 8 additions & 1 deletion src/Util/StateMachine/StateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ class StateMachine : public AsyncEntity {
* @brief Sets the starting state type and triggers the state machine to start ticking.
* @param type The type of the starting state.
*/
void setStartingStateType(const SubclassOf<State>& type) noexcept;
template<typename T, typename ...Args, typename = std::enable_if<std::derived_from<T, State>, T>::type>
void setStartingStateType(Args&&... args) noexcept{
if(current.isValid()){
return;
}

current = newObject<T>(this, std::forward<Args>(args)...);
}

protected:
/**
Expand Down