source: trunk/src/emitter.cpp @ 298

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

Allow more particles, just in case.

  • 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 <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
28    static const int MAX_PARTICLES = 1000;
29
30private:
31    int tiler;
32    float3 gravity;
33    int particles[MAX_PARTICLES];
34    float3 positions[MAX_PARTICLES];
35    float3 velocities[MAX_PARTICLES];
36    int nparticles;
37};
38
39/*
40 * Public Emitter class
41 */
42
43Emitter::Emitter(int tiler, float3 gravity)
44  : data(new EmitterData())
45{
46    data->tiler = tiler;
47    data->gravity = gravity;
48    data->nparticles = 0;
49}
50
51void Emitter::TickGame(float deltams)
52{
53    for (int i = 0; i < data->nparticles; i++)
54    {
55        data->velocities[i] = data->velocities[i] + deltams * data->gravity;
56        data->positions[i] = data->positions[i] + deltams * data->velocities[i];
57        if (data->positions[i].y < -100)
58        {
59            data->particles[i] = data->particles[data->nparticles - 1];
60            data->positions[i] = data->positions[data->nparticles - 1];
61            data->velocities[i] = data->velocities[data->nparticles - 1];
62            data->nparticles--;
63        }
64    }
65
66    Entity::TickGame(deltams);
67}
68
69void Emitter::TickDraw(float deltams)
70{
71    Entity::TickDraw(deltams);
72
73    for (int i = 0; i < data->nparticles; i++)
74        Scene::GetDefault()->AddTile((data->tiler << 16) | data->particles[i],
75                                     data->positions[i].x,
76                                     data->positions[i].y,
77                                     data->positions[i].z, 0);
78}
79
80void Emitter::AddParticle(int id, float3 pos, float3 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
Note: See TracBrowser for help on using the repository browser.