summary refs log tree commit diff stats
path: root/src/game.cpp
blob: ebb792e75338e96edb052bd94f7e796d3aa69e6d (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
#include "game.h"

int Game::addSprite(Sprite sprite) {
  int id = sprites_.size();
  sprites_.push_back(std::move(sprite));
  spritesByY_.emplace(sprite.loc().y(), id);
  return id;
}

void Game::moveSprite(int id, vec2i newLoc) {
  Sprite& sprite = sprites_.at(id);
  bool changedY = (sprite.loc().y() != newLoc.y());
  if (changedY) {
    spritesByY_.erase(std::make_tuple(sprite.loc().y(), id));
  }
  sprite.loc() = newLoc;
  if (changedY) {
    spritesByY_.emplace(newLoc.y(), id);
  }
}

void Game::tick() {
  if (!cameraLocked_) {
    const Sprite& follow = getSprite(followingSprite_);
    vec2i mapBounds = map_->getMapSize() * map_->getTileSize();

    cameraPos_ = follow.loc() - (cameraFov_ / 2);

    if (cameraPos_.x() < 0) {
      cameraPos_.x() = 0;
    }
    if (cameraPos_.y() < 0) {
      cameraPos_.y() = 0;
    }
    if (cameraPos_.x() + cameraFov_.w() >= mapBounds.w()) {
      cameraPos_.x() = mapBounds.w() - cameraFov_.w() - 1;
    }
    if (cameraPos_.y() + cameraFov_.h() >= mapBounds.h()) {
      cameraPos_.y() = mapBounds.h() - cameraFov_.h() - 1;
    }
  }
}