-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFallingLeaf.cpp
More file actions
154 lines (132 loc) · 2.9 KB
/
FallingLeaf.cpp
File metadata and controls
154 lines (132 loc) · 2.9 KB
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
// FallingLeaf.cpp: implementation of the CFallingLeaf class.
//
//////////////////////////////////////////////////////////////////////
#include "FallingLeaf.h"
#include <time.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFallingLeaf::CFallingLeaf()
{
Sprite = NULL;
Tile = NULL;
Screen = NULL;
x = rand() % 600 + 1;
y = rand() % 400 + 1;
Speed = 3;
iTicker = 0;
zoom = 10;
}
CFallingLeaf::~CFallingLeaf()
{
SAFEDELETE(Sprite);
SAFEDELETE(Tile);
}
void CFallingLeaf::Load(CDXScreen *screen)
{
// Loads a random 1-3 FallingLeaf.
Screen = screen;
char filename[256];
strcpy(filename, (char*)"graphics/falling_leaf.bmp");
Tile = new CDXTile();
if( Tile->Create(Screen, filename, 40, 40, 0) == FALSE )
CDXError( Screen , (char*)"could not load tiles from file FallingLeaf file" );
// set the top left pixel in tiles bitmap as transparent color
Tile->SetColorKey(0);
Sprite = new CDXSprite();
Sprite->Create(Tile);
Sprite->SetAlphaValue(rand() % 156 + 100);
Sprite->SetShadowOffset(15, -15);
Sprite->SetShadowValue(75);
Sprite->SetFrame(1);
x = rand() % 600;
y = rand() % 400;
Speed = 1; // max speed
iTicker = 0;
zoom = 3;
initial_x = x;
life_count = 0; // life until leaf is dead
life_time = rand() % 301 + 500; // each leaf will display for 500 - 850 ticks
bAnimRight = true;
bLeft = true;
iRotateSpeed = rand() % 5 + 1;
bZoomer = rand() % 2;
}
void CFallingLeaf::Move(int iDirection)
{
if (iTicker2++ > 5)
iTicker2 = 0;
// Zoom out as it falls.
int r = rand() % 20;
if (iTicker == 0 && zoom > 0)
zoom -= 0.1;
// Move slightly down.
y++;
// Move left and right.
if (bLeft && x > initial_x - 50)
{
x -= Speed;
}
else if (x <= initial_x - 50)
{
bLeft = false;
Speed = 1;
}
if (!bLeft && x < initial_x + 50)
{
x += Speed;
}
else if (x >= initial_x + 50)
{
bLeft = true;
Speed = 1;
}
if (Speed < 6 && r == 5)
Speed++;
// Leaf dies, reset it.
if (y >= 480)
{
x = rand() % 600;
y = rand() % 400;
Speed = 1; // max speed
iTicker = 0;
zoom = 3;
initial_x = x;
life_count = 0; // life until leaf is dead
life_time = rand() % 301 + 500; // each leaf will display for 50 - 150 ticks
Sprite->SetAlphaValue(rand() % 156 + 100);
iRotateSpeed = rand() % 5 + 1;
bZoomer = rand() % 2;
}
}
void CFallingLeaf::Draw()
{
if (iTicker++ > iRotateSpeed)
{
int iFrame = Sprite->GetFrame();
if (bAnimRight)
{
// Rotating right.
if (++iFrame >= 6)
{
bAnimRight = false;
}
}
else
{
// Rotating left.
if (--iFrame <= 1)
{
bAnimRight = true;
}
}
Sprite->SetFrame(iFrame);
iTicker = 0;
}
Sprite->SetPos(x, y);
Sprite->SetScale(zoom);
if (bZoomer)
Sprite->Draw(Screen->GetBack() , 0 , 0 , CDXBLT_TRANSALPHA);
else
Sprite->Draw(Screen->GetBack() , 0 , 0 , CDXBLT_TRANSSCALED);
}