source: trunk/src/emitter.cpp @ 290

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

Particle emitter test.

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