~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/particle/particleemitterprop.h

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-09-17 10:35:51 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130917103551-az7p3nz9jgxwqjfn
Tags: 1.3.9.15-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  The ManaPlus Client
 
3
 *  Copyright (C) 2006-2009  The Mana World Development Team
 
4
 *  Copyright (C) 2009-2010  The Mana Developers
 
5
 *  Copyright (C) 2011-2013  The ManaPlus Developers
 
6
 *
 
7
 *  This file is part of The ManaPlus Client.
 
8
 *
 
9
 *  This program is free software; you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation; either version 2 of the License, or
 
12
 *  any later version.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 */
 
22
 
 
23
#ifndef PARTICLE_PARTICLEEMITTERPROP_H
 
24
#define PARTICLE_PARTICLEEMITTERPROP_H
 
25
 
 
26
#include <cmath>
 
27
#include <cstdlib>
 
28
 
 
29
#include "localconsts.h"
 
30
 
 
31
/**
 
32
 * Returns a random numeric value that is larger than or equal min and smaller
 
33
 * than max
 
34
 */
 
35
 
 
36
enum ChangeFunc
 
37
{
 
38
    FUNC_NONE = 0,
 
39
    FUNC_SINE,
 
40
    FUNC_SAW,
 
41
    FUNC_TRIANGLE,
 
42
    FUNC_SQUARE
 
43
};
 
44
 
 
45
template <typename T> struct ParticleEmitterProp final
 
46
{
 
47
    ParticleEmitterProp():
 
48
        minVal(0), maxVal(0), changeFunc(FUNC_NONE),
 
49
        changeAmplitude(0), changePeriod(0), changePhase(0)
 
50
    {
 
51
    }
 
52
 
 
53
    void set(const T min, const T max)
 
54
    {
 
55
        minVal = min;
 
56
        maxVal = max;
 
57
    }
 
58
 
 
59
    void set(const T val)
 
60
    {
 
61
        set(val, val);
 
62
    }
 
63
 
 
64
    void setFunction(ChangeFunc func, T amplitude,
 
65
                     const int period, const int phase)
 
66
    {
 
67
        changeFunc = func;
 
68
        changeAmplitude = amplitude;
 
69
        changePeriod = period;
 
70
        if (!changePeriod)
 
71
            changePeriod = 1;
 
72
        changePhase = phase;
 
73
    }
 
74
 
 
75
    T value(int tick) const
 
76
    {
 
77
        tick += changePhase;
 
78
        T val = static_cast<T>(minVal + (maxVal - minVal)
 
79
            * (rand() / (static_cast<double>(RAND_MAX) + 1)));
 
80
 
 
81
        switch (changeFunc)
 
82
        {
 
83
            case FUNC_SINE:
 
84
                val += static_cast<T>(std::sin(M_PI * 2 * (static_cast<double>(
 
85
                    tick % changePeriod) / static_cast<double>(
 
86
                    changePeriod)))) * changeAmplitude;
 
87
                break;
 
88
            case FUNC_SAW:
 
89
                val += static_cast<T>(changeAmplitude * (static_cast<double>(
 
90
                    tick % changePeriod) / static_cast<double>(
 
91
                    changePeriod))) * 2 - changeAmplitude;
 
92
                break;
 
93
            case FUNC_TRIANGLE:
 
94
                if ((tick % changePeriod) * 2 < changePeriod)
 
95
                {
 
96
                    val += changeAmplitude - static_cast<T>((
 
97
                        tick % changePeriod) / static_cast<double>(
 
98
                        changePeriod)) * changeAmplitude * 4;
 
99
                }
 
100
                else
 
101
                {
 
102
                    val += changeAmplitude * -3 + static_cast<T>((
 
103
                        tick % changePeriod) / static_cast<double>(
 
104
                        changePeriod)) * changeAmplitude * 4;
 
105
                    // I have no idea why this works but it does
 
106
                }
 
107
                break;
 
108
            case FUNC_SQUARE:
 
109
                if ((tick % changePeriod) * 2 < changePeriod)
 
110
                    val += changeAmplitude;
 
111
                else
 
112
                    val -= changeAmplitude;
 
113
                break;
 
114
            case FUNC_NONE:
 
115
            default:
 
116
                // nothing
 
117
                break;
 
118
        }
 
119
 
 
120
        return val;
 
121
    }
 
122
 
 
123
    T minVal;
 
124
    T maxVal;
 
125
 
 
126
    ChangeFunc changeFunc;
 
127
    T changeAmplitude;
 
128
    int changePeriod;
 
129
    int changePhase;
 
130
};
 
131
 
 
132
#endif  // PARTICLE_PARTICLEEMITTERPROP_H