source: trunk/src/emitter.cpp @ 735

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

core: get rid of now useless <cstdio> includes.

  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
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
19namespace lol
20{
21
22/*
23 * Emitter implementation class
24 */
25
26class EmitterData
27{
28    friend class Emitter;
29
30    static const int MAX_PARTICLES = 1000;
31
32private:
33    int tiler;
34    vec3 gravity;
35    int particles[MAX_PARTICLES];
36    vec3 positions[MAX_PARTICLES];
37    vec3 velocities[MAX_PARTICLES];
38    int nparticles;
39};
40
41/*
42 * Public Emitter class
43 */
44
45Emitter::Emitter(int tiler, vec3 gravity)
46  : data(new EmitterData())
47{
48    data->tiler = tiler;
49    data->gravity = gravity;
50    data->nparticles = 0;
51}
52
53void Emitter::TickGame(float deltams)
54{
55    for (int i = 0; i < data->nparticles; i++)
56    {
57        data->velocities[i] = data->velocities[i] + deltams * data->gravity;
58        data->positions[i] = data->positions[i] + deltams * 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(deltams);
69}
70
71void Emitter::TickDraw(float deltams)
72{
73    Entity::TickDraw(deltams);
74
75    for (int i = 0; i < data->nparticles; i++)
76        Scene::GetDefault()->AddTile((data->tiler << 16) | data->particles[i],
77                                     data->positions[i].x,
78                                     data->positions[i].y,
79                                     data->positions[i].z, 0);
80}
81
82void Emitter::AddParticle(int id, vec3 pos, vec3 vel)
83{
84    if (data->nparticles >= EmitterData::MAX_PARTICLES)
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
93Emitter::~Emitter()
94{
95    delete data;
96}
97
98} /* namespace lol */
99
Note: See TracBrowser for help on using the repository browser.