Oolite
Loading...
Searching...
No Matches
OOExplosionCloudEntity.m
Go to the documentation of this file.
1/*
2
3OOExplosionCloudEntity.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
28#import "OOColor.h"
30#import "OOTexture.h"
31#import "PlayerEntity.h"
32#import "Universe.h"
33
34#define kExplosionCloudDuration 0.9
35#define kGrowthRateFactor 1.5f
36#define kExplosionCloudAlpha 0.85f
37#define kExplosionBrightnessMult 1.0f
38#define kExplosionDefaultSize 2.5f
39
40// keys for plist file
41static NSString* const kExplosionAlpha = @"alpha";
42static NSString* const kExplosionBrightness = @"brightness";
43static NSString* const kExplosionColors = @"color_order";
44static NSString* const kExplosionCount = @"count";
45static NSString* const kExplosionDuration = @"duration";
46static NSString* const kExplosionGrowth = @"growth_rate";
47static NSString* const kExplosionSize = @"size";
48static NSString* const kExplosionSpread = @"spread";
49static NSString* const kExplosionTexture = @"texture";
50
51@interface OOExplosionCloudEntity (OOPrivate)
52- (id)initExplosionCloudWithEntity:(Entity*)entity size:(float)size andSettings:(NSDictionary*)settings;
53@end
54
55@implementation OOExplosionCloudEntity
56
57- (id)initExplosionCloudWithEntity:(Entity*)entity size:(float)size andSettings:(NSDictionary*)settings
58{
59 unsigned i;
60 unsigned maxCount = [UNIVERSE detailLevel] <= DETAIL_LEVEL_SHADERS ? 10 : 25;
61 HPVector pos = [entity position];
62 Vector vel = [entity velocity];
63
64 if (settings == nil) {
65 _settings = [[NSDictionary dictionary] retain];
66 } else {
67 _settings = [settings retain];
68 }
69
70 unsigned count = [_settings oo_unsignedIntForKey:kExplosionCount defaultValue:25];
71 if (count > maxCount) {
72 count = maxCount;
73 }
74
75 if (size == 0.0) {
76 size = [entity collisionRadius] * [_settings oo_floatForKey:kExplosionSize defaultValue:kExplosionDefaultSize];
77 }
78
79 _growthRate = [_settings oo_floatForKey:kExplosionGrowth defaultValue:kGrowthRateFactor] * size;
80 _alpha = [_settings oo_floatForKey:kExplosionAlpha defaultValue:kExplosionCloudAlpha];
81 _brightnessMult = [_settings oo_floatForKey:kExplosionBrightness defaultValue:kExplosionBrightnessMult];
82 if (_brightnessMult < 1.0f)
83 _brightnessMult = 1.0f;
84 _cloudDuration = [_settings oo_doubleForKey:kExplosionDuration defaultValue:kExplosionCloudDuration];
85
86 GLfloat spread = [_settings oo_floatForKey:kExplosionSpread defaultValue:1.0];
87
88 NSString* textureFile = [_settings oo_stringForKey:kExplosionTexture defaultValue:@"oolite-particle-cloud2.png"];
89
90 _texture = [[OOTexture textureWithName:textureFile
91 inFolder:@"Textures"
92 options:kOOTextureMinFilterMipMap | kOOTextureMagFilterLinear | kOOTextureAlphaMask
93 anisotropy:kOOTextureDefaultAnisotropy
94 lodBias:0.0] retain];
95 if (_texture == nil) {
96 [self release];
97 return nil;
98 }
99
100 GLfloat baseColor[4] = { 1.0, 1.0, 1.0, 1.0 };
101
102 if (magnitude2(vel) > 1000000) {
103 // slow down rapidly translating explosions
104 vel = vector_multiply_scalar(vector_normal(vel), 1000);
105 }
106
107 if ((self = [super initWithPosition:pos velocity:vel count:count minSpeed:size * 0.8f * spread maxSpeed:size * 1.2f * spread duration:_cloudDuration baseColor:baseColor])) {
108 NSString* color_order = [_settings oo_stringForKey:kExplosionColors defaultValue:@"rgb"];
109
110 for (i = 0; i < count; i++) {
111 if ([color_order isEqualToString:@"white"]) {
112 // grey
113 _particleColor[i][0] = _particleColor[i][1] = _particleColor[i][2] = randf();
114 } else {
115 float c1 = randf();
116 float c2 = randf();
117 float c3 = randf();
118 if (c2 > c1) {
119 c2 = c1;
120 }
121 if (c3 > c2) {
122 c3 = c2;
123 }
124 if ([color_order isEqualToString:@"rgb"]) {
125 _particleColor[i][0] = c1;
126 _particleColor[i][1] = c2;
127 _particleColor[i][2] = c3;
128 } else if ([color_order isEqualToString:@"rbg"]) {
129 _particleColor[i][0] = c1;
130 _particleColor[i][1] = c3;
131 _particleColor[i][2] = c2;
132 } else if ([color_order isEqualToString:@"grb"]) {
133 _particleColor[i][0] = c2;
134 _particleColor[i][1] = c1;
135 _particleColor[i][2] = c3;
136 } else if ([color_order isEqualToString:@"gbr"]) {
137 _particleColor[i][0] = c3;
138 _particleColor[i][1] = c1;
139 _particleColor[i][2] = c2;
140 } else if ([color_order isEqualToString:@"brg"]) {
141 _particleColor[i][0] = c2;
142 _particleColor[i][1] = c3;
143 _particleColor[i][2] = c1;
144 } else if ([color_order isEqualToString:@"bgr"]) {
145 _particleColor[i][0] = c3;
146 _particleColor[i][1] = c2;
147 _particleColor[i][2] = c1;
148 }
149 }
150
151 _particleColor[i][0] *= _brightnessMult;
152 _particleColor[i][1] *= _brightnessMult;
153 _particleColor[i][2] *= _brightnessMult;
154 _particleColor[i][3] = _alpha;
155 }
156 }
157 return self;
158}
159
160- (void)dealloc
161{
164 [super dealloc];
165}
166
167+ (instancetype)explosionCloudFromEntity:(Entity*)entity withSettings:(NSDictionary*)settings
168{
169 return [[[self alloc] initExplosionCloudWithEntity:entity size:0 andSettings:settings] autorelease];
170}
171
172+ (instancetype)explosionCloudFromEntity:(Entity*)entity withSize:(float)size andSettings:(NSDictionary*)settings
173{
174 return [[[self alloc] initExplosionCloudWithEntity:entity size:size andSettings:settings] autorelease];
175}
176
177- (void)update:(OOTimeDelta)delta_t
178{
179 [super update:delta_t];
180
181 // Fade out.
182 GLfloat fadeRate = _count * _brightnessMult / 25.0;
183 unsigned i, count = _count;
184 GLfloat(*particleColor)[4] = _particleColor;
185
186 float newAlpha = _alpha * (1 - (_timePassed / _cloudDuration));
187 NSString* color_order = [_settings oo_stringForKey:kExplosionColors defaultValue:@"rgb"];
188 NSUInteger primary = 0, secondary = 1, tertiary = 2;
189
190 if ([color_order isEqualToString:@"rgb"]) {
191 primary = 0;
192 secondary = 1;
193 tertiary = 2;
194 } else if ([color_order isEqualToString:@"rbg"]) {
195 primary = 0;
196 secondary = 2;
197 tertiary = 1;
198 } else if ([color_order isEqualToString:@"grb"]) {
199 primary = 1;
200 secondary = 0;
201 tertiary = 2;
202 } else if ([color_order isEqualToString:@"gbr"]) {
203 primary = 1;
204 secondary = 2;
205 tertiary = 0;
206 } else if ([color_order isEqualToString:@"brg"]) {
207 primary = 2;
208 secondary = 0;
209 tertiary = 1;
210 } else if ([color_order isEqualToString:@"bgr"]) {
211 primary = 2;
212 secondary = 1;
213 tertiary = 0;
214 }
215
216 float fdelta_t = delta_t;
217 for (i = 0; i < count; i++) {
218
219 _particleSize[i] += delta_t * _growthRate;
220
221 particleColor[i][3] = newAlpha;
222 if (![color_order isEqualToString:@"white"]) {
223
224 if (particleColor[i][tertiary] > 0.0f) // fade blue (white to yellow)
225 {
226 particleColor[i][tertiary] -= fdelta_t * 0.5f * fadeRate;
227 if (particleColor[i][tertiary] < 0.0f) {
228 particleColor[i][tertiary] = 0.0f;
229 }
230 } else if (particleColor[i][secondary] > 0.0f) // fade green (yellow to red)
231 {
232 particleColor[i][secondary] -= fdelta_t * fadeRate;
233 if (particleColor[i][secondary] < 0.0f) {
234 particleColor[i][secondary] = 0.0f;
235 }
236 } else if (particleColor[i][primary] > 0.0f) // fade red (red to black)
237 {
238 particleColor[i][primary] -= fdelta_t * 2.0f * fadeRate;
239 if (particleColor[i][primary] < 0.0f) {
240 particleColor[i][primary] = 0.0f;
241 }
242 }
243 }
244 }
245}
246
248{
249 return _texture;
250}
251
252@end
static NSString *const kExplosionSpread
static NSString *const kExplosionAlpha
static NSString *const kExplosionColors
static NSString *const kExplosionSize
static NSString *const kExplosionCount
static NSString *const kExplosionGrowth
static NSString *const kExplosionTexture
static NSString *const kExplosionDuration
static NSString *const kExplosionBrightness
#define DESTROY(x)
float maxSpeed
GLfloat baseColor[4]
unsigned count
return nil
@ DETAIL_LEVEL_SHADERS
Definition OOTypes.h:211
double OOTimeDelta
Definition OOTypes.h:193
OOINLINE OOScalar magnitude2(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:237
OOINLINE Vector vector_multiply_scalar(Vector v, OOScalar s) INLINE_CONST_FUNC
Definition OOVector.h:180
OOINLINE Vector vector_normal(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:270
GLfloat collisionRadius()
Definition Entity.m:830
HPVector position
Definition Entity.m:576
Vector velocity
Definition Entity.m:708
void dealloc()
Definition Entity.m:98
void update:(OOTimeDelta delta_t)
OOTimeDelta _timePassed
GLfloat _particleSize[kFragmentBurstMaxParticles]
GLfloat _particleColor[kFragmentBurstMaxParticles][4]
id textureWithName:inFolder:options:anisotropy:lodBias:(NSString *name,[inFolder] NSString *directory,[options] OOTextureFlags options,[anisotropy] GLfloat anisotropy,[lodBias] GLfloat lodBias)
Definition OOTexture.m:132
voidpf void uLong size
Definition ioapi.h:126
float randf(void)