BattleManager.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <algorithm>
  16. #include <iostream>
  17. #include <cmath>
  18. #include <OgreEntity.h>
  19. #include <OgreRoot.h>
  20. #include <OgreViewport.h>
  21. #include "core/AudioManager.h"
  22. #include "core/BattleManager.h"
  23. #include "core/CameraManager.h"
  24. #include "core/DialogsManager.h"
  25. #include "core/Enemy.h"
  26. #include "core/EntityManager.h"
  27. #include "core/ConfigVar.h"
  28. #include "core/InputManager.h"
  29. #include "core/Logger.h"
  30. #include "core/ScriptManager.h"
  31. #include "core/UiManager.h"
  32. #include "core/XmlFormationFile.h"
  33. /**
  34. * Battle manager singleton.
  35. */
  36. template<>BattleManager *Ogre::Singleton<BattleManager>::msSingleton = nullptr;
  37. ConfigVar cv_debug_battle_grid("debug_battle_grid", "Draw debug battle grid", "false");
  38. ConfigVar cv_debug_battle_axis("debug_battle_axis", "Draw debug battle axis", "false");
  39. const float BattleManager::MODEL_SCALE = 0.0015f;
  40. BattleManager::BattleManager(){
  41. LOG_TRIVIAL("BattleManager created.");
  42. scene_node_ = Ogre::Root::getSingleton().getSceneManager("Scene")
  43. ->getRootSceneNode()->createChildSceneNode("BattleManager");
  44. }
  45. BattleManager::~BattleManager(){
  46. Clear();
  47. Ogre::Root::getSingleton().getSceneManager("Scene")->getRootSceneNode()->removeAndDestroyChild(
  48. "BattleManager"
  49. );
  50. LOG_TRIVIAL("BattleManager destroyed.");
  51. }
  52. void BattleManager::Input(const VGears::Event& event){}
  53. void BattleManager::UpdateDebug(){}
  54. void BattleManager::OnResize(){}
  55. void BattleManager::ClearField(){
  56. LOG_TRIVIAL("Called empty method BattleManager::ClearField()");
  57. }
  58. void BattleManager::ClearBattle(){
  59. formation_id_ = -1;
  60. next_formation_id_ = -1;
  61. // TODO location_ = null;
  62. camera_.clear();
  63. initial_camera_ = 0;
  64. layout_ = LAYOUT::NORMAL;
  65. escape_difficulty_ = 0.0f;
  66. arena_battle_ = false;
  67. show_victory_pose_ = true;
  68. show_spoils_ = true;
  69. preemptive_ = true;
  70. money_ = 0;
  71. spoil_.clear();
  72. enemies_.clear();
  73. // TODO party_.clear();
  74. }
  75. void BattleManager::ClearWorld(){
  76. LOG_TRIVIAL("Called empty method BattleManager::ClearWorld()");
  77. }
  78. void BattleManager::StartBattle(const unsigned int id){
  79. if (module_ == Module::BATTLE){
  80. LOG_ERROR("Started battle in BattleManager, but the manager was already in battle mode.");
  81. return;
  82. }
  83. SetBattleModule();
  84. SetFormationId(id);
  85. AudioManager::getSingleton().SetBattleModule();
  86. CameraManager::getSingleton().SetBattleModule();
  87. DialogsManager::getSingleton().SetBattleModule();
  88. EntityManager::getSingleton().SetBattleModule();
  89. EntityManager::getSingleton().GetBackground2D()->Hide();
  90. InputManager::getSingleton().SetBattleModule();
  91. ScriptManager::getSingleton().SetBattleModule();
  92. UiManager::getSingleton().SetBattleModule();
  93. // Load the formation XML file (name is dddd.xml)
  94. std::string filename = std::to_string(formation_id_);
  95. while (filename.length() < 4) filename = "0" + filename;
  96. filename = "./data/gamedata/formation/" + filename + ".xml";
  97. // This sets the data in the battle manager singleton.
  98. XmlFormationFile(filename).LoadFormation();
  99. // TODO: Music
  100. if (camera_.size() == 0)
  101. LOG_ERROR("Unable to start battle camera. No cameras defined in the BattleManager.");
  102. else if (initial_camera_ > camera_.size()){
  103. LOG_ERROR(
  104. "Unable to set initial battle camera. Default camera is set to "
  105. + std::to_string(initial_camera_) + " but only " + std::to_string(camera_.size())
  106. + " cameras are defined in the CameraManager. Defaulting to first camera.");
  107. CameraManager::getSingleton().StartBattleCamera(
  108. camera_.at(0).location, camera_.at(0).orientation
  109. );
  110. }
  111. else
  112. CameraManager::getSingleton().StartBattleCamera(
  113. camera_.at(initial_camera_).location, camera_.at(initial_camera_).orientation
  114. );
  115. LoadParty();
  116. //EndBattle();// TODO: Debug
  117. }
  118. void BattleManager::EndBattle(){
  119. if (module_ != Module::BATTLE){
  120. LOG_ERROR("Ended battle in BattleManager, but the manager was not in battle mode.");
  121. return;
  122. }
  123. SetPreviousModule();
  124. ClearBattle();
  125. CameraManager::getSingleton().EndBattleCamera();
  126. AudioManager::getSingleton().SetPreviousModule();
  127. CameraManager::getSingleton().SetPreviousModule();
  128. DialogsManager::getSingleton().SetPreviousModule();
  129. EntityManager::getSingleton().SetPreviousModule();
  130. EntityManager::getSingleton().GetBackground2D()->Show();
  131. InputManager::getSingleton().SetPreviousModule();
  132. ScriptManager::getSingleton().ClearBattle();
  133. ScriptManager::getSingleton().SetPreviousModule();
  134. UiManager::getSingleton().SetPreviousModule();
  135. }
  136. std::vector<Enemy> BattleManager::GetEnemies() const{return enemies_;}
  137. void BattleManager::AddEnemy(
  138. const unsigned int id, const Ogre::Vector3 pos, const bool front, const bool visible,
  139. const bool targeteable, const bool active, const std::string cover
  140. ){
  141. if (module_ != Module::BATTLE){
  142. LOG_ERROR(
  143. "Tried to add an enemy to the BattleManager, but the manager was not in battle mode."
  144. );
  145. return;
  146. }
  147. Enemy* enemy = new Enemy(id, pos, front, visible, targeteable, active, cover);
  148. enemies_.push_back(*enemy);
  149. EntityManager::getSingleton().AddBattleEntity(
  150. enemy->GetName() + "_" + std::to_string(enemies_.size() - 1),
  151. "enemies/" + enemy->GetModel() + ".mesh", enemy->GetPos(), Ogre::Degree(0),
  152. Ogre::Vector3(MODEL_SCALE, MODEL_SCALE, MODEL_SCALE), id, visible
  153. );
  154. }
  155. void BattleManager::AddCamera(
  156. const unsigned int id, const Ogre::Vector3 pos, const Ogre::Vector3 dir
  157. ){
  158. if (module_ != Module::BATTLE){
  159. LOG_ERROR(
  160. "Tried to add a camera to the BattleManager, but the manager was not in battle mode."
  161. );
  162. return;
  163. }
  164. BattleCamera camera;
  165. camera.id = id;
  166. camera.location = pos;
  167. camera.orientation = dir;
  168. camera_.push_back(camera);
  169. }
  170. void BattleManager::SetLayout(const LAYOUT layout){
  171. if (module_ != Module::BATTLE){
  172. LOG_ERROR("Tried to set a layout the BattleManager, but it's not in battle mode.");
  173. return;
  174. }
  175. if (layout == LAYOUT::UNKNOWN_0 || layout == LAYOUT::UNKNOWN_1) layout_ = LAYOUT::NORMAL;
  176. else layout_ = layout;
  177. }
  178. void BattleManager::SetFormationId(const int id){
  179. if (module_ != Module::BATTLE){
  180. LOG_ERROR("Tried to set formation int the BattleManager, but it's not in battle mode.");
  181. return;
  182. }
  183. if (id < 0) formation_id_ = -1;
  184. else formation_id_ = id;
  185. }
  186. void BattleManager::SetNextFormationId(const int id){
  187. if (module_ != Module::BATTLE){
  188. LOG_ERROR("Tried to set next formation in the BattleManager, but it's not in battle mode.");
  189. return;
  190. }
  191. if (id < 0) next_formation_id_ = -1;
  192. else next_formation_id_ = id;
  193. }
  194. void BattleManager::SetEscapeability(const float difficulty){
  195. if (module_ != Module::BATTLE){
  196. LOG_ERROR("Tried to set escapability in the BattleManager, but it's not in battle mode.");
  197. return;
  198. }
  199. if (difficulty < 0.0f || difficulty > 1.0f) escape_difficulty_ = -1.0f;
  200. else escape_difficulty_ = difficulty;
  201. }
  202. void BattleManager::SetSkipVictoryPose(const bool skip){
  203. if (module_ != Module::BATTLE){
  204. LOG_ERROR("Tried to set pose mode in the BattleManager, but it's not in battle mode.");
  205. return;
  206. }
  207. show_victory_pose_ = !skip;
  208. }
  209. void BattleManager::SetSkipSpoils(const bool skip){
  210. if (module_ != Module::BATTLE){
  211. LOG_ERROR("Tried to set spoils mode in the BattleManager, but it's not in battle mode.");
  212. return;
  213. }
  214. show_spoils_ = !skip;
  215. }
  216. void BattleManager::SetLocation(const int id, const Ogre::String name){
  217. if (module_ != Module::BATTLE){
  218. LOG_ERROR("Tried to set a location in the BattleManager, but it's not in battle mode.");
  219. return;
  220. }
  221. // TODO
  222. }
  223. void BattleManager::SetArenaBattle(const bool arena){
  224. if (module_ != Module::BATTLE){
  225. LOG_ERROR("Tried to set arena info in the BattleManager, but it's not in battle mode.");
  226. return;
  227. }
  228. arena_battle_ = arena;
  229. }
  230. void BattleManager::SetInitialCamera(const unsigned int id){
  231. if (module_ != Module::BATTLE){
  232. LOG_ERROR("Tried to set the camera in the BattleManager, but it's not in battle mode.");
  233. return;
  234. }
  235. initial_camera_ = id;
  236. }
  237. void BattleManager::LoadParty(){
  238. if (module_ != Module::BATTLE){
  239. LOG_ERROR("Tried to load party in the BattleManager, but it's not in battle mode.");
  240. return;
  241. }
  242. std::vector<int> positions {0, 1, 2};
  243. Ogre::Vector3 position = Ogre::Vector3(0, 5, 0);
  244. // Randomize party member positions.
  245. std::random_shuffle(positions.begin(), positions.end());
  246. for (int i = 0; i < positions.size(); i ++){
  247. EntityManager::getSingleton().AddBattleEntity(
  248. "party_" + std::to_string(i),
  249. "enemies/at_grunt.mesh", position, Ogre::Degree(0),
  250. Ogre::Vector3(MODEL_SCALE, MODEL_SCALE, MODEL_SCALE), 100 + i, true
  251. );
  252. // Next position in Y axis
  253. position.x += ((i + 1) * (i % 2 == 0 ? 5 : -5));
  254. }
  255. }
  256. void BattleManager::UpdateField(){}
  257. void BattleManager::UpdateBattle(){}
  258. void BattleManager::UpdateWorld(){}