-
Notifications
You must be signed in to change notification settings - Fork 2
/
Defesa.c
79 lines (72 loc) · 1.88 KB
/
Defesa.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
#include "Defesa.h"
#include "general.h"
#include "Cenario.h"
#include "Nave.h"
#include <stdlib.h>
#include <math.h>
Defense *initDefense(Point *p, int hp, TIRO_TIPO shotType) {
Defense *d;
d = mallocSafe(sizeof(Defense));
d->position = p;
d->created = clock();
d->hp = DEFAULT_HP;
if (shotType == NORMAL) {
d->accuracy = DEFAULT_ACCURACY;
d->shotPower = DEFAULT_POWER;
d->shotFrequency = DEFAULT_FREQUENCY;
}
return d;
}
int isDefenseDestroyed(Defense *d) {
if (d->hp <= 0)
ship->score += 10;
return d->hp <= 0;
}
void defenseGotHit(Defense *d, int shotPower) {
d->hp -= shotPower;
}
void alterAttackPattern(Defense *d) {
if (d->distance <= CRITICAL_DISTANCE) d->shotFrequency += 2;
}
void collisionsDefense(Defense *d)
{
nodeTiro *t;
nodeTiro *at;
at = tiroList;
t = at->next;
while (t != NULL) {
if (t->tiro->source == NAVE && collision(d->position, 2.0, t->tiro->position, 0.15)) {
defenseGotHit(d, t->tiro->shotPower);
at->next = t->next;
freeTiro(t->tiro);
free(t);
t = at->next;
}
at = t;
if (t != NULL)
t = t->next;
}
}
boolean updateDefense(Defense *d)
{
collisionsDefense(d);
d->position->z -= (double) DZ;
if (d->position->z < Z_ORIGIN) return TRUE;
double shootChance = RandomNumber(0.0, 100.0);
if (shootChance > 97.0) {
Point *p = initPoint(d->position->x, d->position->y - 1.1f, d->position->z - 1.0);
Point *q = initPoint(ship->position->x - d->position->x , ship->position->y - d->position->y,
ship->position->z - d->position->z);
includeTiro(initTiro(p, q, NORMAL, DEFESA));
}
return FALSE;
}
/*Não basta dar free em d, precisa librera a struct position também*/
void freeDefense(Defense *d)
{
if(d != NULL)
{
free(d->position);
free(d);
}
}