-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
59 lines (51 loc) · 1.37 KB
/
Copy pathgame.cpp
File metadata and controls
59 lines (51 loc) · 1.37 KB
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
#include "game.h"
#include <QTimer>
#include <QDebug>
#include <cstdio>
Game::Game(const Config &config, QObject *parent)
: QObject(parent)
, _config(config)
, _timer(new QTimer(this))
{
qRegisterMetaType<std::set<Coordinates>>("std::set<Coordinates>");
connect(_timer, &QTimer::timeout, this, &Game::gameTick);
_timer->setInterval(config.timePerTick.count());
_field = std::make_shared<Field>(config.fieldWidth, config.fieldHeight);
_foodManager = std::make_unique<FoodManager>();
_foodManager->setField(_field);
_foodManager->setApplesCount(_config.applesCount);
_foodManager->restoreApples();
}
void Game::startGame()
{
_timer->start();
}
void Game::stopGame()
{
_timer->stop();
}
void Game::addSnake(std::shared_ptr<Snake> snake)
{
snake->setField(_field);
_snakes[snake->id()] = snake;
}
void Game::removeSnake(int64_t id)
{
auto snakeRecord = _snakes.find(id);
if (snakeRecord == _snakes.end()) return;
snakeRecord->second->leaveField();
_snakes.erase(snakeRecord);
}
std::shared_ptr<const Field> Game::getField() const
{
return _field;
}
void Game::gameTick()
{
_field->startRecordingChangedCells();
for (auto & snakeRecord : _snakes)
snakeRecord.second->moveHead();
_foodManager->restoreApples();
auto changed = _field->stopRecordingChangedCells();
emit updated(changed);
}