1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: |
---|
16 | */ |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
18 | |
---|
19 | #include "projectile.h" |
---|
20 | |
---|
21 | #include "world_entity.h" |
---|
22 | #include "world_entities/weapons/weapon.h" |
---|
23 | #include "model.h" |
---|
24 | #include "util/loading/resource_manager.h" |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | * standard constructor |
---|
31 | */ |
---|
32 | Projectile::Projectile () : WorldEntity() |
---|
33 | { |
---|
34 | this->setClassID(CL_PROJECTILE, "Projectile"); |
---|
35 | |
---|
36 | this->lifeCycle = 0.0; |
---|
37 | this->lifeSpan = 1.0f; /* sec */ |
---|
38 | this->target = NULL; |
---|
39 | this->removeNode(); |
---|
40 | |
---|
41 | this->explosionBuffer = NULL; |
---|
42 | this->engineBuffer = NULL; |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | /** |
---|
47 | * standard deconstructor |
---|
48 | */ |
---|
49 | Projectile::~Projectile () |
---|
50 | { |
---|
51 | if (this->explosionBuffer != NULL) |
---|
52 | ResourceManager::getInstance()->unload(this->explosionBuffer); |
---|
53 | if (this->engineBuffer != NULL) |
---|
54 | ResourceManager::getInstance()->unload(this->engineBuffer); |
---|
55 | /* |
---|
56 | do not delete the test projectModel, since it is pnode |
---|
57 | and will be cleaned out by world |
---|
58 | */ |
---|
59 | //delete this->projectileModel; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | void Projectile::loadExplosionSound(const char* explosionSound) |
---|
64 | { |
---|
65 | if (this->explosionBuffer != NULL) |
---|
66 | ResourceManager::getInstance()->unload(this->explosionBuffer); |
---|
67 | |
---|
68 | else if (explosionSound != NULL) |
---|
69 | { |
---|
70 | this->explosionBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(explosionSound, WAV); |
---|
71 | if (this->explosionBuffer != NULL) |
---|
72 | { |
---|
73 | PRINTF(4)("Loaded sound %s to Pickup: %s.\n", explosionSound, this->getName()); |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | PRINTF(2)("Failed to load sound %s to explosion %s.\n.", explosionSound, this->getName()); |
---|
78 | } |
---|
79 | } |
---|
80 | else |
---|
81 | this->explosionBuffer = NULL; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | void Projectile::loadEngineSound(const char* engineSound) |
---|
86 | { |
---|
87 | if (this->engineBuffer != NULL) |
---|
88 | ResourceManager::getInstance()->unload(this->engineBuffer); |
---|
89 | |
---|
90 | else if (engineSound != NULL) |
---|
91 | { |
---|
92 | this->engineBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(engineSound, WAV); |
---|
93 | if (this->engineBuffer != NULL) |
---|
94 | { |
---|
95 | PRINTF(4)("Loaded sound %s to Pickup: %s.\n", engineSound, this->getName()); |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | PRINTF(2)("Failed to load sound %s to engine %s.\n.", engineSound, this->getName()); |
---|
100 | } |
---|
101 | } |
---|
102 | else |
---|
103 | this->engineBuffer = NULL; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | void Projectile::setMinEnergy(float energyMin) |
---|
108 | { |
---|
109 | this->energyMin = energyMin; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | /** |
---|
114 | * this sets the flight direction of the projectile |
---|
115 | * @param directin in which to flight |
---|
116 | |
---|
117 | this function will calculate a vector out of this to be used in the |
---|
118 | tick function |
---|
119 | */ |
---|
120 | void Projectile::setFlightDirection(const Quaternion& flightDirection) |
---|
121 | { |
---|
122 | Vector v(1, 0, 0); |
---|
123 | this->flightDirection = flightDirection.apply(v); |
---|
124 | this->flightDirection.normalize(); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * sets the velocity vector to a spec speed |
---|
129 | * @param velocity: vector of the velocity |
---|
130 | */ |
---|
131 | void Projectile::setVelocity(const Vector &velocity) |
---|
132 | { |
---|
133 | //Vector offsetVel = |
---|
134 | this->velocity = velocity; |
---|
135 | // offsetVel.normalize(); |
---|
136 | //this->velocity += (offsetVel * 50.0); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | void Projectile::setTarget(PNode* target) |
---|
142 | { |
---|
143 | if (this->target == NULL) |
---|
144 | this->target = new PNode(target, PNODE_PARENT_MODE_DEFAULT | PNODE_REPARENT_ON_PARENTS_REMOVE); |
---|
145 | else |
---|
146 | this->target->setParent(target); |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | /** |
---|
151 | * signal tick, time dependent things will be handled here |
---|
152 | * @param dt since last tick |
---|
153 | */ |
---|
154 | void Projectile::tick (float dt) |
---|
155 | { |
---|
156 | Vector v = this->velocity * (dt); |
---|
157 | this->shiftCoor(v); |
---|
158 | |
---|
159 | if (this->tickLifeCycle(dt)) |
---|
160 | this->destroy(); |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | /** |
---|
165 | * the function gets called, when the projectile is destroyed |
---|
166 | */ |
---|
167 | void Projectile::destroy () |
---|
168 | { |
---|
169 | if (this->explosionBuffer != NULL) |
---|
170 | this->soundSource.play(this->explosionBuffer); |
---|
171 | } |
---|
172 | |
---|