source: trunk/src/emitter.cpp @ 792

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

tiler: ensure AddTile() and BlitTile() accept vec3i arguments.

  • Property svn:keywords set to Id
File size: 2.1 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    TileSet *tileset;
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(TileSet *tileset, vec3 gravity)
46  : data(new EmitterData())
47{
48    data->tileset = tileset;
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->tileset, data->particles[i],
77                                     data->positions[i], 0);
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.