Pie Noon
An open source project by FPL.
 All Classes Pages
character_state_machine.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CHARACTER_STATE_MACHINE_
16 #define CHARACTER_STATE_MACHINE_
17 
18 #include <cstdint>
19 #include "common.h"
20 
21 namespace fpl {
22 namespace pie_noon {
23 
24 struct CharacterStateMachineDef;
25 struct CharacterState;
26 struct Condition;
27 
29  // A set of bits that the state machine will compare against when following
30  // transitions.
31  int32_t is_down;
32  int32_t went_down;
33  int32_t went_up;
34 
35  // The elapsed time of the animation.
36  int animation_time;
37 
38  // The current world time
39  WorldTime current_time;
40 
41  bool is_multiscreen;
42 };
43 
45  public:
46  // Initializes a state machine with the given state machine definition.
47  // This class does not take ownership of the definition.
49  const CharacterStateMachineDef* const state_machine_def);
50 
51  // Resets back to initial conditions. Assumes time is reseting to 0 too.
52  void Reset();
53 
54  // Updates the current state of the state machine.
55  //
56  // inputs is a structure containing the game data that can affect whether or
57  // not a state transition occurs
58  void Update(const ConditionInputs& inputs);
59 
60  const CharacterState* current_state() const { return current_state_; }
61 
62  void SetCurrentState(int new_stateId, WorldTime state_start_time);
63 
64  WorldTime current_state_start_time() const {
65  return current_state_start_time_;
66  }
67 
68  private:
69  const CharacterStateMachineDef* state_machine_def_;
70  const CharacterState* current_state_;
71  WorldTime current_state_start_time_;
72 };
73 
74 bool EvaluateCondition(const Condition* condition,
75  const ConditionInputs& inputs);
76 
77 // Returns true if the state machine is valid. A valid state machine contains
78 // a single state for each state id declared in the StateId enum, and in the
79 // same order.
80 //
81 // If the state machine is invalid, returns false and prints an logs an error to
82 // SDL's error interface.
83 bool CharacterStateMachineDef_Validate(
84  const CharacterStateMachineDef* const state_machine_def);
85 
86 } // pie_noon
87 } // fpl
88 
89 #endif // CHARACTER_STATE_MACHINE_
Definition: character_state_machine.h:28
Definition: character_state_machine.h:44