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