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