summary refs log tree commit diff stats
path: root/src/transform_system.h
blob: 67073477be62b8fed3a0cc10741e0cc9f54267a2 (plain) (blame)
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
#ifndef TRANSFORM_SYSTEM_H_BA2633BC
#define TRANSFORM_SYSTEM_H_BA2633BC

#include <range/v3/all.hpp>
#include <set>
#include <map>
#include <tuple>
#include "direction.h"
#include "system.h"
#include "vector.h"
#include "sprite.h"
#include "consts.h"

class Game;

struct AxisResult {
  Direction dir;
  bool blocked = false;
  int colliderSprite = -1;
};

struct CollisionResult {
  AxisResult horiz;
  AxisResult vert;
};

class TransformSystem : public System {
public:

  static constexpr SystemKey Key = SystemKey::Transform;

  TransformSystem(Game& game) : game_(game) {}

  void initSprite(int spriteId, vec2i loc, SpriteLayer layer = SpriteLayer::Normal);

  void setUpCollision(int spriteId, vec2i offset, vec2i size, bool solid);

  void undoCollision(int spriteId);

  void moveSprite(int spriteId, vec2i newLoc);

  auto getSpritesByY(SpriteLayer layer) const {
    return spritesByY_[static_cast<size_t>(layer)] | ranges::views::transform([] (const std::tuple<int, int>& val) {
      return std::get<1>(val);
    });
  }

  CollisionResult checkCollision(int spriteId, vec2i curLoc, vec2i newLoc, Direction dir);

  CharacterMedium getMediumAtPosition(int spriteId, vec2i newLoc);

  void destroySprite(int spriteId) override;

private:

  Game& game_;
  std::set<std::tuple<int, int>> spritesByY_[NUM_SPRITE_LAYERS];

  struct Collidable {
    int lower;
    int upper;

    Collidable(int lower, int upper) : lower(lower), upper(upper) {}
  };

  // The way these work is that the key is (axis position, entity ID). The
  // entity ID being there makes the mapping unique and makes removal easy.
  using asc_collidables_type =
    std::map<
      std::tuple<int, int>,
      const Collidable,
      std::less<std::tuple<int, int>>>;

  using desc_collidables_type =
    std::map<
      std::tuple<int, int>,
      const Collidable,
      std::greater<std::tuple<int, int>>>;

  desc_collidables_type leftCollidables_;
  asc_collidables_type rightCollidables_;
  desc_collidables_type upCollidables_;
  asc_collidables_type downCollidables_;

  void addCollidable(int spriteId);

  void removeCollidable(int spriteId);
};

#endif /* end of include guard: TRANSFORM_SYSTEM_H_BA2633BC */