-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeapon.cpp
260 lines (221 loc) · 5.77 KB
/
Weapon.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
Copyright (C) 2011-2012 Alican Sekerefe
TeamTwo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TeamTwo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact: [email protected]
*/
#include "Weapon.h"
#include "Util/Logger.h"
#include "Util/Converter.h"
#include "SoundController.h"
Weapon::Weapon(int ownerID,int weaponType,unsigned char team)
{
this->team = team;
this->ownerID=ownerID;
this->check=true;
this->fireTimer=new Ogre::Timer();
this->reloadTimer=new Ogre::Timer();
this->nextFireTime=this->reloadEndTime=-1;
this->reloading=false;
this->fired=false;
this->justFired=false;
this->fireVisualized=false;
this->weaponType=weaponType;
this->weaponLight=NULL;
this->weaponNode=NULL;
this->visualizationCurrentFrame=0;
this->visualizationEndFrame=4;
this->fireSoundID="SOUND_NOT_SET";
this->local=false;
}
void Weapon::show()
{
if(weaponNode!=NULL)
weaponNode->setVisible(true);
if(weaponLight!=NULL)
weaponLight->setVisible(false);
if(muzzleFlashNode!=NULL)
muzzleFlashNode->setVisible(false);
}
void Weapon::hide()
{
if(weaponNode!=NULL)
weaponNode->setVisible(false);
if(weaponLight!=NULL)
weaponLight->setVisible(false);
if(muzzleFlashNode!=NULL)
muzzleFlashNode->setVisible(false);
}
void Weapon::zero()
{
clipAmmo=0;
clipCount=0;
totalAmmo=0;
}
void Weapon::update()
{
if(reloading && reloadEndTime<=reloadTimer->getMilliseconds())
{
clipAmmo=(totalAmmo>=clipSize?clipSize:totalAmmo);
clipCount=(int)Ogre::Math::Ceil(totalAmmo/(float)clipSize);
reloadEndTime=-1;
reloadTimer->reset();
Logger::getSingleton()->addLine("reloaded.");
reloading=false;
}
if(fired && nextFireTime<=fireTimer->getMilliseconds())
{
fired=false;
nextFireTime=-1;
fireTimer->reset();
}
if(justFired)
{
if(visualizationCurrentFrame>visualizationEndFrame)
{
justFired=false;
weaponLight->setVisible(false);
muzzleFlashNode->setVisible(false);
}
else
visualizationCurrentFrame++;
}
}
void Weapon::fire()
{
if(check && canFire())
{
nextFireTime=fireTimer->getMilliseconds()+(1000./roundsPerSecond);
clipAmmo--;
totalAmmo--;
fired=true;
//animations...
}
else if(!check)
fired=true;
if (fired)
{
SoundController::getSingleton()->playSound(fireSoundID,true);
if(weaponLight!=NULL)
{
weaponLight->setPosition(bulletOrigin);
weaponLight->setVisible(true);
muzzleFlashNode->setVisible(true);
justFired = fireVisualized = true;
visualizationCurrentFrame = 0;
}
}
}
void Weapon::reload()
{
if(check && (canReload()))
{
Logger::getSingleton()->addLine("reloading...");
reloadEndTime=reloadTimer->getMilliseconds()+reloadDuration;
reloading=true;
if(reloadSoundIDs.size()>0)
{
srand(time(NULL));
int reloadIndex=rand()%reloadSoundIDs.size();
SoundController::getSingleton()->playSound(reloadSoundIDs[reloadIndex],true);
}
//animations..
}
}
void Weapon::addClip(int clips)
{
int _total = (totalAmmo + (getClipSize() * clips));
_total=(_total>getMaxClip()*getClipSize()?getMaxClip()*getClipSize():_total);
clipCount = (int) Ogre::Math::Ceil(_total / (float) clipSize);
totalAmmo = _total;
}
bool Weapon::canTakeClips()
{
int clipDiff=getClipSize()-getClipAmmo();
bool extra=(clipDiff+totalAmmo==getMaxClip()*getClipSize()?true:false);
return !extra && totalAmmo<getClipSize()*getMaxClip()?true:false;
}
bool Weapon::canFire()
{
return !fired && (getClipAmmo()>0) && !reloading;
}
bool Weapon::canReload()
{
return getClipAmmo()<getClipSize() && !reloading && getClipCount()>0;
}
void Weapon::setSceneNode(Ogre::SceneNode* weaponNode)
{
this->weaponNode=weaponNode;
}
Ogre::SceneNode* Weapon::getWeaponNode()
{
return weaponNode;
}
Ogre::Light* Weapon::getWeaponLight()
{
return weaponLight;
}
int Weapon::getTotalAmmo()
{
return totalAmmo;
}
int Weapon::getMaxClip()
{
return maxClip;
}
int Weapon::getClipAmmo()
{
return clipAmmo;
}
int Weapon::getClipCount()
{
return clipCount;
}
int Weapon::getClipSize()
{
return clipSize;
}
int Weapon::getWeaponSlot()
{
return weaponSlot;
}
AnimationController* Weapon::getAnimationController()
{
return animationController;
}
Ogre::Vector3 Weapon::getBulletOrigin()
{
return bulletOrigin;
}
void Weapon::disableAmmunitionCheck()
{
check=false;
}
void Weapon::enableAmmunitionCheck()
{
check=true;
}
int Weapon::getWeaponType()
{
return weaponType;
}
SceneNode* Weapon::getMuzzleFlashNode()
{
return muzzleFlashNode;
}
void Weapon::setClipAmmo(int ammo)
{
clipAmmo=ammo;
}
Ogre::Entity* Weapon::getWeaponEntity()
{
return weaponEntity;
}