8432360ab9252a32f98608fe63775fc9aafb7f11
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 begin of Pong game

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

164) protected:
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

165) 
166)   /// operator connection name suffix for left player's connection
167)   static std::string const OpConnSuffixLeft;
168)   /// operator connection name suffix for right player's connection
169)   static std::string const OpConnSuffixRight;
170) 
171)   ColorFile m_fileBallColor;     ///< color file for ball color
172)   ColorFile m_fileLineColor;     ///< color file for center line
173)   ColorFile m_filePadColor;      ///< color file for phone player pad
174)   ColorFile m_fileComputerColor; ///< color file for computer player pad
175)   ColorData m_ballColor;         ///< ball color
176)   ColorData m_lineColor;         ///< center line color
177)   ColorData m_padColor;          ///< phone player pad color
178)   ColorData m_computerColor;     ///< computer player pad color
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

179)   OpConn   *m_pConnLeft;         ///< operator connection left player (or NULL)
180)   OpConn   *m_pConnRight;        ///< operator connection right player (o NULL)
Stefan Schuermans pong: implement phone players

Stefan Schuermans authored 5 years ago

181)   int       m_ballPosX;          ///< ball position X
182)   int       m_ballPosY;          ///< ball position Y
183)   int       m_ballDirX;          ///< ball direction X
184)   int       m_ballDirY;          ///< ball direction Y
185)   int       m_padSize;           ///< size of player pads
186)   int       m_leftPosY;          ///< position of top pixel of left pad
187)   int       m_rightPosY;         ///< position of top pixel of right pad
188)   int       m_leftDelay;         ///< delay for computer moving left pad
189)   int       m_rightDelay;        ///< delay for computer moving right pad
Stefan Schuermans pong: goals, score, speedup

Stefan Schuermans authored 5 years ago

190)   int       m_goalDelay;         ///< delay after goal (blinking ball)
191)   int       m_bounceCnt;         ///< how often the current ball bounced at pad
192)   int       m_scoreLeft;         ///< score of left player
193)   int       m_scoreRight;        ///< score of right player