| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2015 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "particlefactory.hpp" |
| 17 | |
| 18 | #include "layers.hpp" |
| 19 | #include "renderer.hpp" |
| 20 | |
| 21 | #include <MCAssetManager> |
| 22 | #include <MCGLColor> |
| 23 | #include <MCParticle> |
| 24 | #include <MCPhysicsComponent> |
| 25 | #include <MCRandom> |
| 26 | #include <MCSurfaceParticle> |
| 27 | #include <MCSurfaceParticleRenderer> |
| 28 | #include <MCWorld> |
| 29 | #include <MCWorldRenderer> |
| 30 | |
| 31 | #include <cassert> |
| 32 | |
| 33 | ParticleFactory * ParticleFactory::m_instance = nullptr; |
| 34 | |
| 35 | ParticleFactory::ParticleFactory() |
| 36 | { |
| 37 | assert(!ParticleFactory::m_instance); |
| 38 | ParticleFactory::m_instance = this; |
| 39 | preCreateParticles(); |
| 40 | } |
| 41 | |
| 42 | ParticleFactory & ParticleFactory::instance() |
| 43 | { |
| 44 | assert(ParticleFactory::m_instance); |
| 45 | return *ParticleFactory::m_instance; |
| 46 | } |
| 47 | |
| 48 | void ParticleFactory::preCreateSurfaceParticles(size_t count, std::string typeId, ParticleFactory::ParticleType typeEnum, MCSurfacePtr surface, bool alphaBlend, bool hasShadow) |
| 49 | { |
| 50 | m_particleRenderers[typeEnum] = std::make_shared<MCSurfaceParticleRenderer>(); |
| 51 | |
| 52 | for (size_t i = 0; i < count; i++) |
| 53 | { |
| 54 | auto particle = new MCSurfaceParticle(typeId, surface); |
| 55 | particle->setFreeList(m_freeLists[typeEnum]); |
| 56 | particle->setCustomDeathCondition([](MCParticle & self) { |
| 57 | return self.location().k() <= 0; |
| 58 | }); |
| 59 | particle->setAlphaBlend(alphaBlend); |
| 60 | particle->setHasShadow(hasShadow); |
| 61 | |
| 62 | // Initially push to list of free particles |
| 63 | m_freeLists[typeEnum].push_back(particle); |
| 64 | |
| 65 | // Hack: Store for deletion |
| 66 | m_delete.push_back(std::unique_ptr<MCParticle>(particle)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void ParticleFactory::preCreateParticles() |
| 71 | { |
| 72 | preCreateSurfaceParticles(500, "SMOKE", Smoke, MCAssetManager::surfaceManager().surface("smoke"), true); |
| 73 | |
| 74 | preCreateSurfaceParticles(500, "OFFSMOKE", OffTrackSmoke, MCAssetManager::surfaceManager().surface("smoke"), true); |
| 75 | |
| 76 | preCreateSurfaceParticles(500, "SPARKLE", Sparkle, MCAssetManager::surfaceManager().surface("sparkle"), true); |
| 77 | |
| 78 | preCreateSurfaceParticles(100, "LEAF", Leaf, MCAssetManager::surfaceManager().surface("leaf"), false, true); |
| 79 | |
| 80 | preCreateSurfaceParticles(500, "MUD", Mud, MCAssetManager::surfaceManager().surface("mud"), false, true); |
| 81 | |
| 82 | preCreateSurfaceParticles(500, "ONSKID", OnTrackSkidMark, MCAssetManager::surfaceManager().surface("skid"), true); |
| 83 | |
| 84 | preCreateSurfaceParticles(500, "OFFSKID", OffTrackSkidMark, MCAssetManager::surfaceManager().surface("skid"), true); |
| 85 | } |
| 86 | |
| 87 | void ParticleFactory::doParticle( |
| 88 | ParticleType type, MCVector3dFR location, MCVector3dFR initialVelocity, int angle) |
| 89 | { |
| 90 | switch (type) |
| 91 | { |
| 92 | case DamageSmoke: |
| 93 | doDamageSmoke(location, initialVelocity); |
| 94 | break; |
| 95 | |
| 96 | case SkidSmoke: |
| 97 | doSkidSmoke(location, initialVelocity); |
| 98 | break; |
| 99 | |
| 100 | case Smoke: |
| 101 | doSmoke(location, initialVelocity); |
| 102 | break; |
| 103 | |
| 104 | case OffTrackSmoke: |
| 105 | doOffTrackSmoke(location); |
| 106 | break; |
| 107 | |
| 108 | case OnTrackSkidMark: |
| 109 | doOnTrackSkidMark(location, angle); |
| 110 | break; |
| 111 | |
| 112 | case OffTrackSkidMark: |
| 113 | doOffTrackSkidMark(location, angle); |
| 114 | break; |
| 115 | |
| 116 | case Sparkle: |
| 117 | doSparkle(location, initialVelocity); |
| 118 | break; |
| 119 | |
| 120 | case Mud: |
| 121 | doMud(location, initialVelocity); |
| 122 | break; |
| 123 | |
| 124 | case Leaf: |
| 125 | doLeaf(location, initialVelocity); |
| 126 | break; |
| 127 | |
| 128 | default: |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | MCSurfaceParticle * ParticleFactory::newSurfaceParticle(ParticleType typeEnum) const |
| 134 | { |
| 135 | MCSurfaceParticle * p = nullptr; |
| 136 | auto & freeList = m_freeLists[typeEnum]; |
| 137 | if (freeList.size()) |
| 138 | { |
| 139 | p = dynamic_cast<MCSurfaceParticle *>(freeList.back()); |
| 140 | assert(p); |
| 141 | freeList.pop_back(); |
| 142 | } |
| 143 | |
| 144 | return p; |
| 145 | } |
| 146 | |
| 147 | void ParticleFactory::doDamageSmoke(MCVector3dFR location, MCVector3dFR velocity) const |
| 148 | { |
| 149 | if (const auto smoke = ParticleFactory::newSurfaceParticle(Smoke)) |
| 150 | { |
| 151 | smoke->init(location + MCVector3dF(0, 0, 10), 12, 3000); |
| 152 | smoke->setColor(MCGLColor(0.1f, 0.1f, 0.1f, 0.25f)); |
| 153 | smoke->setAnimationStyle(MCParticle::AnimationStyle::FadeOutAndExpand); |
| 154 | smoke->rotate(MCRandom::getValue() * 360); |
| 155 | smoke->physicsComponent().setVelocity(velocity + MCRandom::randomVector3dPositiveZ() * 0.2f); |
| 156 | smoke->addToWorld(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void ParticleFactory::doSkidSmoke(MCVector3dFR location, MCVector3dFR velocity) const |
| 161 | { |
| 162 | if (const auto smoke = ParticleFactory::newSurfaceParticle(Smoke)) |
| 163 | { |
| 164 | smoke->init(location + MCVector3dF(0, 0, 5), 6, 3000); |
| 165 | smoke->setColor(MCGLColor(1.0f, 1.0f, 1.0f, 0.1f)); |
| 166 | smoke->setAnimationStyle(MCParticle::AnimationStyle::FadeOutAndExpand); |
| 167 | smoke->rotate(MCRandom::getValue() * 360); |
| 168 | smoke->physicsComponent().setVelocity(velocity + MCRandom::randomVector3dPositiveZ() * 0.1f); |
| 169 | smoke->addToWorld(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void ParticleFactory::doSmoke(MCVector3dFR location, MCVector3dFR velocity) const |
| 174 | { |
| 175 | if (const auto smoke = ParticleFactory::newSurfaceParticle(Smoke)) |
| 176 | { |
| 177 | smoke->init(location + MCVector3dF(0, 0, 10), 12, 3000); |
| 178 | smoke->setColor(MCGLColor(0.75f, 0.75f, 0.75f, 0.15f)); |
| 179 | smoke->setAnimationStyle(MCParticle::AnimationStyle::FadeOutAndExpand); |
| 180 | smoke->rotate(MCRandom::getValue() * 360); |
| 181 | smoke->physicsComponent().setVelocity(velocity + MCRandom::randomVector3dPositiveZ() * 0.1f); |
| 182 | smoke->addToWorld(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void ParticleFactory::doOffTrackSmoke(MCVector3dFR location) const |
| 187 | { |
| 188 | if (const auto smoke = ParticleFactory::newSurfaceParticle(OffTrackSmoke)) |
| 189 | { |
| 190 | smoke->init(location + MCVector3dF(0, 0, 10), 15, 3000); |
| 191 | smoke->setColor(MCGLColor(0.6f, 0.4f, 0.0f, 0.25f)); |
| 192 | smoke->setAnimationStyle(MCParticle::AnimationStyle::FadeOut); |
| 193 | smoke->rotate(MCRandom::getValue() * 360); |
| 194 | smoke->physicsComponent().setVelocity(MCRandom::randomVector3dPositiveZ() * 0.1f); |
| 195 | smoke->addToWorld(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void ParticleFactory::doOnTrackSkidMark(MCVector3dFR location, int angle) const |
| 200 | { |
| 201 | if (const auto skidMark = newSurfaceParticle(OnTrackSkidMark)) |
| 202 | { |
| 203 | skidMark->init(location + MCVector3dF(0, 0, 1), 8, 50000); |
| 204 | skidMark->setColor(MCGLColor(0.1f, 0.1f, 0.1f, 0.25f)); |
| 205 | skidMark->setAnimationStyle(MCParticle::AnimationStyle::FadeOut); |
| 206 | skidMark->rotate(angle); |
| 207 | skidMark->physicsComponent().setVelocity(MCVector3dF(0, 0, 0)); |
| 208 | skidMark->physicsComponent().setAcceleration(MCVector3dF(0, 0, 0)); |
| 209 | skidMark->addToWorld(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void ParticleFactory::doOffTrackSkidMark(MCVector3dFR location, int angle) const |
| 214 | { |
| 215 | if (const auto skidMark = newSurfaceParticle(OffTrackSkidMark)) |
| 216 | { |
| 217 | skidMark->init(location + MCVector3dF(0, 0, 1), 8, 50000); |
| 218 | skidMark->setColor(MCGLColor(0.2f, 0.1f, 0.0f, 0.25f)); |
| 219 | skidMark->setAnimationStyle(MCParticle::AnimationStyle::FadeOut); |
| 220 | skidMark->rotate(angle); |
| 221 | skidMark->physicsComponent().setVelocity(MCVector3dF(0, 0, 0)); |
| 222 | skidMark->physicsComponent().setAcceleration(MCVector3dF(0, 0, 0)); |
| 223 | skidMark->addToWorld(); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void ParticleFactory::doMud(MCVector3dFR location, MCVector3dFR velocity) const |
| 228 | { |
| 229 | if (const auto mud = newSurfaceParticle(Mud)) |
| 230 | { |
| 231 | mud->init(location, 12, 3000); |
| 232 | mud->rotate(MCRandom::getValue() * 360); |
| 233 | mud->setColor(MCGLColor(1.0f, 1.0f, 1.0f, 0.5f)); |
| 234 | mud->setAnimationStyle(MCParticle::AnimationStyle::Shrink); |
| 235 | mud->physicsComponent().setVelocity(velocity + MCVector3dF(0, 0, 4.0f)); |
| 236 | mud->physicsComponent().setAcceleration(MCWorld::instance().gravity()); |
| 237 | mud->addToWorld(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void ParticleFactory::doSparkle(MCVector3dFR location, MCVector3dFR velocity) const |
| 242 | { |
| 243 | if (const auto sparkle = ParticleFactory::newSurfaceParticle(Sparkle)) |
| 244 | { |
| 245 | sparkle->init(location, 2 + MCRandom::getValue() * 2, 1500); |
| 246 | sparkle->setColor(MCGLColor(1.0f, 1.0f, 1.0f, 0.33f)); |
| 247 | sparkle->setAnimationStyle(MCParticle::AnimationStyle::Shrink); |
| 248 | sparkle->physicsComponent().setVelocity(velocity * (0.75f + 0.25f * MCRandom::getValue()) + MCVector3dF(0, 0, 4.0f)); |
| 249 | sparkle->physicsComponent().setAcceleration(MCWorld::instance().gravity() * 0.5f); |
| 250 | sparkle->addToWorld(); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void ParticleFactory::doLeaf(MCVector3dFR location, MCVector3dFR velocity) const |
| 255 | { |
| 256 | if (const auto leaf = newSurfaceParticle(Leaf)) |
| 257 | { |
| 258 | leaf->init(location, 5, 3000); |
| 259 | leaf->setAnimationStyle(MCParticle::AnimationStyle::Shrink); |
| 260 | leaf->rotate(MCRandom::getValue() * 360); |
| 261 | leaf->setColor(MCGLColor(0.0, 0.75f, 0.0, 0.75f)); |
| 262 | leaf->physicsComponent().setVelocity(velocity + MCVector3dF(0, 0, 2.0f) + MCRandom::randomVector3d() * 0.5f); |
| 263 | leaf->physicsComponent().setAngularVelocity((MCRandom::getValue() - 0.5f) * 5.0f); |
| 264 | leaf->physicsComponent().setMomentOfInertia(1.0f); |
| 265 | leaf->physicsComponent().setAcceleration(MCVector3dF(0, 0, -2.5f)); |
| 266 | leaf->addToWorld(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | ParticleFactory::~ParticleFactory() |
| 271 | { |
| 272 | ParticleFactory::m_instance = nullptr; |
| 273 | } |