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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "main.h"
#include "shot.h"
#include "gun.h"
#ifndef BUBLIC_SERVER
#include "image.h"
#include "screen_world.h"
#include "layer.h"
#endif
#ifdef BUBLIC_SERVER
#include "publicServer.h"
#endif
#ifndef BUBLIC_SERVER
static SDL_Surface *g_shot_simple;
static SDL_Surface *g_shot_lasserX;
static SDL_Surface *g_shot_lasserY;
#endif
static bool_t isShotInit = FALSE;
bool_t isShotInicialized()
{
return isShotInit;
}
void initShot()
{
#ifndef BUBLIC_SERVER
assert( isImageInicialized() == TRUE );
#endif
#ifndef BUBLIC_SERVER
g_shot_simple = addImageData("shot.png", IMAGE_ALPHA, "shot", IMAGE_GROUP_BASE);
g_shot_lasserX = addImageData("lasserX.png", IMAGE_NO_ALPHA, "lasserX", IMAGE_GROUP_BASE);
g_shot_lasserY = addImageData("lasserY.png", IMAGE_NO_ALPHA, "lasserY", IMAGE_GROUP_BASE);
#endif
isShotInit = TRUE;
}
shot_t* newShot(int x,int y, int px, int py, int gun, tux_t *author)
{
shot_t *new;
static int last_id = 0;
assert( author != NULL );
new = malloc( sizeof(shot_t) );
memset(new, 0, sizeof(shot_t) );
new->id = ++last_id;
new->x = x;
new->y = y;
new->px = px;
new->py = py;
new->gun = gun;
new->author = author;
new->position = author->position;
new->isCanKillAuthor = FALSE;
switch( gun )
{
case GUN_SIMPLE :
new->w = GUN_SHOT_WIDTH;
new->h = GUN_SHOT_HEIGHT;
#ifndef BUBLIC_SERVER
new->img = g_shot_simple;
#endif
break;
case GUN_LASSER :
switch( author-> position )
{
case TUX_RIGHT :
case TUX_LEFT :
new->w = GUN_LASSER_HORIZONTAL;
new->h = GUN_SHOT_VERTICAL;
#ifndef BUBLIC_SERVER
new->img = g_shot_lasserX;
#endif
break;
case TUX_UP :
case TUX_DOWN :
new->w = GUN_SHOT_VERTICAL;
new->h = GUN_LASSER_HORIZONTAL;
#ifndef BUBLIC_SERVER
new->img = g_shot_lasserY;
#endif
break;
}
break;
default :
assert( ! "Premenna weapon ma zlu hodnotu !" );
break;
}
return new;
}
#ifndef BUBLIC_SERVER
void drawShot(shot_t *p)
{
assert( p != NULL );
addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, TUX_LAYER);
}
void drawListShot(list_t *listShot)
{
shot_t *thisShot;
int i;
assert( listShot != NULL );
for( i = 0 ; i < listShot->count ; i++ )
{
thisShot = (shot_t *)listShot->list[i];
assert( thisShot != NULL );
drawShot(thisShot);
}
}
#endif
int isConflictWithListShot(list_t *listShot, int x, int y, int w, int h)
{
shot_t *thisShot;
int i;
assert( listShot != NULL );
for( i = 0 ; i < listShot->count ; i++ )
{
thisShot = (shot_t *)listShot->list[i];
assert( thisShot != NULL );
if( conflictSpace(x, y, w, h,
thisShot->x, thisShot->y, thisShot->w, thisShot->h) )
{
return 1;
}
}
return 0;
}
void eventMoveListShot(list_t *listShot)
{
shot_t *thisShot;
int i;
assert( listShot != NULL );
for( i = 0 ; i < listShot->count ; i++ )
{
thisShot = (shot_t *)listShot->list[i];
assert( thisShot != NULL );
thisShot->x += thisShot->px;
thisShot->y += thisShot->py;
if( thisShot->x+thisShot->w < 0 || thisShot->x > WINDOW_SIZE_X ||
thisShot->y+thisShot->h < 0 || thisShot->y > WINDOW_SIZE_Y )
{
delListItem(listShot, i, destroyShot);
i--;
continue;
}
}
}
static int myAbs(int n)
{
return ( n > 0 ? n : -n );
}
static int getSppedShot(shot_t *shot)
{
return ( myAbs(shot->px) > myAbs(shot->py) ? myAbs(shot->px) : myAbs(shot->py) );
}
static void eventOnlyLasser(shot_t *shot)
{
switch( shot->position )
{
case TUX_RIGHT :
case TUX_LEFT :
shot->w = GUN_LASSER_HORIZONTAL;
shot->h = GUN_SHOT_VERTICAL;
#ifndef BUBLIC_SERVER
shot->img = g_shot_lasserX;
#endif
break;
case TUX_UP :
case TUX_DOWN :
shot->w = GUN_SHOT_VERTICAL;
shot->h = GUN_LASSER_HORIZONTAL;
#ifndef BUBLIC_SERVER
shot->img = g_shot_lasserY;
#endif
break;
}
}
static void transformShot(shot_t *shot, int position)
{
int speed;
speed = getSppedShot(shot);
switch( position )
{
case TUX_UP :
shot->px = 0;
shot->py = -speed;
break;
case TUX_LEFT :
shot->px = -speed;
shot->py = 0;
break;
case TUX_RIGHT :
shot->px = speed;
shot->py = 0;
break;
case TUX_DOWN :
shot->px = 0;
shot->py = +speed;
break;
}
shot->position = position;
shot->isCanKillAuthor = TRUE;
if( shot->gun == GUN_LASSER )
{
eventOnlyLasser(shot);
}
}
void moveShot(shot_t *shot, int position, int src_x, int src_y,
int dist_x, int dist_y, int dist_w, int dist_h)
{
int offset = 0;
switch( shot->position )
{
case TUX_UP :
case TUX_DOWN :
offset = shot->x - src_x;
break;
case TUX_RIGHT :
case TUX_LEFT :
offset = shot->y - src_y;
break;
}
transformShot(shot, position);
switch( shot->position )
{
case TUX_UP :
shot->x = dist_x + offset;
shot->y = dist_y;
break;
case TUX_LEFT :
shot->x = dist_x;
shot->y = dist_y + offset;
break;
case TUX_RIGHT :
shot->x = dist_x + dist_w;
shot->y = dist_y + offset;
break;
case TUX_DOWN :
shot->x = dist_x + offset;
shot->y = dist_y + dist_h;
break;
}
}
void destroyShot(shot_t *p)
{
assert( p != NULL );
free(p);
}
void quitShot()
{
isShotInit = FALSE;
}
|