8c6f3b863a06de3973b3a9e585956a7001c68a43
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 pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

29) 
30) namespace Blinker {
31) 
32) /// pong game
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

34) {
35) public:
36)   /**
37)    * @brief constructor
38)    * @param[in] name module name
39)    * @param[in] mgrs managers
40)    * @param[in] dirBase base directory
41)    */
42)   Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase);
43) 
44)   /// virtual destructor
45)   virtual ~Pong();
46) 
47) private:
48)   /// copy constructor disabled
49)   Pong(const Pong &that);
50) 
51)   /// assignment operator disabled
52)   const Pong & operator=(const Pong &that);
53) 
54) public:
55)   /// check for update of configuration (derived game), return true on update
56)   virtual bool updateConfigGame();
57) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

96) protected:
97)   /// re-initialize game (e.g. due to config change)
98)   virtual void reinitialize();
99) 
100)   /// redraw current game image, expected to call sendFrame() at end
101)   virtual void redraw();
102) 
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

103)   /// callback when requested time reached
104)   virtual void timeCall();
105) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

138)   /// computer player for left pad
139)   void computerLeft();
140) 
141)   /// computer player for right pad
142)   void computerRight();
143) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

144)   /// bounce ball
145)   void bounceBall();
146) 
147)   /// bounce ball at sides
148)   void bounceBallSide();
149) 
150)   /// bounce ball at left pad
151)   void bounceBallLeft();
152) 
153)   /// bounce ball at right pad
154)   void bounceBallRight();
155) 
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

156)   /// detect goal
157)   void detectGoal();
158) 
Stefan Schuermans pong: split large time func...

Stefan Schuermans authored 5 years ago

159)   /// request next time call - or cancel request if not needed
160)   void planTimeCall();
161) 
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

162)   /// start ball
163)   void startBall();
164) 
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

165) protected:
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

166) 
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

167)   /// descriptor for delay value
168)   static ValueDescr const c_delayDescr;
169) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

174) 
175)   ColorFile m_fileBallColor;     ///< color file for ball color
176)   ColorFile m_fileLineColor;     ///< color file for center line
177)   ColorFile m_filePadColor;      ///< color file for phone player pad
178)   ColorFile m_fileComputerColor; ///< color file for computer player pad
Stefan Schuermans pong: display score

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

181)   UIntFile  m_fileDelay;         ///< file for initial delay in ms per frame
182) 
183)   ColorData    m_ballColor;     ///< ball color
184)   ColorData    m_lineColor;     ///< center line color
185)   ColorData    m_padColor;      ///< phone player pad color
186)   ColorData    m_computerColor; ///< computer player pad color
187)   ColorData    m_scoreColor;    ///< score color when ball is moving
188)   ColorData    m_goalColor;     ///< score color during goal
189)   unsigned int m_delay;         ///< initial delay in ms per frame (ball speed)
190) 
191)   OpConn *m_pConnLeft;  ///< operator connection left player (or NULL)
192)   OpConn *m_pConnRight; ///< operator connection right player (or NULL)
193) 
194)   int m_ballPosX;   ///< ball position X
195)   int m_ballPosY;   ///< ball position Y
196)   int m_ballDirX;   ///< ball direction X
197)   int m_ballDirY;   ///< ball direction Y
198)   int m_padSize;    ///< size of player pads
199)   int m_leftPosY;   ///< position of top pixel of left pad
200)   int m_rightPosY;  ///< position of top pixel of right pad
201)   int m_leftDelay;  ///< delay for computer moving left pad
202)   int m_rightDelay; ///< delay for computer moving right pad
203)   int m_goalDelay;  ///< delay after goal (blinking ball)
204)   int m_bounceCnt;  ///< how often the current ball bounced at pad
205)   int m_scoreLeft;  ///< score of left player
206)   int m_scoreRight; ///< score of right player