]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/traps.cpp
Don't link pak tool with SDL.
[quix0rs-blobwars.git] / src / traps.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "traps.h"
22
23 /**
24 * Adds a trap to the map
25 * @param name The name of the trap group
26 * @param trapType The type of trap this is
27 * @param damage The damage the trap does
28 * @param speed The speed at which the trap moves
29 * @param startY The starting X location of the trap
30 * @param startY The starting Y location of the trap
31 * @param startY The ending X location of the trap
32 * @param startY The ending Y location of the trap
33 * @param wait1 The first state wait time
34 * @param wait2 The second state wait time
35 * @param sprite The Sprite to use with this trap
36 * @param active The active state of this trap
37 */
38 void addTrap(const char *name, int trapType, int damage, int speed, int startX, int startY, int endX, int endY, int wait1, int wait2, const char *sprite, bool active)
39 {
40         Trap *trap = new Trap();
41
42         trap->setName(name);
43         trap->setTrapType(trapType);
44         trap->setDamage(damage);
45         trap->setSpeed(speed);
46         trap->setDestinations(startX, startY, endX, endY);
47         
48         if (wait1 == -1) wait1 = Math::rrand(10, 60);
49         if (wait2 == -1) wait2 = Math::rrand(10, 60);
50
51         trap->setWaitTimes(wait1, wait2);
52         trap->setSprite(graphics.getSprite(sprite, true));
53         trap->active = active;
54
55         if (trap->type == TRAP_TYPE_BARRIER)
56         {
57                 trap->currentAction = TRAP_FIRSTACTION;
58                 trap->thinktime = wait1;
59                 if (trap->thinktime == 0)
60                         trap->thinktime = 1;
61         }
62
63         map.addTrap(trap);
64 }
65
66 /**
67 * Toggles the active state of a trap
68 * @param The trap to toggle
69 */
70 void toggleTrap(Trap *trap)
71 {
72         trap->active = !trap->active;
73
74         if (trap->type == TRAP_TYPE_MINE)
75         {
76                 if (trap->active)
77                 {
78                         trap->setSprite(graphics.getSprite("ActiveMine", true));
79                 }
80                 else
81                 {
82                         trap->setSprite(graphics.getSprite("InActiveMine", true));
83                 }
84         }
85 }
86
87 /**
88 * Draws the chain link for the spiked ball traps.
89 * @param trap The trap that the chain link should be drawn from
90 */
91 void drawTrapChain(Trap *trap)
92 {
93         float x = trap->startX;
94         float y = trap->startY;
95         int tx = (int)trap->x + (trap->sprite->image[0]->w / 2);
96         int ty = (int)trap->y + (trap->sprite->image[0]->h / 2);
97         float dx, dy;
98
99         Math::calculateSlope(tx, ty, trap->startX, trap->startY, &dx, &dy);
100
101         dx *= 7;
102         dy *= 7;
103
104         SDL_Surface *chainLink = graphics.getSprite("ChainLink", true)->getCurrentFrame();
105
106         if ((dx == 0) && (dy == 0))
107                 return;
108
109         while (true)
110         {
111                 graphics.blit(chainLink, (int)(x - engine.playerPosX), (int)(y - engine.playerPosY), graphics.screen, true);
112                 
113                 x += dx;
114                 y += dy;
115
116                 if (Collision::collision(x, y, 3, 3, tx, ty, 4, 4))
117                         break;
118         }
119 }
120
121 /**
122 * Checks for a trap colliding with enemies or the Player
123 * @param trap The trap to perform the check
124 * @return Whether the trap made contact with an enemy or the Player
125 */
126 bool doTrapCollisions(Trap *trap)
127 {
128         bool hit = false;
129
130         if ((trap->type == TRAP_TYPE_SPIKE) && (trap->currentAction != TRAP_FIRSTACTION))
131         {
132                 return false;
133         }
134
135         Entity *enemy = (Entity*)map.enemyList.getHead();
136
137         while (enemy->next != NULL)
138         {
139                 enemy = (Entity*)enemy->next;
140
141                 if (Collision::collision(trap->x, trap->y, trap->width, trap->height, enemy->x, enemy->y, enemy->width, enemy->height))
142                 {
143                         throwAndDamageEntity(enemy, trap->damage, -5, 5, -8);
144                         hit = true;
145                 }
146         }
147
148         if ((player.immune > 0) && (player.immune <= 120))
149         {
150                 return hit;
151         }
152
153         if (Collision::collision(trap->x, trap->y, trap->width, trap->height, player.x, player.y, player.width, player.height))
154         {
155                 throwAndDamageEntity(&player, trap->damage, -5, 5, -8);
156                 
157                 if (trap->damage == 10)
158                 {
159                         player.health = 0;
160                 }
161                 
162                 if (trap->type == TRAP_TYPE_MINE)
163                 {
164                         for (int i = 0 ; i < 10 ; i++)
165                         {
166                                 addExplosion(trap->x + Math::rrand(-15, 15), trap->y + Math::rrand(-15, 15), 50, &engine.world);
167                         }
168                 }
169                 hit = true;
170         }
171
172         return hit;
173 }
174
175 /**
176 * Loops through all the traps and makes them do their thing according
177 * to the type of trap that they are
178 */
179 void doTraps()
180 {
181         Trap *trap = (Trap*)map.trapList.getHead();
182         Trap *previous = trap;
183
184         bool remove = false;
185         int x, y, mx, my;
186         unsigned int absX, absY;
187
188         while (trap->next != NULL)
189         {
190                 trap = (Trap*)trap->next;
191                 
192                 x = (int)(trap->x - engine.playerPosX);
193                 y = (int)(trap->y - engine.playerPosY);
194
195                 if (trap->type == TRAP_TYPE_SWING)
196                 {
197                         absX = abs(x - trap->endX);
198                         absY = abs(y - trap->endY);
199                 }
200                 else
201                 {
202                         absX = abs(x);
203                         absY = abs(y);
204                 }
205                 
206                 /*
207                         Barrier traps must be do at all times. Otherwise there is an "break" in the
208                         line if more than one barrier is used over a large space and the barrier becomes
209                         inconsistant.
210                 */
211
212                 if (((absX < 800) && (absY < 600)) || (trap->type == TRAP_TYPE_BARRIER) || (trap->type == TRAP_TYPE_FLAME))
213                 {
214                         remove = false;
215
216                         if ((trap->think()) && (trap->active))
217                         {
218                                 switch (trap->type)
219                                 {
220                                         case TRAP_TYPE_MINE:
221                                                 audio.playSound(SND_ROCKET, CH_SPAWN);
222                                                 break;
223                                         case TRAP_TYPE_SPIKE:
224                                                 audio.playSound(SND_ROCKET, CH_SPAWN);
225                                                 break;
226                                         case TRAP_TYPE_SWING:
227                                                 audio.playSound(SND_THROW, CH_SPAWN);
228                                                 break;
229                                         case TRAP_TYPE_BARRIER:
230                                                 if ((absX <= 640) && (absY <= 480))
231                                                 {
232                                                         audio.playSound(SND_ELECTRICITY1 + Math::prand() % 3, CH_SPAWN);
233                                                 }
234                                                 break;
235                                         case TRAP_TYPE_FLAME:
236                                                 if ((absX <= 640) && (absY <= 480))
237                                                 {
238                                                         audio.playSound(SND_FIRECRACKLE, CH_SPAWN);
239                                                 }
240                                                 break;
241                                 }
242                         }
243
244                         if (trap->type == TRAP_TYPE_MINE)
245                         {
246                                 mx = (int)trap->x >> BRICKSHIFT;
247                                 my = (int)(trap->y + trap->dy + trap->sprite->image[0]->h) >> BRICKSHIFT;
248
249                                 if (map.isSolid(mx, my))
250                                 {
251                                         trap->y = (my * BRICKSIZE) - trap->sprite->image[0]->h;
252                                         trap->dy = 0;
253                                 }
254                         }
255
256                         if (trap->type == TRAP_TYPE_SWING)
257                         {
258                                 drawTrapChain(trap);
259                         }
260
261                         switch (trap->type)
262                         {
263                                 case TRAP_TYPE_FLAME:
264                                 
265                                         graphics.blit(trap->sprite->getCurrentFrame(), x, y, graphics.screen, true);
266
267                                         if ((absX <= 640) && (absY <= 480))
268                                         {
269                                                 if (trap->currentAction == TRAP_FIRSTACTION)
270                                                 {
271                                                         if (trap->active)
272                                                         {
273                                                                 engine.world.place((int)trap->x + (trap->startX * 8), (int)trap->y + (trap->startY * 8));
274                                                                 engine.world.currentWeapon = &weapon[WP_FLAMETHROWER];
275         
276                                                                 if (trap->startX == -1)
277                                                                 {
278                                                                         addBullet(&engine.world, Math::rrand(-500, -100) / 100.00, Math::rrand(-20, 20) / 100.00);
279                                                                 }
280                                                                 else if (trap->startX == 1)
281                                                                 {
282                                                                         addBullet(&engine.world, Math::rrand(100, 500) / 100.00, Math::rrand(-2, 20) / 100.00);
283                                                                 }
284                                                                 else if (trap->startY == -1)
285                                                                 {
286                                                                         addBullet(&engine.world, Math::rrand(-20, 20) / 100.00, Math::rrand(-500, -100) / 100.00);
287                                                                 }
288                                                                 else if (trap->startY == 1)
289                                                                 {
290                                                                         addBullet(&engine.world, Math::rrand(-20, 20) / 100.00, Math::rrand(100, 500) / 100.00);
291                                                                 }
292                                                         }
293                                                 }
294                                         }
295                                         break;
296
297                                 case TRAP_TYPE_BARRIER:
298                                         if (trap->currentAction == TRAP_FIRSTACTION)
299                                         {
300                                                 if (trap->active)
301                                                 {
302                                                         graphics.blit(trap->sprite->getCurrentFrame(), x, y, graphics.screen, false);
303                                                         doTrapCollisions(trap);
304                                                 }
305                                         }
306                                         break;
307
308                                 default:
309                                         graphics.blit(trap->sprite->getCurrentFrame(), x, y, graphics.screen, false);
310                                 if (trap->active)
311                                         {
312                                                 remove = doTrapCollisions(trap);
313                                         }
314                                         break;
315                         }
316                 }
317
318
319                 if (trap->type != TRAP_TYPE_MINE)
320                         remove = false;
321
322                 if (!remove)
323                 {
324                         previous = trap;
325                 }
326                 else
327                 {
328                         map.trapList.remove(previous, trap);
329                         trap = previous;
330                 }
331         }
332 }