Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOFlashEffectEntity.m
Go to the documentation of this file.
1/*
2
3OOFlashEffectEntity.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
27#import "Universe.h"
28#import "PlayerEntity.h"
29#import "OOColor.h"
30#import "OOTexture.h"
32
33
34#define kLaserFlashDuration 0.3f
35#define kExplosionFlashDuration 0.4f
36#define kGrowthRateFactor 150.0f // if average flashSize is 80 then this is 12000
37#define kMinExplosionGrowth 600.0f
38#define kLaserFlashInitialSize 1.0f
39#define kExplosionFlashAlpha 0.5f
40
42
43
44@interface OOFlashEffectEntity (Private)
45
46// Designated initializer.
47- (id) initWithPosition:(HPVector)pos size:(float)size color:(OOColor *)color duration:(float)duration;
48
49+ (void) resetGraphicsState;
50
51- (void) performUpdate:(OOTimeDelta)delta_t;
52
53@end
54
55
56@implementation OOFlashEffectEntity
57
58- (id) initExplosionFlashWithPosition:(HPVector)pos velocity:(Vector)vel size:(float)size
59{
60 if ((self = [self initWithPosition:pos size:size color:[OOColor whiteColor] duration:kExplosionFlashDuration]))
61 {
62 _growthRate = fmax(_growthRate, kMinExplosionGrowth);
63 _alpha = kExplosionFlashAlpha;
64 [self setVelocity:vel];
65 }
66 return self;
67}
68
69
70- (id) initLaserFlashWithPosition:(HPVector)pos velocity:(Vector)vel color:(OOColor *)color
71{
72 if ((self = [self initWithPosition:pos size:kLaserFlashInitialSize color:color duration:kLaserFlashDuration]))
73 {
74 [self setVelocity:vel];
75 _alpha = 1.0f;
76 }
77 return self;
78}
79
80
81+ (instancetype) explosionFlashFromEntity:(Entity *)entity
82{
83 return [[[self alloc] initExplosionFlashWithPosition:[entity position] velocity:[entity velocity] size:[entity collisionRadius]] autorelease];
84}
85
86
87+ (instancetype) laserFlashWithPosition:(HPVector)pos velocity:(Vector)vel color:(OOColor *)color
88{
89 return [[[self alloc] initLaserFlashWithPosition:pos velocity:vel color:color] autorelease];
90}
91
92
93- (id) initWithPosition:(HPVector)pos size:(float)size color:(OOColor *)color duration:(float)duration
94{
95 if ((self = [super initWithDiameter:size]))
96 {
97 [self setPosition:pos];
98 _duration = duration;
99 _growthRate = kGrowthRateFactor * size;
100 [self setColor:color alpha:1.0f];
101 assert([self collisionRadius] == 0 && [self energy] == 0 && magnitude([self velocity]) == 0);
102 }
103 return self;
104}
105
106
107- (void) update:(OOTimeDelta)delta_t
108{
109 [super update:delta_t];
110
111 float tf = _duration * 0.667f;
112 float tf1 = _duration - tf;
113
114 // Scale up.
115 _diameter += delta_t * _growthRate;
116
117 // Fade in and out.
118 OOTimeDelta lifeTime = [self timeElapsedSinceSpawn];
119 _colorComponents[3] = _alpha * ((lifeTime < tf) ? (lifeTime / tf) : (_duration - lifeTime) / tf1);
120
121 // Disappear as necessary.
122 if (lifeTime > _duration) [UNIVERSE removeEntity:self];
123}
124
125
126- (OOTexture *) texture
127{
128 if (sFlashTexture == nil) [OOFlashEffectEntity setUpTexture];
129 return sFlashTexture;
130}
131
132
133+ (void) setUpTexture
134{
135 if (sFlashTexture == nil)
136 {
137 sFlashTexture = [[OOTexture textureWithName:@"oolite-particle-flash.png"
138 inFolder:@"Textures"
139 options:kOOTextureMinFilterMipMap | kOOTextureMagFilterLinear | kOOTextureAlphaMask
140 anisotropy:kOOTextureDefaultAnisotropy
141 lodBias:0.0] retain];
142 [[OOGraphicsResetManager sharedManager] registerClient:(id<OOGraphicsResetClient>)[OOFlashEffectEntity class]];
143 }
144}
145
146
147+ (void) resetGraphicsState
148{
149 [sFlashTexture release];
151}
152
153@end
#define kGrowthRateFactor
#define kExplosionFlashDuration
#define kLaserFlashInitialSize
static OOTexture * sFlashTexture
#define kMinExplosionGrowth
#define kLaserFlashDuration
#define kExplosionFlashAlpha
return nil
double OOTimeDelta
Definition OOTypes.h:224
#define UNIVERSE
Definition Universe.h:833
void setVelocity:(Vector vel)
Definition Entity.m:757
GLfloat collisionRadius()
Definition Entity.m:905
HPVector position
Definition Entity.h:112
Vector velocity
Definition Entity.h:140
void registerClient:(id< OOGraphicsResetClient > client)
OOGraphicsResetManager * sharedManager()
oneway void release()
Definition OOTexture.m:614
id textureWithName:inFolder:options:anisotropy:lodBias:(NSString *name,[inFolder] NSString *directory,[options] OOTextureFlags options,[anisotropy] GLfloat anisotropy,[lodBias] GLfloat lodBias)
Definition OOTexture.m:134
voidpf void uLong size
Definition ioapi.h:134