Skip to content

Commit 025671c

Browse files
committed
Adding ActorComponent, ActorEvent, and triggerEvents methods.
1 parent c504e23 commit 025671c

15 files changed

+294
-6
lines changed

Source/Actor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "ActorBone.hpp"
33
#include "ActorRootBone.hpp"
44
#include "ActorIKTarget.hpp"
5+
#include "ActorEvent.hpp"
56
#include "BinaryReader.hpp"
67
#include "BlockReader.hpp"
78
#include "Exceptions/OverflowException.hpp"
@@ -254,6 +255,9 @@ void Actor::readComponentsBlock(BlockReader* block)
254255
m_SolverNodeCount++;
255256
component = ActorIKTarget::read(this, componentBlock);
256257
break;
258+
case BlockType::ActorEvent:
259+
component = ActorEvent::read(this, componentBlock);
260+
break;
257261
default:
258262
// Not handled/expected block.
259263
break;

Source/Actor.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ namespace nima
2222
Animations = 8,
2323
Atlases = 9,
2424
Atlas = 10,
25-
ActorIKTarget = 11
25+
ActorIKTarget = 11,
26+
ActorEvent = 12
2627
};
2728

2829
class Actor

Source/ActorComponent.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#include <cassert>
66
using namespace nima;
77

8+
ActorComponent::ActorComponent(ComponentType type) : ActorComponent(nullptr, type)
9+
{
10+
11+
}
12+
813
ActorComponent::ActorComponent(Actor* actor, ComponentType type) :
914
m_Type(type),
1015
m_Parent(nullptr),

Source/ActorComponent.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace nima
1818
ActorBone = 3,
1919
ActorRootBone = 4,
2020
ActorImage = 5,
21-
ActorIKTarget = 11
21+
ActorIKTarget = 11,
22+
ActorEvent = 12
2223
};
2324

2425

@@ -35,6 +36,7 @@ namespace nima
3536
unsigned short m_Idx;
3637

3738
protected:
39+
ActorComponent(ComponentType type);
3840
ActorComponent(Actor* actor, ComponentType type);
3941

4042
public:

Source/ActorEvent.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "ActorEvent.hpp"
2+
#include "BlockReader.hpp"
3+
using namespace nima;
4+
5+
ActorEvent::ActorEvent() : ActorComponent(ComponentType::ActorEvent)
6+
{
7+
8+
}
9+
10+
ActorComponent* ActorEvent::makeInstance(Actor* resetActor)
11+
{
12+
ActorEvent* eventInstance = new ActorEvent();
13+
eventInstance->copy(this, resetActor);
14+
return eventInstance;
15+
16+
}
17+
18+
void ActorEvent::copy(ActorComponent* node, Actor* resetActor)
19+
{
20+
Base::copy(node, resetActor);
21+
}
22+
23+
ActorEvent* ActorEvent::read(Actor* actor, BlockReader* reader, ActorEvent* component)
24+
{
25+
if(component == nullptr)
26+
{
27+
component = new ActorEvent();
28+
}
29+
30+
Base::read(actor, reader, component);
31+
32+
return component;
33+
}

Source/ActorEvent.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef _NIMA_ACTOREVENT_HPP_
2+
#define _NIMA_ACTOREVENT_HPP_
3+
4+
#include <string>
5+
#include "ActorComponent.hpp"
6+
7+
namespace nima
8+
{
9+
class ActorEvent : public ActorComponent
10+
{
11+
typedef ActorComponent Base;
12+
public:
13+
ActorEvent();
14+
ActorComponent* makeInstance(Actor* resetActor) override;
15+
void copy(ActorComponent* node, Actor* resetActor);
16+
17+
static ActorEvent* read(Actor* actor, BlockReader* reader, ActorEvent* component = NULL);
18+
};
19+
}
20+
#endif

Source/ActorNode.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef _NIMA_ACTORNODE_HPP_
22
#define _NIMA_ACTORNODE_HPP_
33

4-
#include <string>
54
#include <vector>
65
#include <nima/Mat2D.hpp>
76
#include <nima/Vec2D.hpp>

Source/Animation/ActorAnimation.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ActorAnimation.hpp"
2+
#include "../ActorComponent.hpp"
23
#include "../BlockReader.hpp"
34

45
using namespace nima;
@@ -41,6 +42,14 @@ void ActorAnimation::apply(float time, Actor* actor, float mix)
4142
}
4243
}
4344

45+
void ActorAnimation::triggerEvents(ActorComponent** components, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events)
46+
{
47+
for(auto keyedComponent : m_TriggerComponents)
48+
{
49+
keyedComponent->triggerEvents(components, fromTime, toTime, events);
50+
}
51+
}
52+
4453
void ActorAnimation::read(BlockReader* reader, ActorComponent** components)
4554
{
4655
m_Name = reader->readString();
@@ -53,6 +62,14 @@ void ActorAnimation::read(BlockReader* reader, ActorComponent** components)
5362

5463
for(int i = 0; i < m_AnimatedComponentsCount; i++)
5564
{
56-
m_AnimatedComponents[i].read(reader, components);
65+
ComponentAnimation& animatedComponent = m_AnimatedComponents[i];
66+
animatedComponent.read(reader, components);
67+
68+
// Put actor events in a separate list...
69+
ActorComponent* component = components[animatedComponent.componentIndex()];
70+
if(component != nullptr && component->type() == ComponentType::ActorEvent)
71+
{
72+
m_TriggerComponents.push_back(&animatedComponent);
73+
}
5774
}
5875
}

