The Table Engine
collision_script.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "collision_script.h"
4 #include "scenemanager.h"
5 #include "gameobject.h"
6 #include "collide.h"
7 #include "texture.h"
8 
10  m_collide = false;
11 }
12 
14  auto collide = m_owner->getComponent<Collide>();
15 
16  auto players = SceneManager::getInstance().getSceneTree()->findGameObjectsByTag("Player");
17  if (players.empty()) return;
18  auto playerCollide = players[0]->getComponent<Collide>();
19 
20  m_collide = collide->isColliding(playerCollide);
21 
23  for (auto arrow : arrows) {
24  auto arrowCollide = arrow->getComponent<Collide>();
25  if (arrowCollide == nullptr) continue;
26  if (collide->isColliding(arrowCollide)) {
28  arrow->getSceneNode()->setDestroy(true);
29  }
30  }
31 }
32 
34  auto collide = m_owner->getComponent<Collide>();
35  if (collide == nullptr) return;
36 
37  if (m_collide) {
38  // draw a red rectangle around the object this script is attached to equal to its hitbox
39  SDL_SetRenderDrawColor(SceneManager::getInstance().getRenderer(), 255, 0, 0, 255);
40  SDL_RenderDrawRect(SceneManager::getInstance().getRenderer(), collide->getRect());
41  }
42 }
Component to handle collisions between game objects.
Definition: collide.h:24
CollisionScript()
Constructs a new Collision Script object.
void update() override
Updates the collision status of the associated game object.
void render() override
Renders collision-related visuals if necessary.
SceneNode * getSceneNode()
Gets the SceneNode associated with this GameObject.
Definition: gameobject.cpp:36
T * getComponent()
Gets a component of the specified type.
Definition: gameobject.h:123
static SceneManager & getInstance()
Retrieves the singleton instance of SceneManager.
SceneTree * getSceneTree()
Retrieves the current scene tree managing the game objects.
void setDestroy(bool destroy)
Sets the destruction state of this node.
Definition: scene.cpp:65
std::vector< GameObject * > findGameObjectsByTag(std::string tag)
Finds GameObjects by a tag.
Definition: scene.cpp:114
GameObject * m_owner
Pointer to the GameObject that owns this script.
Definition: script.h:25
Defines the Collide class, a component for managing collision detection and response.
Defines the SceneManager class for managing scenes in the game.
Defines the Texture component for managing textures within the Game Engine, encapsulating SDL texture...