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,7 +17,7 @@ Actor::Actor(Game* game)
1717 ,mRotation(Quaternion::Identity)
1818 ,mScale(1 .0f )
1919 ,mGame(game)
20- ,mRecomputeTransform (true )
20+ ,mRecomputeWorldTransform (true )
2121{
2222 mGame ->AddActor (this );
2323}
@@ -37,12 +37,12 @@ void Actor::Update(float deltaTime)
3737{
3838 if (mState == EActive)
3939 {
40- if (mRecomputeTransform )
41- {
42- ComputeWorldTransform ();
43- }
40+ ComputeWorldTransform ();
41+
4442 UpdateComponents (deltaTime);
4543 UpdateActor (deltaTime);
44+
45+ ComputeWorldTransform ();
4646 }
4747}
4848
@@ -58,21 +58,25 @@ void Actor::UpdateActor(float deltaTime)
5858{
5959}
6060
61- void Actor::ComputeWorldTransform ( )
61+ void Actor::ProcessInput ( const uint8_t * keyState )
6262{
63- mRecomputeTransform = false ;
64- // Scale, then rotate, then translate
65- mWorldTransform = Matrix4::CreateScale (mScale );
66- mWorldTransform *= Matrix4::CreateFromQuaternion (mRotation );
67- mWorldTransform *= Matrix4::CreateTranslation (mPosition );
68-
69- // Inform components world transform updated
70- for (auto comp : mComponents )
63+ if (mState == EActive)
7164 {
72- comp->OnUpdateWorldTransform ();
65+ // First process input for components
66+ for (auto comp : mComponents )
67+ {
68+ comp->ProcessInput (keyState);
69+ }
70+
71+ ActorInput (keyState);
7372 }
7473}
7574
75+ void Actor::ActorInput (const uint8_t * keyState)
76+ {
77+
78+ }
79+
7680void Actor::RotateToNewForward (const Vector3& forward)
7781{
7882 // Figure out difference between original (unit x) and new
@@ -97,12 +101,42 @@ void Actor::RotateToNewForward(const Vector3& forward)
97101 }
98102}
99103
104+ void Actor::ComputeWorldTransform ()
105+ {
106+ if (mRecomputeWorldTransform )
107+ {
108+ mRecomputeWorldTransform = false ;
109+ // Scale, then rotate, then translate
110+ mWorldTransform = Matrix4::CreateScale (mScale );
111+ mWorldTransform *= Matrix4::CreateFromQuaternion (mRotation );
112+ mWorldTransform *= Matrix4::CreateTranslation (mPosition );
113+
114+ // Inform components world transform updated
115+ for (auto comp : mComponents )
116+ {
117+ comp->OnUpdateWorldTransform ();
118+ }
119+ }
120+ }
121+
100122void Actor::AddComponent (Component* component)
101123{
102- mComponents .emplace_back (component);
103- std::sort (mComponents .begin (), mComponents .end (), [](Component* a, Component* b) {
104- return a->GetUpdateOrder () < b->GetUpdateOrder ();
105- });
124+ // Find the insertion point in the sorted vector
125+ // (The first element with a order higher than me)
126+ int myOrder = component->GetUpdateOrder ();
127+ auto iter = mComponents .begin ();
128+ for (;
129+ iter != mComponents .end ();
130+ ++iter)
131+ {
132+ if (myOrder < (*iter)->GetUpdateOrder ())
133+ {
134+ break ;
135+ }
136+ }
137+
138+ // Inserts element before position of iterator
139+ mComponents .insert (iter, component);
106140}
107141
108142void Actor::RemoveComponent (Component* component)
0 commit comments