git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / weapons / WeaponScatterDirection.cpp
bloba85715fe1ecc91ea0d16886adc100b4aca297be8
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
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.
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.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <weapons/WeaponScatterDirection.h>
22 #include <weapons/AccessoryStore.h>
23 #include <engine/ActionController.h>
24 #include <landscapemap/LandscapeMaps.h>
25 #include <landscapedef/LandscapeTex.h>
26 #include <common/Defines.h>
27 #include <math.h>
29 REGISTER_ACCESSORY_SOURCE(WeaponScatterDirection);
31 WeaponScatterDirection::WeaponScatterDirection()
36 WeaponScatterDirection::~WeaponScatterDirection()
41 bool WeaponScatterDirection::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
43 if (!Weapon::parseXML(context, accessoryNode)) return false;
45 // Get the next weapon
46 XMLNode *subNode = 0;
47 if (!accessoryNode->getNamedChild("aimedweapon", subNode)) return false;
49 // Check next weapon is correct type
50 AccessoryPart *accessory = context.getAccessoryStore().
51 createAccessoryPart(context, parent_, subNode);
52 if (!accessory || accessory->getType() != AccessoryPart::AccessoryWeapon)
54 return subNode->returnError("Failed to find sub weapon, not a weapon");
56 aimedWeapon_ = (Weapon*) accessory;
58 if (!accessoryNode->getNamedChild("direction", direction_)) return false;
59 if (!accessoryNode->getNamedChild("directionoffset", directionOffset_)) return false;
61 return true;
64 void WeaponScatterDirection::fireWeapon(ScorchedContext &context,
65 WeaponFireContext &weaponContext, FixedVector &position, FixedVector &v)
67 RandomGenerator &random = context.getActionController().getRandom();
68 FixedVector vel;
69 vel[0] += direction_[0] - directionOffset_[0] +
70 directionOffset_[0] * 2 * random.getRandFixed();
71 vel[1] += direction_[1] - directionOffset_[1] +
72 directionOffset_[1] * 2 * random.getRandFixed();
73 vel[2] += direction_[2] - directionOffset_[2] +
74 directionOffset_[2] * 2 * random.getRandFixed();
76 aimedWeapon_->fireWeapon(context, weaponContext, position, vel);