-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemyBullet.c
165 lines (129 loc) · 3.68 KB
/
enemyBullet.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
/**********************************************************************
Module: enemyBullet.c
Author: Junseok Lee
Represents an enemy bullet.
**********************************************************************/
#include "enemyBullet.h"
//Enemy bullet body data
char * ENEMY_BULLET_BODY[1] = {"."};
void newEnemyBullet(enemyBullet * b)
{
b->row = b->startRow;
b->col = b->startCol;
b->state = TRANSIT_E;
}
void eBulletMove(enemyBullet * b, player * p)
{
//Bullet out of bound: set to CLEAN
if(b->row > GAME_ROWS - 2){
wrappedMutexLock(&b->mutex);
b->state = CLEAN;
wrappedMutexUnlock(&b->mutex);
}
//Enenmy bullet collides with player ship
if(shipHit(p,b))
{
//set bullet state
wrappedMutexLock(&b->mutex);
b->state = PLAYERHIT;
wrappedMutexUnlock(&b->mutex);
//kill player
wrappedMutexLock(&p->mutex);
p->state = DEAD;
wrappedMutexUnlock(&p->mutex);
}
//Otherwise move bullet up
else{
wrappedMutexLock(&b->mutex);
b->prevRow = b->row;
b->row++;
wrappedMutexUnlock(&b->mutex);
}
}
int shipHit(player * p,enemyBullet * b){
int success = false;
if( ((p->row == b->row-1) || (p->row == b->row)) && //bullet's row matches player's
((p->col == b->col-1) || (p->col == b->col-2) || (p->col == b->col))) //bullet's column matches player's
{
success = true;
}
return success;
}
enemyBullet* spawnEnemyBullet(int startRow, int startCol)
{
enemyBullet * b = (enemyBullet*)(malloc(sizeof(enemyBullet)));
if(b == NULL) return NULL; //CHECK FOR MALLOC FAILURE
b->startCol = startCol;
b->startRow = startRow;
b->running = true;
//first bullet: set as head
if(enemyBulletHead == NULL){
wrappedMutexLock(&enemyBulletMutex);
enemyBulletHead = b;
wrappedMutexUnlock(&enemyBulletMutex);
}
//add to the enemy bullet list
else{
enemyBullet * node = enemyBulletHead;
//traverse to end of list
while(node->next != NULL)
{
node = node->next;
}
wrappedMutexLock(&enemyBulletMutex);
node->next = b; //set as last node
wrappedMutexUnlock(&enemyBulletMutex);
}
//Init mutex
wrappedMutexInit(&b->mutex, NULL);
//create and run thread
wrappedPthreadCreate(&(b->thread), NULL, runEBulletT, (void*)b);
return b;
}
void *runEBulletT(void *data)
{
enemyBullet * b = (enemyBullet*)data;
newEnemyBullet(b); //initialize new bullet
while (b->running && gameRunning)
{
switch(b->state)
{
case TRANSIT_E:
eBulletMove(b,playerPtr); //move bullet
break;
case PLAYERHIT:
b->running = false; //stop running
break;
case CLEAN:
b->running = false; //stop running
break;
default:
break;
}
enemyBulletRedraw(b, true); //redraw moved bullet, lock = true
sleepTicks(ENEMY_BULLET_SPEED); //sleep for desired bullet speed
}
return NULL;
}
void enemyBulletRedraw(enemyBullet * b, bool lock)
{
if(lock)
{
//clear and draw image
wrappedMutexLock(&b->mutex);
consoleClearImage(b->prevRow, b->col, 1, 1);
consoleDrawImage(b->row, b->col, ENEMY_BULLET_BODY, 1);
wrappedMutexUnlock(&b->mutex);
}
}
void freeEnemyBullets(enemyBullet * head){
enemyBullet * temp;
//free each node
while (head != NULL){
temp = head;
head = head->next;
free(temp);
}
//set head to null
if(enemyBulletHead) enemyBulletHead = NULL;
}