| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- // ----------------------------------------------------------------
- // From Game Programming in C++ by Sanjay Madhav
- // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
- //
- // Released under the BSD License
- // See LICENSE in root directory for full details.
- // ----------------------------------------------------------------
- #include "AIComponent.h"
- #include "Actor.h"
- #include "AIState.h"
- #include <SDL/SDL_log.h>
- AIComponent::AIComponent(class Actor* owner)
- :Component(owner)
- ,mCurrentState(nullptr)
- {
- }
- void AIComponent::Update(float deltaTime)
- {
- if (mCurrentState)
- {
- mCurrentState->Update(deltaTime);
- }
- }
- void AIComponent::ChangeState(const std::string& name)
- {
- // First exit the current state
- if (mCurrentState)
- {
- mCurrentState->OnExit();
- }
-
- // Try to find the new state from the map
- auto iter = mStateMap.find(name);
- if (iter != mStateMap.end())
- {
- mCurrentState = iter->second;
- // We're entering the new state
- mCurrentState->OnEnter();
- }
- else
- {
- SDL_Log("Could not find AIState %s in state map", name.c_str());
- mCurrentState = nullptr;
- }
- }
- void AIComponent::RegisterState(AIState* state)
- {
- mStateMap.emplace(state->GetName(), state);
- }
|