#include "Actor.h" #include "EngineFrame/Scene/Scene.h" #include Actor::Actor() { } Actor::~Actor() { } void Actor::Init() { } void Actor::HandleEvents(SDL_Event *e) { } void Actor::Update(float deltaTime) { Actor_base::Update(deltaTime); RefPtr 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 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 me = this; Parent->m_Actors.Remove(me); RefPtr 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) { m_Components.PushBack(Component); Component->OnAdded(this); // 排序组件 Component->ReorderComponents(); // 如果组件有transform标签,则设置其位置 if (Component->hasTag(Tag::TRANSFORM)) Component->SetIterationPos(Pos); } void Actor::RemoveComponent(RefPtr Component) { Component->Parent = nullptr; m_Components.Remove(Component); } void Actor::SetPos(VecPos pos) { this->Pos = pos; RefPtr 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(); } }