summary refs log tree commit diff stats
path: root/src/grid_cache.h
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2019-02-20 16:44:09 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2019-02-20 16:44:09 -0500
commitd9c201cbf2fbfe315137e141d886a9bbfa6794ba (patch)
tree2dfaf8db9e43e456386796d797884ea18d63befe /src/grid_cache.h
parent8996810b1356c2224d4f34423fd4211de20da238 (diff)
downloaddispatcher-d9c201cbf2fbfe315137e141d886a9bbfa6794ba.tar.gz
dispatcher-d9c201cbf2fbfe315137e141d886a9bbfa6794ba.tar.bz2
dispatcher-d9c201cbf2fbfe315137e141d886a9bbfa6794ba.zip
Started implementing schedule
Scheduled entities move downward every beat, although they have a speed that is twice the bpm, so essentially they move every other beat?

Broke the simulation loop such that different parts run independently -- made it horizontal rather than vertical.

Encapsulated the grid cache so that more than one position field could be cached. This is used to make sure that an entity can't move into a space that something else is already moving into.

Fixed issue where an entity could move perpendicularly into the space an entity was moving out of.
Diffstat (limited to 'src/grid_cache.h')
-rw-r--r--src/grid_cache.h59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/grid_cache.h b/src/grid_cache.h new file mode 100644 index 0000000..e837f55 --- /dev/null +++ b/src/grid_cache.h
@@ -0,0 +1,59 @@
1#ifndef GRID_CACHE_H_67BBE74D
2#define GRID_CACHE_H_67BBE74D
3
4#include <unordered_map>
5#include <unordered_set>
6#include "level.h"
7
8template <typename T>
9class GridCache {
10public:
11
12 explicit GridCache(const Level& level) : width_(level.getSize().w())
13 {
14 }
15
16 void set(T value, vec2s pos)
17 {
18 if (reverse_.count(value))
19 {
20 size_t oldPosIndex = reverse_.at(value);
21 lookup_[oldPosIndex].erase(value);
22 }
23
24 size_t newPosIndex = getIndex(pos);
25 lookup_[newPosIndex].insert(value);
26 reverse_[value] = newPosIndex;
27 }
28
29 void remove(T value)
30 {
31 if (reverse_.count(value))
32 {
33 size_t index = reverse_.at(value);
34 lookup_[index].erase(value);
35 reverse_.erase(value);
36 }
37 }
38
39 const std::unordered_set<T>& at(vec2s pos) const
40 {
41 size_t index = getIndex(pos);
42
43 return lookup_[index];
44 }
45
46private:
47
48 inline size_t getIndex(const vec2s& pos) const
49 {
50 return pos.x() + pos.y() * width_;
51 }
52
53 size_t width_;
54
55 mutable std::unordered_map<size_t, std::unordered_set<T>> lookup_;
56 std::unordered_map<T, size_t> reverse_;
57};
58
59#endif /* end of include guard: GRID_CACHE_H_67BBE74D */