Files
IAEngine/Engine/Src/Inc/IAEngine/Nodes/Node2D.hpp
Isuru Samarathunga 09131d7fab TileMap Component
2025-10-11 23:54:31 +05:30

205 lines
5.0 KiB
C++

// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <IAEngine/Components/IComponent.hpp>
#include <IAEngine/Nodes/INode.hpp>
namespace ia::iae
{
class Node2D : public INode
{
public:
Node2D(IN CONST String &name);
~Node2D();
public:
template<typename _component_type> _component_type *AddComponent()
{
const auto t = new _component_type(this);
m_components.pushBack(t);
return t;
}
template<typename _component_type> _component_type *GetComponent()
{
for (auto &c : m_components)
{
_component_type *comp = dynamic_cast<_component_type *>(c.get());
if (comp)
return comp;
}
return nullptr;
}
template<typename _component_type> Vector<_component_type *> GetComponents()
{
Vector<_component_type *> result;
for (auto &c : m_components)
{
_component_type *comp = dynamic_cast<_component_type *>(c.get());
if (comp)
result.pushBack(comp);
}
return result;
}
VOID AddChild(IN RefPtr<Node2D> node)
{
m_children.pushBack(node);
}
public:
VOID Translate(IN Vec2 v)
{
m_local.Position += v;
RecalculatePosition();
}
VOID Scale(IN FLOAT32 v)
{
m_local.Scale *= v;
RecalculateScale();
}
VOID Scale(IN Vec2 v)
{
m_local.Scale *= v;
RecalculateScale();
}
VOID Rotate(IN FLOAT32 v)
{
m_local.Rotation += v;
RecalculateRotation();
}
VOID SetLocalPosition(IN CONST Vec2 &v)
{
m_local.Position = v;
RecalculatePosition();
}
VOID SetLocalScale(IN CONST Vec2 &v)
{
m_local.Scale = v;
RecalculateScale();
}
VOID SetLocalRotation(IN FLOAT32 v)
{
m_local.Rotation = v;
RecalculateRotation();
}
public:
CONST Vec2 &GetPosition() CONST
{
return m_global.Position;
}
CONST Vec2 &GetScale() CONST
{
return m_global.Scale;
}
CONST FLOAT32 &GetRotation() CONST
{
return m_global.Rotation;
}
CONST Vec2 &GetLocalPosition() CONST
{
return m_local.Position;
}
CONST Vec2 &GetLocalScale() CONST
{
return m_local.Scale;
}
CONST FLOAT32 &GetLocalRotation() CONST
{
return m_local.Rotation;
}
UINT8 &Layer()
{
return m_layer;
}
INT16 &SortIndex()
{
return m_sortIndex;
}
CONST UINT8 &Layer() CONST
{
return m_layer;
}
CONST INT16 &SortIndex() CONST
{
return m_sortIndex;
}
protected:
VOID RecalculatePosition()
{
m_global.Position = (m_parent ? m_parent->GetPosition() : Vec2{}) + m_local.Position;
for (auto &c : m_children)
c->RecalculatePosition();
}
VOID RecalculateRotation()
{
m_global.Rotation = (m_parent ? m_parent->GetRotation() : 0) + m_local.Rotation;
for (auto &c : m_children)
c->RecalculateRotation();
}
VOID RecalculateScale()
{
m_global.Scale = (m_parent ? m_parent->GetScale() : Vec2{}) + m_local.Scale;
for (auto &c : m_children)
c->RecalculateScale();
}
protected:
RefPtr<Node2D> m_parent{};
Vector<IComponent *> m_components;
Vector<RefPtr<Node2D>> m_children{};
private:
struct
{
FLOAT32 Rotation{0.0f};
Vec2 Position{0.0f, 0.0f};
Vec2 Scale{1.0f, 1.0f};
} m_local{}, m_global{};
UINT8 m_layer{};
INT16 m_sortIndex{};
public:
VOID Draw();
VOID DebugDraw();
VOID Update();
VOID FixedUpdate();
};
} // namespace ia::iae