1 // $Id: particle_factory.cxx,v 1.13 2003/07/28 09:23:46 grumbel Exp $
3 // Construo - A wire-frame construction game
4 // Copyright (C) 2002 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.
22 #include "lisp_reader.hxx"
23 #include "zoom_graphic_context.hxx"
24 #include "particle.hxx"
25 #include "construo_error.hxx"
27 #include "particle_factory.hxx"
29 ParticleFactory::ParticleFactory (World
* w
)
30 : world (w
), particle_id_count(0)
34 ParticleFactory::ParticleFactory (World
* w
, lisp_object_t
* cursor
)
35 : world (w
),particle_id_count (0)
37 while(!lisp_nil_p(cursor
))
39 lisp_object_t
* obj
= lisp_car(cursor
);
42 float mass
= 1.0f
/10.0f
;
46 obj
= lisp_cdr(obj
); // skip particle 'marker'
48 LispReader
reader(obj
);
49 reader
.read_vector ("pos", &pos
);
50 reader
.read_vector ("velocity", &velocity
);
51 reader
.read_float ("mass", &mass
);
52 reader
.read_bool ("fixed", &fixed
);
53 reader
.read_int ("id", &id
);
55 switch (world
->file_version
)
64 if (id
>= particle_id_count
)
65 particle_id_count
= id
+ 1;
67 particles
.push_back(new Particle (id
, pos
, velocity
, mass
, fixed
));
69 cursor
= lisp_cdr (cursor
);
73 ParticleFactory::ParticleFactory (World
* w
, const ParticleFactory
& pmgr
)
76 particle_id_count
= pmgr
.particle_id_count
;
77 for (CParticleIter i
= pmgr
.particles
.begin (); i
!= pmgr
.particles
.end (); ++i
)
78 particles
.push_back(new Particle(**i
));
82 ParticleFactory::operator= (const ParticleFactory
& pmgr
)
84 ConstruoAssert (0, "Don't use this");
85 for (CParticleIter i
= pmgr
.particles
.begin ();
86 i
!= pmgr
.particles
.end ();
89 particles
.push_back (new Particle (*(*i
)));
95 ParticleFactory::add_particle (const Vector2d
& arg_pos
, const Vector2d
& arg_velocity
, float m
, bool f
)
97 Particle
* p
= new Particle(particle_id_count
++,
100 particles
.push_back(p
);
105 ParticleFactory::add_particle (const Particle
& particle
)
107 Particle
* p
= new Particle (particle
);
108 p
->id
= particle_id_count
++,
109 particles
.push_back(p
);
114 ParticleFactory::remove_particle (Particle
* p
)
116 // Remove the particle itself
117 for (ParticleIter i
= particles
.begin (); i
!= particles
.end (); ++i
)
128 struct particle_obsolete
130 inline bool operator()(Particle
* p
)
132 return (p
->spring_links
== 0
133 && p
->velocity
.x
== 0
134 && fabsf(p
->velocity
.y
) < 0.1f
);
139 ParticleFactory::update (float delta
)
141 for (CParticleIter i
= particles
.begin (); i
!= particles
.end (); ++i
)
144 // FIXME: There is no need to do this on any update, doing it only
145 //once a second should be enough
146 particles
.erase(std::remove_if(particles
.begin(), particles
.end(),
147 particle_obsolete()),
152 ParticleFactory::draw (ZoomGraphicContext
* gc
)
154 for (CParticleIter i
= particles
.begin (); i
!= particles
.end (); ++i
)
159 ParticleFactory::lookup_particle (int id
)
161 // FIXME: Could need optimization
162 for (ParticleIter i
= particles
.begin ();
163 i
!= particles
.end ();
166 if ((*i
)->get_id () == id
)
173 ParticleFactory::clear ()
175 for (CParticleIter i
= particles
.begin (); i
!= particles
.end (); ++i
)
181 ParticleFactory::write_lisp(FILE* out
)
183 fputs(" (particles\n", out
);
184 for (CParticleIter i
= particles
.begin (); i
!= particles
.end (); ++i
)
186 lisp_object_t
* obj
= (*i
)->serialize ();
188 lisp_dump (obj
, out
);