summaryrefslogtreecommitdiffstats
path: root/Game.cpp
blob: 8f8fcabeee862652979d26161a1354b6bf8df973 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#define _USE_MATH_DEFINES
#include <math.h>

#include "Game.h"
#include "AIController.h"
#include "KeyboardController.h"


Game::Game(int width, int height): m_width(width), m_height(height)
{
	initField(width, height);
	m_vpPlayers.clear();
}
void Game::setSize(int width, int height)
{
	m_width =width;
	m_height =height;
	initField(width, height);
}
Game::~Game(void)
{
	while(m_vpPlayers.size() > 0)
	{
		delete m_vpPlayers.back();
		m_vpPlayers.pop_back();
	}
}

void Game::initField(int width, int height) {

	m_Field.init(width, height);
	
}
Player* Game::addPlayer()
{
	PlayerNumber numPlayers = m_vpPlayers.size();
	m_vpPlayers.push_back(new Player());
	initPlayer(m_vpPlayers.back(), numPlayers);
	return m_vpPlayers.back();
}
void Game::initPlayer(Player* pPlayer, PlayerNumber playerID)
{
	pPlayer->setNumber(playerID);
	pPlayer->revive();

	if (pPlayer->getController())
	{
		pPlayer->getController()->reset();
	}
}

void Game::start()
{
	placePlayers();
	m_gameRunning = false;
}
void Game::placePlayers()
{
	PlayerNumber num = m_vpPlayers.size();
	for(PlayerNumber i = 0; i<num; ++i)
	{
		int x = (int)(m_width  * (0.5f + 0.3f*cosf((2.f*(float) M_PI*i)/num+(float)M_PI)));
		int y = (int)(m_height * (0.5f + 0.3f*sinf((2.f*(float) M_PI*i)/num+(float)M_PI)));

		VelocityVector velocity;
		if (abs(x-m_width/2) >= abs(y-m_height/2)) {
			if (x-m_width/2 >= 0) {
				velocity = VelocityVector(-1,0);
			} else {
				velocity = VelocityVector(1,0);
			}
		} else {
			if (y-m_height/2 >= 0) {
				velocity = VelocityVector(0,-1);
			} else {
				velocity = VelocityVector(0,1);
			}
		}
	
		m_vpPlayers[i]->setCoordinates(Coordinates(x,y));
		m_vpPlayers[i]->setVelocity(velocity);

		m_Field.getCell(Coordinates(x,y)).setState(PLAYER);
		m_Field.getCell(Coordinates(x,y)).setPlayer(m_vpPlayers[i]);
	}
}


void Game::restart()
{
	initField(m_width, m_height);
	PlayerNumber num = m_vpPlayers.size();

	for(PlayerNumber i = 0; i<num; ++i)
	{
		initPlayer(m_vpPlayers[i], i);
	}
	placePlayers();
	m_gameRunning = true;
}

Player* Game::getPlayerByID(PlayerNumber id) {
	return(m_vpPlayers[id]);
}

Field& Game::getField() {
	return(m_Field);
}

void Game::pauseGame() {
	m_gameRunning = false;
}

void Game::resumeGame() {
	m_gameRunning = true;
}

void Game::toggleGame() {
	m_gameRunning = ! m_gameRunning;
}

bool Game::isRunning() {
	return m_gameRunning;
}

// main function to be called within glut loop
void Game::updateGame(uint timeElapsedMs) {
	if (m_gameRunning) {
	// ask the AI if they wanna change something?
	for(std::vector<Player*>::iterator it = m_vpPlayers.begin(); it != m_vpPlayers.end(); ++it) {
		if ((*it)->isAlive()) {
			if ((*it)->getController()) {
 				(*it)->getController()->think();
			}
		}
	}

	// move everyone and update cells
	std::vector<Coordinates> path;
	std::vector< std::vector<Coordinates> > secondPassPath;

	for(std::vector<Player*>::iterator it = m_vpPlayers.begin(); it != m_vpPlayers.end(); ++it) {
		if ((*it)->isAlive()) {
			path = (*it)->move();
			secondPassPath.push_back(path);

			std::vector<Coordinates>::iterator coord = path.begin();
			if (m_Field.getCell(*coord).getState() == PLAYER) // someone else could have gone BOOM
				m_Field.getCell(*coord).setState(PLAYER_WALL);
			++coord;

			for( coord; coord != path.end(); ++coord) {
				try {
					Cell& cell = m_Field.getCell(*coord);
					if (!(cell.getState() == EMPTY_CELL || (cell.getState() == PLAYER && (cell.getPlayer()->getNumber() == (*it)->getNumber())))) {
						killPlayer(*it);
						cell.setState(BOOM);
						if (cell.getState() == PLAYER_JUST_MOVED) {
							killPlayer((cell.getPlayer()));
						}
					} else {
						cell.setState(PLAYER_JUST_MOVED);
						cell.setPlayer(*it);
					}
				} catch (std::out_of_range e) {
					killPlayer(*it);
					m_Field.getCell(*(coord-1)).setState(BOOM);
				}
			}
		}
	}

	// second pass to cleanup PLAYER_JUST_MOVED cells
	bool boomed = false;
	for(std::vector< std::vector<Coordinates> >::iterator it = secondPassPath.begin(); it != secondPassPath.end(); ++it) {
		boomed = false;
		try {
			for(std::vector<Coordinates>::iterator coord = it->begin(); coord != it->end(); ++coord) {
				Cell& cell = m_Field.getCell(*coord);
				if (cell.getState() == BOOM)
					boomed = true;
	 			else if (boomed)
					cell.setState(EMPTY_CELL);
				else if (coord == it->end() - 1)
					cell.setState(PLAYER);
				else
					cell.setState(PLAYER_WALL);
			}
		} catch (std::exception out_of_bounds) {} // don't do anything
	}
  }
}

void Game::killPlayer(Player *player)
{
	player->kill();
	PlayerNumber alivePlayers = 0;
	for(std::vector<Player*>::iterator it = m_vpPlayers.begin(); it != m_vpPlayers.end(); ++it) {
		if ((*it)->isAlive()) {
			alivePlayers ++;
		}
	}
	if (alivePlayers <= 1)
	{
		pauseGame();
	}
}