1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Fabien Vultier |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file MunitionPickup.cc |
---|
31 | @brief Implementation of the MunitionPickup class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "MunitionPickup.h" |
---|
35 | |
---|
36 | #include <sstream> |
---|
37 | #include "core/CoreIncludes.h" |
---|
38 | #include "core/XMLPort.h" |
---|
39 | |
---|
40 | #include "worldentities/pawns/SpaceShip.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | RegisterClass(MunitionPickup); |
---|
45 | |
---|
46 | /** |
---|
47 | @brief |
---|
48 | Constructor. Registers the object and initializes the member variables. |
---|
49 | */ |
---|
50 | MunitionPickup::MunitionPickup(Context* context) : Pickup(context) |
---|
51 | { |
---|
52 | RegisterObject(MunitionPickup); |
---|
53 | |
---|
54 | this->initialize(); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | @brief |
---|
59 | Destructor. |
---|
60 | */ |
---|
61 | MunitionPickup::~MunitionPickup() |
---|
62 | { |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | @brief |
---|
67 | Initializes the member variables. |
---|
68 | */ |
---|
69 | void MunitionPickup::initialize(void) |
---|
70 | { |
---|
71 | //Defines who is allowed to pick up the pickup. |
---|
72 | this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); |
---|
73 | munitionAmount_ = 1; |
---|
74 | setMunitionName("LaserMunition"); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | @brief |
---|
79 | Method for creating a MunitionPickup object through XML. |
---|
80 | */ |
---|
81 | void MunitionPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
82 | { |
---|
83 | SUPER(MunitionPickup, XMLPort, xmlelement, mode); |
---|
84 | |
---|
85 | XMLPortParam(MunitionPickup, "amount", setMunitionAmount, getMunitionAmount, xmlelement, mode); |
---|
86 | XMLPortParam(MunitionPickup, "munitiontype", setMunitionName, getMunitionName, xmlelement, mode); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | @brief |
---|
91 | Is called when the pickup has transisted from used to unused or the other way around. |
---|
92 | */ |
---|
93 | void MunitionPickup::changedUsed(void) |
---|
94 | { |
---|
95 | SUPER(MunitionPickup, changedUsed); |
---|
96 | |
---|
97 | Pawn* pawn = this->carrierToPawnHelper(); |
---|
98 | |
---|
99 | if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. |
---|
100 | this->Pickupable::destroy(); |
---|
101 | |
---|
102 | |
---|
103 | // If the pickup has transited to used. |
---|
104 | if(this->isUsed()) |
---|
105 | { |
---|
106 | //Get pointer to the appropriate munition |
---|
107 | Munition* munition = pawn->getMunition(&munitionType_); |
---|
108 | if (munition) |
---|
109 | { |
---|
110 | // Add munition |
---|
111 | munition->addMunition(munitionAmount_); |
---|
112 | // This will destroy the pickp |
---|
113 | this->setUsed(false); |
---|
114 | } |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | this->Pickupable::destroy(); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | @brief |
---|
124 | Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. |
---|
125 | @return |
---|
126 | A pointer to the Pawn, or NULL if the conversion failed. |
---|
127 | */ |
---|
128 | Pawn* MunitionPickup::carrierToPawnHelper(void) |
---|
129 | { |
---|
130 | PickupCarrier* carrier = this->getCarrier(); |
---|
131 | Pawn* pawn = orxonox_cast<Pawn*>(carrier); |
---|
132 | |
---|
133 | if(pawn == NULL) |
---|
134 | { |
---|
135 | orxout(internal_error, context::pickups) << "Invalid PickupCarrier in MunitionPickup." << endl; |
---|
136 | } |
---|
137 | return pawn; |
---|
138 | } |
---|
139 | |
---|
140 | void MunitionPickup::setMunitionName(const std::string& munitionname) |
---|
141 | { |
---|
142 | Identifier* identifier = ClassByString(munitionname); |
---|
143 | if (identifier) |
---|
144 | { |
---|
145 | this->munitionType_ = identifier; |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | orxout(internal_warning) << "No munition class defined in MunitionPickup." << endl; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | void MunitionPickup::setMunitionAmount(int munitionAmount) |
---|
154 | { |
---|
155 | if (munitionAmount > 0) |
---|
156 | { |
---|
157 | munitionAmount_ = munitionAmount; |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | munitionAmount = 0; |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|