1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include <weapons/WeaponSound.h>
22 #include <engine/ActionController.h>
23 #include <actions/SoundAction.h>
24 #include <common/Defines.h>
25 #include <stdlib.h>
26 
27 REGISTER_ACCESSORY_SOURCE(WeaponSound);
28 
WeaponSound()29 WeaponSound::WeaponSound() :
30 	gain_(1), relative_(false),
31 	rolloff_(1), referenceDistance_(75)
32 {
33 
34 }
35 
~WeaponSound()36 WeaponSound::~WeaponSound()
37 {
38 
39 }
40 
getSound()41 const char *WeaponSound::getSound()
42 {
43 	std::string &sound = sounds_[rand() % sounds_.size()];
44 	return sound.c_str();
45 }
46 
parseXML(AccessoryCreateContext & context,XMLNode * accessoryNode)47 bool WeaponSound::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
48 {
49 	if (!Weapon::parseXML(context, accessoryNode)) return false;
50 
51 	std::string sound;
52 	while (accessoryNode->getNamedChild("sound", sound, false))
53 	{
54 		if (!S3D::checkDataFile(sound.c_str())) return false;
55 		sounds_.push_back(sound);
56 	}
57 	accessoryNode->getNamedChild("gain", gain_, false);
58 	accessoryNode->getNamedChild("rolloff", rolloff_, false);
59 	accessoryNode->getNamedChild("referencedistance", referenceDistance_, false);
60 	accessoryNode->getNamedChild("relative", relative_, false);
61 	return true;
62 }
63 
fireWeapon(ScorchedContext & context,WeaponFireContext & weaponContext,FixedVector & position,FixedVector & velocity)64 void WeaponSound::fireWeapon(ScorchedContext &context,
65 	WeaponFireContext &weaponContext, FixedVector &position, FixedVector &velocity)
66 {
67 	context.getActionController().addAction(
68 		new SoundAction(position, this));
69 }
70