-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbullet.c
252 lines (200 loc) · 6.9 KB
/
bullet.c
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
/**********************************************************************
Module: bullet.c
Author: Junseok Lee
Represents a player bullet
**********************************************************************/
#include "bullet.h"
void newPlayerBullet(bullet *b)
{
b->row = b->startRow;
b->col = b->startCol;
b->state = TRANSIT;
}
void playerBulletMove(bullet *b)
{
enemy * e = head; //head enemy
//collide when bullets hits ceiling
if(b->row < TOP_WALL){
wrappedMutexLock(&b->mutex);
b->state = COLLISION;
wrappedMutexUnlock(&b->mutex);
}
//Traverse through enemy list to check if the bullet hit
while(e != NULL)
{
//when player bullet hits a caterpillar
if(enemyHit(e,b))
{
enemy* newEnemy;
node * nodeToAdd = (node*)(malloc(sizeof(node)));
if(nodeToAdd == NULL) return; //check for malloc failure
//set bulllet state to collide
wrappedMutexLock(&b->mutex);
b->state = COLLISION;
wrappedMutexUnlock(&b->mutex);
//save enemy state set it to split
wrappedMutexLock(&e->mutex);
e->prevState = e->state;
e->state = SPLIT;
wrappedMutexUnlock(&e->mutex);
int prevLength = e->length; //enemy previous length
int prevRow = e->row; //enemy previous row
int prevCol = e->col; //enemy previous column
//SPLIT ENEMY MOVING LEFT
if(e->direction == LEFT){
//cut enemy length
wrappedMutexLock(&e->mutex);
e->length = e->length - (b->col - e->col)-1;
wrappedMutexUnlock(&e->mutex);
//move cut enemy back
enemyMove(e,e->row, b->col);
//spawn new enemy in front of the moved old enemy
newEnemy = spawnEnemy(prevLength-(e->length+1), prevRow, prevCol,LIST_TYPE_BODY,ENEMY_SPEED_FAST,LEFT);
}
//SPLIT ENEMY MOVING RIGHT
else if (e->direction == RIGHT){
//cut enemy length
wrappedMutexLock(&e->mutex);
e->length = e->length-(e->col-b->col)-1;
wrappedMutexUnlock(&e->mutex);
enemyMove(e,e->row, b->col); //move cut enemy back
//spawn new enemy in front of the moved old enemy
newEnemy = spawnEnemy(prevLength-(e->length+1), prevRow, prevCol,LIST_TYPE_BODY,ENEMY_SPEED_FAST,RIGHT);
}
nodeToAdd->e = newEnemy;
//ADD NEW ENEMY TO END OF LIST
wrappedMutexLock(&caterpillarMutex);
node * cur = nodeHead;
//traverse to end of list
while(cur->next != NULL)
{
cur = cur->next;
}
cur->next = nodeToAdd; //enemy is now @ end of list
wrappedMutexUnlock(&caterpillarMutex);
break;
}
//move to next enemy on llist
e = e->next;
}
//keep moving up if bullet hasn't collided
if(b->state != COLLISION)
{
wrappedMutexLock(&b->mutex);
b->prevRow = b->row;
b->row--;
wrappedMutexUnlock(&b->mutex);
}
}
int enemyHit(enemy * e, bullet * b){
int bulletCol = b->col;
int success = false;
//enemy going left & bullet hit a part of enemy
bool leftHit = (e->direction == LEFT) && ((bulletCol-(e->length) < (e->col)) && (bulletCol-(e->length) >= (e->col - e->length)) && (b->row == e->row+ENEMY_HEIGHT));
//enemy going right & bullet hit body)
bool rightHit = (e->direction == RIGHT) && (((bulletCol + e->length) > (e->col)) && ((bulletCol+e->length) <= (e->col + e->length)) && (b->row == e->row+ENEMY_HEIGHT));
bool moving = (e->state != DEAD_E && e->state != SPLIT);
if((leftHit || rightHit) && moving)
{
success = true;
}
return success;
}
bullet* spawnBullet(int startRow, int startCol)
{
bullet* b = (bullet*)(malloc(sizeof(bullet)));
b->startCol = startCol;
b->startRow = startRow;
b->running = true;
//set as head if bullet list is undefined
if(bulletHead == NULL){
wrappedMutexLock(&bulletMutex);
bulletHead = b;
wrappedMutexUnlock(&bulletMutex);
}
//add to bullet list
else{
bullet * node = bulletHead;
//traverse to end of list
while(node->next != NULL)
{
node = node->next;
}
//set as last element of list
wrappedMutexLock(&bulletMutex);
node->next = b;
wrappedMutexUnlock(&bulletMutex);
}
//Init mutex
pthread_mutex_init(&b->mutex, NULL);
//create & run bullet thread
wrappedPthreadCreate(&(b->thread), NULL, runBulletT, (void*)b);
return b;
}
void *runBulletT(void *data)
{
bullet* b = (bullet*)data;
newPlayerBullet(b);
while (b->running && gameRunning)
{
switch(b->state)
{
case TRANSIT:
playerBulletMove(b);
bulletRedraw(b, true); //redraw moved bullet, lock = true
break;
case COLLISION:
wrappedMutexLock(&b->mutex);
b->running = false;
wrappedMutexUnlock(&b->mutex);
break;
case DESTROY:
b->running = false;
break;
default:
break;
}
//sleep for next bullet tick
sleepTicks(PLAYER_BULLET_SPEED);
}
return NULL;
}
char * BULLET_BODY[BULLET_SIZE] = {"@"};
void bulletRedraw(bullet *b, bool lock)
{
if(lock)
{
wrappedMutexLock(&b->mutex);
consoleClearImage(b->prevRow, b->col, BULLET_SIZE, BULLET_SIZE);
consoleDrawImage(b->row, b->col, BULLET_BODY, BULLET_SIZE);
wrappedMutexUnlock(&b->mutex);
}
}
void killBullets(){
if(bulletHead != NULL){
bullet * b = bulletHead;
//traverse through bullet list
while (b != NULL){
wrappedMutexLock(&b->mutex);
consoleClearImage(b->row,b->col,1,1);
wrappedMutexUnlock(&b->mutex);
b = b->next;
//free(temp);
}
}
}
void freeBullets(bullet * headB){
bullet * current = headB;
bullet * temp;
//return if head
if( current == NULL || current->next == NULL) return;
wrappedMutexLock(&bulletMutex);
while (current != NULL){
//wrappedPthreadJoin(current->thread,NULL);
temp = current;
current = current->next;
free(temp);
}
bulletHead = NULL; //set bullet head to null
wrappedMutexUnlock(&bulletMutex);
}