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

Game::Game(uint width, uint 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)
	{
		m_vPlayers.push_back(Player(i, Coordinates(5,5+i*10), VelocityVector(0,1))); // TODO: place players correctly :P
		m_Field.getCell(Coordinates(5,5+i*10)).setState(PLAYER);
		m_Field.getCell(Coordinates(5,5+i*10)).setPlayer(&m_vPlayers[i]);
	}
}

Game::~Game(void)
{
  //	~m_Field; ?
  // same for each player ?
}


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) {
  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::exception out_of_bounds) { // doesn't that catch just any exception and name it out_of_bounds?
					player->kill(); // set the cell before as boom?
				}
			}
		}
	}

	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 (coord == it->end() -1) {
					cell.setState(PLAYER);
				} else {
					cell.setState(PLAYER_WALL);
				}
			} catch (std::exception out_of_bounds) {} // don't do anything
		}
  }
}