XmlMapFile.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "engine/audio/AudioManager.h"
  16. #include "engine/entity/EntityManager.h"
  17. #include "common/Logger.h"
  18. #include "core/ScriptManager.h"
  19. #include "core/XmlBackground2DFile.h"
  20. #include "core/XmlMapFile.h"
  21. #include "map/VGearsBackground2DFileManager.h"
  22. #include "map/VGearsWalkmeshFileManager.h"
  23. #include "TextHandler.h"
  24. XmlMapFile::XmlMapFile(const Ogre::String& file): XmlFile(file){}
  25. XmlMapFile::~XmlMapFile(){}
  26. void XmlMapFile::LoadMap(){
  27. TiXmlNode* node = file_.RootElement();
  28. if (node == nullptr || node->ValueStr() != "map"){
  29. LOG_ERROR(file_.ValueStr() + " is not a valid fields map file! No <map> in root.");
  30. return;
  31. }
  32. node = node->FirstChild();
  33. while (node != nullptr){
  34. if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "walkmesh"){
  35. Ogre::String name(GetString(node, "file_name"));
  36. if (!name.empty()){
  37. VGears::WalkmeshFileManager::getSingleton();
  38. VGears::WalkmeshFilePtr walkmesh = VGears::WalkmeshFileManager::getSingleton()
  39. .load(name, "FIELDS").staticCast<VGears::WalkmeshFile>();
  40. EntityManager::getSingleton().GetWalkmesh()->load(walkmesh);
  41. }
  42. }
  43. else if (
  44. node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "movement_rotation"
  45. ){
  46. Ogre::Degree rotation = Ogre::Degree(GetFloat(node, "degree", 0));
  47. EntityManager::getSingleton().SetPlayerMoveRotation(Ogre::Radian(rotation));
  48. }
  49. else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "encounters"){
  50. // Random encounter tables. Encounters are enabled until a script disables them.
  51. EntityManager::getSingleton().ClearEncounterTables();
  52. for (TiXmlNode* table = node->FirstChild(); table != nullptr; table = table->NextSibling()){
  53. if (table->Type() != TiXmlNode::TINYXML_ELEMENT || table->ValueStr() != "table") continue;
  54. std::vector<std::pair<int, int>> battles;
  55. std::vector<std::pair<int, int>> specials;
  56. for (TiXmlNode* entry = table->FirstChild(); entry != nullptr; entry = entry->NextSibling()){
  57. if (entry->Type() != TiXmlNode::TINYXML_ELEMENT) continue;
  58. const std::pair<int, int> encounter(GetInt(entry, "id"), GetInt(entry, "probability"));
  59. if (entry->ValueStr() == "battle") battles.push_back(encounter);
  60. else if (entry->ValueStr() == "special") specials.push_back(encounter);
  61. }
  62. EntityManager::getSingleton().AddEncounterTable(
  63. GetInt(table, "id", 1), GetInt(table, "rate"), battles, specials
  64. );
  65. }
  66. EntityManager::getSingleton().SetRandomEncounters(true);
  67. }
  68. else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "background2d"){
  69. Ogre::String name = GetString(node, "file_name");
  70. if (name != ""){
  71. // TODO Migrate this code to a Resource and use it's group to load the background.
  72. VGears::Background2DFilePtr background
  73. = VGears::Background2DFileManager::getSingleton()
  74. .load(name, "FIELDS").staticCast<VGears::Background2DFile>();
  75. EntityManager::getSingleton().GetBackground2D()->load(background);
  76. }
  77. }
  78. else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "texts"){
  79. TextHandler::getSingleton().LoadFieldText(GetString(node, "file_name"));
  80. }
  81. else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "entity_model"){
  82. Ogre::String name = GetString(node, "name");
  83. if (name == ""){
  84. LOG_ERROR("There is no name specified for <entity_model> tag.");
  85. continue;
  86. }
  87. Ogre::String file_name = GetString(node, "file_name");
  88. if (file_name == ""){
  89. LOG_ERROR("There is no file_name specified for <entity_model> tag.");
  90. continue;
  91. }
  92. Ogre::Vector3 position(GetVector3(node, "position"));
  93. Ogre::Degree direction(Ogre::Degree(GetFloat(node, "direction")));
  94. Ogre::Vector3 scale(GetVector3(node, "scale", Ogre::Vector3::UNIT_SCALE));
  95. Ogre::Quaternion orientation(GetQuaternion(node, "root_orientation"));
  96. int index(GetInt(node, "index"));
  97. EntityManager::getSingleton().AddEntity(
  98. name, file_name, position, direction, scale, orientation, index
  99. );
  100. }
  101. else if (
  102. node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "entity_trigger"
  103. ){
  104. Ogre::String name = GetString(node, "name");
  105. if (name == ""){
  106. LOG_ERROR("There is no name specified for <entity_trigger> tag.");
  107. continue;
  108. }
  109. Ogre::Vector3 point1 = GetVector3(node, "point1", Ogre::Vector3::ZERO);
  110. Ogre::Vector3 point2 = GetVector3(node, "point2", Ogre::Vector3::ZERO);
  111. bool enabled = GetBool(node, "enabled", false);
  112. EntityManager::getSingleton().AddEntityTrigger(name, point1, point2, enabled);
  113. }
  114. else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "entity_point"){
  115. Ogre::String name = GetString(node, "name");
  116. if (name == ""){
  117. LOG_ERROR("There is no name specified for <entity_point> tag.");
  118. continue;
  119. }
  120. Ogre::Vector3 position = GetVector3(node, "position");
  121. float rotation = GetFloat(node, "rotation");
  122. EntityManager::getSingleton().AddEntityPoint(name, position, rotation);
  123. }
  124. else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "entity_script"){
  125. Ogre::String name = GetString(node, "name");
  126. if (name == ""){
  127. LOG_ERROR("There is no name specified for <entity_script> tag.");
  128. continue;
  129. }
  130. EntityManager::getSingleton().AddEntityScript(name);
  131. }
  132. else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "script"){
  133. Ogre::String file_name = GetString(node, "file_name");
  134. if (file_name != "") ScriptManager::getSingleton().RunFile("fields/" + file_name);
  135. }
  136. else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "tracks"){
  137. for (TiXmlNode* track = node->FirstChild(); track; track = track->NextSibling()){
  138. int id = GetInt(track, "id");
  139. int track_id = GetInt(track, "track_id");
  140. AudioManager::getSingleton().AddTrack(id, track_id);
  141. }
  142. }
  143. node = node->NextSibling();
  144. }
  145. }