00001 /* 00002 * The Mana World 00003 * Copyright (C) 2006 The Mana World Development Team 00004 * 00005 * This file is part of The Mana World. 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 */ 00021 00022 #include <cmath> 00023 00029 enum ChangeFunc 00030 { 00031 FUNC_NONE, 00032 FUNC_SINE, 00033 FUNC_SAW, 00034 FUNC_TRIANGLE, 00035 FUNC_SQUARE 00036 }; 00037 00038 template <typename T> struct ParticleEmitterProp 00039 { 00040 ParticleEmitterProp(): 00041 changeFunc(FUNC_NONE) 00042 { 00043 } 00044 00045 void set(T min, T max) 00046 { 00047 minVal=min; maxVal=max; 00048 } 00049 00050 void set(T val) 00051 { 00052 set(val, val); 00053 } 00054 00055 void setFunction(ChangeFunc func, T amplitude, int period, int phase) 00056 { 00057 changeFunc = func; 00058 changeAmplitude = amplitude; 00059 changePeriod = period; 00060 changePhase = phase; 00061 } 00062 00063 T value(int tick) 00064 { 00065 tick += changePhase; 00066 T val = (T) (minVal + (maxVal - minVal) * (rand() / ((double) RAND_MAX + 1))); 00067 00068 switch (changeFunc) 00069 { 00070 case FUNC_SINE: 00071 val += (T) std::sin(M_PI * 2 * ((double)(tick%changePeriod) / (double)changePeriod)) * changeAmplitude; 00072 break; 00073 case FUNC_SAW: 00074 val += (T) (changeAmplitude * ((double)(tick%changePeriod) / (double)changePeriod)) * 2 - changeAmplitude; 00075 break; 00076 case FUNC_TRIANGLE: 00077 if ((tick%changePeriod) * 2 < changePeriod) 00078 { 00079 val += changeAmplitude - (T)((tick%changePeriod) / (double)changePeriod) * changeAmplitude * 4; 00080 } else { 00081 val += changeAmplitude * -3 + (T)((tick%changePeriod) / (double)changePeriod) * changeAmplitude * 4; 00082 // I have no idea why this works but it does 00083 } 00084 break; 00085 case FUNC_SQUARE: 00086 if ((tick%changePeriod) * 2 < changePeriod) 00087 { 00088 val += changeAmplitude; 00089 } else { 00090 val -= changeAmplitude; 00091 } 00092 break; 00093 case FUNC_NONE: 00094 default: 00095 //nothing 00096 break; 00097 } 00098 00099 return val; 00100 } 00101 00102 T minVal; 00103 T maxVal; 00104 00105 ChangeFunc changeFunc; 00106 T changeAmplitude; 00107 int changePeriod; 00108 int changePhase; 00109 };