1
/*
2
 *  The Mana World
3
 *  Copyright (C) 2008  The Mana World Development Team
4
 *
5
 *  This file is part of The Mana World.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
#include <cassert>
23
24
#include "particle.h"
25
#include "particlecontainer.h"
26
27
28
ParticleContainer::ParticleContainer(ParticleContainer *parent,
29
                                     bool delParent):
30
    mDelParent(delParent),
31
    mNext(parent)
32
{}
33
34
ParticleContainer::~ParticleContainer()
35
{
36
    clearLocally();
37
    if (mDelParent)
38
        delete mNext;
39
}
40
41
void ParticleContainer::clear()
42
{
43
    clearLocally();
44
    if (mNext)
45
        mNext->clear();
46
}
47
48
void ParticleContainer::moveTo(float x, float y)
49
{
50
    if (mNext)
51
        mNext->moveTo(x, y);
52
}
53
54
// -- particle list ----------------------------------------
55
56
ParticleList::ParticleList(ParticleContainer *parent, bool delParent):
57
    ParticleContainer(parent, delParent)
58
{}
59
60
ParticleList::~ParticleList()
61
{}
62
63
void ParticleList::addLocally(Particle *particle)
64
{
65
    if (particle)
66
    {
67
        // The effect may not die without the beings permission or we segfault
68
        particle->disableAutoDelete();
69
        mElements.push_back(particle);
70
    }
71
}
72
73
void ParticleList::removeLocally(Particle *particle)
74
{
75
    for (std::list<Particle *>::iterator it = mElements.begin();
76
         it != mElements.end(); it++)
77
    {
78
        if (*it == particle) {
79
            (*it)->kill();
80
            mElements.erase(it);
81
        }
82
    }
83
}
84
85
void ParticleList::clearLocally()
86
{
87
    for (std::list<Particle *>::iterator it = mElements.begin();
88
         it != mElements.end(); it++)
89
        (*it)->kill();
90
91
    mElements.clear();
92
}
93
94
void ParticleList::moveTo(float x, float y)
95
{
96
    ParticleContainer::moveTo(x, y);
97
98
    for (std::list<Particle *>::iterator it = mElements.begin();
99
         it != mElements.end();)
100
    {
101
        (*it)->moveTo(x, y);
102
        if ((*it)->isExtinct())
103
        {
104
            (*it)->kill();
105
            it = mElements.erase(it);
106
        }
107
        else
108
            it++;
109
    }
110
}
111
112
// -- particle vector ----------------------------------------
113
114
ParticleVector::ParticleVector(ParticleContainer *parent, bool delParent):
115
    ParticleContainer(parent, delParent)
116
{}
117
118
ParticleVector::~ParticleVector()
119
{}
120
121
void ParticleVector::setLocally(int index, Particle *particle)
122
{
123
    assert(index >= 0);
124
125
    delLocally(index);
126
127
    if (mIndexedElements.size() <= (unsigned) index)
128
        mIndexedElements.resize(index + 1, NULL);
129
130
    if (particle)
131
        particle->disableAutoDelete();
132
    mIndexedElements[index] = particle;
133
}
134
135
void ParticleVector::delLocally(int index)
136
{
137
    assert(index >= 0);
138
139
    if (mIndexedElements.size() <= (unsigned) index)
140
        return;
141
142
    Particle *p = mIndexedElements[index];
143
    if (p)
144
    {
145
        mIndexedElements[index] = NULL;
146
        p->kill();
147
    }
148
}
149
150
void ParticleVector::clearLocally()
151
{
152
    for (unsigned int i = 0; i < mIndexedElements.size(); i++)
153
        delLocally(i);
154
}
155
156
void ParticleVector::moveTo(float x, float y)
157
{
158
    ParticleContainer::moveTo(x, y);
159
160
    for (std::vector<Particle *>::iterator it = mIndexedElements.begin();
161
         it != mIndexedElements.end(); it++) {
162
        if (*it)
163
        {
164
            (*it)->moveTo(x, y);
165
166
            if ((*it)->isExtinct())
167
            {
168
                (*it)->kill();
169
                *it = NULL;
170
            }
171
        }
172
    }
173
}