-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite_t.h
276 lines (262 loc) · 6.16 KB
/
sprite_t.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#ifndef SPRITE_T_H_INCLUDED
#define SPRITE_T_H_INCLUDED
#include<bits/stdc++.h>
#include"pointd.h"
#include"SDL2_header.h"
std::map<int, bool> keyboard;
const int default_life=12;
const int Game::SCREEN_WIDTH = 640;
const int Game::SCREEN_HEIGHT = 480;
Mix_Chunk* soundbullet,*soundexplosion,*sounddie,*soundhurt,*soundenemyhurt,*soundsuperweapon;
Mix_Music* sound;
const std::string RES_PATH_MUS = getResourcePath("sound");
Image *imagePlayer[2], *imagebullet_player, *imageEnemy,*imageexplosion,*imagebullet_enemy ;
//detect collide between bullet_player and enemy,player and enemy
/*template<class T1,class T2>
bool collide(T1 a,T2 b)
{
return (a.pos.x>=b.pos.x-a.crashing_width and a.pos.x<=b.pos.x+b.crashing_width and a.pos.y>=b.pos.y-a.crashing_length and a.pos.y<=b.pos.y+b.crashing_length );
}*/
template<class T1,class T2>
bool collide(T1 a,T2 b)
{
return (fabs(a.pos.x-b.pos.x)<=(a.crashing_width+b.crashing_width)/2 and fabs(a.pos.y-b.pos.y)<=(a.crashing_length+b.crashing_length)/2);
}
//detect collide between bullet_enemy and player
template<class T1,class T2>
bool collide2(T1 a,T2 b)
{
return (fabs(a.pos.x-b.pos.x)<=(a.width+b.width)/2 and fabs(a.pos.y-b.pos.y)<=(a.length+b.length)/2);
}
void keyDown()
{
keyboard[Game::keyValue] = true;
}
void keyUp()
{
keyboard[Game::keyValue] = false;
}
PointD drawtext(const std::string &str,int posx,int posy,int w_rate=1,int h_rate=1)
{
Image* text=Game::textToImage(str);
int w,h;
Game::getImageSize(text,w,h);
Game::drawImage(text,posx-w/2,posy-h);
cleanup(text);
}
Mix_Chunk* loadmusic(const char* a )
{
char address[100],b[100];
std::stringstream ss;
ss<<RES_PATH_MUS;
ss>>b;
ss.clear();
strcpy(address,b);
strcat(address,a);
Mix_Chunk* sound=Mix_LoadWAV(address);
return sound;
}
void playmusic(Mix_Chunk* music)
{
Mix_PlayChannel( -1, music, 0 );
}
class sprite_t
{
public:
int life;
PointD pos;
//size used for collide
int width;
int length;
double speed;
PointD velocity;
//size used for collide2
int crashing_width;
int crashing_length;
bool alive()
{
return life>0;
}
Image* image;
void draw()
{
Game::drawImage( image, pos.x-crashing_width/2,pos.y-crashing_length/2 );
}
} ;
//the position where the bullet is added
class fire
{
public:
fire(const PointD &b)
{
pos=b;
}
PointD pos;
};
class player:public sprite_t
{
public:
player(const int &a)
{
width=2;
length=2;
reborn=false;
pos=PointD(Game::SCREEN_WIDTH/2,Game::SCREEN_HEIGHT/2 );
shootings.push_back(fire(pos));
score=0;
life=5;
speed = 5;
superweapons=1;
cooling_down=0;
image=imagePlayer[a];
Game::getImageSize(image,crashing_width,crashing_length);
}
std::vector<fire> shootings;
int score;
//record whether a player is in supertime status(caused by injury) and how long the remaining super time
bool reborn;
int supertime;
bool move;
//the number of superweapons available
int superweapons;
//the cooling down time of superweapon
int cooling_down;
void setvisible(){
if(alive() and reborn)
{
Game::setImageAlpha(image,128);
}
else if(!reborn and alive())
{
Game::setImageAlpha(image,255);
}
else
{
//dead
Game::setImageAlpha(image,0);
}
}
void update()
{
//update reborn status
if(reborn)
{
supertime--;
if(supertime<0)
{
reborn=false;
}
}
if(cooling_down>0)
{
cooling_down--;
}
//Stop player
if(!move)
{
velocity = velocity * 0.8;
if(velocity.length() < 0.1 )
velocity = PointD();
}
}
void keyevent(int a,int b,int c,int d)
{
move=false;
if( keyboard[a] )
{
velocity = velocity + PointD(0,-1)*speed;
move = true;
}
if( keyboard[b] )
{
velocity = velocity + PointD(0,+1)*speed;
move = true;
}
if( keyboard[c])
{
velocity = velocity + PointD(-1,0)*speed;
move = true;
}
if( keyboard[d])
{
velocity = velocity + PointD(+1,0)*speed;
move = true;
}
double len = velocity.length();
if( len > speed )
{
velocity = velocity/len*speed;
}
}
void new_pos()
{
//avoid player getting out of the boundary
pos = pos + velocity;
if(pos.x<0)
{
pos.x=0;
velocity=PointD(0,0);
}
else if(pos.x>Game::SCREEN_WIDTH)
{
pos.x=Game::SCREEN_WIDTH;
velocity=PointD(0,0);
}
else if(pos.y<0)
{
pos.y=0;
velocity=PointD(0,0);
}
else if( pos.y>Game::SCREEN_HEIGHT)
{
pos.y=Game::SCREEN_HEIGHT;
velocity=PointD(0,0);
}
}
};
class enemy:public sprite_t
{
public:
enemy(const int &a )
{
pos=PointD(a,0) ;
image=imageEnemy;
Game::getImageSize(image,width,length);
crashing_length=length;
crashing_width=width;
life=5;
width*=0.7;
velocity=PointD(0,2);
}
std::vector<fire> shootings;
};
class bullet:public sprite_t
{
public:
template<class T>
bullet(const T a,const int & i)
{
pos=a.pos;
velocity.x=0;
velocity.y=(i==0?8:4);
image=(i==0?imagebullet_player:imagebullet_enemy);
Game::getImageSize(image,width,length);
crashing_length=length;
crashing_width=width;
}
};
class explosion:public sprite_t
{
public:
template<class T>
explosion(const T &a)
{
pos=a.pos;
life=default_life;
image=imageexplosion;
Game::getImageSize(image,width,length);
crashing_length=length;
crashing_width=width;
}
};
#endif // SPRITE_T_H_INCLUDED