XmlWorldMapFile.cpp 6.9 KB

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