92f7925256891c6a5ceb984ba2ac403b89b008fe
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 move time call logic to gam...

Stefan Schuermans authored 5 years ago

33) class Pong: public Game, public OpReqIf
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)   /**
Stefan Schuermans fix comment typo

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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 move time call logic to gam...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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 move time call logic to gam...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

161) 
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

162)   /// start ball
163)   void startBall();
164) 
Stefan Schuermans pong: end game when max sco...

Stefan Schuermans authored 5 years ago

165)   /// game over: close player connections and deactivate
166)   void gameOver();
167) 
Stefan Schuermans begin of Pong game

Stefan Schuermans authored 5 years ago

168) protected:
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

169) 
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

174) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

179) 
180)   ColorFile m_fileBallColor;     ///< color file for ball color
181)   ColorFile m_fileLineColor;     ///< color file for center line
182)   ColorFile m_filePadColor;      ///< color file for phone player pad
183)   ColorFile m_fileComputerColor; ///< color file for computer player pad
Stefan Schuermans pong: display score

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

186)   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

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

Stefan Schuermans authored 5 years ago

188) 
189)   ColorData    m_ballColor;     ///< ball color
190)   ColorData    m_lineColor;     ///< center line color
191)   ColorData    m_padColor;      ///< phone player pad color
192)   ColorData    m_computerColor; ///< computer player pad color
193)   ColorData    m_scoreColor;    ///< score color when ball is moving
194)   ColorData    m_goalColor;     ///< score color during goal
195)   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

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

Stefan Schuermans authored 5 years ago

197) 
198)   OpConn *m_pConnLeft;  ///< operator connection left player (or NULL)
199)   OpConn *m_pConnRight; ///< operator connection right player (or NULL)
200) 
201)   int m_ballPosX;   ///< ball position X
202)   int m_ballPosY;   ///< ball position Y
203)   int m_ballDirX;   ///< ball direction X
204)   int m_ballDirY;   ///< ball direction Y
205)   int m_padSize;    ///< size of player pads
206)   int m_leftPosY;   ///< position of top pixel of left pad
207)   int m_rightPosY;  ///< position of top pixel of right pad
208)   int m_leftDelay;  ///< delay for computer moving left pad
209)   int m_rightDelay; ///< delay for computer moving right pad
210)   int m_goalDelay;  ///< delay after goal (blinking ball)
211)   int m_bounceCnt;  ///< how often the current ball bounced at pad
212)   int m_scoreLeft;  ///< score of left player
213)   int m_scoreRight; ///< score of right player