1 | #include "dxstdafx.h"
|
---|
2 | #include ".\weapon.h"
|
---|
3 | #include "Player.h"
|
---|
4 | #include "Scene.h"
|
---|
5 | #include "SoundNode.h"
|
---|
6 | #include "GameManager.h"
|
---|
7 |
|
---|
8 | Weapon::Weapon(void) : Node() {
|
---|
9 | for(int i=0;i<STATESIZE;i++) {
|
---|
10 | this->setStateTime(i, 0);
|
---|
11 | this->soundNode[i] = NULL;
|
---|
12 | this->soundNames[i] = "";
|
---|
13 | }
|
---|
14 | this->enteredState = true;
|
---|
15 | this->setState(this->STATE_IDLE);
|
---|
16 | this->munitionQuantity = 0;
|
---|
17 | this->Cweapon = 12.5;
|
---|
18 |
|
---|
19 | //this->leftPos.setXYZ(1.4f, 1.4f, -0.5f);
|
---|
20 | //this->rightPos.setXYZ(-1.4f, 1.4f, -0.5f);
|
---|
21 | this->leftPos.setXYZ(1.6f, 1.6f, -0.5f);
|
---|
22 | this->rightPos.setXYZ(-1.6f, 1.6f, -0.5f);
|
---|
23 | this->bFire = false;
|
---|
24 | this->player = NULL;
|
---|
25 | D3DXMatrixIdentity(&this->myWorldMatrix);
|
---|
26 | }
|
---|
27 |
|
---|
28 | Weapon::~Weapon(void) {
|
---|
29 | }
|
---|
30 |
|
---|
31 | void Weapon::setPlayer(Player &_player) {
|
---|
32 | this->player = &_player;
|
---|
33 | }
|
---|
34 |
|
---|
35 | void Weapon::setStateTime(int state, float time)
|
---|
36 | {
|
---|
37 | this->timeForState[state] = time;
|
---|
38 | }
|
---|
39 |
|
---|
40 | void Weapon::setStateSound(int state, std::string soundfile)
|
---|
41 | {
|
---|
42 | if(soundfile.compare("")!=0) {
|
---|
43 | this->soundNames[state] = soundfile;
|
---|
44 | this->soundNode[state] = (SoundNode*) this->myScene->createNode(this->myScene->NODE_SOUND);//, *this);
|
---|
45 | this->soundNode[state]->loadFile(soundfile, false);
|
---|
46 | this->soundNode[state]->setKillSoundNodeAfterPlayed(false);
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | void Weapon::addMunition(int quantity)
|
---|
51 | {
|
---|
52 | this->munitionQuantity+=quantity;
|
---|
53 | }
|
---|
54 |
|
---|
55 | int Weapon::getAmo()
|
---|
56 | {
|
---|
57 | return this->munitionQuantity;
|
---|
58 | }
|
---|
59 |
|
---|
60 | void Weapon::fire()
|
---|
61 | {
|
---|
62 | this->bFire = true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void Weapon::update(float dt)
|
---|
66 | {
|
---|
67 | this->myStateTime+=dt;
|
---|
68 | switch(this->myState) {
|
---|
69 | case this->STATE_IDLE:
|
---|
70 | if(this->enteredState) {
|
---|
71 | this->enteredState = false;
|
---|
72 | }
|
---|
73 | if(this->munitionQuantity<=0) {
|
---|
74 | this->setState(this->STATE_EMPTY);
|
---|
75 | Node::update(dt);
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | if(this->bFire) {
|
---|
79 | this->bFire = false;
|
---|
80 | this->setState(this->STATE_PRELOAD);
|
---|
81 | }
|
---|
82 | this->enteredState = false;
|
---|
83 | break;
|
---|
84 | case this->STATE_PRELOAD:
|
---|
85 | if(this->enteredState) {
|
---|
86 | if(this->soundNode[this->myState])
|
---|
87 | this->soundNode[this->myState]->play();
|
---|
88 | this->enteredState = false;
|
---|
89 | }
|
---|
90 | this->bFire = false;
|
---|
91 | if(this->myStateTime>=this->timeForState[this->myState])
|
---|
92 | this->setState(this->STATE_FIRE);
|
---|
93 | break;
|
---|
94 | case this->STATE_FIRE:
|
---|
95 | if(this->enteredState) {
|
---|
96 | if(this->soundNode[this->myState])
|
---|
97 | this->soundNode[this->myState]->play();
|
---|
98 | this->doFire();
|
---|
99 | this->munitionQuantity--;
|
---|
100 | this->enteredState = false;
|
---|
101 | }
|
---|
102 | this->bFire = false;
|
---|
103 | if(this->myStateTime>=this->timeForState[this->myState])
|
---|
104 | this->setState(this->STATE_POSTLOAD);
|
---|
105 | break;
|
---|
106 | case this->STATE_POSTLOAD:
|
---|
107 | if(this->enteredState) {
|
---|
108 | if(this->soundNode[this->myState])
|
---|
109 | this->soundNode[this->myState]->play();
|
---|
110 | this->enteredState = false;
|
---|
111 | }
|
---|
112 | if(this->myStateTime>=this->timeForState[this->myState])
|
---|
113 | this->setState(this->STATE_IDLE);
|
---|
114 | break;
|
---|
115 | case this->STATE_EMPTY:
|
---|
116 | if(this->enteredState) {
|
---|
117 | this->enteredState = false;
|
---|
118 | }
|
---|
119 | if(this->munitionQuantity>0) {
|
---|
120 | this->setState(this->STATE_IDLE);
|
---|
121 | }
|
---|
122 | if( this->munitionQuantity<=0 &&
|
---|
123 | this->bFire &&
|
---|
124 | this->soundNode[this->myState] &&
|
---|
125 | this->soundNode[this->myState]->getTimeToPlay()==-1) {
|
---|
126 |
|
---|
127 | this->myScene->manager->printToConsole("playing empty sound!");
|
---|
128 | this->soundNode[this->myState]->play();
|
---|
129 | this->soundNode[this->myState]->setPosition(this->getPosition());
|
---|
130 | this->bFire = false;
|
---|
131 | }
|
---|
132 | break;
|
---|
133 | }
|
---|
134 | Node::update(dt);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void Weapon::setState(int state) {
|
---|
138 | this->myState = state;
|
---|
139 | this->enteredState = true;
|
---|
140 | this->myStateTime = 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void Weapon::setWeaponType(int type)
|
---|
144 | {
|
---|
145 | this->weaponType = type;
|
---|
146 | }
|
---|
147 |
|
---|
148 | int Weapon::getWeaponType()
|
---|
149 | {
|
---|
150 | return this->weaponType;
|
---|
151 | }
|
---|
152 |
|
---|
153 | void Weapon::setFireAt(Vector _fireAt)
|
---|
154 | {
|
---|
155 | this->vFireAt = _fireAt;
|
---|
156 | }
|
---|
157 |
|
---|
158 | void Weapon::calcWorldMatrix(D3DXMATRIX &pMatWorld)
|
---|
159 | {
|
---|
160 | D3DXVECTOR4 temp;
|
---|
161 | this->vRealFireAt = this->vRealFireAt + (this->vFireAt-this->vRealFireAt)*this->Cweapon*this->myScene->getDeltaTime();
|
---|
162 |
|
---|
163 | D3DXVec4Transform(&temp, &this->leftPos, &pMatWorld);
|
---|
164 | Vector left(temp.x, temp.y, temp.z);
|
---|
165 | this->orientWeaponTo(*this->leftWeapon, left);
|
---|
166 |
|
---|
167 | D3DXVec4Transform(&temp, &this->rightPos, &pMatWorld);
|
---|
168 | Vector right(temp.x, temp.y, temp.z);
|
---|
169 | this->orientWeaponTo(*this->rightWeapon, right);
|
---|
170 |
|
---|
171 | this->getAbsoluteVector(this->absMinAABBox, this->minAABBox);
|
---|
172 | this->getAbsoluteVector(this->absMaxAABBox, this->maxAABBox);
|
---|
173 | this->getAbsoluteVector(this->myAbsPos, Vector(0, 0, 0));
|
---|
174 | }
|
---|
175 |
|
---|
176 | void Weapon::orientWeaponTo(Object3d &obj, Vector &origin)
|
---|
177 | {
|
---|
178 | //LookAtMatrix aufstellen
|
---|
179 | D3DXMATRIXA16 wMat;
|
---|
180 | D3DXMatrixTranslation(&wMat, origin.x, origin.y, origin.z);
|
---|
181 |
|
---|
182 | //rotationsmatrix berechnen
|
---|
183 | //Alpha
|
---|
184 | Vector diffXZ;
|
---|
185 | Vector diffYZ;
|
---|
186 | Vector dirZ(0, 0, 1);
|
---|
187 | Vector diff(this->vRealFireAt.x-origin.x, this->vRealFireAt.y-origin.y, this->vRealFireAt.z-origin.z);
|
---|
188 | diff.normalize();
|
---|
189 | diffXZ = diff;
|
---|
190 | diffYZ = diff;
|
---|
191 |
|
---|
192 | //Alpha
|
---|
193 | diff.y = 0;
|
---|
194 | diffYZ.normalize();
|
---|
195 |
|
---|
196 | //Beta
|
---|
197 | diffXZ.y = 0;
|
---|
198 | diffXZ.normalize();
|
---|
199 |
|
---|
200 | float a = (diffYZ.y!=0) ? -acos(diffYZ.dotProd(diff))*diffYZ.y/abs(diffYZ.y): -acos(diffYZ.dotProd(diff));
|
---|
201 | //a+=D3DX_PI/2;
|
---|
202 | float b = (diffXZ.x!=0) ? acos(diffXZ.dotProd(dirZ))*diffXZ.x/abs(diffXZ.x) : acos(diffXZ.dotProd(dirZ));
|
---|
203 | float g = 0;
|
---|
204 |
|
---|
205 | D3DXQUATERNION quat;
|
---|
206 | D3DXMATRIXA16 rotMat;
|
---|
207 | D3DXQuaternionIdentity(&quat);
|
---|
208 | D3DXQuaternionRotationYawPitchRoll(&quat, b, a, g);
|
---|
209 | D3DXMatrixRotationQuaternion(&rotMat, &quat);
|
---|
210 |
|
---|
211 | D3DXMatrixMultiply(&wMat, &rotMat, &wMat);
|
---|
212 |
|
---|
213 | obj.setWorldMatrix(wMat);
|
---|
214 | }
|
---|
215 |
|
---|
216 | void Weapon::setSoftKill(bool _softKill, float _duration) {
|
---|
217 | this->setTimeToLive(_duration);
|
---|
218 | Node::setSoftKill(_softKill, _duration);
|
---|
219 | this->leftWeapon->setTimeToLive(_duration);
|
---|
220 | this->leftWeapon->setSoftKill(_softKill, _duration);
|
---|
221 | this->rightWeapon->setTimeToLive(_duration);
|
---|
222 | this->rightWeapon->setSoftKill(_softKill, _duration);
|
---|
223 | } |
---|