]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/particles.cpp
Prevent a segmentation fault when using the -map option without specifying a map.
[quix0rs-blobwars.git] / src / particles.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 "particles.h"
23
24 void addWindParticles()
25 {
26         int c = graphics.white;
27         float x, y, dx, dy;
28         
29
30         for (int i = 0 ; i < 50 ; i++)
31         {
32                 if (player.x < 320)
33                 {
34                         x = Math::rrand(-100, 700);
35                 }
36                 else
37                 {
38                         x = player.x + Math::rrand(-450, 450);
39                 }
40                         
41                 c = Math::prand() % 4;
42                 switch (c)
43                 {
44                         case 0: c = graphics.white; break;
45                         case 1: c = graphics.lightGrey; break;
46                         case 2: c = graphics.grey; break;
47                         case 3: c = graphics.darkGrey; break;
48                 }
49                 
50                 y = player.y + Math::rrand(-450, 450);
51                 dx = Math::rrand(1, 100) * map.windPower;
52                 dx /= 100;
53                 dy = Math::rrand(1, 10); dy /= 10;
54                 map.addParticle(x, y, dx, dy, 120, c, NULL, PAR_WEIGHTLESS);
55         }
56 }
57
58 void addColorParticles(float x, float y, int amount, int color)
59 {
60         int c = color;
61         float dx, dy;
62
63         for (int i = 0 ; i < amount ; i++)
64         {
65                 if (color == -1)
66                 {
67                         c = Math::prand() % 5;
68                         switch (c)
69                         {
70                                 case 0: c = graphics.white; break;
71                                 case 1: c = graphics.grey; break;
72                                 case 2: c = graphics.blue; break;
73                                 case 3: c = graphics.cyan; break;
74                                 case 4: c = graphics.red; break;
75                         }
76                 }
77
78                 dx = Math::rrand(-30, 30); dx /= 30;
79                 dy = Math::rrand(-30, 30); dy /= 30;
80                 map.addParticle(x, y, dx, dy, Math::rrand(5, 30), c, NULL, 0);
81         }
82 }
83
84 void addFireTrailParticle(float x, float y)
85 {
86         map.addParticle(x, y, 0, 0, 12, graphics.red, graphics.getSprite("SmallExplosion", true), PAR_WEIGHTLESS);
87 }
88
89 void addFireParticles(float x, float y, int amount)
90 {
91         map.addParticle(x + Math::rrand(-2, 2), y + Math::rrand(-2, 2), 0, 1, Math::rrand(5, 30), graphics.red, graphics.getSprite("Explosion", true), PAR_COLLIDES);
92 }
93
94 void addBubble(float x, float y)
95 {
96         if ((Math::prand() % 50) == 0)
97         {
98                 map.addParticle(x + Math::prand() % BRICKSIZE, y + 19, 0, Math::rrand(-3, -1), Math::rrand(30, 90), graphics.red, graphics.getSprite("Bubble", true), PAR_COLLIDES + PAR_WEIGHTLESS);
99         }
100 }
101
102 void throwStalagParticles(float x, float y)
103 {
104         Sprite *stalagPiece = graphics.getSprite("StalagPiece", true);
105         
106         int amount = Math::rrand(3, 6);
107         
108         for (int i = 0 ; i < amount ; i++)
109         {
110                 map.addParticle(x, y, Math::rrand(-2, 2), Math::rrand(-3, -1), Math::rrand(5, 30), graphics.red, stalagPiece, 0);
111         }
112 }
113
114 void throwBrickParticles(float x, float y)
115 {
116         int amount = Math::rrand(4, 8);
117         
118         Sprite *wallPiece = graphics.getSprite("WallPiece", true);
119         
120         for (int i = 0 ; i < amount ; i++)
121         {
122                 map.addParticle(x, y, Math::rrand(-2, 2), Math::rrand(-4, -1), Math::rrand(5, 30), graphics.red, wallPiece, 0);
123         }
124 }
125
126 void addTeleportParticles(float x, float y, int amount, int soundToPlay)
127 {
128         Sprite *teleportStar = graphics.getSprite("TeleportStar", true);
129         float dx, dy;
130
131         for (int i = 0 ; i < amount ; i++)
132         {
133                 dx = Math::rrand(-30, 30); dx /= 20;
134                 dy = Math::rrand(-30, 30); dy /= 20;
135                 map.addParticle(x, y, dx, dy, Math::rrand(30, 60), graphics.red, teleportStar, PAR_WEIGHTLESS);
136         }
137
138         if (soundToPlay != -1)
139         {
140                 audio.playSound(soundToPlay, CH_SPAWN);
141         }
142 }
143
144 void doParticles()
145 {
146         Particle *particle = (Particle*)map.particleList.getHead();
147         Particle *previous = particle;
148
149         int x, y;
150
151         while (particle->next != NULL)
152         {
153                 particle = (Particle*)particle->next;
154
155                 x = (int)(particle->x - engine.playerPosX);
156                 y = (int)(particle->y - engine.playerPosY);
157
158                 if (particle->sprite == NULL)
159                 {
160                         graphics.lock(graphics.screen);
161                         
162                         graphics.putPixel(x, y, particle->color, graphics.screen);
163                         
164                         graphics.unlock(graphics.screen);
165                 }
166                 else
167                 {
168                         graphics.blit(particle->getFrame(), x, y, graphics.screen, false);
169                 }
170
171                 particle->health--;
172
173                 particle->move();
174
175                 if (!(particle->flags & PAR_WEIGHTLESS))
176                 {
177                         particle->dy += 0.1;
178                 }
179
180                 x = (int)particle->x >> BRICKSHIFT;
181                 y = (int)particle->y >> BRICKSHIFT;
182
183                 if (particle->flags & PAR_COLLIDES)
184                 {
185                         if (map.isSolid(x, y))
186                         {
187                                 particle->health = 0;
188                         }
189                 }
190
191                 if (particle->health > 0)
192                 {
193                         previous = particle;
194                 }
195                 else
196                 {
197                         map.particleList.remove(previous, particle);
198                         particle = previous;
199                 }
200         }
201 }