Peoplemon  0.1.0
Peoplemon 3 game source documentation
StorageSystem.cpp
Go to the documentation of this file.
2 
3 #include <algorithm>
4 
5 namespace core
6 {
7 namespace player
8 {
10 
12  for (int b = 0; b < BoxCount; ++b) {
13  for (int x = 0; x < BoxWidth; ++x) {
14  for (int y = 0; y < BoxHeight; ++y) {
15  if (spaceFree(b, x, y)) {
16  boxes[b].emplace_back(ppl, b, sf::Vector2i(x, y));
17  boxes[b].back().peoplemon.heal();
18  return true;
19  }
20  }
21  }
22  }
23  return false;
24 }
25 
26 void StorageSystem::add(unsigned int box, const sf::Vector2i& pos,
27  const pplmn::OwnedPeoplemon& ppl) {
28  boxes[box].emplace_back(ppl, box, pos);
29  boxes[box].back().peoplemon.heal();
30 }
31 
32 void StorageSystem::remove(unsigned int b, const sf::Vector2i& pos) {
33  auto& box = boxes[b];
34  for (unsigned int i = 0; i < box.size(); ++i) {
35  if (box[i].position == pos) {
36  box.erase(box.begin() + i);
37  --i;
38  }
39  }
40 }
41 
43  const sf::Vector2i& newPos) {
44  // find peoplemon being moved
45  const auto it = std::find_if(
46  boxes[ppl.boxNumber].begin(),
47  boxes[ppl.boxNumber].end(),
48  [&ppl](const pplmn::StoredPeoplemon& cmp) { return ppl.position == cmp.position; });
49  if (it == boxes[ppl.boxNumber].end()) {
50  BL_LOG_CRITICAL << "Could not find Peoplemon to move at position: " << ppl.position;
51  return nullptr;
52  }
53 
54  // handle swap
55  const auto oit = std::find_if(
56  boxes[newBox].begin(), boxes[newBox].end(), [&newPos](const pplmn::StoredPeoplemon& cmp) {
57  return newPos == cmp.position;
58  });
59  if (oit != boxes[newBox].end()) {
60  std::swap(it->peoplemon, oit->peoplemon);
61  return &*oit;
62  }
63 
64  // move position
65  ppl.position = newPos;
66 
67  // change box if different
68  if (newBox != ppl.boxNumber) {
69  const unsigned int ogBox = ppl.boxNumber;
70  ppl.boxNumber = newBox;
71  boxes[newBox].emplace_back(ppl);
72  boxes[ogBox].erase(it);
73  return &boxes[newBox].back();
74  }
75 
76  return &*it;
77 }
78 
79 bool StorageSystem::spaceFree(int box, int x, int y) const {
80  if (box < 0 || static_cast<unsigned int>(box) >= boxes.size()) return false;
81 
82  if (boxes[box].size() == BoxWidth * BoxHeight) return false;
83  for (const auto& stored : boxes[box]) {
84  if (stored.position.x == x && stored.position.y == y) return false;
85  }
86  return true;
87 }
88 
89 pplmn::StoredPeoplemon* StorageSystem::get(unsigned int box, const sf::Vector2i& pos) {
90  for (auto& ppl : boxes[box]) {
91  if (ppl.position == pos) { return &ppl; }
92  }
93  return nullptr;
94 }
95 
96 const std::vector<pplmn::StoredPeoplemon>& StorageSystem::getBox(unsigned int box) const {
97  return boxes[box];
98 }
99 
100 } // namespace player
101 } // namespace core
Core classes and functionality for both the editor and game.
Represents an instance of a peoplemon. Can be a wild peoplemon, trainer, or player peoplemon....
Represents a Peoplemon in storage.
std::uint16_t boxNumber
Which box the peoplemon is in.
sf::Vector2i position
The position of the peoplemon in the box.
static constexpr int BoxCount
pplmn::StoredPeoplemon * get(unsigned int box, const sf::Vector2i &pos)
Returns the peoplemon at the given position in the given box.
static constexpr int BoxHeight
static constexpr int BoxWidth
pplmn::StoredPeoplemon * move(pplmn::StoredPeoplemon &ppl, unsigned int newBox, const sf::Vector2i &newPos)
Moves the given stored peoplemon from its current location to the new location.
StorageSystem()
Construct a new Storage System.
void remove(unsigned int box, const sf::Vector2i &position)
Clears the peoplemon from the given position, if one is there.
const std::vector< pplmn::StoredPeoplemon > & getBox(unsigned int box) const
Returns the given box. Performs no bounds check.
bool add(const pplmn::OwnedPeoplemon &ppl)
Adds the given peoplemon to storage in a free slot.
bool spaceFree(int box, int x, int y) const
Returns whether or not the given storage space is free.