fb06c1c4fd5890e8d48b110fbcbe7b43adaf90c4
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:
56)   /// check for update of configuration (derived game), return true on update
57)   virtual bool updateConfigGame();
58) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

59)   /**
Stefan Schuermans fix comment typo

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

106) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

157)   /// detect goal
158)   void detectGoal();
159) 
Stefan Schuermans move time call logic to gam...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

162) 
Stefan Schuermans pong: make ball move

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

169) protected:
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

170) 
Stefan Schuermans pong: configurable speed

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

175) 
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

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

Stefan Schuermans authored 5 years ago

189) 
Stefan Schuermans sound support for pong game

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

197)   ColorData    m_ballColor;     ///< ball color
198)   ColorData    m_lineColor;     ///< center line color
199)   ColorData    m_padColor;      ///< phone player pad color
200)   ColorData    m_computerColor; ///< computer player pad color
201)   ColorData    m_scoreColor;    ///< score color when ball is moving
202)   ColorData    m_goalColor;     ///< score color during goal
203)   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

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

Stefan Schuermans authored 5 years ago

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