Oolite
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 "OOColor.h"
29#import "OOTexture.h"
30#import "PlayerEntity.h"
31#import "Universe.h"
32
33#define kLaserFlashDuration 0.3f
34#define kExplosionFlashDuration 0.4f
35#define kGrowthRateFactor 150.0f // if average flashSize is 80 then this is 12000
36#define kMinExplosionGrowth 600.0f
37#define kLaserFlashInitialSize 1.0f
38#define kExplosionFlashAlpha 0.5f
39
41
42@interface OOFlashEffectEntity (Private)
43
44// Designated initializer.
45- (id)initWithPosition:(HPVector)pos size:(float)size color:(OOColor*)color duration:(float)duration;
46
48
49- (void)performUpdate:(OOTimeDelta)delta_t;
50
51@end
52
53@implementation OOFlashEffectEntity
54
55- (id)initExplosionFlashWithPosition:(HPVector)pos velocity:(Vector)vel size:(float)size
56{
57 if ((self = [self initWithPosition:pos size:size color:[OOColor whiteColor] duration:kExplosionFlashDuration])) {
60 [self setVelocity:vel];
61 }
62 return self;
63}
64
65- (id)initLaserFlashWithPosition:(HPVector)pos velocity:(Vector)vel color:(OOColor*)color
66{
67 if ((self = [self initWithPosition:pos size:kLaserFlashInitialSize color:color duration:kLaserFlashDuration])) {
68 [self setVelocity:vel];
69 _alpha = 1.0f;
70 }
71 return self;
72}
73
74+ (instancetype)explosionFlashFromEntity:(Entity*)entity
75{
76 return [[[self alloc] initExplosionFlashWithPosition:[entity position] velocity:[entity velocity] size:[entity collisionRadius]] autorelease];
77}
78
79+ (instancetype)laserFlashWithPosition:(HPVector)pos velocity:(Vector)vel color:(OOColor*)color
80{
81 return [[[self alloc] initLaserFlashWithPosition:pos velocity:vel color:color] autorelease];
82}
83
84- (id)initWithPosition:(HPVector)pos size:(float)size color:(OOColor*)color duration:(float)duration
85{
86 if ((self = [super initWithDiameter:size])) {
87 [self setPosition:pos];
88 _duration = duration;
89 _growthRate = kGrowthRateFactor * size;
90 [self setColor:color alpha:1.0f];
91 assert([self collisionRadius] == 0 && [self energy] == 0 && magnitude([self velocity]) == 0);
92 }
93 return self;
94}
95
96- (void)update:(OOTimeDelta)delta_t
97{
98 [super update:delta_t];
99
100 float tf = _duration * 0.667f;
101 float tf1 = _duration - tf;
102
103 // Scale up.
104 _diameter += delta_t * _growthRate;
105
106 // Fade in and out.
107 OOTimeDelta lifeTime = [self timeElapsedSinceSpawn];
108 _colorComponents[3] = _alpha * ((lifeTime < tf) ? (lifeTime / tf) : (_duration - lifeTime) / tf1);
109
110 // Disappear as necessary.
111 if (lifeTime > _duration)
112 [UNIVERSE removeEntity:self];
113}
114
121
123{
124 if (sFlashTexture == nil) {
125 sFlashTexture = [[OOTexture textureWithName:@"oolite-particle-flash.png"
126 inFolder:@"Textures"
127 options:kOOTextureMinFilterMipMap | kOOTextureMagFilterLinear | kOOTextureAlphaMask
128 anisotropy:kOOTextureDefaultAnisotropy
129 lodBias:0.0] retain];
130 [[OOGraphicsResetManager sharedManager] registerClient:(id<OOGraphicsResetClient>)[OOFlashEffectEntity class]];
131 }
132}
133
135{
136 [sFlashTexture release];
138}
139
140@end
#define kGrowthRateFactor
#define kExplosionFlashDuration
#define kLaserFlashInitialSize
static OOTexture * sFlashTexture
#define kMinExplosionGrowth
#define kLaserFlashDuration
#define kExplosionFlashAlpha
return nil
double OOTimeDelta
Definition OOTypes.h:193
OOINLINE OOScalar magnitude(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:242
GLfloat timeElapsedSinceSpawn()
Definition Entity.m:985
void setVelocity:(Vector vel)
Definition Entity.m:703
void update:(OOTimeDelta delta_t)
Definition Entity.m:850
GLfloat collisionRadius()
Definition Entity.m:830
HPVector position
Definition Entity.m:576
Vector velocity
Definition Entity.m:708
void setPosition:(HPVector posn)
Definition Entity.m:608
void registerClient:(id< OOGraphicsResetClient > client)
OOGraphicsResetManager * sharedManager()
void setColor:alpha:(OOColor *color,[alpha] GLfloat alpha)
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