AirsoftTimer software based on mbed

Dependencies:   mbed TextLCD keypad

Files at this revision

API Documentation at this revision

Comitter:
sillevl
Date:
Sat May 23 17:48:03 2015 +0000
Parent:
13:ece97a1108cc
Child:
15:78116b7254d5
Commit message:
added ObjectiveFactory class and small refactoring on game

Changed in this revision

Airsofttimer.cpp Show annotated file Show diff for this revision Revisions of this file
games/DummyGame.cpp Show annotated file Show diff for this revision Revisions of this file
games/Game.cpp Show annotated file Show diff for this revision Revisions of this file
games/Game.h Show annotated file Show diff for this revision Revisions of this file
games/GameFactory.cpp Show annotated file Show diff for this revision Revisions of this file
games/GameFactory.h Show annotated file Show diff for this revision Revisions of this file
objectives/Objective.cpp Show annotated file Show diff for this revision Revisions of this file
objectives/Objective.h Show annotated file Show diff for this revision Revisions of this file
objectives/ObjectiveFactory.cpp Show annotated file Show diff for this revision Revisions of this file
objectives/ObjectiveFactory.h Show annotated file Show diff for this revision Revisions of this file
objectives/WaitForKeyObjective.cpp Show annotated file Show diff for this revision Revisions of this file
objectives/WaitForKeyObjective.h Show annotated file Show diff for this revision Revisions of this file
--- a/Airsofttimer.cpp	Sat May 23 17:15:42 2015 +0000
+++ b/Airsofttimer.cpp	Sat May 23 17:48:03 2015 +0000
@@ -36,7 +36,7 @@
     while(true){
         // first we need to select a game from the available games list
         int game_number = select_game();
-        Game* game = GameFactory::create_game(board, game_number);
+        Game* game = GameFactory::create(board, game_number);
         game->setup();
         game->run();
         delete game;
--- a/games/DummyGame.cpp	Sat May 23 17:15:42 2015 +0000
+++ b/games/DummyGame.cpp	Sat May 23 17:48:03 2015 +0000
@@ -1,5 +1,5 @@
 #include "DummyGame.h"
-#include "../objectives/WaitForKeyObjective.h"
+#include "../objectives/ObjectiveFactory.h"
 
 const char* NAME = "DummyGame";
 
@@ -8,7 +8,7 @@
 }
 
 void DummyGame::run(){ 
-    WaitForKeyObjective* objective = new WaitForKeyObjective();
+    Objective* objective = new ObjectiveFactory->create(this, 0);
     objective->start();
 }
 
--- a/games/Game.cpp	Sat May 23 17:15:42 2015 +0000
+++ b/games/Game.cpp	Sat May 23 17:48:03 2015 +0000
@@ -14,9 +14,29 @@
     leds->off(Leds::ALL);
 }
 
+Board* Game::getBoard(){
+    return board;
+}
+
 uint32_t Game::keyEvent(uint32_t key){
     // play key pressed sound ?
     // do nothing by default
     return 0;
 }
 
+void Game::init(){
+    board->lcd->cls();
+    board->lcd->printf("Starting game");
+    board->lcd->locate(1,0);
+    board->lcd->printf("DummyGame");
+    wait(3);
+}
+
+void Game::end(){
+    board->lcd->cls();
+    board->lcd->printf("Ending game");
+    board->lcd->locate(1,0);
+    board->lcd->printf("DummyGame");
+    wait(5);
+}
+
--- a/games/Game.h	Sat May 23 17:15:42 2015 +0000
+++ b/games/Game.h	Sat May 23 17:48:03 2015 +0000
@@ -11,6 +11,8 @@
     virtual void setup() = 0;
     virtual void run() = 0;
     
+    Board* getBoard();
+    
     protected:
     Board* board;
     
@@ -23,6 +25,9 @@
     
     uint32_t keyEvent(uint32_t key);
     
+    void init();
+    void end();
+    
 };
 
 
--- a/games/GameFactory.cpp	Sat May 23 17:15:42 2015 +0000
+++ b/games/GameFactory.cpp	Sat May 23 17:48:03 2015 +0000
@@ -1,7 +1,7 @@
 #include "GameFactory.h"
 #include "DummyGame.h"
 
