-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lightning.es
275 lines (238 loc) · 7.45 KB
/
Lightning.es
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
607
%{
#include "StdH.h"
#include "Entities/BackgroundViewer.h"
#include "Entities/WorldSettingsController.h"
#include "Entities/Light.h"
%}
%{
struct ThunderInfo
{
INDEX ti_iSound;
FLOAT ti_fThunderStrikeDelay;
};
struct ThunderInfo _atiThunderSounds[3] =
{
{ SOUND_THUNDER1, 0.6f},
{ SOUND_THUNDER2, 0.0f},
{ SOUND_THUNDER3, 0.0f},
};
%}
class CLightning: CMovableModelEntity {
name "Lightning";
thumbnail "Thumbnails\\Lightning.tbn";
features "IsTargetable", "HasName";
properties:
1 CEntityPointer m_penTarget "Target" 'T' COLOR(C_BLUE|0xFF), // ptr to lightninig target
2 CEntityPointer m_penwsc, // ptr to world settings controller
3 CTString m_strName "Name" 'N' = "Lightning", // class name
4 FLOAT m_tmLightningStart = -1.0f, // lightning start time
5 CSoundObject m_soThunder, // sound channel
6 BOOL m_bBackground "Background" 'B' =FALSE,
7 CEntityPointer m_penLight "Light" 'L' COLOR(C_CYAN|0xFF), // ptr to light
8 ANIMATION m_iLightAnim "Light Animation" 'A' = 0,
9 INDEX m_iSoundPlaying = 0,
10 FLOAT m_fLightningPower "Lightning power" 'P' = 1.0f, // lightning's ligting power
11 FLOAT m_fSoundDelay "Sound delay" 'D' = 0.0f, // thunder's delay
components:
1 model MODEL_TELEPORT "Models\\Editor\\Teleport.mdl",
2 texture TEXTURE_TELEPORT "Models\\Editor\\BoundingBox.tex",
3 sound SOUND_THUNDER1 "Sounds\\Environment\\Thunders\\Thunder1.wav",
4 sound SOUND_THUNDER2 "Sounds\\Environment\\Thunders\\Thunder2.wav",
5 sound SOUND_THUNDER3 "Sounds\\Environment\\Thunders\\Thunder3.wav",
functions:
void Precache(void)
{
CMovableModelEntity::Precache();
PrecacheSound(SOUND_THUNDER1);
PrecacheSound(SOUND_THUNDER2);
PrecacheSound(SOUND_THUNDER3);
}
/* Get anim data for given animation property - return NULL for none. */
CAnimData *GetAnimData(SLONG slPropertyOffset)
{
if (m_penLight==NULL) {
return NULL;
}
// if light entity
if (IsOfClass(m_penLight, "Light"))
{
CLight *penLight = (CLight*)&*m_penLight;
if (slPropertyOffset==offsetof(CLightning, m_iLightAnim))
{
return penLight->m_aoLightAnimation.GetData();
}
}
else
{
WarningMessage("Target '%s' is not of light class!", m_penLight->GetName());
}
return NULL;
};
void RenderParticles(void)
{
if( m_penTarget==NULL || m_tmLightningStart == -1) {return;};
TIME tmNow = _pTimer->GetLerpedCurrentTick();
// if lightning is traveling
if(
((tmNow-m_tmLightningStart) > 0.0f) &&
((tmNow-m_tmLightningStart) < 1.5f) )
{
// render lightning particles
FLOAT3D vSrc = GetPlacement().pl_PositionVector;
FLOAT3D vDst = m_penTarget->GetPlacement().pl_PositionVector;
Particles_Lightning( vSrc, vDst, m_tmLightningStart);
}
}
procedures:
LightningStike()
{
// choose random sound
m_iSoundPlaying = 1+IRnd()%( ARRAYCOUNT(_atiThunderSounds)-1);
if( m_fSoundDelay != 0)
{
m_iSoundPlaying=0;
}
m_soThunder.SetVolume(1.5f*m_fLightningPower, 1.5f*m_fLightningPower);
m_soThunder.SetPitch(Lerp(0.9f, 1.2f, FRnd()));
if( m_fSoundDelay == 0.0f)
{
// play thunder !
PlaySound(m_soThunder, _atiThunderSounds[ m_iSoundPlaying].ti_iSound, 0);
}
// wait for sound to progress to lightning strike
if (_atiThunderSounds[ m_iSoundPlaying].ti_fThunderStrikeDelay>0.0f) {
autowait( _atiThunderSounds[ m_iSoundPlaying].ti_fThunderStrikeDelay);
}
// remember current time as lightning start time
TIME tmNow = _pTimer->CurrentTick();
m_tmLightningStart = tmNow;
// also in world settings controller
((CWorldSettingsController *)&*m_penwsc)->m_tmLightningStart = tmNow;
// set power of lightning
((CWorldSettingsController *)&*m_penwsc)->m_fLightningPower = m_fLightningPower;
// trigger light animation
if( m_penLight != NULL)
{
EChangeAnim eChange;
eChange.iLightAnim = m_iLightAnim;
eChange.bLightLoop = FALSE;
m_penLight->SendEvent(eChange);
}
if( m_fSoundDelay != 0.0f)
{
// wait given delay time
autowait( m_fSoundDelay);
// play thunder !
PlaySound(m_soThunder, _atiThunderSounds[ m_iSoundPlaying].ti_iSound, 0);
}
// wait until end of sound
wait( GetSoundLength(_atiThunderSounds[ m_iSoundPlaying].ti_iSound)-
_atiThunderSounds[ m_iSoundPlaying].ti_fThunderStrikeDelay)
{
on (ETimer) :
{
stop;
}
otherwise() :
{
resume;
};
}
return EBegin();
}
Main(EVoid)
{
// set appearance
InitAsEditorModel();
SetPhysicsFlags(EPF_MODEL_IMMATERIAL);
SetCollisionFlags(ECF_IMMATERIAL);
// set appearance
SetModel(MODEL_TELEPORT);
SetModelMainTexture(TEXTURE_TELEPORT);
// see if it is lightning on background
if (m_bBackground)
{
SetFlags(GetFlags()|ENF_BACKGROUND);
}
else
{
SetFlags(GetFlags()&~ENF_BACKGROUND);
}
// obtain bcg viewer entity
CBackgroundViewer *penBcgViewer = (CBackgroundViewer *) GetWorld()->GetBackgroundViewer();
if( penBcgViewer == NULL)
{
// don't do anything
return;
}
// obtain world settings controller
m_penwsc = penBcgViewer->m_penWorldSettingsController;
if( m_penwsc == NULL)
{
// don't do anything
return;
}
// must be world settings controller entity
if (!IsOfClass(m_penwsc, "WorldSettingsController"))
{
// don't do anything
return;
}
// lightning target must be marker
if( (m_penTarget == NULL) || (!IsOfClass(m_penTarget, "Marker")) )
{
if( m_penTarget != NULL)
{
WarningMessage("Target '%s' is not of Marker class!", m_penTarget->GetName());
}
// don't do anything
return;
}
// stretch model
FLOAT3D vDirection =
(m_penTarget->GetPlacement().pl_PositionVector-
GetPlacement().pl_PositionVector);
FLOAT3D vStretch = vDirection;
vStretch(1) = 1.0f;
vStretch(2) = 1.0f;
vStretch(3) = -vDirection.Length();
// set entity orientation
CPlacement3D pl = GetPlacement();
DirectionVectorToAngles(vDirection.Normalize(), pl.pl_OrientationAngle);
SetPlacement(pl);
GetModelObject()->StretchModel(vStretch);
ModelChangeNotify();
// correct power factor to fall under 0-1 boundaries
m_fLightningPower = Clamp( m_fLightningPower, 0.0f, 1.0f);
// spawn in world editor
autowait(0.1f);
while (TRUE)
{
wait()
{
on (EBegin) : { resume; }
on (ETrigger eTrigger) :
{
call LightningStike();
resume;
}
otherwise() :
{
resume;
};
};
}
}
};