You are on page 1of 4

/* Start Header ****************************************************************

*/
/*!
/file StateMachine.cpp
/author Team42, Jiawei Gu
/e-mail Team42@digipen.edu, jiawei.gu@digipen.edu
/date 10/04/2015
/Brief Copyright (C) 2015 DigiPen Institute of Technology. Reproduction or
disclosure of this file or its contents without the prior written consent of
DigiPen Institute of Technology is prohibited.
*/
/* End Header ******************************************************************
*/
#include "StateMachine.h"
#include
#include
#include
#include

"Precompiled.h"
"../../Diagnostics/Assert.h"
"../../Behaviors/Behavior.h"
"../../Systems/LogicSystem/LogicSystem.h"

namespace TrillianComponent
{
StateMachine::StateMachine():
states(),
currentState(""),
nextState(""),
status(StateMachineStatus::OnEnter),
IEventListener(TrillianEvent::Priority::Low)
{}
StateMachine::~StateMachine()
{
UnregisterEvents();
for (auto & it : states)
delete it.second;
}
StateMachine & StateMachine::operator= (const StateMachine & stateMachin
e)
{
auto i = stateMachine.states.begin();
for (i; i != stateMachine.states.end(); ++i)
{
TrillianBehavior::Behavior* behaviour = i->second->Copy(
);
states.insert(std::make_pair(i->first, behaviour));
}
currentState = stateMachine.currentState;
events = stateMachine.events;
return *this;
}
void StateMachine::Initialize()
{
RegisterEvents();
}
bool StateMachine::Deserialize(const TrillianSerialization::JsonBlock& b

lockConfig)
{
ASSERT(blockConfig.HasMember("FilePath"), "Missing File Path in
Component: StateMachine.");
std::string filePath = blockConfig["FilePath"].GetString();
TrillianSerialization::JsonDocument document;
bool success = TrillianSerialization::Json::Deserialize(document
, filePath);
ASSERT(success, "Fail to Read Json File: ", filePath);
ASSERT(document.HasMember("Behaviors"), "Missing Behaviors in St
ateMachine: ", filePath);
auto & behaviors = document["Behaviors"];
for (auto behavior = behaviors.Begin(); behavior != behaviors.En
d(); ++behavior)
{
ASSERT(behavior->HasMember("Type"), "Missing Type in Sta
teMachine: ", filePath);
auto & type = (*behavior)["Type"];
std::string name = type.GetString();
TrillianBehavior::Behavior * bhvr = LOGIC_INSTANCE.Creat
eBehavior(name, *behavior);
states[name] = bhvr;
}
ASSERT(blockConfig.HasMember("StartBehavior"), "Missing Start St
ate in Component: StateMachine.");
SetStartState(blockConfig["StartBehavior"].GetString());
if (blockConfig.HasMember("Events"))
{
auto & sEvents = blockConfig["Events"];
for (auto event = sEvents.Begin(); event != sEvents.End(
); ++event)
events.push_back(event->GetString());
}
return true;
}
bool StateMachine::Serialize(TrillianSerialization::JsonBlock& document,
TrillianSerialization::Allocator& allocator)
{
std::map<std::string, TrillianBehavior::Behavior *>::iterator it
;
for (it = states.begin(); it != states.end(); it++)
{
TrillianBehavior::Behavior * behavior = static_cast<Tril
lianBehavior::Behavior *>(it->second);
behavior->Serialize(document, allocator);
}
return true;
}
void StateMachine::RegisterState(TrillianBehavior::Behavior * state)
{
states[state->GetName()] = state;
}
void StateMachine::Update(Ogre::Real dt)

{
if (currentState == "")
return;
switch (status)
{
case StateMachineStatus::OnEnter:
states[currentState]->OnEnter(this->GetOwner());
status = StateMachineStatus::OnUpdate;
break;
case StateMachineStatus::OnUpdate:
nextState = states[currentState]->OnUpdate(this->GetOwne
r(), dt);
if (nextState != currentState)
status = StateMachineStatus::OnExit;
break;
case StateMachineStatus::OnExit:
states[currentState]->OnExit(this->GetOwner());
if (nextState == "CB_Pop")
{
ASSERT(stack.size() != 0, "State Machine Stack S
ize 0");
currentState = stack.back();
stack.pop_back();
}
else if (nextState.substr(0, 3) == "CB_")
{
if (currentState.substr(0, 3) != "CB_")
stack.push_back(currentState);
currentState = nextState;
}
else
currentState = nextState;
status = StateMachineStatus::OnEnter;
break;
}
}
void StateMachine::SetStartState(const std::string & startState)
{
currentState = startState;
}
TrillianUtility::Hashing::Hash StateMachine::GetListenerId() const
{
return GetOwner()->GetId();
}
bool StateMachine::HandleEvent(const TrillianEvent::BaseEvent* event)
{
states[currentState]->OnMessage(GetOwner(), event);
return true;
}
const std::string & StateMachine::GetCurrentState() const
{
return currentState;
}
void StateMachine::RegisterEvents()
{

for (auto & event : events)


Register(event);
}
void StateMachine::UnregisterEvents()
{
for (auto & event : events)
Unregister(event);
}
}

You might also like