Otclient  14/8/2020
particlemanager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "particlemanager.h"
25 #include <framework/otml/otml.h>
26 
28 
29 bool ParticleManager::importParticle(std::string file)
30 {
31  try {
32  file = g_resources.guessFilePath(file, "otps");
33 
35  for(const OTMLNodePtr& node : doc->children()) {
36  if(node->tag() == "Effect") {
38  particleEffectType->load(node);
39  m_effectsTypes[particleEffectType->getName()] = particleEffectType;
40  }
41  else if(node->tag() == "Particle") {
42  ParticleTypePtr particleType = ParticleTypePtr(new ParticleType);
43  particleType->load(node);
44  m_particleTypes[particleType->getName()] = particleType;
45  }
46  }
47  return true;
48  } catch(stdext::exception& e) {
49  g_logger.error(stdext::format("could not load particles file %s: %s", file, e.what()));
50  return false;
51  }
52 }
53 
55 {
56  try {
58  particleEffect->load(m_effectsTypes[name]);
59  m_effects.push_back(particleEffect);
60  return particleEffect;
61  } catch(stdext::exception& e) {
62  g_logger.error(stdext::format("failed to create effect '%s': %s", name, e.what()));
63  return nullptr;
64  }
65 }
66 
68 {
69  m_effects.clear();
70  m_effectsTypes.clear();
71  m_particleTypes.clear();
72 }
73 
75 {
76  for(auto it = m_effects.begin(); it != m_effects.end();) {
77  const ParticleEffectPtr& particleEffect = *it;
78 
79  if(particleEffect->hasFinished()) {
80  it = m_effects.erase(it);
81  } else {
82  particleEffect->update();
83  ++it;
84  }
85  }
86 }
ParticleEffectTypePtr
stdext::shared_object_ptr< ParticleEffectType > ParticleEffectTypePtr
Definition: declarations.h:63
otml.h
ParticleType::load
void load(const OTMLNodePtr &node)
Definition: particletype.cpp:48
ParticleEffectType
Definition: particleeffect.h:31
OTMLDocument::parse
static OTMLDocumentPtr parse(const std::string &fileName)
Parse OTML from a file.
Definition: otmldocument.cpp:36
ParticleManager::createEffect
ParticleEffectPtr createEffect(const std::string &name)
Definition: particlemanager.cpp:54
ParticleTypePtr
stdext::shared_object_ptr< ParticleType > ParticleTypePtr
Definition: declarations.h:58
resourcemanager.h
Logger::error
void error(const std::string &what)
Definition: logger.h:54
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
ParticleEffectPtr
stdext::shared_object_ptr< ParticleEffect > ParticleEffectPtr
Definition: declarations.h:62
particlemanager.h
stdext::format
std::string format()
Definition: format.h:82
ParticleEffect
Definition: particleeffect.h:48
ParticleManager::importParticle
bool importParticle(std::string file)
Definition: particlemanager.cpp:29
g_resources
ResourceManager g_resources
Definition: resourcemanager.cpp:32
g_logger
Logger g_logger
Definition: logger.cpp:35
g_particles
ParticleManager g_particles
Definition: particlemanager.cpp:27
ParticleType::getName
std::string getName()
Definition: particletype.h:38
stdext::exception::what
virtual const char * what() const
Definition: exception.h:37
ParticleManager::poll
void poll()
Definition: particlemanager.cpp:74
stdext::shared_object_ptr< OTMLDocument >
ParticleType
Definition: particletype.h:31
ParticleManager::terminate
void terminate()
Definition: particlemanager.cpp:67
ParticleManager
Definition: particlemanager.h:30
ResourceManager::guessFilePath
std::string guessFilePath(const std::string &filename, const std::string &type)
Definition: resourcemanager.cpp:352
stdext::exception
Definition: exception.h:31