VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
Game.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef GAME_H
18 #define GAME_H
19 
20 #include <QList>
21 #include "PlayerProfile.h"
22 
24 class LevelProgression;
26 class QObject;
27 class UiInternal;
28 
35 class Game : public QObject {
36  Q_OBJECT
37 public:
38  virtual ~Game();
39 
47  void init();
51  bool isUiActive() const { return mIsUiActive; }
55  LevelProgressionList* getLevelProgressionList() const { return mLevelProgressionList; }
62  void tryLoadLevel();
66  DebugMetricsMonitor* getDebugMetricsMonitor() const { return mDebugMetricsMonitor; }
72  void updatePlayerScore(int playerId, int amountChanged);
73 
77  Q_INVOKABLE UiInternal* getUi() const { return mUi; }
83  return mCurrentLevelProgression;
84  }
91  Q_INVOKABLE LevelInfo* getCurrentLevelInfo() const;
98  Q_INVOKABLE bool isEnvironmentUnlocked(const QString& progressionName,
99  const QString& environmentName) const;
105  Q_INVOKABLE bool isLevelUnlocked(const QString& progressionName,
106  const QString& levelName) const;
115  Q_INVOKABLE int getNumStars(const QString& progressionName, const QString& levelName) const;
120  Q_INVOKABLE int getLastPlayedEnvironmentIndex(const QString& progressionName) const;
126  Q_INVOKABLE int getLastPlayedLevelIndexInEnvironment(const QString& progressionName,
127  const QString& environmentName) const;
135  Q_INVOKABLE void unlockAchievement(const QString& name, bool immediately = true);
144  Q_INVOKABLE void incrementAchievement(const QString& name, int numSteps,
145  bool immediately = false);
155  Q_INVOKABLE void setAchievementSteps(const QString& name, int minSteps,
156  bool immediately = false);
164  Q_INVOKABLE void revealAchievement(const QString& name, bool immediately = true);
165 
169  static Game* getInstance();
170 signals:
171 
184  void levelCompleted(bool gameIsOver, int oneStarThreshold, int twoStarThreshold,
185  int threeStarThreshold, int fourStarThreshold, int playerScore);
186 
192  void playerScoreChanged(int playerId, int score);
193 
194 public slots:
198  void onLevelCompleted();
199 
204  void onSignedIntoCloudChanged(bool signedIntoCloud);
205 
211  void onCloudDataLoaded(int statusCode, const QString& data);
212 
219  QString onCloudDataConflict(const QString& localData, const QString& serverData);
220 
221 private slots:
222  void onUiActiveChanged(bool isActive);
223  void onChangeLevelProgressionRequested();
224  void onChangeLevelRequested();
225  void onOpeningCinematicCompleted();
226  void onPlayLevelRequested(LevelProgression* levelProgression, const QString& levelName);
227  void onPlayNextLevelRequested();
228  void onQuitRequested();
229  void onSignIntoCloudRequested();
230  void onMuteBGMRequested();
231  void onUnmuteBGMRequested();
232  void onBeforeUpdate();
233  void onAfterUpdate();
234  void onLevelChanged();
235 
236 private:
237  struct ScoringInfo {
238  int oneStarThreshold;
239  int twoStarThreshold;
240  int threeStarThreshold;
241  int perfectScore;
242  };
243 
244  Game(QObject* parent = nullptr);
245 
246  ScoringInfo getScoringInfo() const;
247  int getStarScore(int playerScore) const;
248  QString getLevelTrackerTag(const QString& eventName) const;
249  void abandonCurrentLevel();
250 
251  DebugMetricsMonitor* mDebugMetricsMonitor = nullptr;
252  bool mIsInitialized = false;
253  bool mIsUiActive = false;
254  volatile bool mIsPlayingLevel = false;
255  LevelProgressionList* mLevelProgressionList = nullptr;
256  LevelProgression* mCurrentLevelProgression = nullptr;
257  PlayerProfile mPlayerProfile;
258  UiInternal* mUi = nullptr;
259  // Trigger Energy Crisis achievement by collecting mScoreUpdateComboAchievement points in under
260  // mTimeForComboAchievement seconds.
261  int mScoreUpdateComboAchievement = 10;
262  float mTimeForComboAchievement = 5.0f;
263  // Event queue counter for the number of score updates that have occured over the last
264  // mTimeForComboAchievement seconds.
265  QList<long long> mScoreUpdateTimes;
266 
267  static Game* sInstance;
268 
269  // A [0,1) value that describes where between completionThreshold and activationThreshold a two
270  // star score is awarded. A value of 0 grants two stars at exactly the completionThreshold and
271  // above. A value of 0.5 would grant two stars for a score greater than or equal to the average
272  // of completionThreshold and activationThreshold.
273  static constexpr float TWO_STAR_THRESHOLD_FACTOR = 0.35f;
274  // A [0,1) value that describes where between completionThreshold and the calculated
275  // twoStarThreshold a three star score is awarded. A value of 0.5 would grant three stars for
276  // a score greater than or equal to the average of completionThreshold and the calculated
277  // twoStarThreshold.
278  static constexpr float THREE_STAR_THRESHOLD_FACTOR = 0.5f;
279 };
280 Q_DECLARE_METATYPE(Game*)
281 #endif
LevelProgressionList * getLevelProgressionList() const
Returns the unique LevelProgressionList for the game.
Definition: Game.h:55
Q_INVOKABLE LevelInfo * getCurrentLevelInfo() const
Returns the current LevelInfo selected by the player.
Q_INVOKABLE int getLastPlayedLevelIndexInEnvironment(const QString &progressionName, const QString &environmentName) const
Returns the index of the last played level in a given environment.
Q_INVOKABLE bool isLevelUnlocked(const QString &progressionName, const QString &levelName) const
Returns whether or not the specified level is unlocked in the specified progression.
Tracks and provides mechanisms for reporting various debug metrics.
Definition: DebugMetricsMonitor.h:32
Internal backing C++ class for the Ui QML item.
Definition: UiInternal.h:34
void onLevelCompleted()
Respond to a level being completed.
void updatePlayerScore(int playerId, int amountChanged)
Updates score and completes level if appropriate.
static Game * getInstance()
Gets the singleton Game.
Metadata for a Level.
Definition: LevelInfo.h:31
void init()
Initialize the game.
void levelCompleted(bool gameIsOver, int oneStarThreshold, int twoStarThreshold, int threeStarThreshold, int fourStarThreshold, int playerScore)
Emitted on level completed.
Grouping of related Environments together into a logical, ordered list.
Definition: LevelProgression.h:34
Q_INVOKABLE int getLastPlayedEnvironmentIndex(const QString &progressionName) const
Returns the index of the environment last played in the specified progression.
QString onCloudDataConflict(const QString &localData, const QString &serverData)
Respond to a conflict in the loading of GPGS cloud data.
DebugMetricsMonitor * getDebugMetricsMonitor() const
Returns the unique DebugMetricsMonitor for the game.
Definition: Game.h:66
Q_INVOKABLE LevelProgression * getCurrentLevelProgression() const
Returns the current LevelProgression selected by the player.
Definition: Game.h:82
The top-level Game object for VoltAir.
Definition: Game.h:35
Q_INVOKABLE UiInternal * getUi() const
Returns the main UI element.
Definition: Game.h:77
bool isUiActive() const
Returns if any of the UI screens are active.
Definition: Game.h:51
Represents a player's profile or "save game" state.
Definition: PlayerProfile.h:44
Collection of LevelProgressions.
Definition: LevelProgressionList.h:31
void onSignedIntoCloudChanged(bool signedIntoCloud)
Respond to a change to the state of GPGS cloud sign-in.
Q_INVOKABLE void setAchievementSteps(const QString &name, int minSteps, bool immediately=false)
Sets a Google Play Games Services incremental achievement to have a minimum number of steps...
void playerScoreChanged(int playerId, int score)
Emitted when a player's score changes.
void onCloudDataLoaded(int statusCode, const QString &data)
Respond to the GPGS cloud data being loaded.
Q_INVOKABLE int getNumStars(const QString &progressionName, const QString &levelName) const
Returns the highest number of stars achieved on a level, as logged in the PlayerProfile.
void tryLoadLevel()
Informs the engine to load the current level.
Q_INVOKABLE void incrementAchievement(const QString &name, int numSteps, bool immediately=false)
Increments a Google Play Games Services incremental achievement.
QObject * parent() const
Q_INVOKABLE bool isEnvironmentUnlocked(const QString &progressionName, const QString &environmentName) const
Returns whether or not the specified environment is unlocked in the specified progression.
Q_INVOKABLE void revealAchievement(const QString &name, bool immediately=true)
Reveals a Google Play Games Services achievement.
Q_INVOKABLE void unlockAchievement(const QString &name, bool immediately=true)
Unlocks a Google Play Games Services achievement.