-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFiniteTimeActions.cpp
186 lines (150 loc) · 5.3 KB
/
FiniteTimeActions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// FiniteTimeActions.cpp
// Maw Kit
//
// Created by Lluís Ulzurrun de Asanza Sàez on 21/02/16.
//
//
#include "FiniteTimeActions.hpp"
#include "Random.hpp"
namespace MK {
namespace Actions {
//------------------------------------------------------------------------------
// MARK: - RemoveFromParent
//------------------------------------------------------------------------------
RemoveFromParent::RemoveFromParent()
{
}
RemoveFromParent::~RemoveFromParent()
{
}
RemoveFromParent *RemoveFromParent::create( float duration )
{
RemoveFromParent *action = new ( std::nothrow ) RemoveFromParent();
action->initWithDuration( duration );
action->autorelease();
return action;
}
bool RemoveFromParent::initWithDuration( float duration )
{
return cocos2d::ActionInterval::initWithDuration( duration );
}
RemoveFromParent *RemoveFromParent::clone() const
{
auto a = new ( std::nothrow ) RemoveFromParent();
a->initWithDuration( this->_duration );
a->autorelease();
return a;
}
RemoveFromParent *RemoveFromParent::reverse() const
{
CCASSERT( false, "reverse() not supported in RemoveFromParent" );
return nullptr;
}
void RemoveFromParent::startWithTarget( cocos2d::Node *target )
{
cocos2d::ActionInterval::startWithTarget( target );
}
void RemoveFromParent::update( float time )
{
if ( time > 0 && this->_target != nullptr ) {
this->_target->removeFromParent();
this->_target = nullptr;
}
}
//------------------------------------------------------------------------------
// MARK: - Shake
//------------------------------------------------------------------------------
cocos2d::Sequence *Shake::create( float duration, float strength )
{
auto halfDuration = duration / 2;
auto rotationDuration = halfDuration / 7;
return cocos2d::Sequence::create(
cocos2d::RotateBy::create( rotationDuration, strength ),
cocos2d::RotateBy::create( rotationDuration, -2 * strength ),
cocos2d::RotateBy::create( rotationDuration, strength * 1.7 ),
cocos2d::RotateBy::create( rotationDuration, -1.2 * strength ),
cocos2d::RotateBy::create( rotationDuration, 0.8 * strength ),
cocos2d::RotateBy::create( rotationDuration, -0.4 * strength ),
cocos2d::RotateBy::create( rotationDuration, 0.1 * strength ),
cocos2d::DelayTime::create( halfDuration ), nullptr );
}
//------------------------------------------------------------------------------
// MARK: - LinealShake
//------------------------------------------------------------------------------
cocos2d::Sequence *
LinealShake::create( float duration, float x_strength, float y_strength, float z_strength )
{
auto halfDuration = duration / 2;
auto movementDuration = halfDuration / 7;
cocos2d::Vec3 strength( x_strength, y_strength, z_strength );
return cocos2d::Sequence::create(
cocos2d::MoveBy::create( movementDuration, strength ),
cocos2d::MoveBy::create( movementDuration, -2 * strength ),
cocos2d::MoveBy::create( movementDuration, strength * 1.7 ),
cocos2d::MoveBy::create( movementDuration, -1.2 * strength ),
cocos2d::MoveBy::create( movementDuration, 0.8 * strength ),
cocos2d::MoveBy::create( movementDuration, -0.4 * strength ),
cocos2d::MoveBy::create( movementDuration, 0.1 * strength ),
cocos2d::DelayTime::create( halfDuration ), nullptr );
}
//------------------------------------------------------------------------------
// MARK: - MoveToNode
//------------------------------------------------------------------------------
MoveToNode::MoveToNode()
{
}
MoveToNode::~MoveToNode()
{
}
MoveToNode *MoveToNode::create( float duration, cocos2d::Node *destinationNode )
{
MoveToNode *action = new ( std::nothrow ) MoveToNode();
action->initWithDuration( duration, destinationNode );
action->autorelease();
return action;
}
bool MoveToNode::initWithDuration( float duration, cocos2d::Node *destination )
{
if ( cocos2d::ActionInterval::initWithDuration( duration ) ) {
this->destinationNode = destination;
}
return false;
}
MoveToNode *MoveToNode::clone() const
{
auto a = new ( std::nothrow ) MoveToNode();
a->initWithDuration( this->_duration, this->destinationNode );
a->autorelease();
return a;
}
MoveToNode *MoveToNode::reverse() const
{
CCASSERT( false, "reverse() not supported in MoveToNode" );
return nullptr;
}
void MoveToNode::startWithTarget( cocos2d::Node *target )
{
cocos2d::ActionInterval::startWithTarget( target );
this->originalPosition = this->_target->getPosition();
for ( cocos2d::Node *parent = this->_target->getParent(); parent != nullptr;
parent = parent->getParent() ) {
this->originalPosition = parent->convertToWorldSpace( this->originalPosition );
}
}
void MoveToNode::update( float time )
{
cocos2d::Point finalPosition = this->destinationNode->getPosition();
cocos2d::Point anchor = this->destinationNode->getAnchorPoint();
cocos2d::Size size = this->destinationNode->getContentSize();
finalPosition += cocos2d::Point( ( 0.5 - anchor.x ) * size.width,
( 0.5 - anchor.y ) * size.height );
for ( cocos2d::Node *parent = this->destinationNode->getParent();
parent != nullptr; parent = parent->getParent() ) {
finalPosition = parent->convertToWorldSpace( finalPosition );
}
cocos2d::Vec2 delta = finalPosition - this->originalPosition;
this->_target->setPosition( this->originalPosition + delta * time );
}
}; // namespace Actions
}; // namespace MK