Source/Animation/ActorAnimation.hpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@
22
#define _NIMA_ACTORANIMATION_HPP_
33

44
#include <string>
5+
#include <vector>
56
#include "ComponentAnimation.hpp"
67

78
namespace nima
89
{
910
class BlockReader;
1011
class ActorNode;
11-
12+
class ActorEvent;
13+
14+
struct ActorAnimationEvent
15+
{
16+
ActorEvent* actorEvent;
17+
float keyFrameTime;
18+
float elapsedTime;
19+
20+
ActorAnimationEvent(ActorEvent* av, float time, float elapsed) :
21+
actorEvent(av),
22+
keyFrameTime(time),
23+
elapsedTime(elapsed)
24+
{
25+
26+
}
27+
};
28+
1229
class ActorAnimation
1330
{
1431
private:
@@ -19,7 +36,10 @@ namespace nima
1936
ComponentAnimation* m_AnimatedComponents;
2037
int m_AnimatedComponentsCount;
2138

39+
std::vector<ComponentAnimation*> m_TriggerComponents;
40+
2241
public:
42+
2343
ActorAnimation();
2444
~ActorAnimation();
2545
const std::string& name() const;
@@ -30,6 +50,8 @@ namespace nima
3050

3151
void apply(float time, Actor* actor, float mix);
3252

53+
void triggerEvents(ActorComponent** components, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events);
54+
3355
};
3456
}
3557
#endif

Source/Animation/ComponentAnimation.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include "ComponentAnimation.hpp"
2+
#include "ActorAnimation.hpp"
3+
#include "KeyFrames/KeyFrame.hpp"
24
#include "../Actor.hpp"
35
#include "../BlockReader.hpp"
46

57
using namespace nima;
68

79
ComponentAnimation::ComponentAnimation() :
10+
m_ComponentIndex(0),
811
m_Properties(NULL),
912
m_PropertiesCount(0)
1013
{
@@ -30,6 +33,95 @@ void ComponentAnimation::apply(float time, Actor* actor, float mix)
3033
}
3134
}
3235

36+
void ComponentAnimation::triggerEvents(ActorComponent** components, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events)
37+
{
38+
for(int i = 0; i < m_PropertiesCount; i++)
39+
{
40+
PropertyAnimation& property = m_Properties[i];
41+
switch(property.type())
42+
{
43+
case PropertyType::Trigger:
44+
{
45+
int keyFramesCount = property.keyFramesCount();
46+
if(keyFramesCount == 0)
47+
{
48+
continue;
49+
}
50+
int idx = 0;
51+
// Binary find the keyframe index.
52+
{
53+
int mid = 0;
54+
float element = 0.0f;
55+
int start = 0;
56+
int end = keyFramesCount-1;
57+
58+
while (start <= end)
59+
{
60+
mid = ((start + end) >> 1);
61+
element = property.keyFrame(mid)->time();
62+
if (element < toTime)
63+
{
64+
start = mid + 1;
65+
}
66+
else if (element > toTime)
67+
{
68+
end = mid - 1;
69+
}
70+
else
71+
{
72+
start = mid;
73+
break;
74+
}
75+
}
76+
77+
idx = start;
78+
}
79+
80+
if(idx == 0)
81+
{
82+
if(property.keyFrame(0)->time() == toTime)
83+
{
84+
ActorEvent* actorEvent = reinterpret_cast<ActorEvent*>(components[m_ComponentIndex]);
85+
events.emplace_back(ActorAnimationEvent(actorEvent, toTime, 0.0f));
86+
//ActorComponent component = components[keyedComponent.ComponentIndex];
87+
//triggerEvents.Add(new AnimationEventArgs(component.Name, component, property.PropertyType, toTime, 0.0f));
88+
}
89+
}
90+
else
91+
{
92+
for(int k = idx-1; k >= 0; k--)
93+
{
94+
float frameTime = property.keyFrame(k)->time();
95+
96+
if(frameTime > fromTime)
97+
{
98+
ActorEvent* actorEvent = reinterpret_cast<ActorEvent*>(components[m_ComponentIndex]);
99+
events.emplace_back(ActorAnimationEvent(actorEvent, frameTime, toTime-frameTime));
100+
101+
//ActorComponent component = components[keyedComponent.ComponentIndex];
102+
//triggerEvents.Add(new AnimationEventArgs(component.Name, component, property.PropertyType, frameTime, toTime-frameTime));
103+
/*triggered.push({
104+
name:component._Name,
105+
component:component,
106+
propertyType:property._Type,
107+
keyFrameTime:frame._Time,
108+
elapsed:toTime-frame._Time
109+
});*/
110+
}
111+
else
112+
{
113+
break;
114+
}
115+
}
116+
}
117+
break;
118+
}
119+
default:
120+
break;
121+
}
122+
}
123+
}
124+
33125
void ComponentAnimation::read(BlockReader* reader, ActorComponent** components)
34126
{
35127
m_ComponentIndex = reader->readUnsignedShort();

0 commit comments

Comments
 (0)