Commit | Line | Data |
---|---|---|
e68f847b PG |
1 | /* |
2 | * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
a483ed75 | 18 | #include "game.h" |
f32a9b7c PG |
19 | |
20 | #include <pg/debug.h> | |
a483ed75 | 21 | |
054d658f PG |
22 | #include <vector> |
23 | using std::vector; | |
24 | ||
054d658f | 25 | #include "entityCreator.h" |
33b8c69b | 26 | #include "effectManager.h" |
054d658f | 27 | |
ad9f1fb6 PG |
28 | #include "GameStates/GameState.h" |
29 | #include "GameStates/Running.h" | |
30 | #include "GameStates/Paused.h" | |
31 | #include "GameStates/CreatingPolygon.h" | |
32 | ||
68b2316d | 33 | |
ad9f1fb6 PG |
34 | /// ***** Private Variables ***** |
35 | ||
36 | // The stack of active game states | |
c16d1683 | 37 | static vector<GameState*> s_active_States; |
ad9f1fb6 PG |
38 | |
39 | // Pointers to each possible game state | |
c16d1683 PG |
40 | // inserted and removed from the s_active_States |
41 | static vector<GameState*> s_possible_States; | |
ad9f1fb6 | 42 | |
033c5535 | 43 | // true if the top state requested itself to be poped |
c16d1683 | 44 | static bool s_bPopState; |
033c5535 | 45 | // pointer to a state wishing to be added |
c16d1683 | 46 | static GameState* s_pPushState; |
033c5535 | 47 | |
ad9f1fb6 PG |
48 | |
49 | /// ***** Public Methods ***** | |
68b2316d PG |
50 | |
51 | void game::init() | |
ad9f1fb6 | 52 | { |
c16d1683 PG |
53 | s_bPopState = false; |
54 | s_pPushState = NULL; | |
033c5535 | 55 | |
054d658f | 56 | // create starting entities |
054d658f | 57 | creator::init(); |
c16d1683 | 58 | effect::init(); |
054d658f | 59 | |
c16d1683 PG |
60 | Running* pRunning = new Running(); |
61 | s_possible_States.push_back(pRunning); | |
62 | s_possible_States.push_back(new Paused()); | |
63 | s_possible_States.push_back(new CreatingPolygon()); | |
a483ed75 | 64 | |
c16d1683 | 65 | s_active_States.push_back(pRunning); |
33b8c69b | 66 | |
ca2d526e | 67 | DPF(0, "World Created"); |
ad9f1fb6 PG |
68 | } |
69 | ||
68b2316d | 70 | void game::clean() |
ad9f1fb6 | 71 | { |
33b8c69b | 72 | effect::clean(); |
054d658f PG |
73 | creator::clean(); |
74 | ||
c16d1683 PG |
75 | for(unsigned int i=0; i < s_possible_States.size(); i++) |
76 | { | |
77 | delete s_possible_States[i]; | |
78 | } | |
79 | s_possible_States.clear(); | |
80 | s_active_States.clear(); | |
ad9f1fb6 PG |
81 | } |
82 | ||
10baa49b | 83 | void game::handleInput() |
ad9f1fb6 | 84 | { |
10baa49b PG |
85 | creator::handleInput(); |
86 | ||
c16d1683 | 87 | int iLast = s_active_States.size() -1; |
10baa49b | 88 | |
c16d1683 | 89 | for(unsigned int i=0; i < s_possible_States.size(); i++) |
c46074c1 | 90 | { |
c16d1683 | 91 | GameState* pState = s_possible_States[i]; |
033c5535 | 92 | |
c16d1683 PG |
93 | if(s_active_States[iLast] != pState && pState->pushMe()) |
94 | { | |
95 | s_pPushState = pState; | |
96 | } | |
c46074c1 | 97 | } |
033c5535 PG |
98 | |
99 | ||
ad9f1fb6 | 100 | for( int i = 0; |
c16d1683 | 101 | i <= iLast; |
ad9f1fb6 PG |
102 | i++ ) |
103 | { | |
c16d1683 | 104 | if( i == iLast ) |
033c5535 | 105 | { |
c16d1683 PG |
106 | if(s_active_States[i]->popMe()) |
107 | s_bPopState = true; | |
033c5535 | 108 | else |
c16d1683 | 109 | s_active_States[i]->handleInput(true); |
033c5535 | 110 | } |
ad9f1fb6 | 111 | else |
c16d1683 | 112 | s_active_States[i]->handleInput(false); |
ad9f1fb6 | 113 | } |
ad9f1fb6 PG |
114 | } |
115 | ||
c16d1683 | 116 | void game::update(float fTimeStep) |
ad9f1fb6 | 117 | { |
c16d1683 | 118 | if(s_bPopState) |
033c5535 | 119 | { |
c16d1683 PG |
120 | s_active_States.pop_back(); |
121 | s_bPopState = false; | |
033c5535 PG |
122 | } |
123 | ||
c16d1683 | 124 | if(s_pPushState != NULL) |
c46074c1 | 125 | { |
c16d1683 PG |
126 | s_active_States.push_back(s_pPushState); |
127 | s_pPushState = NULL; | |
c46074c1 PG |
128 | } |
129 | ||
c16d1683 | 130 | int iLast = s_active_States.size() -1; |
ad9f1fb6 | 131 | for( int i = 0; |
c16d1683 | 132 | i <= iLast; |
ad9f1fb6 PG |
133 | i++ ) |
134 | { | |
c16d1683 PG |
135 | if( i == iLast ) |
136 | s_active_States[i]->update(fTimeStep, true); | |
ad9f1fb6 | 137 | else |
c16d1683 | 138 | s_active_States[i]->update(fTimeStep, false); |
ad9f1fb6 PG |
139 | } |
140 | } | |
141 | ||
68b2316d | 142 | void game::draw() |
ad9f1fb6 | 143 | { |
c16d1683 | 144 | int iLast = s_active_States.size() -1; |
ad9f1fb6 | 145 | for( int i = 0; |
c16d1683 | 146 | i <= iLast; |
ad9f1fb6 PG |
147 | i++ ) |
148 | { | |
c16d1683 PG |
149 | if( i == iLast ) |
150 | s_active_States[i]->draw(true); | |
ad9f1fb6 | 151 | else |
c16d1683 | 152 | s_active_States[i]->draw(false); |
ad9f1fb6 PG |
153 | } |
154 | } |