1 /*
2    File:        Game.h
3   Description: Game management
4   Program:     BlockOut
5   Author:      Jean-Luc PONS
6 
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11 
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16 */
17 
18 #include <math.h>
19 #include "GLApp/GLApp.h"
20 #include "GLApp/GLSprite.h"
21 #include "GLApp/GLMatrix.h"
22 #include "GLApp/GLFont.h"
23 #include "BotPlayer.h"
24 #include "Sprites.h"
25 #include "SetupManager.h"
26 #include "SoundManager.h"
27 
28 class Game {
29 
30   public:
31     Game();
32 
33     // Set manager
34     void SetSetupManager(SetupManager *manager);
35     void SetSoundManager(SoundManager *manager);
36 
37     // Initialise device objects
38     int Create(int width,int height);
39 
40     // Render the Game
41     void Render();
42 
43     // Release device objects
44     void InvalidateDeviceObjects();
45 
46     // Start a game
47     void StartGame(int width,int height,float fTime);
48 
49     // Start a practice
50     void StartPractice(int width,int height,float fTime);
51 
52     // Start a demo
53     void StartDemo(int width,int height,float fTime);
54 
55     // Process game
56     int Process(BYTE *keys,float fTime);
57 
58     // Ask full repaint
59     void FullRepaint();
60 
61     // Get score
62     SCOREREC *GetScore();
63 
64     // Set view matrix
65     void SetViewMatrix(GLfloat *mView);
66 
67 
68     GLFont2D *pFont;
69 
70   private:
71 
72     // The pit
73     Pit thePit;
74 
75     // PolyCubes
76     PolyCube allPolyCube[NB_POLYCUBE]; // All polycubes (41 items)
77     int   possible[NB_POLYCUBE];       // All possible polycubes for the setup
78     int   nbPossible;                  // Number of possible polycube
79 
80     // Game sprites
81     Sprites sprites;
82 
83     // Manager
84     SetupManager *setupManager;
85     SoundManager *soundManager;
86 
87     // Game stuff
88     SCOREREC score;     // Score
89     int   level;        // Current level
90     int   highScore;    // High score of the current game setup
91     int   gameMode;     // Game mode
92     int   pIdx;         // Current polycube index (in the allPolyCube array)
93     int   exitValue;    // Go back to menu when 1
94     int   cubePerLevel; // Number of cube per level
95     int   dropPos;      // Drop position
96     int   cursorPos;    // Descent cursor
97 
98     // AI stuff (Demo mode)
99     BotPlayer botPlayer;       // AI player
100     AI_MOVE AIMoves[MAX_MOVE]; // AI computed moves
101     int     nbAIMove;          // Number of AI moves
102     int     curAIMove;         // Current AI move
103     float   lastAIMoveTime;    // Last move time stamp
104     BOOL    demoFlag;          // Demo flag
105     BOOL    practiceFlag;      // Practice flag
106 
107     // PolyCube coordinates
108     GLfloat     mat[16];     // Global transform matrix
109     GLMatrix    matRot;      // Current rotation matrix (frame)
110     GLMatrix    newMatRot;   // Virtual rotation matrix
111     VERTEX      vPos;        // Current position (frame)
112     VERTEX      vTransPos;   // Virtual position
113     VERTEX      vOrgPos;     // Origin of translation
114     int         xPos;        // Virtual position
115     int         yPos;        // Virtual position
116     int         zPos;        // Virtual position
117 
118     // Time stamps
119     float startTranslateTime;
120     float startRotateTime;
121     float startRedTime;
122     float startPauseTime;
123     float startStepTime;
124     float startGameTime;
125     float startSpark;
126     float startEndTime;
127 
128     // Animation time
129     float animationTime;
130     float stepTime;
131 
132     // Modes
133     int   rotateMode;
134     BOOL  dropMode;
135     BOOL  dropped;
136     BOOL  lastKeyMode;
137     BOOL  redMode;
138 
139     // Misc
140     float curTime;
141     int   fullRepaint;
142     int   transparent;
143     BOOL  inited;
144     int   style;
145     int   lineWidth;
146     BOOL  endAnimStarted;
147 
148     // Constant matrix
149     GLMatrix matRotOx;
150     GLMatrix matRotOy;
151     GLMatrix matRotOz;
152     GLMatrix matRotNOx;
153     GLMatrix matRotNOy;
154     GLMatrix matRotNOz;
155 
156     // Viewport and transformation matrix
157     GLVIEWPORT   spriteView;
158     GLVIEWPORT   pitView;
159     GLfloat      matProj[16];
160     GLfloat      matView[16];
161     GLfloat      pitMatrix[16];
162 
163     // Background
164     Sprite2D           background;
165 
166     // Spark
167     Sprite2D           spark;     // Spark
168 
169     // Help mode
170     GLMatrix  matAI;
171     float startShowAI;
172 
173     // Private methods
174     void HandleKey(BYTE *keys);
175     BOOL StartRotate(int rType);
176     BOOL StartTranslate(int tType);
177     void InitTranslate();
178     void StartDrop();
179     void AddPolyCube();
180     void NewPolyCube(BYTE *keys);
181     int  SelectPolyCube();
182     void TransformCube(GLMatrix *matRot,BLOCKITEM *cubes,int nbCube,int tx,int ty,int tz);
183     BOOL IsOverlap(GLMatrix *matRot,int tx,int ty,int tz,int *ox,int *oy,int *oz);
184     BOOL IsOverlap(GLMatrix *matRot,int tx,int ty,int tz);
185     BOOL IsOverlap(GLMatrix *matRot,int tx,int ty,int tz,BLOCKITEM *pos);
186     BOOL IsOverlap(GLMatrix *matRot,int tx,int ty,int tz,int *ox,int *oy,int *oz,BLOCKITEM *pos);
187     BOOL IsLower();
188     int  GetBottom();
189     int InitPolyCube(BOOL transparent,float wEdge);
190     void ComputeScore(int nbLines,BOOL pitEmpty);
191     void StartSpark(BLOCKITEM *pos);
192     void RenderPracticeHelp();
193     void ComputeHelp();
194 
195 };
196