summary refs log tree commit diff stats
path: root/src/systems/mapping.cpp
blob: 5b63ded617a0ab92cf497079b03d8f345453ac82 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "mapping.h"
#include "components/mappable.h"
#include "game.h"
#include "consts.h"

template <typename Storage>
inline void addBoundary(
  Storage& boundaries,
  int axis,
  int lower,
  int upper,
  MappableComponent::Boundary::Type type)
{
  boundaries.emplace(std::piecewise_construct,
    std::tie(axis),
    std::tie(axis, lower, upper, type));
}

void MappingSystem::render(Texture& texture)
{
  auto entities = game_.getEntityManager().getEntitiesWithComponents<
    MappableComponent>();

  for (id_type entity : entities)
  {
    auto& mappable = game_.getEntityManager().
      getComponent<MappableComponent>(entity);

    const Map& map = game_.getWorld().getMap(mappable.getMapId());

    for (int i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++)
    {
      int x = i % MAP_WIDTH;
      int y = i / MAP_WIDTH;
      int tile = map.getTiles()[i];

      if (tile > 0)
      {
        Rectangle dst {
          x * TILE_WIDTH,
          y * TILE_HEIGHT,
          TILE_WIDTH,
          TILE_HEIGHT};

        Rectangle src {
          (tile % TILESET_COLS) * TILE_WIDTH,
          (tile / TILESET_COLS) * TILE_HEIGHT,
          TILE_WIDTH,
          TILE_HEIGHT};

        texture.blit(mappable.getTileset(), std::move(src), std::move(dst));
      }
    }

    int startX = ((GAME_WIDTH / TILE_WIDTH) / 2) - (map.getTitle().size() / 2);
    for (size_t i = 0; i < map.getTitle().size(); i++)
    {
      Rectangle src {
        (map.getTitle()[i] % FONT_COLS) * TILE_WIDTH,
        (map.getTitle()[i] / FONT_COLS) * TILE_HEIGHT,
        TILE_WIDTH,
        TILE_HEIGHT};

      Rectangle dst {
        (startX + static_cast<int>(i)) * TILE_WIDTH,
        24 * TILE_HEIGHT,
        TILE_WIDTH,
        TILE_HEIGHT};

      texture.blit(mappable.getFont(), std::move(src), std::move(dst));
    }
  }
}

void MappingSystem::loadMap(size_t mapId)
{
  id_type mapEntity = game_.getEntityManager().emplaceEntity();

  auto& mappable = game_.getEntityManager().
    emplaceComponent<MappableComponent>(mapEntity,
      Texture("res/tiles.png"),
      Texture("res/font.bmp"));

  mappable.setMapId(mapId);

  const Map& map = game_.getWorld().getMap(mappable.getMapId());

  for (size_t i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++)
  {
    size_t x = i % MAP_WIDTH;
    size_t y = i / MAP_WIDTH;
    int tile = map.getTiles()[i];

    if ((tile >= 5) && (tile <= 7))
    {
      addBoundary(
        mappable.getDownBoundaries(),
        y * TILE_HEIGHT,
        x * TILE_WIDTH,
        (x + 1) * TILE_WIDTH,
        MappableComponent::Boundary::Type::platform);
    } else if ((tile > 0) && (tile < 28))
    {
      addBoundary(
        mappable.getRightBoundaries(),
        x * TILE_WIDTH,
        y * TILE_HEIGHT,
        (y+1) * TILE_HEIGHT,
        MappableComponent::Boundary::Type::wall);

      addBoundary(
        mappable.getLeftBoundaries(),
        (x+1) * TILE_WIDTH,
        y * TILE_HEIGHT,
        (y+1) * TILE_HEIGHT,
        MappableComponent::Boundary::Type::wall);

      addBoundary(
        mappable.getDownBoundaries(),
        y * TILE_HEIGHT,
        x * TILE_WIDTH,
        (x+1) * TILE_WIDTH,
        MappableComponent::Boundary::Type::wall);

      addBoundary(
        mappable.getUpBoundaries(),
        (y+1) * TILE_HEIGHT,
        x * TILE_WIDTH,
        (x+1) * TILE_WIDTH,
        MappableComponent::Boundary::Type::wall);
    } else if (tile == 42)
    {
      addBoundary(
        mappable.getDownBoundaries(),
        y * TILE_HEIGHT,
        x * TILE_WIDTH,
        (x+1) * TILE_WIDTH,
        MappableComponent::Boundary::Type::danger);
    }
  }
}