-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.c
186 lines (156 loc) · 5.45 KB
/
enemy.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
//
// Created by Kamil Walkowicz on 18/01/2018.
//
#include "classes.h"
#include "config.h"
#include "window.h"
void Enemy_ctor(EnemyArr* enemies, float enterTime, float x, float y,
MRectPtrArr* fg, SDL_Renderer* ren) {
Enemy obj;
MRect_ctor(&obj.super, "enemy",
x, y, ENEMY_W, ENEMY_H,
loadTexture(ren, "enemies/black1.png")
);
obj.super.super.explosionTex = loadTexture(ren, "enemies/explode.png");
obj.enterTime = enterTime;
obj.size = 1;
obj.idx = 0;
obj.path = calloc(obj.size, sizeof(Shape));
Enemy_addPoint(&obj, x, y);
EnemyArr_add(enemies, obj, fg);
MRectPtrArr_add(fg, (MRect*)&enemies->arr[enemies->idx - 1]);
MRectPtrArr_sort(fg, 'x');
}
uint Enemy_addPoint(Enemy* self, float x, float y) {
Shape point;
Shape_ctor(&point, x, y);
if(self->idx + 1 >= self->size) {
self->size *= 2;
Shape* temp = realloc(self->path, self->size * sizeof(Shape));
if(temp != NULL)
self->path = temp;
else {
free(self->path);
printf("\nMemory alloc problem! Aborting!\n");
return 1;
}
}
self->path[self->idx] = point;
self->idx++;
return 0;
}
void Enemy_delPoint(Enemy* self, EnemyArr* enemies, MRectPtrArr* fg) {
if(self->idx) {
for(uint id = 0; id < self->idx - 1; id++)
self->path[id] = self->path[id + 1];
self->idx--;
} else {
Rect_destroy((Rect*)self);
size_t id = self - enemies->arr;
EnemyArr_del(enemies, (uint)id, fg);
}
}
uint Enemy_update(Enemy* self, float td, EnemyArr* enemies, MRectPtrArr* fg) {
if(self->enterTime - td >= 0) {
self->enterTime -= td;
return 0;
}
int diff = (int)(ENEMY_VELOCITY_GOAL * td / sqrt(2));
if(abs((int)(self->super.super.super.x - self->path->x)) < diff &&
abs((int)(self->super.super.super.y - self->path->y)) < diff)
Enemy_delPoint(self, enemies, fg);
Vector_ctor(&self->direction,
self->path->x - self->super.super.super.x,
self->path->y - self->super.super.super.y
);
self->super.vectors.velocityGoal.x = ENEMY_VELOCITY_GOAL *
self->direction.x / (float)sqrt(
self->direction.x *
self->direction.x +
(double)self->direction.y
* self->direction.y
);
self->super.vectors.velocityGoal.y = self->super.vectors.velocityGoal.x *
self->direction.y /
self->direction.x;
return 0;
}
void Enemy_explode(Enemy* self, EnemyArr* enemies,
MRectPtrArr* fg, SDL_Renderer* ren) {
self->super.vectors.velocityGoal.x = 0;
self->super.vectors.velocityGoal.y = 0;
SDL_Rect clip = {
((int) self->super.super.explosionState % 5) * ENEMY_EXPLOSION_TILE_W,
((int) self->super.super.explosionState / 5) * ENEMY_EXPLOSION_TILE_H,
ENEMY_EXPLOSION_TILE_W, ENEMY_EXPLOSION_TILE_H
};
renderTexture(self->super.super.explosionTex, ren,
(int) self->super.super.super.x -
(ENEMY_EXPLOSION_W - ENEMY_W) / 2,
(int) self->super.super.super.y -
(ENEMY_EXPLOSION_H - ENEMY_H) / 2,
ENEMY_EXPLOSION_W,
ENEMY_EXPLOSION_H,
&clip
);
if(self->super.super.explosionState == 5) {
Rect_destroy((Rect *) self);
}
if(self->super.super.explosionState < 10)
self->super.super.explosionState++;
if(self->super.super.explosionState == 10) {
size_t idx = self - enemies->arr;
EnemyArr_del(enemies, (uint) idx, fg);
}
}
void EnemyArr_ctor(EnemyArr* self) {
self->size = 1;
self->idx = 0;
self->arr = calloc(self->size, sizeof(Enemy));
}
uint EnemyArr_add(EnemyArr* self, Enemy obj, MRectPtrArr* fg) {
Enemy* prevAddr = self->arr;
if(self->idx + 1 >= self->size) {
self->size *= 2;
Enemy* temp = realloc(self->arr, self->size * sizeof(Enemy));
if(temp != NULL)
self->arr = temp;
else {
free(self->arr);
printf("\nMemory alloc problem! Aborting!\n");
return 1;
}
}
self->arr[self->idx] = obj;
self->idx++;
if(self->arr != prevAddr) {
Enemy* max = prevAddr + (self->idx - 1) * sizeof(Enemy);
for(uint i = 0; i < fg->idx; i++)
if((MRect*)prevAddr <= fg->arr[i] && fg->arr[i] <= (MRect*)max) {
size_t idx = ((Enemy*)fg->arr[i] - prevAddr);
fg->arr[i] = (MRect*)&self->arr[idx];
}
}
return 0;
}
void EnemyArr_update(EnemyArr* self, float td, MRectPtrArr* fg) {
for(uint i = 0; i < self->idx; i++)
Enemy_update(&self->arr[i], td, self, fg);
}
void EnemyArr_del(EnemyArr* self, uint id, MRectPtrArr* fg) {
Enemy* max = self->arr + (self->idx - 1) * sizeof(Enemy);
for(uint i = 0; i < fg->idx; i++) {
if((MRect*)self->arr <= fg->arr[i] && fg->arr[i] <= (MRect*)max) {
size_t idx = (Enemy*)fg->arr[i] - self->arr;
if(idx > id) {
fg->arr[i] = (MRect*)&self->arr[idx - 1];
}
}
}
MRectPtrArr_del(fg, (MRect*)&self->arr[id]);
if(id <= self->idx + 1) {
for(; id < self->idx - 1; id++)
self->arr[id] = self->arr[id + 1];
self->idx--;
}
}