source: trunk/src/emitter.cpp @ 686

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

Put everything in the "lol" namespace. Better late than never.

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