| 195 | } |
| 196 | |
| 197 | void SetAdvancedSpriteSequence(Particle& particle, GAME_OBJECT_ID objectID, ParticleAnimType animationType, float frameRate) |
| 198 | { |
| 199 | // Ensure valid lifespan |
| 200 | if (particle.life <= 0) |
| 201 | { |
| 202 | particle.on = false; |
| 203 | ParticleDynamics[particle.dynamic].On = false; |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | // Calculate particle's age and normalized progress |
| 208 | float particleAge = particle.sLife - particle.life; // Elapsed time since spawn |
| 209 | float normalizedAge = particleAge / particle.sLife; // Progress as a fraction [0.0, 1.0] |
| 210 | |
| 211 | // Retrieve sprite sequence information |
| 212 | //int firstFrame = Objects[objectID].meshIndex; // Starting sprite index |
| 213 | int totalFrames = -Objects[objectID].nmeshes; // Total frames (assuming nmeshes is negative) |
| 214 | if (totalFrames <= 0) |
| 215 | { |
| 216 | particle.SpriteSeqID = objectID; |
| 217 | particle.SpriteID = 0; // Default to the first frame if no valid frames exist |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | particle.SpriteSeqID = objectID; |
| 222 | |
| 223 | // Handle animation modes |
| 224 | switch (animationType) |
| 225 | { |
| 226 | case ParticleAnimType::Loop: // Frames loop sequentially |
| 227 | { |
| 228 | float frameDuration = frameRate > 0 ? 1.0f / frameRate : 1.0f / totalFrames; // Duration per frame |
| 229 | int currentFrame = (int)(particleAge / frameDuration) % totalFrames; // Wrap frames |
| 230 | particle.SpriteID = currentFrame; |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | case ParticleAnimType::OneShot: // Frames play once, then freeze on the last frame |
| 235 | { |
| 236 | float totalDuration = frameRate > 0 ? totalFrames / frameRate : particle.sLife; |
| 237 | int currentFrame = (int)(particleAge / (totalDuration / totalFrames)); |
| 238 | if (currentFrame >= totalFrames) |
| 239 | currentFrame = totalFrames - 1; // Clamp to the last frame |
| 240 | particle.SpriteID = currentFrame; |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | case ParticleAnimType::BackAndForth: // Frames go forward and then backward |
| 245 | { |
| 246 | float frameDuration = frameRate > 0 ? 1.0f / frameRate : 1.0f / totalFrames; |
| 247 | int totalFrameSteps = totalFrames * 2 - 2; // Forward and backward frames (avoiding double-count of last frame) |
| 248 | int step = (int)(particleAge / frameDuration) % totalFrameSteps; |
| 249 | int currentFrame = step < totalFrames ? step : totalFrames - (step - totalFrames) - 1; |
| 250 | particle.SpriteID = currentFrame; |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | case ParticleAnimType::LifetimeSpread: // Distribute all frames evenly over lifetime |