1 /* Projectile.h
2 Copyright (c) 2014 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 #ifndef PROJECTILE_H_
14 #define PROJECTILE_H_
15 
16 #include "Body.h"
17 
18 #include "Angle.h"
19 #include "Point.h"
20 
21 #include <memory>
22 #include <vector>
23 
24 class Government;
25 class Ship;
26 class Visual;
27 class Weapon;
28 
29 
30 
31 // Class representing a projectile (a moving object which can hit ships or
32 // asteroids and can potentially be hit by anti-missile systems). A projectile
33 // may either move at a constant heading and velocity, or may accelerate or
34 // change course to track its target. Also, when they hit their target or reach
35 // the end of their lifetime, some projectiles split into "sub-munitions," new
36 // projectiles that may look different or travel in a new direction.
37 class Projectile : public Body {
38 public:
39 	Projectile(const Ship &parent, Point position, Angle angle, const Weapon *weapon);
40 	Projectile(const Projectile &parent, const Weapon *weapon);
41 	// Ship explosion.
42 	Projectile(Point position, const Weapon *weapon);
43 
44 	/* Functions provided by the Body base class:
45 	Frame GetFrame(int step = -1) const;
46 	const Point &Position() const;
47 	const Point &Velocity() const;
48 	const Angle &Facing() const;
49 	Point Unit() const;
50 	const Government *GetGovernment() const;
51 	*/
52 
53 	// Move the projectile. It may create effects or submunitions.
54 	void Move(std::vector<Visual> &visuals, std::vector<Projectile> &projectiles);
55 	// This projectile hit something. Create the explosion, if any. This also
56 	// marks the projectile as needing deletion.
57 	void Explode(std::vector<Visual> &visuals, double intersection, Point hitVelocity = Point());
58 	// Get the amount of clipping that should be applied when drawing this projectile.
59 	double Clip() const;
60 	// This projectile was killed, e.g. by an anti-missile system.
61 	void Kill();
62 
63 	// Find out if this is a missile, and if so, how strong it is (i.e. what
64 	// chance an anti-missile shot has of destroying it).
65 	int MissileStrength() const;
66 	// Get information on the weapon that fired this projectile.
67 	const Weapon &GetWeapon() const;
68 
69 	// Find out which ship this projectile is targeting. Note: this pointer is
70 	// not guaranteed to be dereferenceable, so only use it for comparing.
71 	const Ship *Target() const;
72 	// This function is much more costly, so use it only if you need to get a
73 	// non-const shared pointer to the target.
74 	std::shared_ptr<Ship> TargetPtr() const;
75 
76 	// Get the distance that this projectile has traveled.
77 	double DistanceTraveled() const;
78 
79 
80 private:
81 	void CheckLock(const Ship &target);
82 
83 
84 private:
85 	const Weapon *weapon = nullptr;
86 
87 	std::weak_ptr<Ship> targetShip;
88 	const Ship *cachedTarget = nullptr;
89 	const Government *targetGovernment = nullptr;
90 
91 	double clip = 1.;
92 	int lifetime = 0;
93 	double distanceTraveled = 0;
94 	bool hasLock = true;
95 };
96 
97 
98 
99 #endif
100