/
Asteroid.cpp
81 lines (65 loc) · 1.29 KB
/
Asteroid.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
/*
* Asteroid.cpp
*
* Created on: Apr 18, 2012
* Author: thomas
*/
#include "Asteroid.h"
Asteroid::Asteroid(int x, int y, SDL_Surface *sprite, Player *p) {
GameObject();
m_posX = x;
m_posY = y;
m_velX = 1;
m_velY = 0;
m_dirX = -1;
m_dirY = 0;
m_boundX = 35;
m_boundY = 35;
m_curFrame = 0;
m_frameWidth = 64;
m_frameHeight = 64;
m_frameCount = 0;
m_frameDelay = 3;
m_maxFrame = 64;
m_animationColumns = 8;
m_animationRow = 0;
m_animationDirection = 1;
SetSprite(sprite);
SetID(ENEMY);
Asteroid::p=p;
}
void Asteroid::Update() {
GameObject::Update();
if (++m_frameCount >= m_frameDelay) {
m_curFrame += m_animationDirection;
if ((m_curFrame * m_animationRow) >= m_maxFrame-m_animationColumns) {
m_curFrame = 0;
m_animationRow = 0;
} else if (m_curFrame >= m_animationColumns) {
m_animationRow += m_animationDirection;
m_curFrame = 0;
}
/*
else if (m_curFrame <= 0){
m_curFrame = m_maxFrame;
}
*/
m_frameCount = 0;
}
if (m_posX + m_frameWidth < 0) {
Collided(BORDER);
}
}
void Asteroid::Draw() {
GameObject::DrawSprite();
}
void Asteroid::Collided(int objectID) {
if (objectID == BORDER) {
Asteroid::p->LoseLife();
} else {
//printf("asteroid collided with: %d \n", objectID);
}
if (objectID != ENEMY) {
SetAlive(false);
}
}