e897205a2f2e4ef710f8b48d0a3b8c7e93cd8632
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

1) /* Blinker
2)    Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
3)    Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4)    a blinkenarea.org project */
5) 
6) #ifndef BLINKER_PONG_H
7) #define BLINKER_PONG_H
8) 
9) #include <string>
10) #include <vector>
11) 
12) #include <BlinkenLib/BlinkenFrame.h>
13) 
14) #include "Color.h"
15) #include "ColorFile.h"
16) #include "File.h"
17) #include "Format.h"
18) #include "FormatFile.h"
19) #include "Game.h"
20) #include "Mgrs.h"
21) #include "Module.h"
Stefan Schuermans sound support for pong game

Stefan Schuermans authored 5 years ago

22) #include "NameFile.h"
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

23) #include "OpConn.h"
24) #include "OpConnIf.h"
25) #include "OpReqIf.h"
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

26) #include "OutStreamFile.h"
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

27) #include "Time.h"
28) #include "TimeCallee.h"
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

29) #include "UIntFile.h"
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

30) 
31) namespace Blinker {
32) 
33) /// pong game
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

34) class Pong: public Game, public OpReqIf
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

35) {
36) public:
37)   /**
38)    * @brief constructor
39)    * @param[in] name module name
40)    * @param[in] mgrs managers
41)    * @param[in] dirBase base directory
42)    */
43)   Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase);
44) 
45)   /// virtual destructor
46)   virtual ~Pong();
47) 
48) private:
49)   /// copy constructor disabled
50)   Pong(const Pong &that);
51) 
52)   /// assignment operator disabled
53)   const Pong & operator=(const Pong &that);
54) 
55) public:
Stefan Schuermans change of some game params...

Stefan Schuermans authored 5 years ago

56)   /**
57)    * @brief check for update of configuration (derived game)
58)    * @param[in,out] doReinit set to true to ask for reinitialization
59)    * @param[in,out] doRedraw set to true to ask for redrawing
60)    */
61)   virtual void updateConfigGame(bool &doReinit, bool &doRedraw);
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

62) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

63)   /**
Stefan Schuermans fix comment typo

Stefan Schuermans authored 5 years ago

64)    * @brief check if accepting new operator connection is possible
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

65)    * @param[in] name operator interface name
66)    * @return if accepting new connection is possible
67)    */
68)   virtual bool acceptNewOpConn(const std::string &name);
69) 
70)   /**
71)    * @brief new operator connection
72)    * @param[in] name operator interface name
73)    * @param[in] pConn operator connection object
74)    *
75)    * The new connection may not yet be used for sending inside this callback.
76)    */
77)   virtual void newOpConn(const std::string &name, OpConn *pConn);
78) 
79)   /**
80)    * @brief key command received on operator connection
81)    * @param[in] pConn operator connection object
82)    * @param[in] key key that was pressed
83)    */
84)   virtual void opConnRecvKey(OpConn *pConn, char key);
85) 
86)   /**
87)    * @brief play command received on operator connection
88)    * @param[in] pConn operator connection object
89)    * @param[in] sound name of sound to play
90)    */
91)   virtual void opConnRecvPlay(OpConn *pConn, const std::string &sound);
92) 
93)   /**
94)    * @brief operator connection is closed
95)    * @param[in] pConn operator connection object
96)    *
97)    * The connection may not be used for sending any more in this callback.
98)    */
99)   virtual void opConnClose(OpConn *pConn);
100) 
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

101) protected:
102)   /// re-initialize game (e.g. due to config change)
103)   virtual void reinitialize();
104) 
105)   /// redraw current game image, expected to call sendFrame() at end
106)   virtual void redraw();
107) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

108)   /// process next time step of game
109)   virtual void timeStep();
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

110) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

111)   /**
112)    * @brief process key received from phone player
113)    * @param[in] key received key from player
114)    * @param[in,out] padPosY y position of player's pad
115)    */
116)   void processKey(char key, int &padPosY);
117) 
Stefan Schuermans pong: make computer slow

Stefan Schuermans authored 5 years ago

118)   /**
119)    * @brief delay processing for computer players
120)    * @param[in,out] delay delay variable of computer player
121)    * @return whether computer player is allowed to move
122)    */
123)   bool computerDelay(int &delay) const;
124) 
Stefan Schuermans pong: make computer perfect

Stefan Schuermans authored 5 years ago

