Oolite
Loading...
Searching...
No Matches
OOLaserShotEntity.m
Go to the documentation of this file.
1/*
2
3OOLaserShotEntity.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 "OOLaserShotEntity.h"
27#import "OOMacroOpenGL.h"
28#import "ShipEntity.h"
29#import "Universe.h"
30
32#import "OOTexture.h"
33
34#import "MyOpenGLView.h"
35
36#define kLaserDuration (0.09) // seconds
37
38// Default colour
39#define kLaserRed (1.0f)
40#define kLaserGreen (0.0f)
41#define kLaserBlue (0.0f)
42
43// Brightness - set to 1.0 for legacy laser appearance
44#define kLaserBrightness (5.0f)
45
46// Constant alpha
47#define kLaserAlpha (0.45f / kLaserBrightness)
48
49#define kLaserCoreWidth (0.4f)
50#define kLaserFlareWidth (1.8f)
51#define kLaserHalfWidth (3.6f)
52
55
56@implementation OOLaserShotEntity
57
58- (instancetype)initLaserFromShip:(ShipEntity*)srcEntity direction:(OOWeaponFacing)direction offset:(Vector)offset
59{
60 if (!(self = [super init]))
61 return nil;
62
64 Vector middle = OOBoundingBoxCenter([srcEntity boundingBox]);
65
66 NSCParameterAssert([srcEntity isShip] && [ship isShip]);
67
68 [self setStatus:STATUS_EFFECT];
69
70 if (ship == srcEntity) {
71 // main laser offset
72 [self setPosition:HPvector_add([ship position], vectorToHPVector(OOVectorMultiplyMatrix(offset, [ship drawRotationMatrix])))];
73 } else {
74 // subentity laser
75 [self setPosition:[srcEntity absolutePositionForSubentityOffset:vectorToHPVector(middle)]];
76 }
77
82
83 switch (direction) {
86 break;
87
90 break;
91
93 quaternion_rotate_about_axis(&q, q_up, M_PI / 2.0);
94 break;
95
97 quaternion_rotate_about_axis(&q, q_up, -M_PI / 2.0);
98 break;
99 }
100
101 [self setOrientation:quaternion_multiply(q, q0)];
102 [self setOwner:ship];
105
106 _color[0] = kLaserRed / 3.0;
107 _color[1] = kLaserGreen / 3.0;
108 _color[2] = kLaserBlue / 3.0;
109 _color[3] = kLaserAlpha;
110
111 _offset = (ship == srcEntity) ? offset : middle;
112 _relOrientation = q;
113
114 return self;
115}
116
117+ (instancetype)laserFromShip:(ShipEntity*)ship direction:(OOWeaponFacing)direction offset:(Vector)offset
118{
119 return [[[self alloc] initLaserFromShip:ship direction:direction offset:offset] autorelease];
120}
121
122- (void)dealloc
123{
124 [self setColor:nil];
125
126 [super dealloc];
127}
128
130{
131 return [NSString stringWithFormat:@"ttl: %.3fs - %@ orientation %@", _lifetime, [super descriptionComponents], QuaternionDescription([self orientation])];
132}
133
134- (void)setColor:(OOColor*)color
135{
139 // Ignore alpha; _color[3] is constant.
140}
141
142- (void)setRange:(GLfloat)range
143{
144 _range = range;
145 [self setCollisionRadius:range];
146}
147
148- (void)update:(OOTimeDelta)delta_t
149{
150 [super update:delta_t];
151 _lifetime -= delta_t;
152 ShipEntity* ship = [self owner];
153
154 if ([ship isPlayer]) {
155 /*
156 Reposition this shot accurately. This overrides integration over
157 velocity in -[Entity update:], which is considered sufficient for
158 NPC ships.
159 */
160 [self setPosition:HPvector_add([ship position], vectorToHPVector(OOVectorMultiplyMatrix(_offset, [ship drawRotationMatrix])))];
161 [self setOrientation:quaternion_multiply(_relOrientation, [ship normalOrientation])];
162 }
163
164 if (_lifetime < 0) {
165 [UNIVERSE removeEntity:self];
166 }
167}
168
169static const GLfloat kLaserVertices[] = {
170 1.0f,
171 0.0f,
172 0.0f,
173 1.0f,
174 0.0f,
175 1.0f,
176 -1.0f,
177 0.0f,
178 1.0f,
179 -1.0f,
180 0.0f,
181 0.0f,
182
183 0.0f,
184 1.0f,
185 0.0f,
186 0.0f,
187 1.0f,
188 1.0f,
189 0.0f,
190 -1.0f,
191 1.0f,
192 0.0f,
193 -1.0f,
194 0.0f,
195};
196
197- (void)drawImmediate:(bool)immediate translucent:(bool)translucent
198{
199 if (!translucent || [UNIVERSE breakPatternHide])
200 return;
201
204
205 /* FIXME: spread damage across the lifetime of the shot,
206 hurting whatever is hit in a given frame.
207 -- Ahruman 2011-01-31
208 */
209 OOGL(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
210 OOGL(glEnable(GL_TEXTURE_2D));
212
214 [[self texture1] apply];
215 GLfloat s = sinf([UNIVERSE getTime]);
216 GLfloat phase = s * (_range / 200.0f);
217 GLfloat phase2 = (1.0f + s) * (_range / 200.0f);
218 GLfloat phase3 = -s * (_range / 500.0f);
219 GLfloat phase4 = -(1.0f + s) * (_range / 500.0f);
220
221 GLfloat laserTexCoords[] = {
222 0.0f, phase, 0.0f, phase2, 1.0f, phase2, 1.0f, phase,
223
224 0.0f, phase, 0.0f, phase2, 1.0f, phase2, 1.0f, phase
225 };
226 GLfloat laserTexCoords2[] = {
227 0.0f, phase3, 0.0f, phase4, 1.0f, phase4, 1.0f, phase3,
228
229 0.0f, phase3, 0.0f, phase4, 1.0f, phase4, 1.0f, phase3
230 };
231
232 OOGL(glColor4fv(_color));
233 glVertexPointer(3, GL_FLOAT, 0, kLaserVertices);
234 glTexCoordPointer(2, GL_FLOAT, 0, laserTexCoords2);
235 glDrawArrays(GL_QUADS, 0, 8);
236
239 glDrawArrays(GL_QUADS, 0, 8);
240
241 [[self texture2] apply];
243 OOGL(glColor4f(_color[0], _color[1], _color[2], 0.9));
244 glTexCoordPointer(2, GL_FLOAT, 0, laserTexCoords);
245 glDrawArrays(GL_QUADS, 0, 8);
246
248 OOGL(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
249 OOGL(glDisable(GL_TEXTURE_2D));
250
252 OOCheckOpenGLErrors(@"OOLaserShotEntity after drawing %@", self);
253}
254
255- (BOOL)isEffect
256{
257 return YES;
258}
259
261{
262 return NO;
263}
264
269
274
276{
277 if (sShotTexture == nil) {
278 sShotTexture = [[OOTexture textureWithName:@"oolite-laser-blur.png"
279 inFolder:@"Textures"
280 options:kOOTextureMinFilterMipMap | kOOTextureMagFilterLinear | kOOTextureAlphaMask | kOOTextureRepeatT
281 anisotropy:kOOTextureDefaultAnisotropy / 2.0
282 lodBias:0.0] retain];
283 [[OOGraphicsResetManager sharedManager] registerClient:(id<OOGraphicsResetClient>)[OOLaserShotEntity class]];
284
285 sShotTexture2 = [[OOTexture textureWithName:@"oolite-laser-blur2.png"
286 inFolder:@"Textures"
287 options:kOOTextureMinFilterMipMap | kOOTextureMagFilterLinear | kOOTextureAlphaMask | kOOTextureRepeatT
288 anisotropy:kOOTextureDefaultAnisotropy / 2.0
289 lodBias:0.0] retain];
290 }
291}
292
294{
295 if (sShotTexture2 == nil)
296 [self setUpTexture];
297 return sShotTexture2;
298}
299
301{
302 if (sShotTexture == nil)
303 [self setUpTexture];
304 return sShotTexture;
305}
306
308{
309 [sShotTexture release];
311 [sShotTexture2 release];
313}
314
315@end
OOINLINE Vector OOBoundingBoxCenter(BoundingBox bb) INLINE_CONST_FUNC
#define kLaserFlareWidth
#define kLaserRed
static OOTexture * sShotTexture2
static const GLfloat kLaserVertices[]
#define kLaserHalfWidth
static OOTexture * sShotTexture
#define kLaserCoreWidth
#define kLaserBrightness
#define kLaserGreen
#define kLaserBlue
#define kLaserDuration
#define kLaserAlpha
#define OO_ENTER_OPENGL()
#define M_PI
Definition OOMaths.h:54
void OOGLScaleModelView(Vector scale)
void OOGLPushModelView(void)
OOMatrix OOGLPopModelView(void)
@ OPENGL_STATE_ADDITIVE_BLENDING
Definition OOOpenGL.h:123
#define OOVerifyOpenGLState()
Definition OOOpenGL.h:133
BOOL OOCheckOpenGLErrors(NSString *format,...)
Definition OOOpenGL.m:38
#define OOSetOpenGLState(STATE)
Definition OOOpenGL.h:132
#define OOGL(statement)
Definition OOOpenGL.h:262
return nil
Vector vector_forward_from_quaternion(Quaternion quat) CONST_FUNC
void quaternion_rotate_about_axis(Quaternion *quat, Vector axis, OOScalar angle) NONNULL_FUNC
const Quaternion kIdentityQuaternion
Vector vector_up_from_quaternion(Quaternion quat) CONST_FUNC
double OOTimeDelta
Definition OOTypes.h:193
OOWeaponFacing
Definition OOTypes.h:195
@ WEAPON_FACING_FORWARD
Definition OOTypes.h:196
@ WEAPON_FACING_NONE
Definition OOTypes.h:201
@ WEAPON_FACING_AFT
Definition OOTypes.h:197
@ WEAPON_FACING_PORT
Definition OOTypes.h:198
@ WEAPON_FACING_STARBOARD
Definition OOTypes.h:199
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
OOINLINE Vector vector_multiply_scalar(Vector v, OOScalar s) INLINE_CONST_FUNC
Definition OOVector.h:180
#define UNIVERSE
Definition Universe.h:827
Quaternion orientation
Definition Entity.m:682
void setOrientation:(Quaternion quat)
Definition Entity.m:676
void update:(OOTimeDelta delta_t)
Definition Entity.m:850
unsigned isShip
Definition Entity.m:125
void setCollisionRadius:(GLfloat amount)
Definition Entity.m:840
void setOwner:(Entity *ent)
Definition Entity.m:542
NSString * descriptionComponents()
Definition Entity.m:115
ShipEntity * rootShipEntity()
Definition Entity.m:566
BoundingBox boundingBox
Definition Entity.m:666
unsigned isPlayer
Definition Entity.m:145
HPVector position
Definition Entity.m:576
HPVector absolutePositionForSubentityOffset:(HPVector offset)
Definition Entity.m:632
Quaternion normalOrientation()
Definition Entity.m:687
Vector velocity
Definition Entity.m:708
id init()
Definition Entity.m:64
void setStatus:(OOEntityStatus stat)
Definition Entity.m:728
id owner()
Definition Entity.m:548
void dealloc()
Definition Entity.m:98
void setPosition:(HPVector posn)
Definition Entity.m:608
OOMatrix drawRotationMatrix()
Definition Entity.m:808
float blueComponent()
Definition OOColor.m:339
float redComponent()
Definition OOColor.m:329
float greenComponent()
Definition OOColor.m:334
void registerClient:(id< OOGraphicsResetClient > client)
OOGraphicsResetManager * sharedManager()
void setRange:(GLfloat range)
NSString * descriptionComponents()
void setColor:(OOColor *color)
void apply()
Definition OOTexture.m:258
id textureWithName:inFolder:options:anisotropy:lodBias:(NSString *name,[inFolder] NSString *directory,[options] OOTextureFlags options,[anisotropy] GLfloat anisotropy,[lodBias] GLfloat lodBias)
Definition OOTexture.m:132
GLfloat weaponRange
voidpf uLong offset
Definition ioapi.h:132