Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/weapon/AmmunitionDump.cc @ 882

Last change on this file since 882 was 871, checked in by landauf, 17 years ago

merged core branch to trunk

File size: 2.9 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Reto Grieder
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include "OrxonoxStableHeaders.h"
29
30#include "orxonox/Orxonox.h"
31#include "BulletManager.h"
32#include "util/tinyxml/tinyxml.h"
33#include "core/CoreIncludes.h"
34
35#include "AmmunitionDump.h"
36
37
38namespace orxonox {
39  CreateFactory(AmmunitionDump);
40
41  AmmunitionDump::AmmunitionDump()
42    : numberOfAmmos_(Orxonox::getSingleton()->getBulletMgr()->getNumberOfAmmos()),
43      stock_(new int[numberOfAmmos_]),
44      capacity_(new int[numberOfAmmos_])
45  {
46    RegisterObject(AmmunitionDump);
47
48    for (int i = 0; i < numberOfAmmos_; i++)
49    {
50      stock_[i] = 0;
51      capacity_[i] = 0;
52    }
53  }
54
55
56  AmmunitionDump::~AmmunitionDump()
57  {
58    if (stock_)
59      delete stock_;
60    if (capacity_)
61      delete capacity_;
62  }
63
64  void AmmunitionDump::setDumpSize(const std::string &name, int size)
65  {
66    if (size < 0)
67      return;
68    int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name);
69    if (id == -1)
70      return;
71    capacity_[id] = size;
72  }
73
74
75  int AmmunitionDump::store(const std::string &name, int quantity)
76  {
77    int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name);
78    if (id == -1)
79      return quantity;
80    stock_[id] += quantity;
81    if (stock_[id] > capacity_[id])
82    {
83      quantity = capacity_[id] - stock_[id];
84      stock_[id] = capacity_[id];
85      return quantity;
86    }
87    else
88      return 0;
89  }
90
91
92  int AmmunitionDump::getAmmunition(const std::string &name, int quantity)
93  {
94    int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name);
95    if (id == -1)
96      return 0;
97    if (stock_[id] >= quantity)
98      stock_[id] -= quantity;
99    else
100    {
101      quantity = stock_[id];
102      stock_[id] = 0;
103    }
104    return quantity;
105  }
106
107
108  int AmmunitionDump::getStockSize(const std::string &name)
109  {
110    int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name);
111    // FIXME changed = to ==, check if correct
112    if (id == -1)
113      return -1;
114    return stock_[id];
115  }
116}
Note: See TracBrowser for help on using the repository browser.