/
common.cpp
72 lines (62 loc) · 1.91 KB
/
common.cpp
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
#include "std.hpp"
#include "objects.hpp"
#include "utils.hpp"
#include "common.hpp"
common::GameWorld::GameWorld()
: objects_(4) // TODO: если будет поддерживаться в будущих версиях С++, инициализировать через initializator_list
#ifdef NDEBUG
, random(std::chrono::system_clock::now().time_since_epoch().count())
#endif
{
objects_[0] = std::make_unique<game::Wall>(*this, game::Wall::Position::north);
objects_[1] = std::make_unique<game::Wall>(*this, game::Wall::Position::east);
objects_[2] = std::make_unique<game::Wall>(*this, game::Wall::Position::south);
objects_[3] = std::make_unique<game::Wall>(*this, game::Wall::Position::west);
}
void common::GameWorld::tick()
{
for (auto& obj : objects_) {
obj->step();
}
}
game::GameObject* common::GameWorld::who_were(utils::Point site)
{
bool success;
auto index = find_(site, success);
return success ? objects_[index].get() : nullptr;
}
const game::GameObject* common::GameWorld::who_were(utils::Point site) const
{
bool success;
auto index = find_(site, success);
return success ? objects_[index].get() : nullptr;
}
void common::GameWorld::draw(io::Display& console) const
{
for (auto& ptr : objects_) {
ptr->draw(console);
}
}
game::Snake& common::GameWorld::new_snake(const std::string& name, unsigned level)
{
game::Snake* obj = new game::Snake(*this, level, name);
objects_.push_back(std::unique_ptr<game::Snake>(obj));
new_food();
return *obj;
}
game::Food& common::GameWorld::new_food()
{
game::Food* obj = new game::Food(*this);
objects_.push_back(std::unique_ptr<game::Food>(obj));
return *obj;
}
common::GameWorld::Container_::size_type common::GameWorld::find_(utils::Point site, bool& success) const
{
for (common::GameWorld::Container_::size_type i = 0; i < objects_.size(); i++) {
if (objects_[i]->are_you_here(site)) {
success = true;
return i;
}
}
return success = false;
}