-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmove.cpp
186 lines (171 loc) · 4.43 KB
/
move.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
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
/*This File Contains the Code to move every thing.
It is called every-time a new frame is rendered*/
void main_ship_move()
{
if(ship_left)
{
if(ship_x>=(int)UNIT_MOVE)
{
ship_x -= UNIT_MOVE;
}
else if(ship_x>0)
{
ship_x = 0;
}
}
else if(ship_right)
{
if(ship_x<=(width-ship_span-UNIT_MOVE))
{
ship_x += UNIT_MOVE;
}
else if(ship_x<=(width-ship_span))
{
ship_x = width-ship_span;
}
}
}
void bomb_move()
{
unsigned int i;
float temps = height/((float)FPS);
for(i=0;i<MAX_BOMB;i++)
{
bomb_pos[i][1] -= temps/(bomb_speed[i]);
if(bomb_pos[i][1]<=PADDING_BELOW)
{
bomb_present[i]=false;
BOMB_COUNT--;
}
}
}
void bullet_move()
{
unsigned int i;
float temps = height/((float)FPS);
temps /= BULLET_SEC;
for(i = 0;i < bullets.size(); i++)
{
bullets[i].y += temps;
if(bullets[i].y>=height) bullets.erase(bullets.begin() + i),i--;
}
}
bool collision_bomb_bullet(struct bullet bull, int bomb_ind)
{
if(bomb_present[bomb_ind]==false) return false;
if(bull.y<(bomb_pos[bomb_ind][1]-VARD)) return false;
if(bull.y>(bomb_pos[bomb_ind][1]+VARD)) return false;
if(bull.x<(bomb_pos[bomb_ind][0]-VARD)) return false;
if(bull.x>(bomb_pos[bomb_ind][0]+VARD)) return false;
return true;
}
bool collison_bomb_ship(int bomb_ind)
{
if(bomb_present[bomb_ind]==0) return false;
if(bomb_pos[bomb_ind][1]-VARD>ship_y+VARC) return false;
if(bomb_pos[bomb_ind][0]+VARD<ship_x) return false;
if(bomb_pos[bomb_ind][0]-VARD>ship_x+VARA+VARA+VARB) return false;
int diffy=(bomb_pos[bomb_ind][1]-VARD-ship_y);
int diffx=bomb_pos[bomb_ind][0]-ship_x;
if(diffx+VARD<VARA)
{
if(diffx+VARD==0)
return false;
if((VARC)/(2.0*VARA) < (diffy)/(diffx+VARD))
return false;
else
return true;
}
else if(diffx+VARD<VARA+VARB)
{
if(diffy<VARC)
return true;
else //TEST for inside of top triangle
{
if(diffx+VARD>VARA+VARB/2.0)
return true;
else //CHECK WITH SLOPE
{
if(diffx+VARD==VARA)
return false;
if(((VARC*2)/(VARB*3))<((diffy-VARC/2.0)/(diffx+VARD-VARA)))
return false;
else
return true;
}
}
}
else
{
if(diffx-VARD<=VARA)
{
return true;
}
else if(diffx-VARD<VARA+VARB)
{
if(diffy <= VARC)
return true;
else //test for inside of top triangle
{
if(diffx - VARD <= VARA+VARB/2.0)
return true;
else //CHECK WITH SLOPE
{
if(VARA+VARB==diffx-VARD)
return false;
if(((VARC*2)/(VARB*3))<((diffy-VARC/2.0)/(VARA+VARB-(diffx-VARD))))
return false;
else
return true;
}
}
}
else //check slope of right wing and decide
{
if(VARA + VARA + VARB== diffx - VARD)
return false;
if((VARC/(2.0*VARA)) < (diffy / (VARA + VARA + VARB - (diffx - VARD))))
return false;
else
return true;
}
}
return false;
}
void detect_collisions()
{
//To detect collision between bomb and bullets
unsigned int i,j;
for(i = 0; i < bullets.size(); i++)
for(j = 0;j < MAX_BOMB; j++)
if(bomb_present[j] && collision_bomb_bullet(bullets[i], j))
{
curr_score += BOMB_MAX_SEC - bomb_speed[j] + 1;
bomb_present[j]=false;
BOMB_COUNT--;
bullets.erase(bullets.begin() + i);
i--;
break;
}
//To detect collision between main ship and bomb
unsigned int ii;
for(ii = 0; ii < BOMB_COUNT; ii++)
{
if(collison_bomb_ship(ii))
{
game_state = 5;
}
}
}
void timer_func(int v)
{
if(game_state == 2)
{
main_ship_move();
bomb_move();
bullet_move();
detect_collisions();
}
glutPostRedisplay();
glutTimerFunc(1000/FPS, timer_func, v);
}