1
// $Id: pingu_particle_holder.cxx,v 1.8 2003/03/25 00:56:33 grumbel Exp $
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 "../gui/graphic_context.hxx"
21
#include "../math.hxx"
22
#include "../col_map.hxx"
23
#include "../pingus_resource.hxx"
24
#include "../world.hxx"
25
#include "pingu_particle_holder.hxx"
30
const float x_collision_decrease = 0.3f;
31
const float y_collision_decrease = 0.6f;
34
PinguParticleHolder::PinguParticle::PinguParticle (int x, int y)
35
: livetime(50 + (rand() % 75)),
36
use_frame2((rand() % 5) == 0),
38
velocity(Vector(Math::frand() * 7 - 3.5, Math::frand() * -9))
43
PinguParticleHolder::PinguParticleHolder ()
44
: surface(PingusResource::load_surface("Particles/pingu_explo", "pingus"))
50
PinguParticleHolder::add_particle (int x, int y)
54
// fill gaps from dead entries
55
for (std::vector<PinguParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
59
*it = PinguParticle(x, y);
64
// allocate space for all remaining particles at once
65
particles.reserve(particles.size() + 50 - i);
67
// create remaining entries
69
particles.push_back(PinguParticle(x, y));
73
PinguParticleHolder::update ()
75
// update all contained particles
76
for (std::vector<PinguParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
78
// skip dead particles
82
float tmp_x_add = 0.0f;
83
float tmp_y_add = 0.0f;
86
it->velocity.y += WorldObj::get_world()->get_gravity();
88
if (it->velocity.y > 0)
90
for (tmp_y_add = it->velocity.y; tmp_y_add >= 1.0; --tmp_y_add)
92
if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
94
it->velocity.y *= -y_collision_decrease;
95
tmp_y_add = -tmp_y_add;
101
it->pos.y += tmp_y_add;
105
for (tmp_y_add = it->velocity.y; tmp_y_add <= -1.0; ++tmp_y_add)
107
if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
109
it->velocity.y *= -y_collision_decrease;
110
tmp_y_add = -tmp_y_add;
116
it->pos.y += tmp_y_add;
120
if (it->velocity.x > 0)
122
for (tmp_x_add = it->velocity.x; tmp_x_add >= 1.0; --tmp_x_add)
124
if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
126
it->velocity.x *= -x_collision_decrease;
127
tmp_x_add = -tmp_x_add;
133
it->pos.x += tmp_x_add;
137
for (tmp_x_add = it->velocity.x; tmp_x_add <= -1.0; ++tmp_x_add)
139
if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
141
it->velocity.x *= -x_collision_decrease;
142
tmp_x_add = -tmp_x_add;
148
it->pos.x += tmp_x_add;
157
PinguParticleHolder::draw (GraphicContext& gc)
159
for (std::vector<PinguParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
161
// skip dead particles
165
gc.draw(surface, it->pos, it->use_frame2);
169
} // namespace Particles