~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/particles/pingu_particle_holder.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-02-28 19:44:25 UTC
  • mfrom: (4.1.4 hardy)
  • Revision ID: james.westby@ubuntu.com-20080228194425-e8ilohlijv02kgcf
Tags: 0.7.2-2
* Fix FTBFS with gcc-4.3 by adding the missing include in
  src/input/evdev_device.cpp (Closes: #462238):
   + debian/patches/20_fix_FTBFS_with_gcc-4.3.
* Rename former patch so that the filename reflects the order in which
  the patches are applied:
   - debian/patches/data_dir.patch
   + debian/patches/10_fix_data_directory.
* Bump Standards-Version from 3.7.2 to 3.7.3, no changes needed.
* Add a dh_desktop call in the arch-dep part of debian/rules.
* Adjust the “missing-dep-for-interpreter guile” override since lintian
  now lists an alternative for that dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: pingu_particle_holder.cpp 2986 2007-08-17 16:20:09Z grumbel $
 
2
//
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 1999 Ingo Ruhnke <grumbel@gmx.de>
 
5
//
 
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.
 
10
//
 
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.
 
15
//
 
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.
 
19
 
 
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"
 
26
 
 
27
namespace Particles {
 
28
 
 
29
const float x_collision_decrease = 0.3f;
 
30
const float y_collision_decrease = 0.6f;
 
31
 
 
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))
 
37
{
 
38
}
 
39
 
 
40
 
 
41
PinguParticleHolder::PinguParticleHolder ()
 
42
  : surface(Resource::load_sprite("particles/pingu_explo"))
 
43
{
 
44
}
 
45
 
 
46
 
 
47
void
 
48
PinguParticleHolder::add_particle (int x, int y)
 
49
{
 
50
  int i = 0;
 
51
 
 
52
  // fill gaps from dead entries
 
53
  for (std::vector<PinguParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
 
54
    {
 
55
      if (!it->livetime)
 
56
        {
 
57
          *it = PinguParticle(x, y);
 
58
          ++i;
 
59
        }
 
60
    }
 
61
 
 
62
  // allocate space for all remaining particles at once
 
63
  particles.reserve(particles.size() + 50 - i);
 
64
 
 
65
  // create remaining entries
 
66
  for (; i < 50; ++i)
 
67
    particles.push_back(PinguParticle(x, y));
 
68
}
 
69
 
 
70
void
 
71
PinguParticleHolder::update ()
 
72
{
 
73
  // update all contained particles
 
74
  for (std::vector<PinguParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
 
75
    {
 
76
      // skip dead particles
 
77
      if (!it->livetime)
 
78
        continue;
 
79
 
 
80
      float tmp_x_add = 0.0f;
 
81
      float tmp_y_add = 0.0f;
 
82
 
 
83
      // Simulated gravity
 
84
      it->velocity.y += WorldObj::get_world()->get_gravity();
 
85
 
 
86
      if (it->velocity.y > 0)
 
87
        {
 
88
          for (tmp_y_add = it->velocity.y; tmp_y_add >= 1.0; --tmp_y_add)
 
89
            {
 
90
              if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
 
91
                {
 
92
                  it->velocity.y *= -y_collision_decrease;
 
93
                  tmp_y_add = -tmp_y_add;
 
94
                  --it->pos.y;
 
95
                  break;
 
96
                }
 
97
              ++it->pos.y;
 
98
            }
 
99
          it->pos.y += tmp_y_add;
 
100
        }
 
101
      else
 
102
        {
 
103
          for (tmp_y_add = it->velocity.y; tmp_y_add <= -1.0; ++tmp_y_add)
 
104
            {
 
105
              if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
 
106
                      {
 
107
                  it->velocity.y *= -y_collision_decrease;
 
108
                  tmp_y_add = -tmp_y_add;
 
109
                  ++it->pos.y;
 
110
                  break;
 
111
                }
 
112
              --it->pos.y;
 
113
            }
 
114
          it->pos.y += tmp_y_add;
 
115
        }
 
116
 
 
117
 
 
118
      if (it->velocity.x > 0)
 
119
        {
 
120
          for (tmp_x_add = it->velocity.x; tmp_x_add >= 1.0; --tmp_x_add)
 
121
            {
 
122
              if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
 
123
                      {
 
124
                  it->velocity.x *= -x_collision_decrease;
 
125
                  tmp_x_add = -tmp_x_add;
 
126
                  --it->pos.x;
 
127
                  break;
 
128
                }
 
129
              ++it->pos.x;
 
130
            }
 
131
          it->pos.x += tmp_x_add;
 
132
        }
 
133
      else
 
134
        {
 
135
          for (tmp_x_add = it->velocity.x; tmp_x_add <= -1.0; ++tmp_x_add)
 
136
            {
 
137
              if (world->get_colmap()->getpixel(static_cast<int>(it->pos.x), static_cast<int>(it->pos.y)))
 
138
                {
 
139
                  it->velocity.x *= -x_collision_decrease;
 
140
                  tmp_x_add = -tmp_x_add;
 
141
                  ++it->pos.x;
 
142
                  break;
 
143
                }
 
144
              --it->pos.x;
 
145
            }
 
146
          it->pos.x += tmp_x_add;
 
147
        }
 
148
 
 
149
      --it->livetime;
 
150
    }
 
151
}
 
152
 
 
153
 
 
154
void
 
155
PinguParticleHolder::draw (SceneContext& gc)
 
156
{
 
157
  for (std::vector<PinguParticle>::iterator it=particles.begin(); it != particles.end(); ++it)
 
158
    {
 
159
      // skip dead particles
 
160
      if (!it->livetime)
 
161
        continue;
 
162
 
 
163
      gc.color().draw(surface, it->pos);
 
164
    }
 
165
}
 
166
 
 
167
} // namespace Particles
 
168
 
 
169
/* EOF */