SwitchGame/source/EngineFrame/Actor/Actor.cpp

130 lines
2.5 KiB
C++
Raw Normal View History

2025-09-15 11:28:54 +08:00
#include "Actor.h"
#include "EngineFrame/Scene/Scene.h"
2025-09-18 15:21:43 +08:00
#include <algorithm>
2025-09-15 11:28:54 +08:00
Actor::Actor()
{
}
Actor::~Actor()
{
}
void Actor::Init()
{
}
void Actor::HandleEvents(SDL_Event *e)
{
}
void Actor::Update(float deltaTime)
{
Actor_base::Update(deltaTime);
RefPtr<Component> child = m_Components.GetFirst();
while (child)
{
2025-09-18 15:21:43 +08:00
if (child->hasTag(Tag::UPDATE))
child->Update(deltaTime);
if (child->hasTag(Tag::RENDER))
{
}
2025-09-15 11:28:54 +08:00
child = child->GetNext();
}
}
void Actor::Render(float deltaTime)
{
Actor_base::Render(deltaTime);
RefPtr<Component> child = m_Components.GetFirst();
while (child)
{
2025-09-18 15:21:43 +08:00
if (child->hasTag(Tag::RENDER))
child->Render(deltaTime);
2025-09-15 11:28:54 +08:00
child = child->GetNext();
}
}
void Actor::Clear()
{
}
2025-09-19 12:18:57 +08:00
void Actor::OnAdded(Scene *scene)
{
this->Parent = scene;
}
void Actor::ReorderActors()
{
if (Parent)
{
RefPtr<Actor> me = this;
Parent->m_Actors.Remove(me);
RefPtr<Actor> sibling = Parent->m_Actors.GetLast();
if (sibling && sibling->GetRenderZOrder() > RenderZOrder)
{
sibling = sibling->GetPrev();
while (sibling)
{
if (sibling->GetRenderZOrder() <= RenderZOrder)
break;
sibling = sibling->GetPrev();
}
}
if (sibling)
{
Parent->m_Actors.InsertAfter(me, sibling);
}
else
{
Parent->m_Actors.PushFront(me);
}
}
}
2025-09-15 11:28:54 +08:00
void Actor::AddComponent(RefPtr<Component> Component)
{
m_Components.PushBack(Component);
2025-09-18 15:21:43 +08:00
Component->OnAdded(this);
2025-09-19 12:18:57 +08:00
// 排序组件
2025-09-18 15:21:43 +08:00
Component->ReorderComponents();
2025-09-19 12:18:57 +08:00
// 如果组件有transform标签则设置其位置
if (Component->hasTag(Tag::TRANSFORM))
Component->SetIterationPos(Pos);
2025-09-18 15:21:43 +08:00
}
void Actor::RemoveComponent(RefPtr<Component> Component)
{
Component->Parent = nullptr;
m_Components.Remove(Component);
}
2025-09-19 12:18:57 +08:00
void Actor::SetPos(VecPos pos)
2025-09-18 15:21:43 +08:00
{
this->Pos = pos;
2025-09-19 12:18:57 +08:00
RefPtr<Component> child = m_Components.GetFirst();
while (child)
{
if (child->hasTag(Tag::TRANSFORM))
child->SetIterationPos(pos);
child = child->GetNext();
}
2025-09-18 15:21:43 +08:00
}
2025-09-19 12:18:57 +08:00
VecPos Actor::GetPos()
2025-09-18 15:21:43 +08:00
{
return this->Pos;
}
2025-09-19 12:18:57 +08:00
int Actor::GetRenderZOrder()
{
return this->RenderZOrder;
}
void Actor::SetRenderZOrder(int zOrder)
{
if(this->RenderZOrder != zOrder){
this->RenderZOrder = zOrder;
ReorderActors();
}
}