Line data Source code
1 0 : /* 2 : 3 : OOFlashEffectEntity.m 4 : 5 : 6 : Oolite 7 : Copyright (C) 2004-2013 Giles C Williams and contributors 8 : 9 : This program is free software; you can redistribute it and/or 10 : modify it under the terms of the GNU General Public License 11 : as published by the Free Software Foundation; either version 2 12 : of the License, or (at your option) any later version. 13 : 14 : This program is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU General Public License for more details. 18 : 19 : You should have received a copy of the GNU General Public License 20 : along with this program; if not, write to the Free Software 21 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 22 : MA 02110-1301, USA. 23 : 24 : */ 25 : 26 : #import "OOFlashEffectEntity.h" 27 : #import "Universe.h" 28 : #import "PlayerEntity.h" 29 : #import "OOColor.h" 30 : #import "OOTexture.h" 31 : #import "OOGraphicsResetManager.h" 32 : 33 : 34 0 : #define kLaserFlashDuration 0.3f 35 0 : #define kExplosionFlashDuration 0.4f 36 0 : #define kGrowthRateFactor 150.0f // if average flashSize is 80 then this is 12000 37 0 : #define kMinExplosionGrowth 600.0f 38 0 : #define kLaserFlashInitialSize 1.0f 39 0 : #define kExplosionFlashAlpha 0.5f 40 : 41 0 : static OOTexture *sFlashTexture = nil; 42 : 43 : 44 : @interface OOFlashEffectEntity (Private) 45 : 46 : // Designated initializer. 47 0 : - (id) initWithPosition:(HPVector)pos size:(float)size color:(OOColor *)color duration:(float)duration; 48 : 49 0 : + (void) resetGraphicsState; 50 : 51 0 : - (void) performUpdate:(OOTimeDelta)delta_t; 52 : 53 : @end 54 : 55 : 56 : @implementation OOFlashEffectEntity 57 : 58 0 : - (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 0 : - (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 0 : - (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 0 : - (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 0 : - (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 0 : + (void) resetGraphicsState 148 : { 149 : [sFlashTexture release]; 150 : sFlashTexture = nil; 151 : } 152 : 153 : @end