-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParallaxLayer.cpp
113 lines (80 loc) · 2.43 KB
/
ParallaxLayer.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
#include "ParallaxLayer.h"
#include "SpriteUtil.h"
namespace dxco {
ParallaxLayer::ParallaxLayer(std::string texture, float distance, int yPosition,
bool repeat, int width, int height) {
this->distance = distance;
this->repeat = repeat;
this->yPosition = yPosition;
cocos2d::CCSprite* sprite = SpriteUtil::create(texture, 0, yPosition, width,
height);
this->width = SpriteUtil::getWidth(sprite);
this->height = SpriteUtil::getHeight(sprite);
sprites.push_back(sprite);
cocos2d::CCSize visibleSize =
cocos2d::CCDirector::sharedDirector()->getVisibleSize();
int cant = 0;
if (repeat) {
cant = (visibleSize.width / this->width) + 2;
if (cant == 2) {
cant = 3;
}
}
for (int i = 0; i < cant; i++) {
cocos2d::CCSprite* newSprite = SpriteUtil::create(texture,
(i + 1) * this->width - 2, yPosition, width, height);
this->sprites.push_back(newSprite);
}
}
void ParallaxLayer::move(float deltaX, float deltaY) {
if (this->distance > 9999) {
return ;
}
float x = deltaX / this->distance;
float y = deltaY / this->distance;
if (this->repeat) {
std::vector<cocos2d::CCSprite*> spritesToMove;
for (int i = 0; i < this->sprites.size(); i++) {
cocos2d::CCSprite* currSprite = this->sprites[i];
SpriteUtil::move(currSprite, x, y);
float offset = this->getOffset(currSprite);
if (offset > (this->width / 2)) {
spritesToMove.push_back(currSprite);
}
}
cocos2d::CCSprite* lastSprite = this->getLastSprite();
float xPos = lastSprite->getPosition().x + this->width;
for (int i = 0; i < spritesToMove.size(); i++) {
cocos2d::CCSprite* currSprite = spritesToMove[i];
float xNewPos = xPos + (i * this->width) - 2;
SpriteUtil::moveToAbsolute(currSprite, xNewPos, currSprite->getPositionY());
}
} else {
SpriteUtil::move(this->sprites[0], x, y);
}
}
void ParallaxLayer::restart() {
for (int i = 0; i < this->sprites.size(); i++) {
SpriteUtil::moveTo(this->sprites[i], (i * this->width) -2 , this->yPosition);
}
}
float ParallaxLayer::getOffset(cocos2d::CCSprite* sprite) {
float result = 0;
if (sprite) {
result = -(sprite->getPosition().x);
}
return result;
}
cocos2d::CCSprite* ParallaxLayer::getLastSprite() {
int index = 0;
for (int i = 1; i < this->sprites.size(); i++) {
if (this->sprites[i]->getPosition().x
> this->sprites[index]->getPosition().x) {
index = i;
}
}
return this->sprites[index];
}
ParallaxLayer::~ParallaxLayer() {
}
} /* namespace dxco */