1
// $Id: pingu_particle_holder.cpp 2986 2007-08-17 16:20:09Z grumbel $
3
// Pingus - A free Lemmings clone
4
// Copyright (C) 1999 Ingo Ruhnke <grumbel@gmx.de>
6
// This program is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU General Public License
8
// as published by the Free Software Foundation; either version 2
9
// of the License, or (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
#include "../display/scene_context.hpp"
21
#include "../math.hpp"
22
#include "../col_map.hpp"
23
#include "../resource.hpp"
24
#include "../world.hpp"
25
#include "pingu_particle_holder.hpp"
29
const float x_collision_decrease = 0.3f;
30
const float y_collision_decrease = 0.6f;
32
PinguParticleHolder::PinguParticle::PinguParticle (int x, int y)
33
: livetime(50 + (rand() % 75)),
34
use_frame2((rand() % 5) == 0),
35
pos(Vector3f((float)x, (float)y)),
36
velocity(Vector3f(Math::frand() * 7 - 3.5f, Math::frand() * -9))
41
PinguParticleHolder::PinguParticleHolder ()
42
: surface(Resource::load_sprite("particles/pingu_explo"))
48
PinguParticleHolder::add_particle (int x, int y)
52
// fill gaps from dead entries
53
for (std::vector<PinguParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
57
*it = PinguParticle(x, y);
62
// allocate space for all remaining particles at once
63
particles.reserve(particles.size() + 50 - i);
65
// create remaining entries
67
particles.push_back(PinguParticle(x, y));
71
PinguParticleHolder::update ()
73
// update all contained particles
74
for (std::vector<PinguParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
76
// skip dead particles
80
float tmp_x_add = 0.0f;
81
float tmp_y_add = 0.0f;
84
it->velocity.y += WorldObj::get_world()->get_gravity();
86
if (it->velocity.y > 0)
88
for (tmp_y_add = it->velocity.y; tmp_y_add >= 1.0; --tmp_y_add)
90
if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
92
it->velocity.y *= -y_collision_decrease;
93
tmp_y_add = -tmp_y_add;
99
it->pos.y += tmp_y_add;
103
for (tmp_y_add = it->velocity.y; tmp_y_add <= -1.0; ++tmp_y_add)
105
if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
107
it->velocity.y *= -y_collision_decrease;
108
tmp_y_add = -tmp_y_add;
114
it->pos.y += tmp_y_add;
118
if (it->velocity.x > 0)
120
for (tmp_x_add = it->velocity.x; tmp_x_add >= 1.0; --tmp_x_add)
122
if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
124
it->velocity.x *= -x_collision_decrease;
125
tmp_x_add = -tmp_x_add;
131
it->pos.x += tmp_x_add;
135
for (tmp_x_add = it->velocity.x; tmp_x_add <= -1.0; ++tmp_x_add)
137
if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
139
it->velocity.x *= -x_collision_decrease;
140
tmp_x_add = -tmp_x_add;
146
it->pos.x += tmp_x_add;
155
PinguParticleHolder::draw (SceneContext& gc)
157
for (std::vector<PinguParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
159
// skip dead particles
163
gc.color().draw(surface, it->pos);
167
} // namespace Particles