00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "lisp_reader.hxx"
00021 #include "zoom_graphic_context.hxx"
00022 #include "particle.hxx"
00023 #include "construo_error.hxx"
00024 #include "world.hxx"
00025 #include "particle_factory.hxx"
00026
00027 ParticleFactory::ParticleFactory (World* w)
00028 : world (w), particle_id_count(0)
00029 {
00030 }
00031
00032 ParticleFactory::ParticleFactory (World* w, lisp_object_t* cursor)
00033 : world (w),particle_id_count (0)
00034 {
00035 while(!lisp_nil_p(cursor))
00036 {
00037 lisp_object_t* obj = lisp_car(cursor);
00038 Vector2d pos;
00039 Vector2d velocity;
00040 float mass = 1.0f/10.0f;
00041 bool fixed = false;
00042 int id = -1;
00043
00044 obj = lisp_cdr(obj);
00045
00046 LispReader reader(obj);
00047 reader.read_vector ("pos", &pos);
00048 reader.read_vector ("velocity", &velocity);
00049 reader.read_float ("mass", &mass);
00050 reader.read_bool ("fixed", &fixed);
00051 reader.read_int ("id", &id);
00052
00053 switch (world->file_version)
00054 {
00055 case 0:
00056 case 1:
00057 case 2:
00058 mass = 1.0f/10.0f;
00059 break;
00060 }
00061
00062 if (id >= particle_id_count)
00063 particle_id_count = id + 1;
00064
00065 particles.push_back(new Particle (id, pos, velocity, mass, fixed));
00066
00067 cursor = lisp_cdr (cursor);
00068 }
00069 }
00070
00071 ParticleFactory::ParticleFactory (World* w, const ParticleFactory& pmgr)
00072 : world (w)
00073 {
00074 particle_id_count = pmgr.particle_id_count;
00075 for (CParticleIter i = pmgr.particles.begin (); i != pmgr.particles.end (); ++i)
00076 particles.push_back(new Particle(**i));
00077 }
00078
00079 ParticleFactory&
00080 ParticleFactory::operator= (const ParticleFactory& pmgr)
00081 {
00082 ConstruoAssert (0, "Don't use this");
00083 for (CParticleIter i = pmgr.particles.begin ();
00084 i != pmgr.particles.end ();
00085 ++i)
00086 {
00087 particles.push_back (new Particle (*(*i)));
00088 }
00089 return *this;
00090 }
00091
00092 Particle*
00093 ParticleFactory::add_particle (const Vector2d& arg_pos, const Vector2d& arg_velocity, float m, bool f)
00094 {
00095 Particle* p = new Particle(particle_id_count++,
00096 arg_pos,
00097 arg_velocity, m, f);
00098 particles.push_back(p);
00099 return p;
00100 }
00101
00102 Particle*
00103 ParticleFactory::add_particle (const Particle& particle)
00104 {
00105 Particle* p = new Particle (particle);
00106 p->id = particle_id_count++,
00107 particles.push_back(p);
00108 return p;
00109 }
00110
00111 void
00112 ParticleFactory::remove_particle (Particle* p)
00113 {
00114
00115 for (ParticleIter i = particles.begin (); i != particles.end (); ++i)
00116 {
00117 if (*i == p)
00118 {
00119 delete *i;
00120 particles.erase(i);
00121 return;
00122 }
00123 }
00124 }
00125
00126 struct particle_obsolete
00127 {
00128 inline bool operator()(Particle* p)
00129 {
00130 return (p->spring_links == 0 && p->velocity.x == 0);
00131 }
00132 };
00133
00134 void
00135 ParticleFactory::update (float delta)
00136 {
00137 for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
00138 (*i)->update(delta);
00139
00140 particles.erase(std::remove_if(particles.begin(), particles.end(), particle_obsolete()),
00141 particles.end());
00142 }
00143
00144 void
00145 ParticleFactory::draw (ZoomGraphicContext* gc)
00146 {
00147 for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
00148 (*i)->draw(gc);
00149 }
00150
00151 Particle*
00152 ParticleFactory::lookup_particle (int id)
00153 {
00154
00155 for (ParticleIter i = particles.begin ();
00156 i != particles.end ();
00157 ++i)
00158 {
00159 if ((*i)->get_id () == id)
00160 return *i;
00161 }
00162 return 0;
00163 }
00164
00165 void
00166 ParticleFactory::clear ()
00167 {
00168 for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
00169 delete *i;
00170 particles.clear ();
00171 }
00172
00173 void
00174 ParticleFactory::write_lisp(FILE* out)
00175 {
00176 fputs(" (particles\n", out);
00177 for (CParticleIter i = particles.begin (); i != particles.end (); ++i)
00178 {
00179 lisp_object_t* obj = (*i)->serialize ();
00180 fputs(" ", out);
00181 lisp_dump (obj, out);
00182 lisp_free(obj);
00183 fputc('\n', out);
00184 }
00185 fputs(" )\n", out);
00186 }
00187
00188