-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInfiniteParallaxNode.cpp
169 lines (131 loc) · 5.63 KB
/
InfiniteParallaxNode.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
//
// InfiniteParallaxNode.cpp
// MawKit
//
// Created by Lluís Ulzurrun de Asanza Sàez on 08/04/16.
//
//
#include "InfiniteParallaxNode.hpp"
namespace MK {
typedef InfiniteParallaxNode Self;
constexpr const char *updateLayerCallbackFormat = "update_layer_%d_%c_axis";
void Self::addLayer( cocos2d::Sprite *node, int localZOrder, cocos2d::Vec2 velocity )
{
char *callbackNameXAxis = new char[100];
std::sprintf( callbackNameXAxis, updateLayerCallbackFormat, localZOrder, 'x' );
char *callbackNameYAxis = new char[100];
std::sprintf( callbackNameYAxis, updateLayerCallbackFormat, localZOrder, 'y' );
for ( auto child : this->getChildren() ) {
if ( child->getLocalZOrder() == localZOrder ) {
this->unschedule( callbackNameXAxis );
this->unschedule( callbackNameYAxis );
child->removeFromParent();
}
}
if ( velocity.x != 0 ) {
auto leftNode = cocos2d::Sprite::createWithTexture( node->getTexture() );
leftNode->setAnchorPoint( node->getAnchorPoint() );
leftNode->setContentSize( node->getContentSize() );
leftNode->setPosition( node->getPosition() +
cocos2d::Point( -node->getContentSize().width, 0 ) );
auto rightNode = cocos2d::Sprite::createWithTexture( node->getTexture() );
rightNode->setAnchorPoint( node->getAnchorPoint() );
rightNode->setContentSize( node->getContentSize() );
rightNode->setPosition( node->getPosition() +
cocos2d::Point( node->getContentSize().width, 0 ) );
this->addChild( node, localZOrder );
this->addChild( leftNode, localZOrder );
this->addChild( rightNode, localZOrder );
constexpr const float CALLBACK_CALL_PERIOD = 1.0;
std::function<void( float )> callback_x_axis =
[&, leftNode, rightNode, node, velocity, CALLBACK_CALL_PERIOD]( float ) {
auto distances = velocity * CALLBACK_CALL_PERIOD;
if ( velocity.x < 0 ) {
std::vector<std::pair<cocos2d::Node *, cocos2d::Node *>> nodes = {
{leftNode, rightNode}, {node, leftNode}, {rightNode, node}};
for ( auto pair : nodes ) {
auto left = pair.first;
auto right = pair.second;
if ( left->getPosition().x < -left->getContentSize().width * 1.5 ) {
left->setPosition( cocos2d::Point( right->getPosition().x +
left->getContentSize().width,
left->getPosition().y ) );
}
}
}
else {
std::vector<std::pair<cocos2d::Node *, cocos2d::Node *>> nodes = {
{rightNode, leftNode}, {leftNode, node}, {node, rightNode}};
for ( auto pair : nodes ) {
auto right = pair.first;
auto left = pair.second;
if ( right->getPosition().x > right->getContentSize().width * 1.5 ) {
right->setPosition(
cocos2d::Point( left->getPosition().x - right->getContentSize().width,
right->getPosition().y ) );
}
}
}
leftNode->runAction( cocos2d::MoveBy::create( CALLBACK_CALL_PERIOD, distances ) );
rightNode->runAction( cocos2d::MoveBy::create( CALLBACK_CALL_PERIOD, distances ) );
node->runAction( cocos2d::MoveBy::create( CALLBACK_CALL_PERIOD, distances ) );
};
this->schedule( callback_x_axis, CALLBACK_CALL_PERIOD, callbackNameXAxis );
callback_x_axis( 0 );
}
if ( velocity.y != 0 ) {
auto topNode = cocos2d::Sprite::createWithTexture( node->getTexture() );
topNode->setAnchorPoint( node->getAnchorPoint() );
topNode->setContentSize( node->getContentSize() );
topNode->setPosition( node->getPosition() +
cocos2d::Point( 0, -node->getContentSize().height ) );
auto bottomNode = cocos2d::Sprite::createWithTexture( node->getTexture() );
bottomNode->setAnchorPoint( node->getAnchorPoint() );
bottomNode->setContentSize( node->getContentSize() );
bottomNode->setPosition(
node->getPosition() + cocos2d::Point( 0, node->getContentSize().height ) );
this->addChild( node, localZOrder );
this->addChild( topNode, localZOrder );
this->addChild( bottomNode, localZOrder );
constexpr const float CALLBACK_CALL_PERIOD = 1.0;
std::function<void( float )> callback_y_axis =
[&, topNode, bottomNode, node, velocity, CALLBACK_CALL_PERIOD]( float ) {
auto distances = velocity * CALLBACK_CALL_PERIOD;
if ( velocity.y < 0 ) {
std::vector<std::pair<cocos2d::Node *, cocos2d::Node *>> nodes = {
{topNode, bottomNode}, {node, topNode}, {bottomNode, node}};
for ( auto pair : nodes ) {
auto top = pair.first;
auto bottom = pair.second;
if ( top->getPosition().y < -bottom->getContentSize().height * 1.5 ) {
top->setPosition( cocos2d::Point( top->getPosition().x,
bottom->getPosition().y +
top->getContentSize().height ) );
}
}
}
else {
std::vector<std::pair<cocos2d::Node *, cocos2d::Node *>> nodes = {
{bottomNode, topNode}, {topNode, node}, {node, bottomNode}};
for ( auto pair : nodes ) {
auto bottom = pair.first;
auto top = pair.second;
if ( bottom->getPosition().y > bottom->getContentSize().height * 1.5 ) {
bottom->setPosition(
cocos2d::Point( bottom->getPosition().x,
top->getPosition().y -
bottom->getContentSize().height ) );
}
}
}
topNode->runAction( cocos2d::MoveBy::create( CALLBACK_CALL_PERIOD, distances ) );
bottomNode->runAction( cocos2d::MoveBy::create( CALLBACK_CALL_PERIOD, distances ) );
node->runAction( cocos2d::MoveBy::create( CALLBACK_CALL_PERIOD, distances ) );
};
this->schedule( callback_y_axis, CALLBACK_CALL_PERIOD, callbackNameYAxis );
callback_y_axis( 0 );
}
delete[] callbackNameXAxis;
delete[] callbackNameYAxis;
}
}; // namespace MK