summaryrefslogtreecommitdiffstats
path: root/Game.cpp
blob: 2357845ecfc60e56bc5dc1589e5d5513168238dd (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
#include "Game.h"

Game::Game(int width, int height, PlayerNumber num):
	m_Field(width, height)
{
	//m_vPlayers.resize(num); // is it possible to resize it without trying to fill it? (i.e. fill with null pointers)
	for(PlayerNumber i = 0; i<num; ++i)
	{
		int x = (int)(width  * (0.5 + 0.3*cosf(2*M_PI*(float)i/num+M_PI)));
		int y = (int)(height * (0.5 + 0.3*sinf(2*M_PI*(float)i/num+M_PI)));

		VelocityVector velocity;
		if (abs(x-width/2) >= abs(y-height/2)) {
			if (x-width/2 >= 0) {
				velocity = VelocityVector(-1,0);
			} else {
				velocity = VelocityVector(1,0);
			}
		} else {
			if (y-height/2 >= 0) {
				velocity = VelocityVector(0,-1);
			} else {
				velocity = VelocityVector(0,1);
			}
		}

		m_vPlayers.push_back(Player(i, Coordinates(x,y), velocity));
		m_Field.getCell(Coordinates(x,y)).setState(PLAYER);
		m_Field.getCell(Coordinates(x,y)).setPlayer(&m_vPlayers[i]);
	}
}

Game::~Game(void)
{
	// ~m_Field; ? same for each player ?
	// apparently not needed since we contain the object and not a pointer to it, right?
}


Player& Game::getPlayerByID(PlayerNumber id) {
	return(m_vPlayers[id]);
}

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

void Game::startGame() {
	m_gameStarted = true;
	m_gameRunning = true;
}

void Game::stopGame() {
	m_gameStarted = false;
	m_gameRunning = false;
}

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

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


// main function to be called within glut loop
void Game::updateGame(uint timeElapsedMs) {

	/*	// ask the AI if they wanna change something?
	for(std::vector<Player>::iterator player = m_vPlayers.begin(); player != m_vPlayers.end(); ++player) {
		if (player->getController()) {
			player->getController()->think();
		}
	}*/

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

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

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

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