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
9
CollisionScript::CollisionScript
() {
10
m_collide
=
false
;
11
}
12
13
void
CollisionScript::update
() {
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
22
auto
arrows =
SceneManager::getInstance
().
getSceneTree
()->
findGameObjectsByTag
(
"Arrow"
);
23
for
(
auto
arrow : arrows) {
24
auto
arrowCollide = arrow->getComponent<
Collide
>();
25
if
(arrowCollide ==
nullptr
)
continue
;
26
if
(collide->isColliding(arrowCollide)) {
27
m_owner
->
getSceneNode
()->
setDestroy
(
true
);
28
arrow->getSceneNode()->setDestroy(
true
);
29
}
30
}
31
}
32
33
void
CollisionScript::render
() {
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
}
Collide
Component to handle collisions between game objects.
Definition:
collide.h:24
CollisionScript::CollisionScript
CollisionScript()
Constructs a new Collision Script object.
Definition:
collision_script.cpp:9
CollisionScript::update
void update() override
Updates the collision status of the associated game object.
Definition:
collision_script.cpp:13
CollisionScript::render
void render() override
Renders collision-related visuals if necessary.
Definition:
collision_script.cpp:33
CollisionScript::m_collide
bool m_collide
Definition:
collision_script.h:21
GameObject::getSceneNode
SceneNode * getSceneNode()
Gets the SceneNode associated with this GameObject.
Definition:
gameobject.cpp:36
GameObject::getComponent
T * getComponent()
Gets a component of the specified type.
Definition:
gameobject.h:123
SceneManager::getInstance
static SceneManager & getInstance()
Retrieves the singleton instance of SceneManager.
Definition:
scenemanager.cpp:15
SceneManager::getSceneTree
SceneTree * getSceneTree()
Retrieves the current scene tree managing the game objects.
Definition:
scenemanager.cpp:28
SceneNode::setDestroy
void setDestroy(bool destroy)
Sets the destruction state of this node.
Definition:
scene.cpp:65
SceneTree::findGameObjectsByTag
std::vector< GameObject * > findGameObjectsByTag(std::string tag)
Finds GameObjects by a tag.
Definition:
scene.cpp:114
Script::m_owner
GameObject * m_owner
Pointer to the GameObject that owns this script.
Definition:
script.h:25
collide.h
Defines the Collide class, a component for managing collision detection and response.
collision_script.h
gameobject.h
scenemanager.h
Defines the SceneManager class for managing scenes in the game.
texture.h
Defines the Texture component for managing textures within the Game Engine, encapsulating SDL texture...
Engine
src
scripts
collision_script.cpp
Generated by
1.9.1