summary refs log tree commit diff stats
path: root/src/simulation.cpp
blob: 912f7f01f01a094ce839828eb35be4cdab54d914 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "simulation.h"

#include "consts.h"
#include "level.h"

void Simulation::tick(
  double dt,
  const Uint8* keystate)
{
  for (id_type id : active_)
  {
    Entity& entity = entities_.at(id);

    // Control
    if (entity.controllable &&
        !entity.moving)
    {
      if (keystate[SDL_SCANCODE_LSHIFT] || keystate[SDL_SCANCODE_RSHIFT])
      {
        Direction lookDir = Direction::none;

        if (keystate[SDL_SCANCODE_LEFT])
        {
          lookDir = Direction::left;
        }
        else if (keystate[SDL_SCANCODE_UP])
        {
          lookDir = Direction::up;
        }
        else if (keystate[SDL_SCANCODE_RIGHT])
        {
          lookDir = Direction::right;
        }
        else if (keystate[SDL_SCANCODE_DOWN])
        {
          lookDir = Direction::down;
        }

        vec2s lookPos = posInDir(entity.gridPos, lookDir);

        for (id_type blockId : getGridEntities(lookPos))
        {
          Entity& block = entities_.at(blockId);

          if (!block.moving && block.canBePushedBy.count(ColliderType::player))
          {
            moveEntityOnGrid(blockId, lookDir);
          }
        }
      } else {
        if (keystate[SDL_SCANCODE_LEFT] &&
            moveEntityOnGrid(id, Direction::left, true))
        {
          entity.shouldMoveTo = Direction::left;
        }
        else if (keystate[SDL_SCANCODE_UP] &&
                moveEntityOnGrid(id, Direction::up, true))
        {
          entity.shouldMoveTo = Direction::up;
        }
        else if (keystate[SDL_SCANCODE_RIGHT] &&
                moveEntityOnGrid(id, Direction::right, true))
        {
          entity.shouldMoveTo = Direction::right;
        }
        else if (keystate[SDL_SCANCODE_DOWN] &&
                moveEntityOnGrid(id, Direction::down, true))
        {
          entity.shouldMoveTo = Direction::down;
        }
        else
        {
          entity.shouldMoveTo = Direction::none;
        }

        if (entity.shouldMoveTo != Direction::none)
        {
          moveEntityOnGrid(id, entity.shouldMoveTo);
        }
      }
    }




    // Collision




    // Movement
    if (entity.moving)
    {
      entity.movementTween += entity.speed * dt;

      if (entity.movementTween >= 1.0)
      {
        entity.moving = false;
        setGridPos(id, entity.destPos);
      }
    }

    if (entity.moving)
    {
      entity.pos.x() =
        TILE_SIZE.x() * entity.destPos.x() * entity.movementTween +
        TILE_SIZE.x() * entity.gridPos.x() * (1.0 - entity.movementTween);

      entity.pos.y() =
        TILE_SIZE.y() * entity.destPos.y() * entity.movementTween +
        TILE_SIZE.y() * entity.gridPos.y() * (1.0 - entity.movementTween);
    } else {
      entity.pos = TILE_SIZE * entity.gridPos;
    }
  }
}

Simulation::id_type Simulation::emplaceEntity()
{
  id_type nextId;

  if (!available_.empty())
  {
    nextId = available_.front();
    available_.pop_front();

    entities_.at(nextId) = Entity();
  } else {
    nextId = entities_.size();
    entities_.emplace_back();
  }

  active_.insert(nextId);

  return nextId;
}

void Simulation::deleteEntity(id_type id)
{
  available_.push_back(id);
  active_.erase(id);
}

void Simulation::setGridPos(id_type id, vec2s pos)
{
  Entity& entity = entities_.at(id);

  size_t oldPosIndex =
    entity.gridPos.x() + entity.gridPos.y() * level_.getSize().w();
  gridCache_[oldPosIndex].erase(id);

  entity.gridPos = pos;

  size_t newPosIndex =
    entity.gridPos.x() + entity.gridPos.y() * level_.getSize().w();
  gridCache_[newPosIndex].insert(id);
}

const std::unordered_set<Simulation::id_type>&
  Simulation::getGridEntities(vec2s pos) const
{
  size_t posIndex = pos.x() + pos.y() * level_.getSize().w();

  return gridCache_[posIndex];
}

bool Simulation::moveEntityOnGrid(
  id_type id,
  Direction moveDir,
  bool validate)
{
  bool actuallyMove = true;

  Entity& entity = entities_.at(id);

  vec2s shouldMoveTo = posInDir(entity.gridPos, moveDir);

  switch (moveDir)
  {
    case Direction::left:
    {
      if (entity.gridPos.x() == 0)
      {
        return false;
      }

      break;
    }

    case Direction::right:
    {
      if (entity.gridPos.x() == level_.getSize().w() - 1)
      {
        return false;
      }

      break;
    }

    case Direction::up:
    {
      if (entity.gridPos.y() == 0)
      {
        return false;
      }

      break;
    }

    case Direction::down:
    {
      if (entity.gridPos.y() == level_.getSize().h() - 1)
      {
        return false;
      }

      break;
    }
  }

  if (!level_.getTileset().canEntityMoveTo(
        entity.colliderType,
        level_.at(shouldMoveTo)))
  {
    return false;
  }

  for (id_type blockId : getGridEntities(shouldMoveTo))
  {
    Entity& block = entities_.at(blockId);

    if (!block.moving)
    {
      if (!block.canBePushedBy.count(entity.colliderType))
      {
        return false;
      }

      if (!moveEntityOnGrid(blockId, moveDir, validate))
      {
        return false;
      }
    }

    double entityTimeLeft = 1.0 / entity.speed;
    double blockTimeLeft = (1.0 - block.movementTween) / block.speed;

    if (entityTimeLeft < blockTimeLeft)
    {
      actuallyMove = false;
    }
  }





  if (!validate && actuallyMove)
  {
    entity.moving = true;
    entity.destPos = shouldMoveTo;
    entity.movementTween = 0.0;
  }

  return true;
}

vec2s Simulation::posInDir(vec2s orig, Direction dir)
{
  switch (dir)
  {
    case Direction::left: return orig - vec2s { 1, 0 };
    case Direction::right: return orig + vec2s { 1, 0 };
    case Direction::up: return orig - vec2s { 0, 1 };
    case Direction::down: return orig + vec2s { 0, 1 };
    case Direction::none: return orig;
  }
}