XmlAttackFile.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "core/XmlAttackFile.h"
  16. #include "core/Logger.h"
  17. XmlAttackFile::XmlAttackFile(const Ogre::String& file): XmlFile(file){}
  18. XmlAttackFile::~XmlAttackFile(){}
  19. void XmlAttackFile::ReadParameters(TiXmlNode* node, std::map<std::string, float>& parameters) const{
  20. for (TiXmlNode* child = node->FirstChild(); child != nullptr; child = child->NextSibling()){
  21. if (child->Type() != TiXmlNode::TINYXML_ELEMENT || child->ValueStr() != "Parameter") continue;
  22. parameters[GetString(child, "id")] = GetFloat(child, "value");
  23. }
  24. }
  25. bool XmlAttackFile::LoadAttack(AttackData& attack){
  26. TiXmlNode* root = file_.RootElement();
  27. if (root == nullptr || root->ValueStr() != "Attack"){
  28. LOG_ERROR(file_.ValueStr() + " is not a valid attack file! No <Attack> in root.");
  29. return false;
  30. }
  31. attack.id = GetInt(root, "id");
  32. attack.name = GetString(root, "name");
  33. attack.mp = GetInt(root, "mp");
  34. attack.reflectable = GetInt(root, "reflectable") == 1;
  35. for (TiXmlNode* node = root->FirstChild(); node != nullptr; node = node->NextSibling()){
  36. if (node->Type() != TiXmlNode::TINYXML_ELEMENT) continue;
  37. if (node->ValueStr() == "Damage") ReadParameters(node, attack.damage);
  38. else if (node->ValueStr() == "Target") ReadParameters(node, attack.target);
  39. else if (node->ValueStr() == "Animation") ReadParameters(node, attack.animation);
  40. else if (node->ValueStr() == "Effects"){
  41. for (TiXmlNode* e = node->FirstChild(); e != nullptr; e = e->NextSibling()){
  42. if (e->Type() != TiXmlNode::TINYXML_ELEMENT || e->ValueStr() != "Effect") continue;
  43. AttackData::Effect effect;
  44. effect.id = GetInt(e, "id");
  45. effect.chance = GetFloat(e, "chance");
  46. effect.mode = GetString(e, "mode");
  47. attack.effects.push_back(effect);
  48. }
  49. }
  50. else if (node->ValueStr() == "Elements"){
  51. for (TiXmlNode* e = node->FirstChild(); e != nullptr; e = e->NextSibling()){
  52. if (e->Type() != TiXmlNode::TINYXML_ELEMENT || e->ValueStr() != "Element") continue;
  53. attack.elements.push_back(GetInt(e, "id"));
  54. }
  55. }
  56. }
  57. return true;
  58. }
  59. std::map<int, std::string> XmlAttackFile::LoadIndex(const Ogre::String& file){
  60. std::map<int, std::string> index;
  61. XmlAttackFile xml(file);
  62. TiXmlNode* root = xml.file_.RootElement();
  63. if (root == nullptr || root->ValueStr() != "attacks"){
  64. LOG_ERROR(file + " is not a valid attack index! No <attacks> in root.");
  65. return index;
  66. }
  67. for (TiXmlNode* node = root->FirstChild(); node != nullptr; node = node->NextSibling()){
  68. if (node->Type() != TiXmlNode::TINYXML_ELEMENT || node->ValueStr() != "attack") continue;
  69. index[xml.GetInt(node, "id")] = xml.GetString(node, "file");
  70. }
  71. return index;
  72. }