Oolite
Loading...
Searching...
No Matches
OOFlasherEntity.m
Go to the documentation of this file.
1/*
2
3OOFlasherEntity.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
26#import "OOFlasherEntity.h"
29#import "OOColor.h"
30#import "PlayerEntity.h"
31#import "Universe.h"
32
33@interface OOFlasherEntity (Internal)
34
35- (void)setUpColors:(NSArray*)colorSpecifiers;
37
38@end
39
40@implementation OOFlasherEntity
41
42+ (instancetype)flasherWithDictionary:(NSDictionary*)dictionary
43{
44 return [[[OOFlasherEntity alloc] initWithDictionary:dictionary] autorelease];
45}
46
47- (id)initWithDictionary:(NSDictionary*)dictionary
48{
49 float size = [dictionary oo_floatForKey:@"size" defaultValue:1.0f];
50
51 if ((self = [super initWithDiameter:size])) {
52 _frequency = [dictionary oo_floatForKey:@"frequency" defaultValue:1.0f] * 2.0f;
53 _phase = [dictionary oo_floatForKey:@"phase" defaultValue:0.0f];
54 _brightfraction = [dictionary oo_floatForKey:@"bright_fraction" defaultValue:0.5f];
55
56 [self setUpColors:[dictionary oo_arrayForKey:@"colors"]];
58
59 [self setActive:[dictionary oo_boolForKey:@"initially_on" defaultValue:YES]];
60 }
61 return self;
62}
63
64- (void)dealloc
65{
66 [_colors release];
67
68 [super dealloc];
69}
70
71- (void)setUpColors:(NSArray*)colorSpecifiers
72{
73 NSMutableArray* colors = [NSMutableArray arrayWithCapacity:[colorSpecifiers count]];
74 id specifier = nil;
75 foreach (specifier, colorSpecifiers) {
76 [colors addObject:[OOColor colorWithDescription:specifier saturationFactor:0.75f]];
77 }
78
79 _colors = [colors copy];
80}
81
83{
84 [self setColor:[_colors objectAtIndex:_activeColor] alpha:_colorComponents[3]];
85}
86
87- (BOOL)isActive
88{
89 return _active;
90}
91
92- (void)setActive:(BOOL)active
93{
94 _active = !!active;
95}
96
98{
99 return [OOColor colorWithRed:_colorComponents[0]
100 green:_colorComponents[1]
101 blue:_colorComponents[2]
102 alpha:_colorComponents[3]];
103}
104
105- (float)frequency
106{
107 return _frequency;
108}
109
110- (void)setFrequency:(float)frequency
111{
113}
114
115- (float)phase
116{
117 return _phase;
118}
119
120- (void)setPhase:(float)phase
121{
122 _phase = phase;
123}
124
125- (float)fraction
126{
127 return _brightfraction;
128}
129
130- (void)setFraction:(float)fraction
131{
133}
134
135- (void)update:(OOTimeDelta)delta_t
136{
137 [super update:delta_t];
138
139 _time += delta_t;
140
141 if (_frequency != 0) {
142 float wave = sinf(_frequency * M_PI * (_time + _phase));
143 NSUInteger count = [_colors count];
144 if (count > 1 && wave < 0) {
145 if (!_justSwitched && wave > _wave) // don't test for wave >= _wave - could give wrong results with very low frequencies
146 {
147 _justSwitched = YES;
148 ++_activeColor;
149 _activeColor %= count; //_activeColor = ++_activeColor % count; is potentially undefined operation
150 [self setColor:[_colors objectAtIndex:_activeColor]];
151 }
152 } else if (_justSwitched) {
153 _justSwitched = NO;
154 }
155
156 float threshold = cosf(_brightfraction * M_PI);
157
158 float brightness = _brightfraction;
159 if (wave > threshold) {
160 brightness = _brightfraction + (((1 - _brightfraction) / (1 - threshold)) * (wave - threshold));
161 } else if (wave < threshold) {
162 brightness = _brightfraction + ((_brightfraction / (threshold + 1)) * (wave - threshold));
163 }
164
165 _colorComponents[3] = brightness;
166
167 _wave = wave;
168 } else {
169 _colorComponents[3] = 1.0;
170 }
171}
172
173- (void)drawImmediate:(bool)immediate translucent:(bool)translucent
174{
175 if (_active) {
176 [super drawImmediate:immediate translucent:translucent];
177 }
178}
179
180- (void)drawSubEntityImmediate:(bool)immediate translucent:(bool)translucent
181{
182 if (_active) {
183 [super drawSubEntityImmediate:immediate translucent:translucent];
184 }
185}
186
188{
189 return YES;
190}
191
193{
194 return [self diameter] / 2.0;
195}
196
197- (void)rescaleBy:(GLfloat)factor
198{
199 [self setDiameter:[self diameter] * factor];
200}
201
202- (void)rescaleBy:(GLfloat)factor writeToCache:(BOOL)writeToCache
203{
204 /* Do nothing; this is only needed because of OOEntityWithDrawable
205 implementation requirements */
206}
207
208@end
209
210@implementation Entity (OOFlasherEntityExtensions)
211
213{
214 return NO;
215}
216
217@end
#define M_PI
Definition OOMaths.h:54
unsigned count
return nil
double OOTimeDelta
Definition OOTypes.h:193
void update:(OOTimeDelta delta_t)
Definition Entity.m:850
void dealloc()
Definition Entity.m:98
OOColor * colorWithRed:green:blue:alpha:(float red,[green] float green,[blue] float blue,[alpha] float alpha)
Definition OOColor.m:107
OOColor * colorWithDescription:saturationFactor:(id description,[saturationFactor] float factor)
Definition OOColor.m:140
NSUInteger _activeColor
void setActive:(BOOL active)
void getCurrentColorComponents()
void setUpColors:(NSArray *colorSpecifiers)
OOTimeDelta _time
void setColor:alpha:(OOColor *color,[alpha] GLfloat alpha)
void setColor:(OOColor *color)
void drawSubEntityImmediate:translucent:(bool immediate,[translucent] bool translucent)
void setDiameter:(float diameter)
void drawImmediate:translucent:(bool immediate, [translucent] bool translucent)
voidpf void uLong size
Definition ioapi.h:126