ManaPlus
particlevector.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "particle/particle.h"
26 
27 #include "debug.h"
28 
30  const bool delParent) :
31  ParticleContainer(parent, delParent),
32  mIndexedElements()
33 {}
34 
36 {}
37 
38 void ParticleVector::setLocally(const int index, Particle *const particle)
39 {
40  if (index < 0)
41  return;
42 
43  delLocally(index);
44 
45  if (mIndexedElements.size() <= CAST_SIZE(index))
46  mIndexedElements.resize(index + 1, nullptr);
47 
48  if (particle != nullptr)
49  particle->disableAutoDelete();
50  mIndexedElements[index] = particle;
51 }
52 
53 void ParticleVector::delLocally(const int index)
54 {
55  if (index < 0)
56  return;
57 
58  if (mIndexedElements.size() <= CAST_SIZE(index))
59  return;
60 
61  Particle *const p = mIndexedElements[index];
62  if (p != nullptr)
63  {
64  mIndexedElements[index] = nullptr;
65  p->kill();
66  }
67 }
68 
70 {
71  for (unsigned int i = 0;
72  i < CAST_U32(mIndexedElements.size());
73  i++)
74  {
75  delLocally(i);
76  }
77 }
78 
79 void ParticleVector::moveTo(const float x, const float y)
80 {
82 
83  for (STD_VECTOR<Particle *>::iterator it = mIndexedElements.begin();
84  it != mIndexedElements.end(); ++it)
85  {
86  Particle *const p = *it;
87  if (p != nullptr)
88  {
89  p->moveTo(x, y);
90 
91  if (p->isExtinct())
92  {
93  p->kill();
94  *it = nullptr;
95  }
96  }
97  }
98 }
99 
101 {
102  size_t cnt = 0;
103  for (STD_VECTOR<Particle *>::const_iterator it = mIndexedElements.begin();
104  it != mIndexedElements.end(); ++it)
105  {
106  if (*it != nullptr)
107  cnt ++;
108  }
109  return cnt;
110 }
#define CAST_U32
Definition: cast.h:31
#define CAST_SIZE
Definition: cast.h:34
virtual void moveTo(const float x, const float y)
void setLocally(const int index, Particle *const particle)
void moveTo(const float x, const float y)
size_t usedSize() const
void delLocally(const int index)
std::vector< Particle * > mIndexedElements
ParticleVector(ParticleContainer *const parent, const bool delParent)
void kill()
Definition: particle.h:220
bool isExtinct() const
Definition: particle.h:214
void disableAutoDelete()
Definition: particle.h:227
void moveTo(const Vector &pos)
Definition: particle.h:107