The Table Engine
camera.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "camera.h"
4 #include "health.h"
5 #include "scenemanager.h"
6 #include "gameobject.h"
7 #include "transform.h"
8 
10  m_worldPosition = Vec2(0, 0);
11 }
12 
14  return;
15 }
16 
18  static int health = -1;
19  // auto sceneTree = SceneManager::getInstance().getSceneTree();
20  auto player = SceneManager::getInstance().getSceneTree()->findGameObjectsByTag("Player")[0];
21  if (player == nullptr) return;
22 
23  int playerHealth = player->getComponent<Health>()->getHealth();
24  bool shakeCamera = false;
25  if (playerHealth != health) {
26  shakeCamera = true;
27  health = playerHealth;
28  }
29 
30  Vec2 playerPosition = player->getComponent<Transform>()->getWorldPosition();
31  m_worldPosition = playerPosition;
32 
33  if (shakeCamera) {
34  m_worldPosition.x += rand() % 10 - 5;
35  m_worldPosition.y += rand() % 10 - 5;
36  }
37 
38  if (SceneManager::getInstance().isDemo)
39  {
40  if (m_worldPosition.x < -320)
41  m_worldPosition.x = -320;
42  if (m_worldPosition.x > 320)
43  m_worldPosition.x = 320;
44  if (m_worldPosition.y < -400)
45  m_worldPosition.y = -400;
46  if (m_worldPosition.y > 400)
47  m_worldPosition.y = 400;
48  }
49  else
50  {
51  if (m_worldPosition.x < 320)
52  m_worldPosition.x = 320;
53  if (m_worldPosition.x > 480)
54  m_worldPosition.x = 480;
55  if (m_worldPosition.y < 240)
56  m_worldPosition.y = 240;
57  if (m_worldPosition.y > 560)
58  m_worldPosition.y = 560;
59  }
60 }
61 
63  return m_worldPosition;
64 }
65 
66 void Camera::setWorldPosition(float x, float y) {
67  m_worldPosition = Vec2(x, y);
68 }
Camera()
Definition: camera.cpp:9
void update()
Definition: camera.cpp:17
Vec2 m_worldPosition
Stores the world position of the camera.
Definition: camera.h:45
Vec2 getWorldPosition()
Definition: camera.cpp:62
void setWorldPosition(float x, float y)
Definition: camera.cpp:66
~Camera()
Definition: camera.cpp:13
A component for managing health of game objects.
Definition: health.h:22
static SceneManager & getInstance()
Retrieves the singleton instance of SceneManager.
SceneTree * getSceneTree()
Retrieves the current scene tree managing the game objects.
std::vector< GameObject * > findGameObjectsByTag(std::string tag)
Finds GameObjects by a tag.
Definition: scene.cpp:114
Component for handling the transformation of game objects.
Definition: transform.h:23
Defines the Health class, a component for managing the health of game objects.
Defines the SceneManager class for managing scenes in the game.
A structure to represent 2D vectors.
Definition: vec2.h:16
float y
Y coordinate of the vector.
Definition: vec2.h:18
float x
X coordinate of the vector.
Definition: vec2.h:17
Provides the Transform component for positioning and sizing game objects.