-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlien.h
66 lines (52 loc) · 1.23 KB
/
Alien.h
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
#include <SDL2/SDL.h>
#include <iostream>
struct Alien: public GameEntity {
public:
Alien(float x, float y, int row);
~Alien();
void update(Uint32 delta);
private:
int counter, row_;
static int direction, shared_y;
const static int y_step = 3;
};
int Alien::direction = 1;
int Alien::shared_y;
Alien::Alien(float x, float y, int row) {
this->x = x;
this->y = y;
shared_y = y;
row_ = row;
velocity = 0.025f;
bitmap = SDL_LoadBMP("res/invader.bmp");
if (bitmap == NULL) {
std::cout << "Unable to load alien bitmap: " << SDL_GetError() << std::endl;
}
srcrect = new SDL_Rect();
srcrect->w = bitmap->w / 2;
srcrect->h = bitmap->h;
dstrect.w = srcrect->w * 4;
dstrect.h = srcrect->h * 2;
}
Alien::~Alien() {
delete srcrect;
}
void Alien::update(Uint32 delta) {
// Update animation. Could be improved.
counter += delta;
if (counter >= 1000) {
srcrect->x = (srcrect->x + bitmap->w/2) % bitmap->w;
counter = 0;
}
if (x + dstrect.w > SCREEN_WIDTH - ENEMY_MARGIN) {
direction = -1;
shared_y += y_step;
}
if (x < ENEMY_MARGIN) {
direction = 1;
shared_y += y_step;
}
x += delta * velocity * Alien::direction;
y = shared_y + row_ * 22;
GameEntity::update(delta);
}