-Game* GameFactory::create_game(Board* board, int choice){
+Game* GameFactory::create(Board* board, int choice){
     Game* game = new DummyGame(board);
     return  game;
 }
\ No newline at end of file
--- a/games/GameFactory.h	Sat May 23 17:15:42 2015 +0000
+++ b/games/GameFactory.h	Sat May 23 17:48:03 2015 +0000
@@ -6,7 +6,7 @@
 
 class GameFactory{
     public:
-    static Game* create_game(Board* board, int choice); //factory method
+    static Game* create(Board* board, int choice); //factory method
 
 };
 
--- a/objectives/Objective.cpp	Sat May 23 17:15:42 2015 +0000
+++ b/objectives/Objective.cpp	Sat May 23 17:48:03 2015 +0000
@@ -1,7 +1,16 @@
 
 #include "Objective.h"
 
-Objective::Objective(){
+Objective::Objective(Game* game){
+    this->game = game;
+    Board* board = game->getBoard();
+    lcd = board->lcd;
+    leds = board->leds;
+    key = board->key;
+    button = board->button;
+    keyboard = board->keyboard;
+    buzzer = board->buzzer;
+    
     status = WAITING;    
 }
 
@@ -15,11 +24,6 @@
     }
 }
 
-void Objective::run(){
-    //board->lcd->printf("DummyGame");
-    //board->lcd->printf("press any key to continue");   
-}
-
 void Objective::complete(){
     status = COMPLETED;
 }
--- a/objectives/Objective.h	Sat May 23 17:15:42 2015 +0000
+++ b/objectives/Objective.h	Sat May 23 17:48:03 2015 +0000
@@ -1,18 +1,30 @@
 #ifndef OBJECTIVE_H
 #define OBJECTIVE_H
 
+#include "../games/Game.h"
+
 class Objective{
     
     enum Status { WAITING, ACTIVE, COMPLETED };
     
     public:
-    Objective();
+    Objective(Game* game);
     Status getStatus();
     
     void start();
-    void run();
+    virtual void run() = 0;
     void complete();
     
+    protected:
+    Game* game;
+    
+    LCD* lcd;
+    Leds* leds;
+    Key* key;
+    Button* button;
+    Keyboard* keyboard;
+    Buzzer* buzzer;
+    
     private:
     Status status;
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/objectives/ObjectiveFactory.cpp	Sat May 23 17:48:03 2015 +0000
@@ -0,0 +1,6 @@
+#include "ObjectiveFactory.h"
+#include "WaitForKeyObjective.h"
+
+Objective* ObjectiveFactory::create(Game* game, int choice){
+    return new WaitForKeyObjective(game);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/objectives/ObjectiveFactory.h	Sat May 23 17:48:03 2015 +0000
@@ -0,0 +1,14 @@
+#ifndef GAMEFACTORY_H
+#define GAMEFACTORY_H
+
+#include "../games/Game.h"
+#include "Objective.h"
+
+class ObjectiveFactory{
+    public:
+    static Objective* create(Game* game, int choice); //factory method
+
+};
+
+
+#endif
\ No newline at end of file
--- a/objectives/WaitForKeyObjective.cpp	Sat May 23 17:15:42 2015 +0000
+++ b/objectives/WaitForKeyObjective.cpp	Sat May 23 17:48:03 2015 +0000
@@ -1,9 +1,13 @@
 
 #include "WaitForKeyObjective.h"
 
+WaitForKeyObjective::WaitForKeyObjective(Game* game) : Objective(game){
+    
+}
+
 void WaitForKeyObjective::run(){  
     //board->lcd->printf("DummyGame");
-    //board->lcd->printf("press any key to continue");  
+    lcd->printf("press any key to continue");  
     
     // if key is pressed -> complete();
 }
\ No newline at end of file
--- a/objectives/WaitForKeyObjective.h	Sat May 23 17:15:42 2015 +0000
+++ b/objectives/WaitForKeyObjective.h	Sat May 23 17:48:03 2015 +0000
@@ -6,7 +6,8 @@
 class WaitForKeyObjective : public Objective{
         
     public:
-    void run();
+    WaitForKeyObjective(Game* game);
+    virtual void run();
 
     
 };