|
| 1 | +var GetEaseFunction = require('../../tweens/builder/GetEaseFunction'); |
| 2 | +var GetValue = require('../../utils/object/GetValue'); |
| 3 | +var GetFastValue = require('../../utils/object/GetFastValue'); |
| 4 | +var FloatBetween = require('../../math/FloatBetween'); |
| 5 | + |
| 6 | +function hasOnEmit (def) |
| 7 | +{ |
| 8 | + return (!!def.onEmit && typeof def.onEmit === 'function'); |
| 9 | +} |
| 10 | + |
| 11 | +function hasOnUpdate (def) |
| 12 | +{ |
| 13 | + return (!!def.onUpdate && typeof def.onUpdate === 'function'); |
| 14 | +} |
| 15 | + |
| 16 | +function hasGetters (def) |
| 17 | +{ |
| 18 | + return hasOnEmit(def) || hasOnUpdate(def); |
| 19 | +} |
| 20 | + |
| 21 | +var GetValueOp = function (config, key, defaultValue) |
| 22 | +{ |
| 23 | + var propertyValue = GetFastValue(config, key, defaultValue); |
| 24 | + |
| 25 | + // The returned value sets what the property will be at the START of the particles life, on emit |
| 26 | + var particleEmit = function (particle, key, value) { return value; }; |
| 27 | + |
| 28 | + // The returned value updates the property for the duration of the particles life |
| 29 | + var particleUpdate = function (particle, key, t, value) { return value; }; |
| 30 | + |
| 31 | + var t = typeof(propertyValue); |
| 32 | + |
| 33 | + if (t === 'number') |
| 34 | + { |
| 35 | + // Explicit static value: |
| 36 | + |
| 37 | + // x: 400 |
| 38 | + |
| 39 | + particleEmit = function () |
| 40 | + { |
| 41 | + return propertyValue; |
| 42 | + }; |
| 43 | + |
| 44 | + particleUpdate = function () |
| 45 | + { |
| 46 | + return propertyValue; |
| 47 | + }; |
| 48 | + } |
| 49 | + else if (Array.isArray(propertyValue)) |
| 50 | + { |
| 51 | + // Picks a random element from the array: |
| 52 | + |
| 53 | + // x: [ 100, 200, 300, 400 ] |
| 54 | + |
| 55 | + particleEmit = function () |
| 56 | + { |
| 57 | + var randomIndex = Math.floor(Math.random() * propertyValue.length); |
| 58 | + |
| 59 | + return propertyValue[randomIndex]; |
| 60 | + }; |
| 61 | + } |
| 62 | + else if (t === 'function') |
| 63 | + { |
| 64 | + // The same as setting just the onUpdate function and no onEmit |
| 65 | + // Custom callback, must return a value: |
| 66 | + |
| 67 | + /* |
| 68 | + x: function (particle, key, t, value) |
| 69 | + { |
| 70 | + return value + 50; |
| 71 | + } |
| 72 | + */ |
| 73 | + |
| 74 | + particleUpdate = propertyValue; |
| 75 | + } |
| 76 | + else if (t === 'object' && propertyValue.hasOwnProperty('start') && propertyValue.hasOwnProperty('end')) |
| 77 | + { |
| 78 | + var start = propertyValue.start; |
| 79 | + var end = propertyValue.end; |
| 80 | + |
| 81 | + // A random starting value: |
| 82 | + |
| 83 | + // x: { start: 100, end: 400, randomStart: true } |
| 84 | + |
| 85 | + if (propertyValue.hasOwnProperty('randomStart')) |
| 86 | + { |
| 87 | + particleEmit = function () |
| 88 | + { |
| 89 | + return FloatBetween(start, end); |
| 90 | + }; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + particleEmit = function () |
| 95 | + { |
| 96 | + return start; |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + if (propertyValue.hasOwnProperty('steps')) |
| 101 | + { |
| 102 | + // A stepped (per emit) range |
| 103 | + |
| 104 | + // x: { start: 100, end: 400, steps: 64 } |
| 105 | + |
| 106 | + // Increments a value stored in the emitter |
| 107 | + |
| 108 | + particleUpdate = function (particle, key) |
| 109 | + { |
| 110 | + var emitter = particle.emitter; |
| 111 | + |
| 112 | + return emitter.getNext(key); |
| 113 | + }; |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + // An eased range (defaults to Linear if not specified) |
| 118 | + |
| 119 | + // x: { start: 100, end: 400, [ ease: 'Linear' ] } |
| 120 | + |
| 121 | + var ease = (propertyValue.hasOwnProperty('ease')) ? propertyValue.ease : 'Linear'; |
| 122 | + |
| 123 | + var easeFunc = GetEaseFunction(ease); |
| 124 | + |
| 125 | + particleUpdate = function (particle, key, t, value) |
| 126 | + { |
| 127 | + var data = particle.data[key]; |
| 128 | + |
| 129 | + return data.calc * easeFunc(t) + data.min; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + else if (t === 'object' && hasGetters(propertyValue)) |
| 134 | + { |
| 135 | + // Custom onEmit and onUpdate callbacks |
| 136 | + |
| 137 | + /* |
| 138 | + x: { |
| 139 | + // Called at the start of the particles life, when it is being created |
| 140 | + onEmit: function (particle, key, t, value) |
| 141 | + { |
| 142 | + return value; |
| 143 | + }, |
| 144 | +
|
| 145 | + // Called during the particles life on each update |
| 146 | + onUpdate: function (particle, key, t, value) |
| 147 | + { |
| 148 | + return value; |
| 149 | + } |
| 150 | + } |
| 151 | + */ |
| 152 | + |
| 153 | + if (hasOnEmit(propertyValue)) |
| 154 | + { |
| 155 | + particleEmit = propertyValue.onEmit; |
| 156 | + } |
| 157 | + |
| 158 | + if (hasOnUpdate(propertyValue)) |
| 159 | + { |
| 160 | + particleUpdate = propertyValue.onUpdate; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return { |
| 165 | + onEmit: particleEmit, |
| 166 | + onUpdate: particleUpdate |
| 167 | + }; |
| 168 | +}; |
| 169 | + |
| 170 | +module.exports = GetValueOp; |
0 commit comments