2 Copyright (C) 2004-2010 Parallel Realities
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.
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.
13 See the GNU General Public License for more details.
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.
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
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)
40 Trap *trap = new Trap();
43 trap->setTrapType(trapType);
44 trap->setDamage(damage);
45 trap->setSpeed(speed);
46 trap->setDestinations(startX, startY, endX, endY);
48 if (wait1 == -1) wait1 = Math::rrand(10, 60);
49 if (wait2 == -1) wait2 = Math::rrand(10, 60);
51 trap->setWaitTimes(wait1, wait2);
52 trap->setSprite(graphics.getSprite(sprite, true));
53 trap->active = active;
55 if (trap->type == TRAP_TYPE_BARRIER)
57 trap->currentAction = TRAP_FIRSTACTION;
58 trap->thinktime = wait1;
59 if (trap->thinktime == 0)
67 * Toggles the active state of a trap
68 * @param The trap to toggle
70 void toggleTrap(Trap *trap)
72 trap->active = !trap->active;
74 if (trap->type == TRAP_TYPE_MINE)
78 trap->setSprite(graphics.getSprite("ActiveMine", true));
82 trap->setSprite(graphics.getSprite("InActiveMine", true));
88 * Draws the chain link for the spiked ball traps.
89 * @param trap The trap that the chain link should be drawn from
91 void drawTrapChain(Trap *trap)
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);
99 Math::calculateSlope(tx, ty, trap->startX, trap->startY, &dx, &dy);
104 SDL_Surface *chainLink = graphics.getSprite("ChainLink", true)->getCurrentFrame();
106 if ((dx == 0) && (dy == 0))
111 graphics.blit(chainLink, (int)(x - engine.playerPosX), (int)(y - engine.playerPosY), graphics.screen, true);
116 if (Collision::collision(x, y, 3, 3, tx, ty, 4, 4))
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
126 bool doTrapCollisions(Trap *trap)
130 if ((trap->type == TRAP_TYPE_SPIKE) && (trap->currentAction != TRAP_FIRSTACTION))
135 Entity *enemy = (Entity*)map.enemyList.getHead();
137 while (enemy->next != NULL)
139 enemy = (Entity*)enemy->next;
141 if (Collision::collision(trap->x, trap->y, trap->width, trap->height, enemy->x, enemy->y, enemy->width, enemy->height))
143 throwAndDamageEntity(enemy, trap->damage, -5, 5, -8);
148 if ((player.immune > 0) && (player.immune <= 120))
153 if (Collision::collision(trap->x, trap->y, trap->width, trap->height, player.x, player.y, player.width, player.height))
155 throwAndDamageEntity(&player, trap->damage, -5, 5, -8);
157 if (trap->damage == 10)
162 if (trap->type == TRAP_TYPE_MINE)
164 for (int i = 0 ; i < 10 ; i++)
166 addExplosion(trap->x + Math::rrand(-15, 15), trap->y + Math::rrand(-15, 15), 50, &engine.world);
176 * Loops through all the traps and makes them do their thing according
177 * to the type of trap that they are
181 Trap *trap = (Trap*)map.trapList.getHead();
182 Trap *previous = trap;
186 unsigned int absX, absY;
188 while (trap->next != NULL)
190 trap = (Trap*)trap->next;
192 x = (int)(trap->x - engine.playerPosX);
193 y = (int)(trap->y - engine.playerPosY);
195 if (trap->type == TRAP_TYPE_SWING)
197 absX = abs(x - trap->endX);
198 absY = abs(y - trap->endY);
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
212 if (((absX < 800) && (absY < 600)) || (trap->type == TRAP_TYPE_BARRIER) || (trap->type == TRAP_TYPE_FLAME))
216 if ((trap->think()) && (trap->active))
221 audio.playSound(SND_ROCKET, CH_SPAWN);
223 case TRAP_TYPE_SPIKE:
224 audio.playSound(SND_ROCKET, CH_SPAWN);
226 case TRAP_TYPE_SWING:
227 audio.playSound(SND_THROW, CH_SPAWN);
229 case TRAP_TYPE_BARRIER:
230 if ((absX <= 640) && (absY <= 480))
232 audio.playSound(SND_ELECTRICITY1 + Math::prand() % 3, CH_SPAWN);
235 case TRAP_TYPE_FLAME:
236 if ((absX <= 640) && (absY <= 480))
238 audio.playSound(SND_FIRECRACKLE, CH_SPAWN);
244 if (trap->type == TRAP_TYPE_MINE)
246 mx = (int)trap->x >> BRICKSHIFT;
247 my = (int)(trap->y + trap->dy + trap->sprite->image[0]->h) >> BRICKSHIFT;
249 if (map.isSolid(mx, my))
251 trap->y = (my * BRICKSIZE) - trap->sprite->image[0]->h;
256 if (trap->type == TRAP_TYPE_SWING)
263 case TRAP_TYPE_FLAME:
265 graphics.blit(trap->sprite->getCurrentFrame(), x, y, graphics.screen, true);
267 if ((absX <= 640) && (absY <= 480))
269 if (trap->currentAction == TRAP_FIRSTACTION)
273 engine.world.place((int)trap->x + (trap->startX * 8), (int)trap->y + (trap->startY * 8));
274 engine.world.currentWeapon = &weapon[WP_FLAMETHROWER];
276 if (trap->startX == -1)
278 addBullet(&engine.world, Math::rrand(-500, -100) / 100.00, Math::rrand(-20, 20) / 100.00);
280 else if (trap->startX == 1)
282 addBullet(&engine.world, Math::rrand(100, 500) / 100.00, Math::rrand(-2, 20) / 100.00);
284 else if (trap->startY == -1)
286 addBullet(&engine.world, Math::rrand(-20, 20) / 100.00, Math::rrand(-500, -100) / 100.00);
288 else if (trap->startY == 1)
290 addBullet(&engine.world, Math::rrand(-20, 20) / 100.00, Math::rrand(100, 500) / 100.00);
297 case TRAP_TYPE_BARRIER:
298 if (trap->currentAction == TRAP_FIRSTACTION)
302 graphics.blit(trap->sprite->getCurrentFrame(), x, y, graphics.screen, false);
303 doTrapCollisions(trap);
309 graphics.blit(trap->sprite->getCurrentFrame(), x, y, graphics.screen, false);
312 remove = doTrapCollisions(trap);
319 if (trap->type != TRAP_TYPE_MINE)
328 map.trapList.remove(previous, trap);