source: trunk/src/emitter.cpp @ 1082

Last change on this file since 1082 was 837, checked in by sam, 9 years ago

emitter: fix wrong velocity value computation in the particles.

  • Property svn:keywords set to Id
File size: 2.2 KB
RevLine 
[288]1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cmath>
16
17#include "core.h"
18
[686]19namespace lol
20{
21
[288]22/*
23 * Emitter implementation class
24 */
25
26class EmitterData
27{
28    friend class Emitter;
29
[298]30    static const int MAX_PARTICLES = 1000;
31
[288]32private:
[790]33    TileSet *tileset;
[664]34    vec3 gravity;
[298]35    int particles[MAX_PARTICLES];
[664]36    vec3 positions[MAX_PARTICLES];
37    vec3 velocities[MAX_PARTICLES];
[288]38    int nparticles;
39};
40
41/*
42 * Public Emitter class
43 */
44
[790]45Emitter::Emitter(TileSet *tileset, vec3 gravity)
[288]46  : data(new EmitterData())
47{
[790]48    data->tileset = tileset;
[288]49    data->gravity = gravity;
50    data->nparticles = 0;
51}
52
53void Emitter::TickGame(float deltams)
54{
[290]55    for (int i = 0; i < data->nparticles; i++)
56    {
[837]57        vec3 oldvelocity = data->velocities[i];
58        data->velocities[i] += deltams * data->gravity;
59        data->positions[i] += deltams * 0.5f
60                                      * (oldvelocity + data->velocities[i]);
[290]61        if (data->positions[i].y < -100)
62        {
63            data->particles[i] = data->particles[data->nparticles - 1];
64            data->positions[i] = data->positions[data->nparticles - 1];
65            data->velocities[i] = data->velocities[data->nparticles - 1];
66            data->nparticles--;
67        }
68    }
69
[288]70    Entity::TickGame(deltams);
71}
72
73void Emitter::TickDraw(float deltams)
74{
75    Entity::TickDraw(deltams);
[290]76
77    for (int i = 0; i < data->nparticles; i++)
[790]78        Scene::GetDefault()->AddTile(data->tileset, data->particles[i],
[792]79                                     data->positions[i], 0);
[288]80}
81
[664]82void Emitter::AddParticle(int id, vec3 pos, vec3 vel)
[290]83{
[298]84    if (data->nparticles >= EmitterData::MAX_PARTICLES)
[290]85        return;
86
87    data->particles[data->nparticles] = id;
88    data->positions[data->nparticles] = pos;
89    data->velocities[data->nparticles] = vel;
90    data->nparticles++;
91}
92
[288]93Emitter::~Emitter()
94{
95    delete data;
96}
97
[686]98} /* namespace lol */
99
Note: See TracBrowser for help on using the repository browser.