ManaPlus
particleemitterprop.h
Go to the documentation of this file.
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 
30 
31 #include "localconsts.h"
32 
33 template <typename T> struct ParticleEmitterProp final
34 {
36  minVal(0),
37  maxVal(0),
39  changeAmplitude(0),
40  changePeriod(0),
41  changePhase(0)
42  {
43  }
44 
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 
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  {
80  val += static_cast<T>(std::sin(M_PI * 2 * (static_cast<double>(
81  tick % changePeriod) / static_cast<double>(
83  break;
85  val += static_cast<T>(changeAmplitude * (static_cast<double>(
86  tick % changePeriod) / static_cast<double>(
88  break;
90  if ((tick % changePeriod) * 2 < changePeriod)
91  {
92  val += changeAmplitude - static_cast<T>((
93  tick % changePeriod) / static_cast<double>(
95  }
96  else
97  {
98  val += changeAmplitude * -3 + static_cast<T>((
99  tick % changePeriod) / static_cast<double>(
101  // I have no idea why this works but it does
102  }
103  break;
105  if ((tick % changePeriod) * 2 < changePeriod)
106  val += changeAmplitude;
107  else
108  val -= changeAmplitude;
109  break;
111  default:
112  // nothing
113  break;
114  }
115 
116  return val;
117  }
118 
121 
126 };
127 
128 #endif // PARTICLE_PARTICLEEMITTERPROP_H
#define final
Definition: localconsts.h:46
#define A_DEFAULT_COPY(func)
Definition: localconsts.h:41
ParticleChangeFunc ::T ParticleChangeFuncT
T value(int tick) const
ParticleChangeFuncT changeFunc
void setFunction(ParticleChangeFuncT func, T amplitude, const int period, const int phase)
void set(const T val)
void set(const T min, const T max)