1 /* Visual.cpp
2 Copyright (c) 2017 by Michael Zahniser
3 
4 Endless Sky is free software: you can redistribute it and/or modify it under the
5 terms of the GNU General Public License as published by the Free Software
6 Foundation, either version 3 of the License, or (at your option) any later version.
7 
8 Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10 PARTICULAR PURPOSE.  See the GNU General Public License for more details.
11 */
12 
13 #include "Visual.h"
14 
15 #include "Audio.h"
16 #include "Effect.h"
17 #include "Random.h"
18 
19 using namespace std;
20 
21 
22 
23 // Generate a visual based on the given Effect.
Visual(const Effect & effect,Point pos,Point vel,Angle facing,Point hitVelocity)24 Visual::Visual(const Effect &effect, Point pos, Point vel, Angle facing, Point hitVelocity)
25 	: Body(effect, pos, vel, facing), lifetime(effect.lifetime)
26 {
27 	if(effect.randomLifetime > 0)
28 		lifetime += Random::Int(effect.randomLifetime + 1);
29 
30 	angle += Angle::Random(effect.randomAngle) - Angle::Random(effect.randomAngle);
31 	spin = Angle::Random(effect.randomSpin) - Angle::Random(effect.randomSpin);
32 
33 	velocity *= effect.velocityScale;
34 	velocity += hitVelocity * (1. - effect.velocityScale);
35 	if(effect.randomVelocity)
36 		velocity += angle.Unit() * Random::Real() * effect.randomVelocity;
37 
38 	if(effect.sound)
39 		Audio::Play(effect.sound, position);
40 
41 	if(effect.randomFrameRate)
42 		AddFrameRate(effect.randomFrameRate * Random::Real());
43 }
44 
45 
46 
47 // Step the effect forward.
Move()48 void Visual::Move()
49 {
50 	if(lifetime-- <= 0)
51 		MarkForRemoval();
52 	else
53 	{
54 		position += velocity;
55 		angle += spin;
56 	}
57 }
58