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