forked from pioneerspacesim/pioneer
/
Missile.cpp
90 lines (77 loc) · 1.77 KB
/
Missile.cpp
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
#include "Missile.h"
#include "Serializer.h"
#include "Space.h"
#include "Sfx.h"
#include "ShipType.h"
#include "Lang.h"
Missile::Missile(ShipType::Type type, Body *owner, Body *target): Ship(type)
{
m_power = 0;
if (type == ShipType::MISSILE_GUIDED) m_power = 1;
if (type == ShipType::MISSILE_SMART) m_power = 2;
if (type == ShipType::MISSILE_NAVAL) m_power = 3;
m_owner = owner;
m_target = target;
m_distToTarget = FLT_MAX;
SetLabel(Lang::MISSILE);
AIKamikaze(target);
}
void Missile::ECMAttack(int power_val)
{
if (power_val > m_power) {
OnDamage(0, 1.0f);
}
}
void Missile::PostLoadFixup()
{
Ship::PostLoadFixup();
m_owner = Serializer::LookupBody(m_ownerIndex);
m_target = Serializer::LookupBody(m_targetIndex);
}
void Missile::Save(Serializer::Writer &wr)
{
Ship::Save(wr);
wr.Int32(Serializer::LookupBody(m_owner));
wr.Int32(Serializer::LookupBody(m_target));
wr.Double(m_distToTarget);
wr.Int32(m_power);
}
void Missile::Load(Serializer::Reader &rd)
{
Ship::Load(rd);
m_ownerIndex = rd.Int32();
m_targetIndex = rd.Int32();
m_distToTarget = rd.Double();
m_power = rd.Int32();
}
void Missile::TimeStepUpdate(const float timeStep)
{
Ship::TimeStepUpdate(timeStep);
double dist = (GetPosition() - m_target->GetPosition()).Length();
if ((m_distToTarget < 150.0) && (dist > m_distToTarget)) {
Explode();
}
m_distToTarget = dist;
}
bool Missile::OnDamage(Object *attacker, float kgDamage)
{
if (!IsDead()) {
Explode();
}
return true;
}
void Missile::Explode()
{
Space::KillBody(this);
Space::RadiusDamage(m_owner, GetFrame(), GetPosition(), 200.0f, 10000.0f);
Sfx::Add(this, Sfx::TYPE_EXPLOSION);
}
void Missile::NotifyDeleted(const Body* const deletedBody)
{
if (m_owner == deletedBody) {
Explode();
}
else if (m_target == deletedBody) {
Explode();
}
}