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 PG |
18 | #include "game.h" |
19 | #include "debug.h" | |
20 | ||
054d658f PG |
21 | #include <vector> |
22 | using std::vector; | |
23 | ||
054d658f | 24 | #include "entityCreator.h" |
33b8c69b | 25 | #include "effectManager.h" |
054d658f | 26 | |
ad9f1fb6 PG |
27 | #include "GameStates/GameState.h" |
28 | #include "GameStates/Running.h" | |
29 | #include "GameStates/Paused.h" | |
30 | #include "GameStates/CreatingPolygon.h" | |
31 | ||
68b2316d | 32 | |
ad9f1fb6 PG |
33 | /// ***** Private Variables ***** |
34 | ||
35 | // The stack of active game states | |
36 | vector<GameState*> active_States; | |
37 | ||
38 | // Pointers to each possible game state | |
39 | // inserted and removed from the active_States | |
40 | Running* running; | |
41 | Paused* paused; | |
42 | CreatingPolygon* creating_Polygon; | |
43 | ||
033c5535 PG |
44 | // true if the top state requested itself to be poped |
45 | bool pop_State; | |
46 | // pointer to a state wishing to be added | |
47 | GameState* push_State; | |
48 | ||
ad9f1fb6 PG |
49 | |
50 | /// ***** Public Methods ***** | |
68b2316d PG |
51 | |
52 | void game::init() | |
ad9f1fb6 PG |
53 | { |
54 | running = new Running(); | |
55 | paused = new Paused(); | |
56 | creating_Polygon = new CreatingPolygon(); | |
054d658f | 57 | |
033c5535 PG |
58 | pop_State = false; |
59 | push_State = NULL; | |
60 | ||
054d658f | 61 | // create starting entities |
054d658f PG |
62 | creator::init(); |
63 | ||
a483ed75 PG |
64 | active_States.push_back(running); |
65 | ||
33b8c69b PG |
66 | effect::init(); |
67 | ||
ca2d526e | 68 | DPF(0, "World Created"); |
ad9f1fb6 PG |
69 | } |
70 | ||
68b2316d | 71 | void game::clean() |
ad9f1fb6 | 72 | { |
33b8c69b PG |
73 | effect::clean(); |
74 | ||
054d658f PG |
75 | creator::clean(); |
76 | ||
ad9f1fb6 PG |
77 | delete creating_Polygon; |
78 | delete paused; | |
79 | delete running; | |
80 | } | |
81 | ||
10baa49b | 82 | void game::handleInput() |
ad9f1fb6 | 83 | { |
10baa49b PG |
84 | creator::handleInput(); |
85 | ||
c46074c1 | 86 | int last = active_States.size() -1; |
10baa49b | 87 | |
c46074c1 PG |
88 | if(active_States[last] != running && running->pushMe()) |
89 | { | |
033c5535 | 90 | push_State = running; |
c46074c1 | 91 | } |
033c5535 | 92 | |
c46074c1 PG |
93 | if(active_States[last] != paused && paused->pushMe()) |
94 | { | |
033c5535 | 95 | push_State = paused; |
c46074c1 | 96 | } |
033c5535 | 97 | |
c46074c1 PG |
98 | if(active_States[last] != creating_Polygon && creating_Polygon->pushMe()) |
99 | { | |
033c5535 | 100 | push_State = creating_Polygon; |
c46074c1 | 101 | } |
033c5535 PG |
102 | |
103 | ||
ad9f1fb6 | 104 | for( int i = 0; |
a483ed75 | 105 | i <= last; |
ad9f1fb6 PG |
106 | i++ ) |
107 | { | |
a483ed75 | 108 | if( i == last ) |
033c5535 PG |
109 | { |
110 | if(active_States[i]->popMe()) | |
111 | pop_State = true; | |
112 | else | |
113 | active_States[i]->handleInput(true); | |
114 | } | |
ad9f1fb6 PG |
115 | else |
116 | active_States[i]->handleInput(false); | |
117 | } | |
ad9f1fb6 PG |
118 | } |
119 | ||
68b2316d | 120 | void game::update(float time_step) |
ad9f1fb6 | 121 | { |
033c5535 PG |
122 | if(pop_State) |
123 | { | |
124 | active_States.pop_back(); | |
125 | pop_State = false; | |
126 | } | |
127 | ||
c46074c1 PG |
128 | if(push_State != NULL) |
129 | { | |
130 | active_States.push_back(push_State); | |
131 | push_State = NULL; | |
132 | } | |
133 | ||
a483ed75 | 134 | int last = active_States.size() -1; |
ad9f1fb6 | 135 | for( int i = 0; |
a483ed75 | 136 | i <= last; |
ad9f1fb6 PG |
137 | i++ ) |
138 | { | |
a483ed75 | 139 | if( i == last ) |
ad9f1fb6 PG |
140 | active_States[i]->update(time_step, true); |
141 | else | |
142 | active_States[i]->update(time_step, false); | |
143 | } | |
144 | } | |
145 | ||
68b2316d | 146 | void game::draw() |
ad9f1fb6 | 147 | { |
a483ed75 | 148 | int last = active_States.size() -1; |
ad9f1fb6 | 149 | for( int i = 0; |
a483ed75 | 150 | i <= last; |
ad9f1fb6 PG |
151 | i++ ) |
152 | { | |
a483ed75 | 153 | if( i == last ) |
ad9f1fb6 PG |
154 | active_States[i]->draw(true); |
155 | else | |
156 | active_States[i]->draw(false); | |
157 | } | |
158 | } |