| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- //
- // Copyright (c) 2008-2014 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Context.h"
- #include "FileSystem.h"
- #include "ParticleModel2D.h"
- #include "ResourceCache.h"
- #include "StringUtils.h"
- #include "Sprite2D.h"
- #include "XMLFile.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- static const int srcBlendFuncs[] =
- {
- 1, // GL_ONE
- 1, // GL_ONE
- 0x0306, // GL_DST_COLOR
- 0x0302, // GL_SRC_ALPHA
- 0x0302, // GL_SRC_ALPHA
- 1, // GL_ONE
- 0x0305 // GL_ONE_MINUS_DST_ALPHA
- };
- static const int destBlendFuncs[] =
- {
- 0, // GL_ZERO
- 1, // GL_ONE
- 0, // GL_ZERO
- 0x0303, // GL_ONE_MINUS_SRC_ALPHA
- 1, // GL_ONE
- 0x0303, // GL_ONE_MINUS_SRC_ALPHA
- 0x0304 // GL_DST_ALPHA
- };
- ParticleModel2D::ParticleModel2D(Context* context) :
- Resource(context),
- sourcePosition_(157.97f, 228.41f),// Values from sun.pex
- sourcePositionVariance_(7.0f, 7.0f),
- speed_(260.0f),
- speedVariance_(10.0f),
- particleLifeSpan_(1.000f),
- particleLifespanVariance_(0.700f),
- angle_(0.0f),
- angleVariance_(360.0f),
- gravity_(0.0f, 0.0f),
- radialAcceleration_(-380.0f),
- tangentialAcceleration_(-140.0f),
- radialAccelVariance_(0.0f),
- tangentialAccelVariance_(0.0f),
- startColor_(1.0f, 0.0f, 0.0f, 1.0f),
- startColorVariance_(0.0f, 0.0f, 0.0f, 0.0f),
- finishColor_(1.0f, 1.0f, 0.0f, 1.0f),
- finishColorVariance_(0.0f, 0.0f, 0.0f, 0.0f),
- maxParticles_(600),
- startParticleSize_(60.0f),
- startParticleSizeVariance_(40.0f),
- finishParticleSize_(5.0f),
- FinishParticleSizeVariance_(5.0f),
- duration_(-1.0f),
- emitterType_(EMITTER_TYPE_GRAVITY),
- maxRadius_(100.0f),
- maxRadiusVariance_(0.0f),
- minRadius_(0.0f),
- rotatePerSecond_(0.0f),
- rotatePerSecondVariance_(0.0f),
- blendMode_(BLEND_ALPHA),
- rotationStart_(0.0f),
- rotationStartVariance_(0.0f),
- rotationEnd_(0.0f),
- rotationEndVariance_(0.0f)
- {
- }
- ParticleModel2D::~ParticleModel2D()
- {
- }
- void ParticleModel2D::RegisterObject(Context* context)
- {
- context->RegisterFactory<ParticleModel2D>();
- }
- bool ParticleModel2D::Load(Deserializer& source)
- {
- XMLFile xmlFile(context_);
- if (!xmlFile.Load(source))
- return false;
- XMLElement rootElem = xmlFile.GetRoot("particleEmitterConfig");
- if (!rootElem)
- return false;
- String texture = rootElem.GetChild("texture").GetAttribute("name");
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- sprite_= cache->GetResource<Sprite2D>(GetParentPath(GetName()) + texture);
- if (!sprite_)
- return false;
- sourcePosition_ = ReadVector2(rootElem.GetChild("sourcePosition"));
- sourcePositionVariance_ = ReadVector2(rootElem.GetChild("sourcePositionVariance"));
- speed_ = rootElem.GetChild("speed").GetFloat("value");
- speedVariance_ = rootElem.GetChild("speedVariance").GetFloat("value");
-
- particleLifeSpan_ = rootElem.GetChild("particleLifeSpan").GetFloat("value");
- particleLifespanVariance_ = rootElem.GetChild("particleLifespanVariance").GetFloat("value");
-
- angle_ = 360.0f - rootElem.GetChild("angle").GetFloat("value");
- angleVariance_ = rootElem.GetChild("angleVariance").GetFloat("value");
-
- gravity_ = ReadVector2(rootElem.GetChild("gravity"));
- radialAcceleration_ = rootElem.GetChild("radialAcceleration").GetFloat("value");
- tangentialAcceleration_ = -rootElem.GetChild("tangentialAcceleration").GetFloat("value");
-
- radialAccelVariance_ = rootElem.GetChild("radialAccelVariance").GetFloat("value");
- tangentialAccelVariance_ = rootElem.GetChild("tangentialAccelVariance").GetFloat("value");
- startColor_ = ReadColor(rootElem.GetChild("startColor"));
- startColorVariance_ = ReadColor(rootElem.GetChild("startColorVariance"));
-
- finishColor_ = ReadColor(rootElem.GetChild("finishColor"));
- finishColorVariance_ = ReadColor(rootElem.GetChild("finishColorVariance"));
- maxParticles_ = rootElem.GetChild("maxParticles").GetInt("value");
-
- startParticleSize_ = rootElem.GetChild("startParticleSize").GetFloat("value");
- startParticleSizeVariance_ = rootElem.GetChild("startParticleSizeVariance").GetFloat("value");
-
- finishParticleSize_ = rootElem.GetChild("finishParticleSize").GetFloat("value");
- FinishParticleSizeVariance_ = rootElem.GetChild("FinishParticleSizeVariance").GetFloat("value");
-
- duration_ = rootElem.GetChild("duration").GetFloat("value");
- emitterType_ = (EmitterType2D)rootElem.GetChild("emitterType").GetInt("value");
-
- maxRadius_ = rootElem.GetChild("maxRadius").GetFloat("value");
- maxRadiusVariance_ = rootElem.GetChild("maxRadiusVariance").GetFloat("value");
- minRadius_ = rootElem.GetChild("minRadius").GetFloat("value");
- rotatePerSecond_ = -rootElem.GetChild("rotatePerSecond").GetFloat("value");
- rotatePerSecondVariance_ = rootElem.GetChild("rotatePerSecondVariance").GetFloat("value");
-
- int blendFuncSource = rootElem.GetChild("blendFuncSource").GetInt("value");
- int blendFuncDestination = rootElem.GetChild("blendFuncDestination").GetInt("value");
- blendMode_ = BLEND_ALPHA;
- for (int i = 0; i < MAX_BLENDMODES; ++i)
- {
- if (blendFuncSource == srcBlendFuncs[i] && blendFuncDestination == destBlendFuncs[i])
- {
- blendMode_ = (BlendMode)i;
- break;
- }
- }
- rotationStart_ = -rootElem.GetChild("rotationStart").GetFloat("value");
- rotationStartVariance_ = rootElem.GetChild("rotationStartVariance").GetFloat("value");
-
- rotationEnd_ = -rootElem.GetChild("rotationEnd").GetFloat("value");
- rotationEndVariance_ = rootElem.GetChild("rotationEndVariance").GetFloat("value");
- return true;
- }
- bool ParticleModel2D::Save(Serializer& dest) const
- {
- return false;
- }
- void ParticleModel2D::SetSourcePosition(const Vector2& sourcePosition)
- {
- sourcePosition_ = sourcePosition;
- }
- void ParticleModel2D::SetSourcePositionVariance(const Vector2& sourcePositionVariance)
- {
- sourcePositionVariance_ = sourcePositionVariance;
- }
- void ParticleModel2D::SetSpeed(float speed)
- {
- speed_ = speed;
- }
- void ParticleModel2D::SetSpeedVariance(float speedVariance)
- {
- speedVariance_ = speedVariance;
- }
- void ParticleModel2D::SetParticleLifeSpan(float particleLifeSpan)
- {
- particleLifeSpan_ = particleLifeSpan;
- }
- void ParticleModel2D::SetParticleLifespanVariance(float particleLifespanVariance)
- {
- particleLifespanVariance_ = particleLifespanVariance;
- }
- void ParticleModel2D::SetAngle(float angle)
- {
- angle_ = angle;
- }
- void ParticleModel2D::SetAngleVariance(float angleVariance)
- {
- angleVariance_ = angleVariance;
- }
- void ParticleModel2D::SetGravity(const Vector2& gravity)
- {
- gravity_ = gravity;
- }
- void ParticleModel2D::SetRadialAcceleration(float radialAcceleration)
- {
- radialAcceleration_ = radialAcceleration;
- }
- void ParticleModel2D::SetTangentialAcceleration(float tangentialAcceleration)
- {
- tangentialAcceleration_ = tangentialAcceleration;
- }
- void ParticleModel2D::SetRadialAccelVariance(float radialAccelVariance)
- {
- radialAccelVariance_ = radialAccelVariance;
- }
- void ParticleModel2D::SetTangentialAccelVariance(float tangentialAccelVariance)
- {
- tangentialAccelVariance_ = tangentialAccelVariance;
- }
- void ParticleModel2D::SetStartColor(const Color& startColor)
- {
- startColor_ = startColor;
- }
- void ParticleModel2D::SetStartColorVariance(const Color& startColorVariance)
- {
- startColorVariance_ = startColorVariance;
- }
- void ParticleModel2D::SetFinishColor(const Color& finishColor)
- {
- finishColor_ = finishColor;
- }
- void ParticleModel2D::SetFinishColorVariance(const Color& finishColorVariance)
- {
- finishColorVariance_ = finishColorVariance;
- }
- void ParticleModel2D::SetMaxParticles(int maxParticles)
- {
- maxParticles_ = maxParticles;
- }
- void ParticleModel2D::SetStartParticleSize(float startParticleSize)
- {
- startParticleSize_ = startParticleSize;
- }
- void ParticleModel2D::SetStartParticleSizeVariance(float startParticleSizeVariance)
- {
- startParticleSizeVariance_ = startParticleSizeVariance;
- }
- void ParticleModel2D::SetFinishParticleSize(float finishParticleSize)
- {
- finishParticleSize_ = finishParticleSize;
- }
- void ParticleModel2D::SetFinishParticleSizeVariance(float FinishParticleSizeVariance)
- {
- FinishParticleSizeVariance_ = FinishParticleSizeVariance;
- }
- void ParticleModel2D::SetDuration(float duration)
- {
- duration_ = duration;
- }
- void ParticleModel2D::SetEmitterType(EmitterType2D emitterType)
- {
- emitterType_ = emitterType;
- }
- void ParticleModel2D::SetMaxRadius(float maxRadius)
- {
- maxRadius_ = maxRadius;
- }
- void ParticleModel2D::SetMaxRadiusVariance(float maxRadiusVariance)
- {
- maxRadiusVariance_ = maxRadiusVariance;
- }
- void ParticleModel2D::SetMinRadius(float minRadius)
- {
- minRadius_ = minRadius;
- }
- void ParticleModel2D::SetRotatePerSecond(float rotatePerSecond)
- {
- rotatePerSecond_ = rotatePerSecond;
- }
- void ParticleModel2D::SetRotatePerSecondVariance(float rotatePerSecondVariance)
- {
- rotatePerSecondVariance_ = rotatePerSecondVariance;
- }
- void ParticleModel2D::SetBlendMode(BlendMode blendMode)
- {
- blendMode_ = blendMode;
- }
- void ParticleModel2D::SetRotationStart(float rotationStart)
- {
- rotationStart_ = rotationStart;
- }
- void ParticleModel2D::SetRotationStartVariance(float rotationStartVariance)
- {
- rotationStartVariance_ = rotationStartVariance;
- }
- void ParticleModel2D::SetRotationEnd(float rotationEnd)
- {
- rotationEnd_ = rotationEnd;
- }
- void ParticleModel2D::SetRotationEndVariance(float rotationEndVariance)
- {
- rotationEndVariance_ = rotationEndVariance;
- }
- Color ParticleModel2D::ReadColor(const XMLElement& element) const
- {
- Color color;
- color.r_ = element.GetFloat("red");
- color.g_ = element.GetFloat("green");
- color.b_ = element.GetFloat("blue");
- color.a_ = element.GetFloat("alpha");
- return color;
- }
- Vector2 ParticleModel2D::ReadVector2(const XMLElement& element) const
- {
- // Flip y.
- return Vector2(element.GetFloat("x"), -element.GetFloat("y"));
- }
- }
|