Pie Noon
An open source project by FPL.
 All Classes Pages
game_state.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 GAME_STATE_H_
16 #define GAME_STATE_H_
17 
18 #include <memory>
19 #include <vector>
20 #include "character.h"
21 #include "components/cardboard_player.h"
22 #include "components/drip_and_vanish.h"
23 #include "components/player_character.h"
24 #include "components/scene_object.h"
25 #include "components/shakeable_prop.h"
26 #include "corgi/entity.h"
27 #include "corgi/entity_manager.h"
28 #include "game_camera.h"
29 #include "motive/engine.h"
30 #include "motive/processor.h"
31 #include "motive/util.h"
32 #include "particles.h"
33 
34 namespace pindrop {
35 class AudioEngine;
36 } // namespace pindrop
37 
38 namespace fpl {
39 
40 class SceneDescription;
41 
42 namespace pie_noon {
43 
44 struct Config;
45 struct CharacterArrangement;
46 struct EventData;
47 struct ReceivedPie;
48 class MultiplayerDirector;
49 
50 class PieNoonEntityFactory : public corgi::EntityFactoryInterface {
51  public:
52  virtual corgi::EntityRef CreateEntityFromData(
53  const void* data, corgi::EntityManager* entity_manager);
54 };
55 
56 class GameState {
57  public:
58  enum AnalyticsMode { kNoAnalytics, kTrackAnalytics };
59 
60  GameState();
61  ~GameState();
62 
63  // Returns true if the game has reached it's end-game condition.
64  bool IsGameOver() const;
65 
66  // Return to default configuration.
67  // If demo mode is set, no analytics tracking will be performed.
68  void Reset(AnalyticsMode analytics_mode);
69  void Reset();
70 
71  // Update controller and state machine for each character.
72  void AdvanceFrame(WorldTime delta_time, pindrop::AudioEngine* audio_engine);
73 
74  // To be run before starting a game and after ending one to log data about
75  // gameplay.
76  void PreGameLogging() const;
77  void PostGameLogging() const;
78 
79  // Fill in the position of the characters and pies.
80  void PopulateScene(SceneDescription* scene);
81 
82  // Angle between two characters.
83  motive::Angle AngleBetweenCharacters(CharacterId source_id,
84  CharacterId target_id) const;
85 
86  // motive::Angle to the character's target.
87  motive::Angle TargetFaceAngle(CharacterId id) const;
88 
89  // Returns one of the RenderableId enums.
90  uint16_t CharacterState(CharacterId id) const;
91 
92  // Returns an OR of all the character's logical inputs.
93  uint32_t AllLogicalInputs() const;
94 
95  // Returns the number of characters who are still in the game (that is,
96  // are not KO'd or otherwise incapacitated).
97  // By default counts both human players and AI.
98  int NumActiveCharacters(bool human_only = false) const;
99 
100  // Determines which characters are the winners and losers, and increments
101  // their stats appropriately.
102  void DetermineWinnersAndLosers();
103 
104  // Returns true if the character cannot turn, either because the
105  // character has only one valid direction to face, or because the
106  // character is incapacitated.
107  bool IsImmobile(CharacterId id) const;
108 
109  GameCamera& camera() { return camera_; }
110  const GameCamera& camera() const { return camera_; }
111  mathfu::mat4 CameraMatrix() const;
112 
113  std::vector<std::unique_ptr<Character>>& characters() { return characters_; }
114  const std::vector<std::unique_ptr<Character>>& characters() const {
115  return characters_;
116  }
117 
118  std::vector<std::unique_ptr<AirbornePie>>& pies() { return pies_; }
119  const std::vector<std::unique_ptr<AirbornePie>>& pies() const {
120  return pies_;
121  }
122 
123  const CharacterArrangement& arrangement() const { return *arrangement_; }
124 
125  WorldTime time() const { return time_; }
126 
127  void set_config(const Config* config) { config_ = config; }
128 
129  void set_cardboard_config(const Config* config) {
130  cardboard_config_ = config;
131  }
132 
133  motive::MotiveEngine& engine() { return engine_; }
134  ParticleManager& particle_manager() { return particle_manager_; }
135 
136  // Sets up the players in joining mode, where all they can do is jump up
137  // and down.
138  void EnterJoiningMode();
139 
140  WorldTime GetAnimationTime(const Character& character) const;
141 
142  // Sets the MultiplayerDirector we can talk to to propagate some game state
143  // across the network. You must ensure it stays in memory as long as GameState
144  // does.
145  void RegisterMultiplayerDirector(MultiplayerDirector* director) {
146  multiplayer_director_ = director;
147  }
148 
149  void set_is_multiscreen(bool b) { is_multiscreen_ = b; }
150  bool is_multiscreen() const { return is_multiscreen_; }
151 
152  void set_is_in_cardboard(bool b) { is_in_cardboard_ = b; }
153  bool is_in_cardboard() const { return is_in_cardboard_; }
154 
155  void set_use_undistort_rendering(bool b) { use_undistort_rendering_ = b; }
156  bool use_undistort_rendering() { return use_undistort_rendering_; }
157 
158  private:
159  void ProcessSounds(pindrop::AudioEngine* audio_engine,
160  const Character& character, WorldTime delta_time) const;
161  void CreatePie(CharacterId original_source_id, CharacterId source_id,
162  CharacterId target_id, CharacterHealth original_damage,
163  CharacterHealth damage);
164  float CalculatePieYRotation(CharacterId source_id,
165  CharacterId target_id) const;
166  CharacterId DetermineDeflectionTarget(const ReceivedPie& pie) const;
167  void ProcessEvent(pindrop::AudioEngine* audio_engine, Character* character,
168  unsigned int event, const EventData& event_data);
169  void PopulateConditionInputs(ConditionInputs* condition_inputs,
170  const Character& character) const;
171  void PopulateCharacterAccessories(SceneDescription* scene,
172  uint16_t renderable_id,
173  const mathfu::mat4& character_matrix,
174  int num_accessories, int damage,
175  int health) const;
176  void ProcessConditionalEvents(pindrop::AudioEngine* audio_engine,
177  Character* character, EventData* event_data);
178  void ProcessEvents(pindrop::AudioEngine* audio_engine, Character* character,
179  EventData* data, WorldTime delta_time);
180  void UpdatePiePosition(AirbornePie* pie) const;
181  CharacterId CalculateCharacterTarget(CharacterId id) const;
182  float CalculateCharacterFacingAngleVelocity(const Character* character,
183  WorldTime delta_time) const;
184  int RequestedTurn(CharacterId id) const;
185  motive::Angle TiltTowardsStageFront(const motive::Angle angle) const;
186  motive::Angle TiltCharacterAwayFromCamera(CharacterId id,
187  const motive::Angle angle) const;
188  motive::TwitchDirection FakeResponseToTurn(CharacterId id) const;
189  void AddParticlesToScene(SceneDescription* scene) const;
190  void CreatePieSplatter(pindrop::AudioEngine* audio_engine,
191  const Character& character, int damage);
192  void CreateJoinConfettiBurst(const Character& character);
193  void SpawnParticles(const mathfu::vec3& position, const ParticleDef* def,
194  const int particle_count,
195  const mathfu::vec4& base_tint = mathfu::vec4(1, 1, 1, 1));
196  void ShakeProps(float percent, const mathfu::vec3& damage_position);
197  void AddSplatterToProp(corgi::EntityRef prop);
198 
199  WorldTime time_;
200  // countdown_time_ is in seconds and is derived from the length of the game
201  // given in the config file minus the duration of the current game given in
202  // the time_ variable.
203  int countdown_timer_;
204  GameCamera camera_;
205  GameCameraState camera_base_;
206  std::vector<std::unique_ptr<Character>> characters_;
207  std::vector<std::unique_ptr<AirbornePie>> pies_;
208  motive::MotiveEngine engine_;
209  const Config* config_;
210  const CharacterArrangement* arrangement_;
211  ParticleManager particle_manager_;
212  AnalyticsMode analytics_mode_;
213 
214  // Entity manager that tracks all of our entities.
215  corgi::EntityManager entity_manager_;
216  // Entity factory for creating entities from flatbuffers:
217  PieNoonEntityFactory pie_noon_entity_factory_;
218 
219  // Component for handling movable objects in the scene.
220  SceneObjectComponent sceneobject_component_;
221  // Component for handling static, swaying props in the background.
222  ShakeablePropComponent shakeable_prop_component_;
223  // Component for scenery-splatter behavior.
224  DripAndVanishComponent drip_and_vanish_component_;
225  // Component for drawing player characters:
226  PlayerCharacterComponent player_character_component_;
227  // Component for drawing Cardboard mode information.
228  CardboardPlayerComponent cardboard_player_component_;
229 
230  // For multi-screen mode.
231  MultiplayerDirector* multiplayer_director_;
232 
233  // Whether you are playing in multiscreen mode.
234  bool is_multiscreen_;
235 
236  const Config* cardboard_config_;
237  // Whether you are playing in Cardboard mode.
238  bool is_in_cardboard_;
239  // Whether it should use undistortion rendering in Cardboard.
240  bool use_undistort_rendering_;
241 };
242 
243 } // pie_noon
244 } // fpl
245 
246 #endif // GAME_STATE_H_
Definition: scene_object.h:189
Definition: scene_description.h:60
Definition: character.h:55
Definition: character_state_machine.h:28
Definition: game_camera.h:26
Definition: drip_and_vanish.h:38
Definition: game_state.h:56
Definition: player_character.h:43
Definition: game_state.h:50
Definition: particles.h:130
Definition: cardboard_player.h:38
Definition: game_camera.h:51
Definition: multiplayer_director.h:42
Definition: character.h:207
Definition: shakeable_prop.h:38