Skip to content

Commit

Permalink
moved the position changes in the transform into functions (getter & …
Browse files Browse the repository at this point in the history
…setter)
  • Loading branch information
DanPeled committed May 13, 2024
1 parent e711f42 commit ad1004d
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 23 deletions.
Binary file modified main
Binary file not shown.
File renamed without changes.
8 changes: 4 additions & 4 deletions src/WoopWoop/ECS/Components/Graphics/CircleRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ namespace wpwp
{
void CircleRenderer::start()
{
m_circleShape = sf::CircleShape(entity->transform->scale.x);
m_circleShape = sf::CircleShape(entity->transform->getScale()->x);
material.color = sf::Color::White;
}

void CircleRenderer::update()
{
sf::Vector2f pos(entity->transform->globalPosition.x, entity->transform->globalPosition.y);
sf::Vector2f pos(entity->transform->getPosition()->x, entity->transform->getPosition()->y);
m_circleShape.setPosition(pos);
m_circleShape.setRadius(1);
m_circleShape.setScale(sf::Vector2f(entity->transform->scale.x, entity->transform->scale.y));
m_circleShape.setRotation(entity->transform->rotation.z);
m_circleShape.setScale(sf::Vector2f(entity->transform->getScale()->x, entity->transform->getScale()->y));
m_circleShape.setRotation(entity->transform->getRotation()->z);
m_circleShape.setFillColor(material.color);
wpwp::Engine::getInstance()->draw(m_circleShape);
}
Expand Down
8 changes: 4 additions & 4 deletions src/WoopWoop/ECS/Components/Graphics/SpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ namespace wpwp
{
if (sprite.getTexture())
{
sf::Vector2f pos(entity->transform->globalPosition.x, entity->transform->globalPosition.y);
sf::Vector2f pos(entity->transform->getPosition()->x, entity->transform->getPosition()->y);

// Calculate origin based on unscaled texture size

sprite.setScale(sf::Vector2f(entity->transform->scale.x / m_texture.getSize().x,
entity->transform->scale.y / m_texture.getSize().y));
sprite.setScale(sf::Vector2f(entity->transform->getScale()->x / m_texture.getSize().x,
entity->transform->getScale()->y / m_texture.getSize().y));

sprite.setRotation(entity->transform->rotation.z);
sprite.setRotation(entity->transform->getRotation()->z);
sprite.setColor(material.color);
sprite.setPosition(pos);
unsigned int scalar = 2;
Expand Down
51 changes: 47 additions & 4 deletions src/WoopWoop/ECS/Components/Transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,60 @@ namespace wpwp
struct Transform : public Component
{
public:
sf::Vector3f globalPosition; // Global position of the entity.
sf::Vector3f scale = sf::Vector3f(1, 1, 1); // Scale of the entity.
sf::Vector3f rotation = sf::Vector3f(0, 0, 0); // Rotation of the entity.
/**
* @brief Gets the position of the transform.
*
* @return A pointer to the position vector.
*/
sf::Vector3f *getPosition() { return &m_globalPosition; }

/**
* @brief Sets the position of the transform.
*
* @param position The new position vector.
*/
void setPosition(const sf::Vector3f &position) { m_globalPosition = position; }

/**
* @brief Gets the rotation of the transform.
*
* @return A pointer to the rotation vector.
*/
sf::Vector3f *getRotation() { return &m_rotation; }

/**
* @brief Sets the rotation of the transform.
*
* @param rotation The new rotation vector.
*/
void setRotation(const sf::Vector3f &rotation) { m_rotation = rotation; }

/**
* @brief Gets the scale of the transform.
*
* @return A pointer to the scale vector.
*/
sf::Vector3f *getScale() { return &m_scale; }

/**
* @brief Sets the scale of the transform.
*
* @param scale The new scale vector.
*/
void setScale(const sf::Vector3f &scale) { m_scale = scale; }

/**
* @brief Gets the name of the component.
*
* @return The name of the component.
*/
std::string getName() const override { return "Transform"; }

private:
sf::Vector3f m_globalPosition; // Global position of the entity.
sf::Vector3f m_scale = sf::Vector3f(1, 1, 1); // Scale of the entity.
sf::Vector3f m_rotation = sf::Vector3f(0, 0, 0); // Rotation of the entity.
};
} // namespace wpwp

#endif // TRANSFORM_HPP
#endif // TRANSFORM_HPP
4 changes: 2 additions & 2 deletions src/WoopWoop/ECS/Enitiy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace wpwp
Entity::Entity(sf::Vector3f initialPosition) : transform(new Transform())
{
this->transform = new Transform();
transform->globalPosition = initialPosition;
transform->rotation = sf::Vector3f(0, 0, 0);
transform->setPosition(initialPosition);
transform->setRotation(sf::Vector3f(0, 0, 0));
this->m_components = std::vector<Component *>();
}

Expand Down
12 changes: 6 additions & 6 deletions src/WoopWoop/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ namespace wpwp::Editor
if (selectedEntity)
{
ImGui::Text("Position: ");
ImGui::InputFloat("X", &selectedEntity->transform->globalPosition.x);
ImGui::InputFloat("Y", &selectedEntity->transform->globalPosition.y);
ImGui::InputFloat("X", &selectedEntity->transform->getPosition()->x);
ImGui::InputFloat("Y", &selectedEntity->transform->getPosition()->y);

ImGui::Dummy(ImVec2(0.0f, 20.0f));

ImGui::Text("Scale: ");
ImGui::InputFloat("X scale", &selectedEntity->transform->scale.x);
ImGui::InputFloat("Y scale", &selectedEntity->transform->scale.y);
ImGui::InputFloat("X scale", &selectedEntity->transform->getScale()->x);
ImGui::InputFloat("Y scale", &selectedEntity->transform->getScale()->y);

ImGui::Dummy(ImVec2(0.0f, 20.0f));
float rads = selectedEntity->transform->rotation.z * 3.14 / 180;
float rads = selectedEntity->transform->getScale()->z * 3.14 / 180;
ImGui::SliderAngle("Angle", &rads, 0, 360);
selectedEntity->transform->rotation.z = rads * 180 / 3.14;
selectedEntity->transform->getScale()->z = rads * 180 / 3.14;

for (auto i : comps)
{
Expand Down
2 changes: 1 addition & 1 deletion src/demo/components/mouseController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void MouseController::update()
if (wpwp::Input::isKeyReleased(sf::Keyboard::C))
{
sf::Vector2i mousePos = wpwp::Input::getMouseWorldPosition();
entity->transform->globalPosition = sf::Vector3f(mousePos.x, mousePos.y, 0);
entity->transform->setPosition(sf::Vector3f(mousePos.x, mousePos.y, 0));
}

if (rend)
Expand Down
Binary file modified src/demo/components/mouseController.o
Binary file not shown.
6 changes: 4 additions & 2 deletions src/demo/demoScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ namespace demo

void MainScene::load()
{
// TODO: figure out why it doesnt affect the scale
e = wpwp::Entity(sf::Vector3f(30, 30, 0));
e.transform->scale = sf::Vector3f(100, 100, 0);
e.addComponent(new CircleRenderer());
e.transform->setScale(sf::Vector3f(100, 100, 0));
std::cout << e.transform->getScale()->x << std::endl;
Entity::instantiate(&e);

e1 = wpwp::Entity(sf::Vector3f(800, 30, 0));
e1.transform->scale = sf::Vector3f(100, 100, 0);
e1.transform->setScale(sf::Vector3f(100, 100, 0));
e1.addComponent(new SpriteRenderer());
SpriteRenderer *sp = e1.getComponent<SpriteRenderer>();
e1.addComponent(new MouseController());
Expand Down

0 comments on commit ad1004d

Please sign in to comment.