~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/particle/particlecontainer.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-09-17 10:35:51 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20130917103551-g4o65c0mtfpg5r8m
Tags: upstream-1.3.9.15
Import upstream version 1.3.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2013  The ManaPlus Developers
 
6
 *
 
7
 *  This file is part of The ManaPlus Client.
 
8
 *
 
9
 *  This program is free software; you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation; either version 2 of the License, or
 
12
 *  any later version.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 */
 
22
 
 
23
#include "particle/particle.h"
 
24
#include "particle/particlecontainer.h"
 
25
 
 
26
#include "debug.h"
 
27
 
 
28
typedef std::list<Particle *>::iterator ParticleListIter;
 
29
typedef std::list<Particle *>::const_iterator ParticleListCIter;
 
30
 
 
31
ParticleContainer::ParticleContainer(ParticleContainer *const parent,
 
32
                                     const bool delParent):
 
33
    mNext(parent),
 
34
    mDelParent(delParent)
 
35
{
 
36
}
 
37
 
 
38
ParticleContainer::~ParticleContainer()
 
39
{
 
40
    clearLocally();
 
41
    if (mDelParent)
 
42
    {
 
43
        delete mNext;
 
44
        mNext = nullptr;
 
45
    }
 
46
}
 
47
 
 
48
void ParticleContainer::clear()
 
49
{
 
50
    clearLocally();
 
51
    if (mNext)
 
52
        mNext->clear();
 
53
}
 
54
 
 
55
void ParticleContainer::moveTo(const float x, const float y)
 
56
{
 
57
    if (mNext)
 
58
        mNext->moveTo(x, y);
 
59
}
 
60
 
 
61
// -- particle list ----------------------------------------
 
62
 
 
63
ParticleList::ParticleList(ParticleContainer *const parent,
 
64
                           const bool delParent) :
 
65
    ParticleContainer(parent, delParent),
 
66
    mElements()
 
67
{}
 
68
 
 
69
ParticleList::~ParticleList()
 
70
{}
 
71
 
 
72
void ParticleList::addLocally(Particle *const particle)
 
73
{
 
74
    if (particle)
 
75
    {
 
76
        // The effect may not die without the beings permission or we segfault
 
77
        particle->disableAutoDelete();
 
78
        mElements.push_back(particle);
 
79
    }
 
80
}
 
81
 
 
82
void ParticleList::removeLocally(const Particle *const particle)
 
83
{
 
84
    for (std::list<Particle *>::iterator it = mElements.begin();
 
85
         it != mElements.end(); )
 
86
    {
 
87
        Particle *const p = *it;
 
88
        if (p == particle)
 
89
        {
 
90
            p->kill();
 
91
            it = mElements.erase(it);
 
92
        }
 
93
        else
 
94
        {
 
95
            ++it;
 
96
        }
 
97
    }
 
98
}
 
99
 
 
100
void ParticleList::clearLocally()
 
101
{
 
102
    FOR_EACH (ParticleListCIter, it, mElements)
 
103
        (*it)->kill();
 
104
 
 
105
    mElements.clear();
 
106
}
 
107
 
 
108
void ParticleList::moveTo(const float x, const float y)
 
109
{
 
110
    ParticleContainer::moveTo(x, y);
 
111
 
 
112
    for (std::list<Particle *>::iterator it = mElements.begin();
 
113
         it != mElements.end(); )
 
114
    {
 
115
        Particle *const p = *it;
 
116
        p->moveTo(x, y);
 
117
        if (p->isExtinct())
 
118
        {
 
119
            p->kill();
 
120
            it = mElements.erase(it);
 
121
        }
 
122
        else
 
123
        {
 
124
            ++it;
 
125
        }
 
126
    }
 
127
}
 
128
 
 
129
// -- particle vector ----------------------------------------
 
130
 
 
131
ParticleVector::ParticleVector(ParticleContainer *const parent,
 
132
                               const bool delParent) :
 
133
    ParticleContainer(parent, delParent),
 
134
    mIndexedElements()
 
135
{}
 
136
 
 
137
ParticleVector::~ParticleVector()
 
138
{}
 
139
 
 
140
void ParticleVector::setLocally(const int index, Particle *const particle)
 
141
{
 
142
    if (index < 0)
 
143
        return;
 
144
 
 
145
    delLocally(index);
 
146
 
 
147
    if (mIndexedElements.size() <= static_cast<unsigned>(index))
 
148
        mIndexedElements.resize(index + 1, nullptr);
 
149
 
 
150
    if (particle)
 
151
        particle->disableAutoDelete();
 
152
    mIndexedElements[index] = particle;
 
153
}
 
154
 
 
155
void ParticleVector::delLocally(const int index)
 
156
{
 
157
    if (index < 0)
 
158
        return;
 
159
 
 
160
    if (mIndexedElements.size() <= static_cast<unsigned>(index))
 
161
        return;
 
162
 
 
163
    Particle *const p = mIndexedElements[index];
 
164
    if (p)
 
165
    {
 
166
        mIndexedElements[index] = nullptr;
 
167
        p->kill();
 
168
    }
 
169
}
 
170
 
 
171
void ParticleVector::clearLocally()
 
172
{
 
173
    for (unsigned int i = 0; i < mIndexedElements.size(); i++)
 
174
        delLocally(i);
 
175
}
 
176
 
 
177
void ParticleVector::moveTo(const float x, const float y)
 
178
{
 
179
    ParticleContainer::moveTo(x, y);
 
180
 
 
181
    for (std::vector<Particle *>::iterator it = mIndexedElements.begin();
 
182
         it != mIndexedElements.end(); ++it)
 
183
    {
 
184
        Particle *const p = *it;
 
185
        if (p)
 
186
        {
 
187
            p->moveTo(x, y);
 
188
 
 
189
            if (p->isExtinct())
 
190
            {
 
191
                p->kill();
 
192
                *it = nullptr;
 
193
            }
 
194
        }
 
195
    }
 
196
}