cleaned how balls are added
[physics.git] / src / game.cpp
CommitLineData
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>
22using 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
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
033c5535
PG
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
ad9f1fb6
PG
49
50/// ***** Public Methods *****
68b2316d
PG
51
52void 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
054d658f
PG
68#ifdef DEBUGGING
69 cout << "World Created" << endl;
70#endif
ad9f1fb6
PG
71}
72
68b2316d 73void game::clean()
ad9f1fb6 74{
33b8c69b
PG
75 effect::clean();
76
054d658f
PG
77 creator::clean();
78
ad9f1fb6
PG
79 delete creating_Polygon;
80 delete paused;
81 delete running;
82}
83
10baa49b 84void game::handleInput()
ad9f1fb6 85{
10baa49b
PG
86 creator::handleInput();
87
88
033c5535
PG
89 if(running->pushMe())
90 push_State = running;
91
92 if(paused->pushMe())
93 push_State = paused;
94
95 if(creating_Polygon->pushMe())
96 push_State = creating_Polygon;
97
98
a483ed75 99 int last = active_States.size() -1;
ad9f1fb6 100 for( int i = 0;
a483ed75 101 i <= last;
ad9f1fb6
PG
102 i++ )
103 {
a483ed75 104 if( i == last )
033c5535
PG
105 {
106 if(active_States[i]->popMe())
107 pop_State = true;
108 else
109 active_States[i]->handleInput(true);
110 }
ad9f1fb6
PG
111 else
112 active_States[i]->handleInput(false);
113 }
ad9f1fb6
PG
114}
115
68b2316d 116void game::update(float time_step)
ad9f1fb6 117{
033c5535
PG
118 if(push_State != NULL)
119 {
120 // don't want to pop and push same state, pop wins arbitrary
121 if(!pop_State)
122 active_States.push_back(push_State);
123
124 push_State = NULL;
125 }
126
127 if(pop_State)
128 {
129 active_States.pop_back();
130 pop_State = false;
131 }
132
a483ed75 133 int last = active_States.size() -1;
ad9f1fb6 134 for( int i = 0;
a483ed75 135 i <= last;
ad9f1fb6
PG
136 i++ )
137 {
a483ed75 138 if( i == last )
ad9f1fb6
PG
139 active_States[i]->update(time_step, true);
140 else
141 active_States[i]->update(time_step, false);
142 }
143}
144
68b2316d 145void game::draw()
ad9f1fb6 146{
a483ed75 147 int last = active_States.size() -1;
ad9f1fb6 148 for( int i = 0;
a483ed75 149 i <= last;
ad9f1fb6
PG
150 i++ )
151 {
a483ed75 152 if( i == last )
ad9f1fb6
PG
153 active_States[i]->draw(true);
154 else
155 active_States[i]->draw(false);
156 }
157}