-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bat.cpp
151 lines (133 loc) · 2.99 KB
/
Bat.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
// Bat.cpp: implementation of the CBat class.
//
//////////////////////////////////////////////////////////////////////
#include "Bat.h"
#include <time.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBat::CBat()
{
Sprite = NULL;
Tile = NULL;
Screen = NULL;
Bat1Sound = 0;
Bat2Sound = 0;
x = rand() % 600 + 1;
y = rand() % 400 + 1;
Speed = 3;
iTicker = 0;
FlyingDuration = 0;
bFlyingRight = false;
bFlying = false;
}
CBat::~CBat()
{
SAFEDELETE(Sprite);
SAFEDELETE(Tile);
SAFEDELETE(Bat1Sound);
SAFEDELETE(Bat2Sound);
}
void CBat::Load(CDXScreen *screen, CDXSound *sound)
{
// Loads a random 1-3 Bat.
Screen = screen;
char filename[256];
strcpy(filename, (char*)"graphics/bat.bmp");
Tile = new CDXTile();
if( Tile->Create(Screen, filename, 40, 40, 0) == FALSE )
CDXError( Screen , (char*)"could not load tiles from file bat file" );
// set the top left pixel in tiles bitmap as transparent color
Tile->SetColorKey(0);
Sprite = new CDXSprite();
Sprite->Create(Tile);
Sprite->SetAlphaValue(150);
Sprite->SetShadowOffset(15, -15);
Sprite->SetShadowValue(150);
Sprite->SetFrame(1);
Bat1Sound = new CDXSoundBuffer;
Bat1Sound->Load(sound, (char*)"music/bat1.wav", 4);
Bat2Sound = new CDXSoundBuffer;
Bat2Sound->Load(sound, (char*)"music/bat2.wav", 4);
x = rand() % 600 + 1;
y = rand() % 400 + 1;
FlyingDuration = 0;
bFlyingRight = false;
bFlying = false;
Speed = rand() % 4 + 1; // speed = 1 .. 4
iTicker = 0;
}
void CBat::Move(int iDirection)
{
if (!bFlying)
{
// Bat is roosting, so check if it's time to fly.
if (FlyingDuration++ > 50)
{
// About time to fly, check random shot to take off.
int r = rand() % 500;
if (r == 11 || r == 7)
{
// Take off little bat.
FlyingDuration = rand() % 101 + 50; // fly for 50 - 150 ticks.
bFlyingRight = rand() % 2;
bFlying = true;
if (r == 11)
Bat1Sound->Play();
else
Bat2Sound->Play();
}
}
}
else if (bFlying)
{
// Bat is flying, see if he's tired out and wants to stop.
if (FlyingDuration-- < 1)
{
// It's time to roost.
FlyingDuration = 0;// - (rand() % 101 + 50); // roost for 50 .. 150 + 100 ticks.
bFlying = false;
}
else
{
// Fly bat fly.
int r = rand() % 10;
if (r < 5)
y += rand() % Speed + 1;
else
y -= rand() % Speed + 1;
if (bFlyingRight)
x += Speed;
else
x -= Speed;
if (x >= 640 || x < 0 || y >= 480 || y < 0)
{
// Reset coordinates.
x = rand() % 600 + 1;
y = rand() % 400 + 1;
FlyingDuration = 0;
bFlyingRight = false;
bFlying = false;
Speed = rand() % 4 + 1;
}
}
}
}
void CBat::Draw()
{
if (!bFlying)
{
// Roosting.
Sprite->SetFrame(1);
}
else
{
// Flying.
int iFrame = Sprite->GetFrame();
if (iFrame++ >= 5)
iFrame = 2;
Sprite->SetFrame(iFrame);
}
Sprite->SetPos(x, y);
Sprite->Draw(Screen->GetBack() , 0 , 0 , CDXBLT_TRANSALPHA);
}