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

void CameraSystem::tick(double dt) {
  if (!locked_ && followingSprite_ != -1) {
    const Sprite& follow = game_.getSprite(followingSprite_);
    const Map& map = game_.getMap();
    vec2i mapBounds = map.getMapSize() * map.getTileSize();

    pos_ = follow.loc - vec2i{0, 16} - (fov_ / 2);

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

void CameraSystem::destroySprite(int spriteId) {
  if (followingSprite_ == spriteId) {
    followingSprite_ = -1;
  }
}

void CameraSystem::clearSpriteCache() {
  followingSprite_ = -1;
}