StateControl.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef H_DC_STATE_CONTROL_COMPONENT_INCLUDED
34 #define H_DC_STATE_CONTROL_COMPONENT_INCLUDED
35 
36 #include <Dynacoe/Component.h>
37 #include <string>
38 #include <map>
39 
40 namespace Dynacoe {
41 
51 class StateControl : public Component {
52  public:
53  StateControl();
54 
55 
78  struct StateLoop {
79 
83  Component::EventHandler draw=nullptr,
84  Component::EventHandler init=nullptr,
85  void * data_=nullptr) : Step(step), Draw(draw), Init(init), data(data_){}
86 
90  void * data;
91 
92  bool operator==(const StateLoop & other) {
93  return (Step == other.Step &&
94  Draw == other.Draw &&
95  Init == other.Init &&
96  data == other.data);
97  }
98 
99  };
100 
107  void CreateState(const std::string & tag, StateLoop);
108 
115  void ConnectState(const std::string & tag, StateLoop);
116 
120  void RemoveState(const std::string & tag);
121 
126  void RemoveLoop(const std::string & tag, StateLoop);
127 
133  void Execute(const std::string & beginState);
134 
139  void Halt();
140 
143  bool IsHalted();
144 
147  std::string GetCurrentState();
148 
149 
150 
151  void OnStep();
152  void OnDraw();
153  std::string GetInfo();
154  private:
155  std::map<std::string, std::vector<StateLoop>> states;
156  std::string current;
157  bool midTerminate;
158  bool queuedInit;
159 };
160 }
161 
162 #endif
void OnDraw()
Function that is called upon each Draw iteration. Component Draw()s are run before the entity's draw ...
void RemoveLoop(const std::string &tag, StateLoop)
Specifically removes the given stateloop if associated with the given state tag.
StateLoop(Component::EventHandler step=nullptr, Component::EventHandler draw=nullptr, Component::EventHandler init=nullptr, void *data_=nullptr)
Creates a new StateLoop.
Definition: StateControl.h:82
bool(*)(void *, Component *component, Entity::ID self, Entity::ID source, const std::vector< std::string > &args) EventHandler
A handler for an event.
Definition: Component.h:132
void Halt()
stops all current execution.
void ConnectState(const std::string &tag, StateLoop)
Adds an additional StateLoop to be associated with a state.
void CreateState(const std::string &tag, StateLoop)
Sets up a new valid state.
Definition: AssetID.h:37
std::string GetInfo()
Returns a string containing human-readable information on the state of the component.
void Execute(const std::string &beginState)
Begins the state machine execution loop from the given state.
bool IsHalted()
Returns whether or not the machine is in a halted state.
Designates execution instructions for a state.
Definition: StateControl.h:78
Adds a state machine mechanism to the entity.
Definition: StateControl.h:51
std::string GetCurrentState()
Returns the current state tag. If no execution state is active, the empty string is returned...
void OnStep()
Function that is called upon each Run iteration. Component Run()s are run before the entity's run fun...
Class that extends the functionality of an Entity, but as a removable and addable object...
Definition: Component.h:66
void RemoveState(const std::string &tag)
Removes the state.