125)   /**
126)    * @brief computation of ideal pad position for computer players
127)    * @param[in] padBallX x coordinate of position of ball when hitting the pad
128)    * @param[in] padY current y coordinate of pad
129)    * @param[out] padYmin minimum ideal y position of pad
130)    * @param[out] padYmax maximum ideal y position of pad
131)    */
132)   void computerComputePadPos(int padBallX, int padY,
133)                              int &padYmin, int &padYmax) const;
134) 
135)   /**
136)    * @brief move pad for computer players
137)    * @param[in] padYmin minimum desired y position of pad
138)    * @param[in] padYmax maximum desired y position of pad
139)    * @param[in,out] padPosY y position of pad
140)    */
141)   void computerMovePad(int padYmin, int padYmax, int &padPosY) const;
142) 
Stefan Schuermans implement simple, fast comp...

Stefan Schuermans authored 5 years ago

143)   /// computer player for left pad
144)   void computerLeft();
145) 
146)   /// computer player for right pad
147)   void computerRight();
148) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

149)   /// bounce ball
150)   void bounceBall();
151) 
152)   /// bounce ball at sides
153)   void bounceBallSide();
154) 
155)   /// bounce ball at left pad
156)   void bounceBallLeft();
157) 
158)   /// bounce ball at right pad
159)   void bounceBallRight();
160) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

161)   /// detect goal
162)   void detectGoal();
163) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

164)   /// set time for next time step of game - or unset if not needed
165)   void planTimeStep();
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

166) 
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

167)   /// start ball
168)   void startBall();
169) 
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

170)   /// game over: close player connections and deactivate
171)   void gameOver();
172) 
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

173) protected:
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

174) 
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

175)   /// descriptor for delay value
176)   static ValueDescr const c_delayDescr;
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

177)   /// descriptor for maximum score value
178)   static ValueDescr const c_maxScoreDescr;
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

179) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

180)   /// operator connection name suffix for left player's connection
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

181)   static std::string const c_opConnSuffixLeft;
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

182)   /// operator connection name suffix for right player's connection
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

183)   static std::string const c_opConnSuffixRight;
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

184) 
185)   ColorFile m_fileBallColor;     ///< color file for ball color
186)   ColorFile m_fileLineColor;     ///< color file for center line
187)   ColorFile m_filePadColor;      ///< color file for phone player pad
188)   ColorFile m_fileComputerColor; ///< color file for computer player pad
Stefan Schuermans pong: display score

Stefan Schuermans authored 5 years ago

189)   ColorFile m_fileScoreColor;    ///< color file for score when ball is moving
190)   ColorFile m_fileGoalColor;     ///< color file for score during goal
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

191)   UIntFile  m_fileDelay;         ///< file for initial delay in ms per frame
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

192)   UIntFile  m_fileMaxScore;      ///< file for maximum score (end of game)
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

193) 
Stefan Schuermans sound support for pong game

Stefan Schuermans authored 5 years ago

194)   NameFile m_fileLeftPlayerSound;  ///< "left player" sound name file
195)   NameFile m_fileRightPlayerSound; ///< "right player" sound name file
196)   NameFile m_fileScoreSound;       ///< "you scored" sound name file
197)   NameFile m_fileOtherScoreSound;  ///< "other player scored" sound name file
198)   NameFile m_fileWinSound;         ///< "you win" sound name file
199)   NameFile m_fileLoseSound;        ///< "you lose" sound name file
200) 
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

201)   ColorData    m_ballColor;     ///< ball color
202)   ColorData    m_lineColor;     ///< center line color
203)   ColorData    m_padColor;      ///< phone player pad color
204)   ColorData    m_computerColor; ///< computer player pad color
205)   ColorData    m_scoreColor;    ///< score color when ball is moving
206)   ColorData    m_goalColor;     ///< score color during goal
207)   unsigned int m_delay;         ///< initial delay in ms per frame (ball speed)
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

208)   unsigned int m_maxScore;      ///< maximum score (end of game)
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

209) 
210)   OpConn *m_pConnLeft;  ///< operator connection left player (or NULL)
211)   OpConn *m_pConnRight; ///< operator connection right player (or NULL)
212) 
213)   int m_ballPosX;   ///< ball position X
214)   int m_ballPosY;   ///< ball position Y
215)   int m_ballDirX;   ///< ball direction X
216)   int m_ballDirY;   ///< ball direction Y
217)   int m_padSize;    ///< size of player pads
218)   int m_leftPosY;   ///< position of top pixel of left pad
219)   int m_rightPosY;  ///< position of top pixel of right pad
220)   int m_leftDelay;  ///< delay for computer moving left pad
221)   int m_rightDelay; ///< delay for computer moving right pad
222)   int m_goalDelay;  ///< delay after goal (blinking ball)
223)   int m_bounceCnt;  ///< how often the current ball bounced at pad
224)   int m_scoreLeft;  ///< score of left player
225)   int m_scoreRight; ///< score of right player
Stefan Schuermans fix comment

Stefan Schuermans authored 5 years ago

226) }; // class Pong