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

« back to all changes in this revision

Viewing changes to src/particles/pingu_particle_holder.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Goulais
  • Date: 2004-08-09 10:26:00 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040809102600-lg2q9lfars0q1p42
Tags: 0.6.0-8
Applied patch from Andreas Jochens (Closes: #263992)

Show diffs side-by-side

added added

removed removed

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