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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
64
65
71
72
84
85
86
87
88
89
90
91
92
93
97
98
102
103
107
108
109
110
114
115
/* ... */
#include <gui/game2D_screen/ParallaxBackground.hpp>
#include <BitmapDatabase.hpp>
ParallaxBackground::ParallaxBackground(int16_t width, int16_t height) :
currentAnimationState(NO_ANIMATION),
tickCounter(0)
{
setWidth(width);
setHeight(height);
initializeLayer(background, BITMAP_GAME2D_BACKGROUND_ID, 1, 2, 35);
initializeLayer(treeTops, BITMAP_GAME2D_TREETOPS_ID, 1, 3, 20);
initializeLayer(ground, BITMAP_GAME2D_GROUND_ID, 1, 5, getHeight() - Bitmap(BITMAP_GAME2D_GROUND_ID).getHeight());
initializeLayer(leaves, BITMAP_GAME2D_TOPLEAVES_ID, 1, 6, 0);
}{ ... }
ParallaxBackground::~ParallaxBackground()
{
}{ ... }
void ParallaxBackground::initializeLayer(Layer& layer, BitmapId bmp, int animationUpdateSpeed, int animationWidth, int y)
{
layer.image0.setBitmap(Bitmap(bmp));
layer.image1.setBitmap(Bitmap(bmp));
layer.image2.setBitmap(Bitmap(bmp));
layer.image0.setXY(0, y);
layer.image1.setXY(layer.image0.getRect().right(), y);
layer.image2.setXY(layer.image1.getRect().right(), y);
add(layer.image0);
add(layer.image1);
add(layer.image2);
layer.animationUpdateSpeed = animationUpdateSpeed;
layer.animationWidth = animationWidth;
}{ ... }
void ParallaxBackground::startAnimation()
{
currentAnimationState = ANIMATION_RUNNING;
Application::getInstance()->registerTimerWidget(this);
}{ ... }
void ParallaxBackground::stopAnimation()
{
currentAnimationState = NO_ANIMATION;
Application::getInstance()->unregisterTimerWidget(this);
}{ ... }
void ParallaxBackground::handleTickEvent()
{
tickCounter++;
if (currentAnimationState == ANIMATION_RUNNING)
{
moveLayer(background, tickCounter);
moveLayer(ground, tickCounter);
moveLayer(leaves, tickCounter);
moveLayer(treeTops, tickCounter);
}if (currentAnimationState == ANIMATION_RUNNING) { ... }
}{ ... }
void ParallaxBackground::moveLayer(Layer& layer, int tickCount)
{
if (tickCount % layer.animationUpdateSpeed == 0)
{
layer.image0.moveTo(layer.image0.getX() - layer.animationWidth, layer.image0.getY());
layer.image1.moveTo(layer.image1.getX() - layer.animationWidth, layer.image1.getY());
layer.image2.moveTo(layer.image2.getX() - layer.animationWidth, layer.image2.getY());
if (layer.image0.getRect().right() < 0)
{
layer.image0.moveTo(layer.image2.getRect().right(), layer.image0.getY());
}if (layer.image0.getRect().right() < 0) { ... }
if (layer.image1.getRect().right() < 0)
{
layer.image1.moveTo(layer.image0.getRect().right(), layer.image1.getY());
}if (layer.image1.getRect().right() < 0) { ... }
if (layer.image2.getRect().right() < 0)
{
layer.image2.moveTo(layer.image1.getRect().right(), layer.image2.getY());
}if (layer.image2.getRect().right() < 0) { ... }
}if (tickCount % layer.animationUpdateSpeed == 0) { ... }
}{ ... }
void ParallaxBackground::addPlayerCharacter(AnimatedImage& player)
{
insert(&(ground.image2), player);
}{ ... }
void ParallaxBackground::addCollectibleItem(CollectibleItem& item)
{
insert(&(ground.image2), item);
}{ ... }