TazGraph Project v0.1.0
Loading...
Searching...
No Matches
TransformComponent.h
1#pragma once
2
3#include "../../../Components.h"
4#include "../../../Animators/MovingAnimation.h"
5
6class TransformComponent : public Component //transform as in graphics, we have rotation and scale
7{
8public:
9 glm::vec3 velocity = glm::vec3(0);
10 glm::vec3 rotation = { 0.0f,0.0f,0.0f };
11 glm::vec3 position = glm::vec3(0);
12 glm::vec3 local_position = glm::vec3(0);
13 glm::vec3 local_normal_position = glm::vec3(0);
14 glm::vec3 size = glm::vec3(0);
15
16 glm::vec3 last_position = glm::vec3(0);
17 glm::vec3 last_size = glm::vec3(0);
18 glm::vec3 last_velocity = glm::vec3(0);
19
20 float scale = 1;
21
22 int speed = 1;
23 MovingAnimation moving_animation;
24
25
27 {
28 }
29
30 TransformComponent(float sc)
31 {
32 scale = sc;
33 }
34
35 TransformComponent(glm::vec2 m_position)
36 {
37 position.x = m_position.x;
38 position.y = m_position.y;
39 }
40
41 TransformComponent(glm::vec3 m_position)
42 {
43 position = m_position;
44 }
45
46 TransformComponent(glm::vec2 m_position, layer layer, glm::vec2 m_size, float sc) : TransformComponent(m_position) {
47 position = { m_position.x, m_position.y, getLayerDepth(layer) };
48 size = { m_size.x, m_size.y, 0.0f };
49 scale = sc;
50 }
51
52 TransformComponent(glm::vec2 m_position, layer layer, glm::vec2 size, float sc, int sp) : TransformComponent(m_position, layer, size, sc)
53 {
54 speed = sp;
55 }
56
57 TransformComponent(glm::vec2 m_position, layer layer, glm::vec3 m_size, float sc) : TransformComponent(m_position) {
58 size = m_size;
59 scale = sc;
60 }
61
62 TransformComponent(glm::vec2 m_position, layer layer, glm::vec3 size, float sc, int sp) : TransformComponent(m_position, layer, size, sc)
63 {
64 speed = sp;
65 }
66
67 TransformComponent(glm::vec3 m_position, glm::vec3 m_size, float sc) : TransformComponent(m_position) {
68 size = m_size;
69 scale = sc;
70 }
71
72 void init() override
73 {
74 }
75 void update(float deltaTime) override
76 {
77
78 if (entity->getParentEntity()
79 && (dynamic_cast<NodeEntity*>(entity->getParentEntity())
80 || dynamic_cast<EmptyEntity*>(entity->getParentEntity())))
81 {
82 Entity* parent = entity->getParentEntity();
83 TransformComponent* parentTR = &parent->GetComponent<TransformComponent>();
84 if (!glm::all(glm::equal(local_normal_position, glm::vec3(0.0f)))) {
85 position = parentTR->getPosition() + local_normal_position * parentTR->size / 2.0f;
86 }
87 else {
88 position = parentTR->getPosition() + local_position;
89 }
90
91 }
92
93 if (position == last_position && size == last_size && velocity == last_velocity) {
94 return;
95 }
96
97 entity->cellUpdate();
98
99 last_position = position;
100 last_size = size;
101 last_velocity = velocity;
102
103 position.x += velocity.x * speed * deltaTime;
104 position.y += velocity.y * speed * deltaTime;
105
106 velocity *= 0.98f;
107
108 }
109
110 void initChild() {
111 if (entity->getParentEntity()
112 && (dynamic_cast<NodeEntity*>(entity->getParentEntity())
113 || dynamic_cast<EmptyEntity*>(entity->getParentEntity())))
114 {
115 Entity* parent = entity->getParentEntity();
116 TransformComponent* parentTR = &parent->GetComponent<TransformComponent>();
117
118 if (!glm::all(glm::equal(local_normal_position, glm::vec3(0.0f)))) {
119 position = parentTR->getPosition() + local_normal_position * parentTR->size / 2.0f;
120 }
121 else {
122 position = parentTR->getPosition() + local_position;
123 }
124
125 }
126 }
127
128 glm::vec3 getSizeCenter() {
129 return glm::vec3(size.x * scale / 2, size.y * scale / 2, size.z * scale / 2);
130 }
131
132 glm::vec3 getPosition() {
133 return position;
134 }
135
136 void setPosition_X(float newPosition_X) {
137 position.x = newPosition_X;
138 }
139 void setPosition_Y(float newPosition_Y) {
140 position.y = newPosition_Y;
141 }
142
143 glm::vec3 getVelocity() {
144 return velocity;
145 }
146
147 void setVelocity_X(float newVelocity_X) {
148 velocity.x = newVelocity_X;
149 }
150 void setVelocity_Y(float newVelocity_Y) {
151 velocity.y = newVelocity_Y;
152 }
153
154 void setRotation(glm::vec3 m_rotation) {
155 rotation = m_rotation;
156 }
157
158 void SetMovingAnimation(
159 glm::vec3 m_startingPos,
160 size_t fr,
161 float sp,
162 const Animation::animType type,
163 const glm::vec3 distance,
164 const glm::vec3 dest_rotation,
165 int reps = 0)
166 {
167 moving_animation = MovingAnimation(m_startingPos, fr, sp, type, distance, dest_rotation, reps);
168 }
169
170 void setMoveFrame() {
171
172 float progress = (float)moving_animation.cur_frame_index /
173 (float)moving_animation.total_frames;
174
175 // Use linear interpolation
176 float newX = moving_animation.startingPosition.x +
177 ((moving_animation.distance.x) * progress);
178
179 float newY = moving_animation.startingPosition.y +
180 ((moving_animation.distance.y) * progress); // Fixed typo: was .x instead of .y
181
182 setPosition_X(newX);
183 setPosition_Y(newY);
184 }
185
186 std::string GetComponentName() override {
187 return "TransformComponent";
188 }
189
190 void showGUI(std::vector<BaseComponent*> otherComponents = {}) override {
191 showGUI(otherComponents, { entity });
192 };
193
194 void showGUI(std::vector<BaseComponent*> otherComponents, std::vector<Entity*> otherEntities) override {
195 ImGui::Separator();
196
197 // TazPosition Controls
198 ImGui::Text("TazPosition:");
199 ImGui::SliderFloat3("##position", &position.x, -1000.0f, 1000.0f);
200
201 if (ImGui::Button("Apply TazPosition to All##transform_pos")) {
202 modifyPosition = true;
203 }
204 if (modifyPosition && !otherComponents.empty()) {
205 for (auto& comp : otherComponents) {
206 if (auto* other = dynamic_cast<TransformComponent*>(comp)) {
207 other->position = position;
208 }
209 }
210 }
211
212 // TazSize Controls
213 ImGui::Text("TazSize:");
214 ImGui::SliderFloat3("##size", &size.x, 1.0f, 100.0f);
215
216 // TazRotation Controls
217 ImGui::Text("TazRotation:");
218 ImGui::SliderFloat3("##rotation", glm::value_ptr(rotation), -180.0f, 180.0f);
219
220 ImGui::Text("Scale:");
221 ImGui::SliderFloat("##scale", &scale, 0.1f, 10.0f);
222
223 // Speed Control
224 ImGui::Text("Speed:");
225 ImGui::InputInt("##speed", &speed);
226 }
227
228 TransformComponent& operator=(const TransformComponent& tr) {
229 if (this == &tr) {
230 return *this;
231 }
232
233 velocity = tr.velocity;
234 rotation = tr.rotation;
235 position = tr.position;
236 local_position = tr.local_position;
237 local_normal_position = tr.local_normal_position;
238 size = tr.size;
239
240 scale = tr.scale;
241 speed = tr.speed;
242
243 return *this;
244 }
245};
Definition GECS.h:131
Definition GECSEntity.h:7
Definition GECS.h:224
Definition MovingAnimation.h:9
Definition GECSEntity.h:40
Definition TransformComponent.h:7