source: trunk/src/emitter.cpp @ 2370

Last change on this file since 2370 was 2216, checked in by touky, 7 years ago

New year copyright update.

  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16
17namespace lol
18{
19
20/*
21 * Emitter implementation class
22 */
23
24class EmitterData
25{
26    friend class Emitter;
27
28    static const int MAX_PARTICLES = 1000;
29
30private:
31    TileSet *tileset;
32    vec3 gravity;
33    int particles[MAX_PARTICLES];
34    vec3 positions[MAX_PARTICLES];
35    vec3 velocities[MAX_PARTICLES];
36    int nparticles;
37};
38
39/*
40 * Public Emitter class
41 */
42
43Emitter::Emitter(TileSet *tileset, vec3 gravity)
44  : data(new EmitterData())
45{
46    data->tileset = tileset;
47    data->gravity = gravity;
48    data->nparticles = 0;
49}
50
51void Emitter::TickGame(float seconds)
52{
53    for (int i = 0; i < data->nparticles; i++)
54    {
55        vec3 oldvelocity = data->velocities[i];
56        data->velocities[i] += seconds * data->gravity;
57        data->positions[i] += seconds * 0.5f
58                                      * (oldvelocity + data->velocities[i]);
59        if (data->positions[i].y < -100)
60        {
61            data->particles[i] = data->particles[data->nparticles - 1];
62            data->positions[i] = data->positions[data->nparticles - 1];
63            data->velocities[i] = data->velocities[data->nparticles - 1];
64            data->nparticles--;
65        }
66    }
67
68    Entity::TickGame(seconds);
69}
70
71void Emitter::TickDraw(float seconds)
72{
73    Entity::TickDraw(seconds);
74
75    for (int i = 0; i < data->nparticles; i++)
76        Scene::GetDefault()->AddTile(data->tileset, data->particles[i],
77                                     data->positions[i], 0, vec2(1.0f));
78}
79
80void Emitter::AddParticle(int id, vec3 pos, vec3 vel)
81{
82    if (data->nparticles >= EmitterData::MAX_PARTICLES)
83        return;
84
85    data->particles[data->nparticles] = id;
86    data->positions[data->nparticles] = pos;
87    data->velocities[data->nparticles] = vel;
88    data->nparticles++;
89}
90
91Emitter::~Emitter()
92{
93    delete data;
94}
95
96} /* namespace lol */
97
Note: See TracBrowser for help on using the repository browser.