2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
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.
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.
14 See the GNU General Public License for more details.
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.
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
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)
41 Trap *trap = new Trap();
44 trap->setTrapType(trapType);
45 trap->setDamage(damage);
46 trap->setSpeed(speed);
47 trap->setDestinations(startX, startY, endX, endY);
49 if (wait1 == -1) wait1 = Math::rrand(10, 60);
50 if (wait2 == -1) wait2 = Math::rrand(10, 60);
52 trap->setWaitTimes(wait1, wait2);
53 trap->setSprite(graphics.getSprite(sprite, true));
54 trap->active = active;
56 if (trap->type == TRAP_TYPE_BARRIER)
58 trap->currentAction = TRAP_FIRSTACTION;
59 trap->thinktime = wait1;
60 if (trap->thinktime == 0)
68 * Toggles the active state of a trap
69 * @param The trap to toggle
71 void toggleTrap(Trap *trap)
73 trap->active = !trap->active;
75 if (trap->type == TRAP_TYPE_MINE)
79 trap->setSprite(graphics.getSprite("ActiveMine", true));
83 trap->setSprite(graphics.getSprite("InActiveMine", true));
89 * Draws the chain link for the spiked ball traps.
90 * @param trap The trap that the chain link should be drawn from
92 void drawTrapChain(Trap *trap)
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);
100 Math::calculateSlope(tx, ty, trap->startX, trap->startY, &dx, &dy);
105 SDL_Surface *chainLink = graphics.getSprite("ChainLink", true)->getCurrentFrame();
107 if ((dx == 0) && (dy == 0))
112 graphics.blit(chainLink, (int)(x - engine.playerPosX), (int)(y - engine.playerPosY), graphics.screen, true);
117 if (Collision::collision(x, y, 3, 3, tx, ty, 4, 4))
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
127 bool doTrapCollisions(Trap *trap)
131 if ((trap->type == TRAP_TYPE_SPIKE) && (trap->currentAction != TRAP_FIRSTACTION))
136 Entity *enemy = (Entity*)map.enemyList.getHead();
138 while (enemy->next != NULL)
140 enemy = (Entity*)enemy->next;
142 if (Collision::collision(trap->x, trap->y, trap->width, trap->height, enemy->x, enemy->y, enemy->width, enemy->height))
144 throwAndDamageEntity(enemy, trap->damage, -5, 5, -8);
149 if ((player.immune > 0) && (player.immune <= 120))
154 if (Collision::collision(trap->x, trap->y, trap->width, trap->height, player.x, player.y, player.width, player.height))
156 throwAndDamageEntity(&player, trap->damage, -5, 5, -8);
158 if (trap->damage == 10)
163 if (trap->type == TRAP_TYPE_MINE)
165 for (int i = 0 ; i < 10 ; i++)
167 addExplosion(trap->x + Math::rrand(-15, 15), trap->y + Math::rrand(-15, 15), 50, &engine.world);
177 * Loops through all the traps and makes them do their thing according
178 * to the type of trap that they are
182 Trap *trap = (Trap*)map.trapList.getHead();
183 Trap *previous = trap;
187 unsigned int absX, absY;
189 while (trap->next != NULL)
191 trap = (Trap*)trap->next;
193 x = (int)(trap->x - engine.playerPosX);
194 y = (int)(trap->y - engine.playerPosY);
196 if (trap->type == TRAP_TYPE_SWING)
198 absX = abs(x - trap->endX);
199 absY = abs(y - trap->endY);
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
213 if (((absX < 800) && (absY < 600)) || (trap->type == TRAP_TYPE_BARRIER) || (trap->type == TRAP_TYPE_FLAME))
217 if ((trap->think()) && (trap->active))
222 audio.playSound(SND_ROCKET, CH_SPAWN, trap->x);
224 case TRAP_TYPE_SPIKE:
225 audio.playSound(SND_ROCKET, CH_SPAWN, trap->x);
227 case TRAP_TYPE_SWING:
228 audio.playSound(SND_THROW, CH_SPAWN, trap->x);
230 case TRAP_TYPE_BARRIER:
231 if ((absX <= 640) && (absY <= 480))
233 audio.playSound(SND_ELECTRICITY1 + Math::prand() % 3, CH_SPAWN, trap->x);
236 case TRAP_TYPE_FLAME:
237 if ((absX <= 640) && (absY <= 480))
239 audio.playSound(SND_FIRECRACKLE, CH_SPAWN, trap->x);
245 if (trap->type == TRAP_TYPE_MINE)
247 mx = (int)trap->x >> BRICKSHIFT;
248 my = (int)(trap->y + trap->dy + trap->sprite->image[0]->h) >> BRICKSHIFT;
250 if (map.isSolid(mx, my))
252 trap->y = (my * BRICKSIZE) - trap->sprite->image[0]->h;
257 if (trap->type == TRAP_TYPE_SWING)
264 case TRAP_TYPE_FLAME:
266 graphics.blit(trap->sprite->getCurrentFrame(), x, y, graphics.screen, true);
268 if ((absX <= 640) && (absY <= 480))
270 if (trap->currentAction == TRAP_FIRSTACTION)
274 engine.world.place((int)trap->x + (trap->startX * 8), (int)trap->y + (trap->startY * 8));
275 engine.world.currentWeapon = &weapon[WP_FLAMETHROWER];
277 if (trap->startX == -1)
279 addBullet(&engine.world, Math::rrand(-500, -100) / 100.00, Math::rrand(-20, 20) / 100.00);
281 else if (trap->startX == 1)
283 addBullet(&engine.world, Math::rrand(100, 500) / 100.00, Math::rrand(-2, 20) / 100.00);
285 else if (trap->startY == -1)
287 addBullet(&engine.world, Math::rrand(-20, 20) / 100.00, Math::rrand(-500, -100) / 100.00);
289 else if (trap->startY == 1)
291 addBullet(&engine.world, Math::rrand(-20, 20) / 100.00, Math::rrand(100, 500) / 100.00);
298 case TRAP_TYPE_BARRIER:
299 if (trap->currentAction == TRAP_FIRSTACTION)
303 graphics.blit(trap->sprite->getCurrentFrame(), x, y, graphics.screen, false);
304 doTrapCollisions(trap);
310 graphics.blit(trap->sprite->getCurrentFrame(), x, y, graphics.screen, false);
313 remove = doTrapCollisions(trap);
320 if (trap->type != TRAP_TYPE_MINE)
329 map.trapList.remove(previous, trap);