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