33// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
44//
55// Released under the BSD License
6- // See LICENSE.txt for full details.
6+ // See LICENSE in root directory for full details.
77// ----------------------------------------------------------------
88
99#include " Actor.h"
@@ -17,6 +17,7 @@ Actor::Actor(Game* game)
1717 ,mRotation(Quaternion::Identity)
1818 ,mScale(1 .0f )
1919 ,mGame(game)
20+ ,mRecomputeWorldTransform(true )
2021{
2122 mGame ->AddActor (this );
2223}
@@ -36,8 +37,12 @@ void Actor::Update(float deltaTime)
3637{
3738 if (mState == EActive)
3839 {
40+ ComputeWorldTransform ();
41+
3942 UpdateComponents (deltaTime);
4043 UpdateActor (deltaTime);
44+
45+ ComputeWorldTransform ();
4146 }
4247}
4348
@@ -53,20 +58,60 @@ void Actor::UpdateActor(float deltaTime)
5358{
5459}
5560
61+ void Actor::ProcessInput (const uint8_t * keyState)
62+ {
63+ if (mState == EActive)
64+ {
65+ // First process input for components
66+ for (auto comp : mComponents )
67+ {
68+ comp->ProcessInput (keyState);
69+ }
70+
71+ ActorInput (keyState);
72+ }
73+ }
74+
75+ void Actor::ActorInput (const uint8_t * keyState)
76+ {
77+ }
78+
5679void Actor::ComputeWorldTransform ()
5780{
58- // Scale, then rotate, then translate
59- mWorldTransform = Matrix4::CreateScale (mScale );
60- mWorldTransform *= Matrix4::CreateFromQuaternion (mRotation );
61- mWorldTransform *= Matrix4::CreateTranslation (mPosition );
81+ if (mRecomputeWorldTransform )
82+ {
83+ mRecomputeWorldTransform = false ;
84+ // Scale, then rotate, then translate
85+ mWorldTransform = Matrix4::CreateScale (mScale );
86+ mWorldTransform *= Matrix4::CreateFromQuaternion (mRotation );
87+ mWorldTransform *= Matrix4::CreateTranslation (mPosition );
88+
89+ // Inform components world transform updated
90+ for (auto comp : mComponents )
91+ {
92+ comp->OnUpdateWorldTransform ();
93+ }
94+ }
6295}
6396
6497void Actor::AddComponent (Component* component)
6598{
66- mComponents .emplace_back (component);
67- std::sort (mComponents .begin (), mComponents .end (), [](Component* a, Component* b) {
68- return a->GetUpdateOrder () < b->GetUpdateOrder ();
69- });
99+ // Find the insertion point in the sorted vector
100+ // (The first element with a order higher than me)
101+ int myOrder = component->GetUpdateOrder ();
102+ auto iter = mComponents .begin ();
103+ for (;
104+ iter != mComponents .end ();
105+ ++iter)
106+ {
107+ if (myOrder < (*iter)->GetUpdateOrder ())
108+ {
109+ break ;
110+ }
111+ }
112+
113+ // Inserts element before position of iterator
114+ mComponents .insert (iter, component);
70115}
71116
72117void Actor::RemoveComponent (Component* component)
0 commit comments