GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/particle/particleemitterprop.h Lines: 0 39 0.0 %
Date: 2017-11-29 Branches: 0 34 0.0 %

Line Branch Exec Source
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-2017  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
28
#include "enums/particle/particlechangefunc.h"
29
30
#include "localconsts.h"
31
32
template <typename T> struct ParticleEmitterProp final
33
{
34
    ParticleEmitterProp() :
35
        minVal(0),
36
        maxVal(0),
37
        changeFunc(ParticleChangeFunc::FUNC_NONE),
38
        changeAmplitude(0),
39
        changePeriod(0),
40
        changePhase(0)
41
    {
42
    }
43
44
    A_DEFAULT_COPY(ParticleEmitterProp)
45
46
    void set(const T min, const T max)
47
    {
48
        minVal = min;
49
        maxVal = max;
50
    }
51
52
    void set(const T val)
53
    {
54
        set(val, val);
55
    }
56
57
    void setFunction(ParticleChangeFuncT func,
58
                     T amplitude,
59
                     const int period,
60
                     const int phase)
61
    {
62
        changeFunc = func;
63
        changeAmplitude = amplitude;
64
        changePeriod = period;
65
        if (changePeriod == 0)
66
            changePeriod = 1;
67
        changePhase = phase;
68
    }
69
70
    T value(int tick) const
71
    {
72
        tick += changePhase;
73
        T val = static_cast<T>(minVal + (maxVal - minVal)
74
            * (rand() / (static_cast<double>(RAND_MAX) + 1)));
75
76
        switch (changeFunc)
77
        {
78
            case ParticleChangeFunc::FUNC_SINE:
79
                val += static_cast<T>(std::sin(M_PI * 2 * (static_cast<double>(
80
                    tick % changePeriod) / static_cast<double>(
81
                    changePeriod)))) * changeAmplitude;
82
                break;
83
            case ParticleChangeFunc::FUNC_SAW:
84
                val += static_cast<T>(changeAmplitude * (static_cast<double>(
85
                    tick % changePeriod) / static_cast<double>(
86
                    changePeriod))) * 2 - changeAmplitude;
87
                break;
88
            case ParticleChangeFunc::FUNC_TRIANGLE:
89
                if ((tick % changePeriod) * 2 < changePeriod)
90
                {
91
                    val += changeAmplitude - static_cast<T>((
92
                        tick % changePeriod) / static_cast<double>(
93
                        changePeriod)) * changeAmplitude * 4;
94
                }
95
                else
96
                {
97
                    val += changeAmplitude * -3 + static_cast<T>((
98
                        tick % changePeriod) / static_cast<double>(
99
                        changePeriod)) * changeAmplitude * 4;
100
                    // I have no idea why this works but it does
101
                }
102
                break;
103
            case ParticleChangeFunc::FUNC_SQUARE:
104
                if ((tick % changePeriod) * 2 < changePeriod)
105
                    val += changeAmplitude;
106
                else
107
                    val -= changeAmplitude;
108
                break;
109
            case ParticleChangeFunc::FUNC_NONE:
110
            default:
111
                // nothing
112
                break;
113
        }
114
115
        return val;
116
    }
117
118
    T minVal;
119
    T maxVal;
120
121
    ParticleChangeFuncT changeFunc;
122
    T changeAmplitude;
123
    int changePeriod;
124
    int changePhase;
125
};
126
127
#endif  // PARTICLE_PARTICLEEMITTERPROP_H