-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cpp
More file actions
105 lines (84 loc) · 2.5 KB
/
Copy pathGameObject.cpp
File metadata and controls
105 lines (84 loc) · 2.5 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "GameObject.h"
#include "Components/Sprite.h"
#include "Components/RectShape.h"
#include "Components/Collider.h"
#include "Components/CircleShape.h"
#include "Components/Physics.h"
void GameObject::addSpriteComponent() {
if (Scene::createdSprites[uniqueId] == nullptr) {
new Sprite(uniqueId);
}
}
Sprite* GameObject::getSpriteComponent() const {
return Scene::createdSprites[uniqueId];
}
void GameObject::addRectComponent() {
if (Scene::createdRects[uniqueId] == nullptr) {
new RectShape(uniqueId);
}
}
RectShape* GameObject::getRectComponent() const {
return Scene::createdRects[uniqueId];
}
void GameObject::addColliderComponent() {
if (Scene::createdColliders[uniqueId] == nullptr) {
new Collider(uniqueId);
}
}
Collider* GameObject::getColliderComponent() const {
return Scene::createdColliders[uniqueId];
}
void GameObject::addCircleComponent() {
if (Scene::createdCircles[uniqueId] == nullptr) {
new CircleShape(uniqueId);
}
}
CircleShape* GameObject::getCircleComponent() const {
return Scene::createdCircles[uniqueId];
}
void GameObject::addPhysicsComponent() {
if (Scene::createdPhysics[uniqueId] == nullptr) {
new Physics(uniqueId);
}
}
Physics* GameObject::getPhysicsComponent() const {
return Scene::createdPhysics[uniqueId];
}
void GameObject::setPosition(const sf::Vector2<float>& pos) {
position = pos;
}
GameObject::GameObject() {
allocateId();
Scene::_createdObjects.resize(Scene::_objectCounter);
Scene::_createdObjects[uniqueId] = this;
Scene::createdSprites.resize(Scene::_objectCounter);
Scene::createdSprites[uniqueId] = nullptr;
Scene::createdRects.resize(Scene::_objectCounter);
Scene::createdRects[uniqueId] = nullptr;
Scene::createdColliders.resize(Scene::_objectCounter);
Scene::createdColliders[uniqueId] = nullptr;
Scene::createdCircles.resize(Scene::_objectCounter);
Scene::createdCircles[uniqueId] = nullptr;
Scene::createdPhysics.resize(Scene::_objectCounter);
Scene::createdPhysics[uniqueId] = nullptr;
}
GameObject::~GameObject() {
delete getSpriteComponent();
delete getRectComponent();
delete getColliderComponent();
delete getCircleComponent();
delete getPhysicsComponent();
Scene::_availableId.push_back(uniqueId);
Scene::_createdObjects[uniqueId] = nullptr;
}
// Check if there is available ID from the deleted objects
void GameObject::allocateId() {
if (!Scene::_availableId.empty()) {
uniqueId = Scene::_availableId.back();
Scene::_availableId.pop_back();
}
else {
uniqueId = Scene::_objectCounter;
Scene::_objectCounter++;
}
}