-
Notifications
You must be signed in to change notification settings - Fork 0
/
missle.cpp
155 lines (134 loc) · 2.74 KB
/
missle.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
152
153
154
155
#include "gameObject.cpp"
#ifndef MISSLE_H
#define MISSLE_H
#define MAX_MISSLE_COUNT 5
class Missle : public GameObject
{
public:
Missle(SDL_Renderer * renderer,int y)
{
cout << "[_+_] Firing missle"<<endl;
// create missle image texture
this->texture = new Texture(renderer);
this->texture->loadFromFile("assets/images/missle.png");
// set missle initial position and dimensions
this->position.x = 800;
this->position.y = y;
this->position.w = 54;
this->position.h = 30;
this->rect = this->position;
}
~Missle()
{
delete this->texture;
this->texture = NULL;
}
// render to screen
void render();
// update
void update();
SDL_Rect rect;
};
void Missle::render()
{
this->texture->renderResize(NULL,&this->position,true);
}
void Missle::update()
{
this->position.x -= 2;
this->rect = this->position;
}
class Missles
{
public:
Missles(SDL_Renderer * renderer);
~Missles();
// create a missle instance
void fire(int y);
// renderer all missle instances
void render();
// update
void update();
// check missle collision
bool check_collision(SDL_Rect dragon,SDL_Rect * m);
private:
Missle * sprites[5] = {NULL};
SDL_Renderer * renderer;
};
Missles::Missles(SDL_Renderer * renderer)
{
// copy the rendering context
this->renderer = renderer;
}
void Missles::fire(int y)
{
// search for available missle space
// if cell is free
// create a Missle instance and break;
// else ignore
for(int i = 0; i < MAX_MISSLE_COUNT ; i++)
{
if(this->sprites[i] == NULL)
{
//cout << "Chosen missle sprite id "<<i<<endl;
this->sprites[i] = new Missle(this->renderer,y+80);
break;
}
}
}
Missles::~Missles()
{
for(int i = 0 ; i < MAX_MISSLE_COUNT ; i++)
{
delete this->sprites[i];
this->sprites[i] = NULL;
};
}
void Missles::render()
{
for(int i = 0 ; i < MAX_MISSLE_COUNT; i++)
{
if(this->sprites[i] != NULL)
this->sprites[i]->render();
}
}
void Missles::update()
{
for(int i = 0; i < MAX_MISSLE_COUNT; i++)
{
if(this->sprites[i] != NULL)
{
this->sprites[i]->update();
// if the missle leaves the screen destroy it
if(this->sprites[i]->rect.x <= 0)
{
delete this->sprites[i];
this->sprites[i] = NULL;
}
}
}
}
bool Missles::check_collision(SDL_Rect dragon,SDL_Rect * m)
{
dragon.y += 30;
dragon.h -= 30;
for(int i = 0; i < MAX_MISSLE_COUNT; i++)
{
if(this->sprites[i] != NULL)
{
if(this->sprites[i]->rect.x >= dragon.x && dragon.x +dragon.h >= this->sprites[i]->rect.x)
{
if(this->sprites[i]->rect.y >= dragon.y && dragon.y + dragon.h >= this->sprites[i]->rect.y )
{
m->x = this->sprites[i]->rect.x;
m->y = this->sprites[i]->rect.y;
delete this->sprites[i];
this->sprites[i] = NULL;
return true;
}
}
}
}
return false;
}
#endif