-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcenario.c
More file actions
100 lines (79 loc) · 3.02 KB
/
cenario.c
File metadata and controls
100 lines (79 loc) · 3.02 KB
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
#include "cenario.h"
#include "enemy.h"
#include "enemyQueue.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Cenario* createCenario(Dimension dimension) {
Cenario *cen = malloc (sizeof(Cenario));
cen->enemies = createEnemyQueue();
cen->dimension.x = dimension.x;
cen->dimension.y = dimension.y;
cen->dimension.z = dimension.z;
initEnemies(cen);
srand((unsigned int)time(NULL));
return cen;
}
void refreshCenario(Cenario *cenario, Position shipPosition) {
Enemy *firstEnemy = cenario->enemies->first;
Enemy *lastEnemy = cenario->enemies->last;
float max = BUFFER_SIZE + shipPosition.z;
while (max - lastEnemy->position.z > MAX_DISTANCE_BETWEEN_ENEMIES) {
createNewEnemyInInterval(lastEnemy->position.z, max, cenario);
lastEnemy = cenario->enemies->last;
}
while (shipPosition.z > firstEnemy->position.z) {
free (dequeueEnemy(cenario->enemies));
firstEnemy = cenario->enemies->first;
}
}
static void createNewEnemyInInterval(float min, float max, Cenario *cenario){
int precision = rand();
Position randomPos;
randomPos.x = rand() % (int)cenario->dimension.x;
randomPos.y = 0;
randomPos.z = rand() % (int)(max - min) + min;
enqueueEnemy(createEnemy(randomPos, defaultEnemyDim, precision), cenario->enemies);
}
static void initEnemies(Cenario *cenario) {
Enemy *lastEnemy;
createNewEnemyInInterval(0, MAX_DISTANCE_BETWEEN_ENEMIES, cenario);
lastEnemy = cenario->enemies->last;
while (BUFFER_SIZE - lastEnemy->position.z > MAX_DISTANCE_BETWEEN_ENEMIES) {
createNewEnemyInInterval(lastEnemy->position.z, lastEnemy->position.z + MAX_DISTANCE_BETWEEN_ENEMIES, cenario);
lastEnemy = cenario->enemies->last;
}
}
BOOL verifyShipColision(Ship *ship, Cenario *cenario) {
Enemy *first = cenario->enemies->first;
if (ship->position.x >= first->position.x &&
ship->position.x <= first->position.x + first->dimension.x)
if (ship->position.z >= first->position.z)
return TRUE;
return FALSE;
}
BOOL verifyShotColision(Shot *shot, Cenario *cenario) {
if (isInsideCenario(shot->position, cenario))
return TRUE;
else {
EnemyNode *node = cenario->enemies->head->next;
while (node != cenario->enemies->head) {
if (shot->position.x == node->enemy->position.x)
if (shot->position.z >= node->enemy->position.z) {
gotShotEnemy(node->enemy, 20);
if (node->enemy->life <= 0)
removeEnemyNode(node, cenario->enemies);
return TRUE;
}
node = node->next;
}
}
return FALSE;
}
BOOL isInsideCenario(Position position, Cenario *cenario) {
if (position.x <= 0 || position.x >= cenario->dimension.x ||
position.y <= 0 || position.y >= cenario->dimension.y ||
position.z <= 0 || position.z >= cenario->dimension.z)
return TRUE;
return FALSE;
}