NerfUS game coordinator for the Nerf gun firing range

Dependencies:   HardwareInterface mbed-rtos mbed

Fork of NerfUS by NerfUS

Files at this revision

API Documentation at this revision

Comitter:
Ismael Balafrej
Date:
Fri Mar 17 17:19:42 2017 -0400
Branch:
PlayableGame
Parent:
16:5e6c695468b6
Child:
18:469c8b2a9af9
Commit message:
First Commit of PlayableGame

Changed in this revision

include/GameModes/PrecisionMode.hpp Show annotated file Show diff for this revision Revisions of this file
include/PlayableGame.hpp Show annotated file Show diff for this revision Revisions of this file
include/Target.hpp Show annotated file Show diff for this revision Revisions of this file
source/GameModes/PrecisionMode.cpp Show annotated file Show diff for this revision Revisions of this file
source/PlayableGame.cpp Show annotated file Show diff for this revision Revisions of this file
source/RandomNumberGenerator.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/GameModes/PrecisionMode.hpp	Fri Mar 17 17:19:42 2017 -0400
@@ -0,0 +1,18 @@
+#pragma once
+#include "PlayableGame.hpp"
+#include "Target.hpp"
+
+class PrecisionMode : PlayableGame
+{
+public:
+  ~PrecisionMode();
+
+  TargetInfo GetNextTarget();
+  void OnTargetHit(int timeTaken);
+  void OnTargetMiss();
+  GameStats GetStats();
+
+private:
+  int currentTarget = 0;
+  int numberOfRounds = 5;
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/PlayableGame.hpp	Fri Mar 17 17:19:42 2017 -0400
@@ -0,0 +1,43 @@
+#pragma once
+
+#include <vector>
+#include "Target.hpp"
+#include "RandomNumberGenerator.hpp"
+
+struct GameStats
+{
+  int timeTaken; //ms
+  int numberOfHits;
+  int numberOfMiss;
+  int accuracy;             //4 digits percentage to avoid a float
+  int averageTimePerTarget; //ms
+};
+
+//Abstract class, must be implemented as a GameMode
+class PlayableGame
+{
+public:
+  PlayableGame(std::vector<TargetInfo> *targets, RandomNumberGenerator& random_number_generator);
+  ~PlayableGame();
+
+  virtual bool IsWeaponValid(int weaponId); //Default to any Weapons
+  virtual int GetPoints();
+  virtual GameStats GetStats();
+
+  virtual TargetInfo GetNextTarget() = 0;
+  void Start(); //Create a thread that will call GetNextTarget at set speed
+  void Stop(); //Stop the thread
+
+  void TargetMsgReceived(int )
+
+protected:
+  int points;
+  GameStats stats;
+  TargetInfo GetRandomTarget(int timeout_ms );
+
+  virtual void OnTargetHit(int timeTaken) = 0;
+  virtual void OnTargetMiss() = 0;
+
+private:
+  RandomNumberGenerator& random_number_generator;
+};
\ No newline at end of file
--- a/include/Target.hpp	Thu Feb 23 21:22:57 2017 -0500
+++ b/include/Target.hpp	Fri Mar 17 17:19:42 2017 -0400
@@ -13,6 +13,8 @@
     TargetType type;
     int timeout_ms;
 
+    void riseTarget();
+
     bool operator==(const TargetInfo& other) const
     {
         return (id == other.id)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/GameModes/PrecisionMode.cpp	Fri Mar 17 17:19:42 2017 -0400
@@ -0,0 +1,37 @@
+#include "PrecisionMode.hpp"
+
+TargetInfo PrecisionMode::GetNextTarget()
+{
+    if (numberOfRounds-- == 0) {
+        return NULL;
+    }
+    TargetInfo new_target;
+
+    currentTarget = (currentTarget + 1) % NUMBER_OF_TARGETS;
+    new_target.id = currentTarget;
+    new_target.type = TARGET_TYPE_ENEMY;
+    new_target.timeout_ms = 30000; //30 seconds
+
+    return new_target;
+}
+
+void PrecisionMode::OnTargetHit(int timeTaken)
+{
+    stats.numberOfHits++;
+    points += 2;
+}
+void PrecisionMode::OnTargetMiss()
+{
+    stats.numberOfMiss++;
+    points -= 5;
+}
+
+GameStats PrecisionMode::GetStats()
+{
+    //Seems pretty basic... move to PlayableGame class ?
+    
+    //TODO: Complete the stats
+    //stats.timeTaken
+    //stats.averageTimePerTarget
+    return stats;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/PlayableGame.cpp	Fri Mar 17 17:19:42 2017 -0400
@@ -0,0 +1,33 @@
+#include "PlayableGame.hpp"
+
+PlayableGame::PlayableGame(RandomNumberGenerator& random_number_generator) :
+    random_number_generator(random_number_generator)
+{
+}
+
+bool PlayableGame::IsWeaponValid(int weaponId)
+{
+    return true;
+}
+
+TargetInfo PlayableGame::GetRandomTarget(int timeout_ms)
+{
+    TargetInfo new_target;
+
+    new_target.id = random_number_generator.get(0, NUMBER_OF_TARGETS);
+    new_target.type = random_number_generator.get(0, 1) == 0 ? TARGET_TYPE_ALLY : TARGET_TYPE_ENEMY;
+    new_target.timeout_ms = timeout_ms;
+
+    return new_target;
+}
+
+int PlayableGame::GetPoints()
+{
+    return points;
+}
+
+GameStats PlayableGame::GetStats()
+{
+    stats.accuracy = stats.numberOfHits * 10000 / (stats.numberOfHits + stats.numberOfMiss);
+    return stats;
+}
\ No newline at end of file
--- a/source/RandomNumberGenerator.cpp	Thu Feb 23 21:22:57 2017 -0500
+++ b/source/RandomNumberGenerator.cpp	Fri Mar 17 17:19:42 2017 -0400
@@ -4,6 +4,8 @@
 {
     //TODO: Implement this. For tests, inject a known seed in the constructor
     //(instead of "time(NULL)", which we might use in the real app)
+
+    //What abount rand() ?
     return 42;
 }