SwitchGame/source/EngineFrame/Actor/Actor.cpp

130 lines
2.5 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Actor.h"
#include "EngineFrame/Scene/Scene.h"
#include <algorithm>
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)
{
if (child->hasTag(Tag::UPDATE))
child->Update(deltaTime);
if (child->hasTag(Tag::RENDER))
{
}
child = child->GetNext();
}
}
void Actor::Render(float deltaTime)
{
Actor_base::Render(deltaTime);
RefPtr<Component> child = m_Components.GetFirst();
while (child)
{
if (child->hasTag(Tag::RENDER))
child->Render(deltaTime);
child = child->GetNext();
}
}
void Actor::Clear()
{
}
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);
}
}
}
void Actor::AddComponent(RefPtr<Component> Component)
{
m_Components.PushBack(Component);
Component->OnAdded(this);
// 排序组件
Component->ReorderComponents();
// 如果组件有transform标签则设置其位置
if (Component->hasTag(Tag::TRANSFORM))
Component->SetIterationPos(Pos);
}
void Actor::RemoveComponent(RefPtr<Component> Component)
{
Component->Parent = nullptr;
m_Components.Remove(Component);
}
void Actor::SetPos(VecPos pos)
{
this->Pos = pos;
RefPtr<Component> child = m_Components.GetFirst();
while (child)
{
if (child->hasTag(Tag::TRANSFORM))
child->SetIterationPos(pos);
child = child->GetNext();
}
}
VecPos Actor::GetPos()
{
return this->Pos;
}
int Actor::GetRenderZOrder()
{
return this->RenderZOrder;
}
void Actor::SetRenderZOrder(int zOrder)
{
if(this->RenderZOrder != zOrder){
this->RenderZOrder = zOrder;
ReorderActors();
}
}