Entity.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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 <cmath>
  16. #include <OgreSceneNode.h>
  17. #include <OgreMaterialManager.h>
  18. #include "core/ConfigVar.h"
  19. #include "core/DebugDraw.h"
  20. #include "core/Entity.h"
  21. #include "core/EntityManager.h"
  22. #include "core/Logger.h"
  23. #include "modules/worldmap/WorldmapModule.h"
  24. ConfigVar cv_debug_entity("debug_entity", "Draw entity debug info", "0");
  25. Entity::Entity(const Ogre::String& name, Ogre::SceneNode* node):
  26. name_(name),
  27. scene_node_(node),
  28. height_(1.0f),
  29. //solid_radius_(0.24f),
  30. solid_radius_(0.21f),
  31. solid_(false),
  32. talk_radius_(0.45f),
  33. talkable_(false),
  34. state_(Entity::NONE),
  35. move_auto_speed_(0.7f),
  36. move_walk_speed_(0.7f),
  37. move_run_speed_(0.8f),
  38. move_position_(Ogre::Vector3(0, 0, 0)),
  39. move_entity_(nullptr),
  40. move_stop_distance_(0.0f),
  41. scripted_move_(false),
  42. move_triangle_id_(-1),
  43. move_auto_rotation_(true),
  44. move_auto_animation_(true),
  45. move_animation_walk_("Walk"),
  46. move_animation_run_("Run"),
  47. linear_movement_(LM_UP_TO_DOWN),
  48. linear_start_(0.0f, 0.0f, 0.0f),
  49. linear_end_(0.0f, 0.0f, 0.0f),
  50. linear_dest_triangle_(-1),
  51. jump_start_(0.0f, 0.0f, 0.0f),
  52. jump_end_(0.0f, 0.0f, 0.0f),
  53. jump_seconds_(0.0f),
  54. jump_current_seconds_(0.0f),
  55. jump_dest_triangle_(-1),
  56. offset_position_start_(0.0f, 0.0f, 0.0f),
  57. offset_position_end_(0.0f, 0.0f, 0.0f),
  58. offset_type_(AT_NONE),
  59. offset_seconds_(0.0f),
  60. offset_current_seconds_(0.0f),
  61. turn_direction_(TD_CLOSEST),
  62. turn_direction_start_(0),
  63. turn_direction_end_(0),
  64. turn_entity_(nullptr),
  65. turn_type_(AT_NONE),
  66. turn_seconds_(0.0f),
  67. turn_current_seconds_(0.0f),
  68. animation_speed_(1.0f),
  69. animation_default_("Idle"),
  70. animation_current_name_(""),
  71. animation_auto_play_(true),
  72. animation_state_(Entity::AUTO_ANIMATION),
  73. animation_play_type_(Entity::PLAY_LOOPED),
  74. is_character_(false),
  75. character_id_(-1),
  76. is_line_(false),
  77. debug_hidden_(false)
  78. {
  79. model_root_node_ = scene_node_->createChildSceneNode();
  80. model_node_ = model_root_node_->createChildSceneNode();
  81. direction_ = new EntityDirection();
  82. //direction_->setMaterial("entity/direction");
  83. Ogre::MaterialPtr mat_direction
  84. = Ogre::MaterialManager::getSingleton().getByName("entity/direction");
  85. direction_->setMaterial(mat_direction);
  86. direction_node_ = scene_node_->createChildSceneNode();
  87. direction_node_->attachObject(direction_);
  88. solid_collision_ = new EntityCollision();
  89. //solid_collision_->setMaterial("entity/solid_collision");
  90. Ogre::MaterialPtr mat_solid_collision
  91. = Ogre::MaterialManager::getSingleton().getByName("entity/solid_collision");
  92. solid_collision_->setMaterial(mat_solid_collision);
  93. solid_collision_node_ = scene_node_->createChildSceneNode();
  94. solid_collision_node_->setScale(solid_radius_, solid_radius_, height_);
  95. solid_collision_node_->attachObject(solid_collision_);
  96. talk_collision_ = new EntityCollision();
  97. //talk_collision_->setMaterial("entity/talk_collision");
  98. Ogre::MaterialPtr mat_talk_collision
  99. = Ogre::MaterialManager::getSingleton().getByName("entity/talk_collision");
  100. talk_collision_->setMaterial(mat_talk_collision);
  101. talk_collision_node_ = scene_node_->createChildSceneNode();
  102. talk_collision_node_->setScale(talk_radius_, talk_radius_, height_);
  103. talk_collision_node_->attachObject(talk_collision_);
  104. model_root_node_->setPosition(Ogre::Vector3::ZERO);
  105. model_node_->setPosition(Ogre::Vector3::ZERO);
  106. scene_node_->setPosition(Ogre::Vector3::ZERO);
  107. LOG_TRIVIAL("Entity '" + name_ + "' created.");
  108. }
  109. Entity::~Entity(){
  110. delete solid_collision_;
  111. delete talk_collision_;
  112. scene_node_->removeAndDestroyAllChildren();
  113. LOG_TRIVIAL("Entity '" + name_ + "' destroyed.");
  114. }
  115. void Entity::Update() {
  116. if (move_entity_ != nullptr){
  117. move_position_ = move_entity_->GetPosition();
  118. move_stop_distance_ = GetSolidRadius() + move_entity_->GetSolidRadius();
  119. }
  120. if (turn_entity_ != nullptr){
  121. Ogre::Degree angle = GetDirectionToEntity(turn_entity_);
  122. angle = CalculateTurnAngle(turn_direction_start_, angle);
  123. turn_direction_end_ = angle;
  124. }
  125. }
  126. void Entity::SetDebugHidden(const bool hidden){debug_hidden_ = hidden;}
  127. void Entity::UpdateDebug(){
  128. int debug = debug_hidden_ ? 0 : cv_debug_entity.GetI();
  129. direction_node_->setVisible(debug > 0);
  130. solid_collision_node_->setVisible(debug > 0 && solid_);
  131. talk_collision_node_->setVisible(debug > 0 && talkable_);
  132. // debug output
  133. if (debug > 0){
  134. DEBUG_DRAW.SetColour(Ogre::ColourValue::White);
  135. DEBUG_DRAW.SetScreenSpace(true);
  136. DEBUG_DRAW.SetTextAlignment(DEBUG_DRAW.CENTER);
  137. DEBUG_DRAW.SetFadeDistance(40, 50);
  138. Ogre::Vector3 entity_pos = GetPosition();
  139. DEBUG_DRAW.Text(entity_pos, 0, 0, name_);
  140. DEBUG_DRAW.Text(entity_pos, 0, 12, animation_current_name_);
  141. if (debug > 1){
  142. Ogre::String move_state_string[] = {"NONE", "WALKMESH", "LINEAR", "JUMP"};
  143. DEBUG_DRAW.Text(entity_pos, 0, 24, Ogre::StringConverter::toString(entity_pos));
  144. if (EntityManager::getSingleton().IsWorldModule()){
  145. const int triangle_index
  146. = VGears::WorldmapModule::getSingleton().GetTerrainTriangleUnderEntity(
  147. name_.c_str()
  148. );
  149. DEBUG_DRAW.Text(
  150. entity_pos, 0, 36,
  151. "World map triangle: "
  152. + (triangle_index < 0 ? Ogre::String("UNKNOWN")
  153. : Ogre::StringConverter::toString(triangle_index))
  154. );
  155. }
  156. else{
  157. DEBUG_DRAW.Text(
  158. entity_pos, 0, 36,
  159. "Triangle: " + Ogre::StringConverter::toString(move_triangle_id_)
  160. );
  161. }
  162. DEBUG_DRAW.Text(entity_pos, 0, 48, "Move state: " + move_state_string[state_]);
  163. if (EntityManager::getSingleton().IsWorldModule()){
  164. const Ogre::String terrain_name
  165. = VGears::WorldmapModule::getSingleton().GetTerrainTypeUnderEntity(
  166. name_.c_str()
  167. );
  168. DEBUG_DRAW.Text(
  169. entity_pos, 0, 60,
  170. "Terrain: " + (terrain_name.empty() ? Ogre::String("UNKNOWN") : terrain_name)
  171. );
  172. }
  173. switch(state_){
  174. case Entity::WALKMESH:
  175. DEBUG_DRAW.Line3d(entity_pos, move_position_);
  176. break;
  177. case Entity::LINEAR:
  178. DEBUG_DRAW.Text(
  179. linear_start_, 0, 0,
  180. "Start: " + Ogre::StringConverter::toString(linear_start_)
  181. );
  182. DEBUG_DRAW.Text(
  183. linear_end_, 0, 0, "End: " + Ogre::StringConverter::toString(linear_end_)
  184. );
  185. DEBUG_DRAW.Line3d(linear_start_, linear_end_);
  186. break;
  187. case Entity::JUMP:
  188. DEBUG_DRAW.Text(
  189. jump_start_, 0, 0, "Start: " + Ogre::StringConverter::toString(jump_start_)
  190. );
  191. DEBUG_DRAW.Text(
  192. jump_end_, 0, 0, "End: " + Ogre::StringConverter::toString(jump_end_)
  193. );
  194. Ogre::Vector3 distance = jump_end_ - jump_start_;
  195. Ogre::Vector3 pos1, pos2, pos3;
  196. pos1.x = jump_start_.x + distance.x * 0.25f;
  197. pos1.y = jump_start_.y + distance.y * 0.25f;
  198. float current1 = jump_seconds_ / 4;
  199. pos1.z
  200. = current1 * current1 * -13.08f + current1 * ((distance.z)
  201. / jump_seconds_ + jump_seconds_ * 13.08f) + jump_start_.z;
  202. pos2.x = jump_start_.x + distance.x * 0.5f;
  203. pos2.y = jump_start_.y + distance.y * 0.5f;
  204. float current2 = current1 * 2;
  205. pos2.z
  206. = current2 * current2 * -13.08f + current2 * ((distance.z)
  207. / jump_seconds_ + jump_seconds_ * 13.08f) + jump_start_.z;
  208. pos3.x = jump_start_.x + distance.x * 0.75f;
  209. pos3.y = jump_start_.y + distance.y * 0.75f;
  210. float current3 = current1 * 3;
  211. pos3.z
  212. = current3 * current3 * -13.08f + current3 * ((distance.z)
  213. / jump_seconds_ + jump_seconds_ * 13.08f) + jump_start_.z;
  214. DEBUG_DRAW.Line3d(jump_start_, pos1);
  215. DEBUG_DRAW.Line3d(pos1, pos2);
  216. DEBUG_DRAW.Line3d(pos2, pos3);
  217. DEBUG_DRAW.Line3d(pos3, jump_end_);
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. const Ogre::String& Entity::GetName() const{return name_;}
  224. void Entity::SetPosition(const Ogre::Vector3& position){scene_node_->setPosition(position);}
  225. void Entity::ScriptSetPosition(const float x, const float y, const float z){
  226. SetPosition(Ogre::Vector3(x, y, z));
  227. // Make the entity solid, visible and talkable. TODO: Check if neccessary.
  228. SetVisible(true);
  229. SetSolid(true);
  230. SetTalkable(true);
  231. // If set from a script:
  232. // Reset walkmesh triangle to reattach entity to walkmesh again if needed.
  233. move_triangle_id_ = -1;
  234. }
  235. void Entity::ScriptSetPositionXY(const float x, const float y){
  236. SetPosition(Ogre::Vector3(x, y, GetPosition().z));
  237. move_triangle_id_ = -1;
  238. }
  239. void Entity::ScriptMoveBy(const float x, const float y, const float z){
  240. SetPosition(GetPosition() + Ogre::Vector3(x, y, z));
  241. move_triangle_id_ = -1;
  242. }
  243. const Ogre::Vector3 Entity::GetPosition() const{return scene_node_->getPosition();}
  244. std::tuple<float, float, float> Entity::ScriptGetPosition() const{
  245. Ogre::Vector3 position = scene_node_->getPosition();
  246. return std::make_tuple(position.x, position.y, position.z);
  247. }
  248. void Entity::SetOffset(const Ogre::Vector3& position){
  249. assert(model_root_node_);
  250. model_root_node_->setPosition(position);
  251. }
  252. const Ogre::Vector3 Entity::GetOffset() const{
  253. assert(model_root_node_);
  254. return model_root_node_->getPosition();
  255. }
  256. void Entity::SetRotation(const Ogre::Degree& rotation){
  257. assert(model_root_node_);
  258. float angle
  259. = rotation.valueDegrees() - Ogre::Math::Floor(rotation.valueDegrees() / 360.0f) * 360.0f;
  260. if (angle < 0) angle = 360 + angle;
  261. Ogre::Quaternion q;
  262. // On battle module, rotate on Y axis.
  263. Ogre::Vector3 vec = EntityManager::getSingleton().IsBattleModule()
  264. ? Ogre::Vector3::UNIT_Y : Ogre::Vector3::UNIT_Z;
  265. q.FromAngleAxis(Ogre::Radian(Ogre::Degree(angle)), vec);
  266. model_root_node_->setOrientation(q);
  267. direction_node_->setOrientation(q);
  268. }
  269. void Entity::ScriptSetRotation(const float rotation){SetRotation(Ogre::Degree(rotation));}
  270. Ogre::Degree Entity::GetRotation() const{
  271. assert(model_root_node_);
  272. // The rotation is around the Y axis in battle, around Z in the rest of modules (see
  273. // {@see SetRotation}), so the angle is twice the angle of the component of that axis.
  274. const Ogre::Quaternion q = model_root_node_->getOrientation();
  275. const float axis = EntityManager::getSingleton().IsBattleModule() ? q.y : q.z;
  276. float angle = Ogre::Degree(Ogre::Radian(2.0f * std::atan2(axis, q.w))).valueDegrees();
  277. // Same range as set by SetRotation.
  278. angle = angle - Ogre::Math::Floor(angle / 360.0f) * 360.0f;
  279. return Ogre::Degree(angle);
  280. }
  281. float Entity::ScriptGetRotation() const{return GetRotation().valueDegrees();}
  282. void Entity::setScale(const Ogre::Vector3 &scale) {
  283. assert(model_root_node_);
  284. model_root_node_->setScale(scale);
  285. }
  286. void Entity::ScriptSetScale(const float x, const float y, const float z){
  287. setScale(Ogre::Vector3(x, y, z));
  288. }
  289. void Entity::ScriptSetModelRotation(const float x, const float y, const float z){
  290. assert(model_node_);
  291. model_node_->setOrientation(
  292. Ogre::Quaternion(Ogre::Degree(z), Ogre::Vector3::UNIT_Z)
  293. * Ogre::Quaternion(Ogre::Degree(y), Ogre::Vector3::UNIT_Y)
  294. * Ogre::Quaternion(Ogre::Degree(x), Ogre::Vector3::UNIT_X)
  295. );
  296. }
  297. void Entity::SetIndex(const int index){
  298. assert(model_root_node_);
  299. index_ = index;
  300. }
  301. int Entity::GetIndex(){return index_;}
  302. void Entity::setRootOrientation(const Ogre::Quaternion &root_orientation){
  303. assert(model_node_);
  304. model_node_->setOrientation(root_orientation);
  305. }
  306. float Entity::GetHeight() const{return height_;}
  307. void Entity::SetHeight(const float height){
  308. height_ = height;
  309. solid_collision_node_->setScale(solid_radius_, solid_radius_, height_);
  310. talk_collision_node_->setScale(talk_radius_, talk_radius_, height_);
  311. }
  312. void Entity::SetCollisionDebugOrientation(const Ogre::Quaternion& orientation){
  313. solid_collision_node_->setOrientation(orientation);
  314. talk_collision_node_->setOrientation(orientation);
  315. }
  316. void Entity::SetSolidRadius(const float radius){
  317. solid_radius_ = radius;
  318. solid_collision_node_->setScale(solid_radius_, solid_radius_, height_);
  319. }
  320. float Entity::GetSolidRadius() const{return solid_radius_;}
  321. void Entity::SetSolid(const bool solid){solid_ = solid;}
  322. bool Entity::IsSolid() const{return solid_;}
  323. void Entity::SetTalkRadius(const float radius){
  324. talk_radius_ = radius;
  325. talk_collision_node_->setScale(talk_radius_, talk_radius_, height_);
  326. }
  327. float Entity::GetTalkRadius() const{return talk_radius_;}
  328. void Entity::SetTalkable(const bool talkable){talkable_ = talkable;}
  329. bool Entity::IsTalkable() const{return talkable_;}
  330. void Entity::SetState(const Entity::State state){state_ = state;}
  331. Entity::State Entity::GetState() const{return state_;}
  332. void Entity::SetMoveAutoSpeed(const float speed){move_auto_speed_ = speed;}
  333. float Entity::GetMoveAutoSpeed() const{return move_auto_speed_;}
  334. void Entity::SetMoveWalkSpeed(const float speed){move_walk_speed_ = speed;}
  335. float Entity::GetMoveWalkSpeed() const{return move_walk_speed_;}
  336. void Entity::SetMoveRunSpeed(const float speed){move_run_speed_ = speed;}
  337. float Entity::GetMoveRunSpeed() const{return move_run_speed_;}
  338. void Entity::SetMovePosition(const Ogre::Vector3& target){
  339. move_position_ = target;
  340. scripted_move_ = false;
  341. }
  342. const Ogre::Vector3& Entity::GetMovePosition() const{return move_position_;}
  343. float Entity::GetMoveStopDistance() const{return move_stop_distance_;}
  344. bool Entity::IsScriptedMove() const{return scripted_move_;}
  345. void Entity::SetMoveTriangleId(const int triangle){move_triangle_id_ = triangle;}
  346. int Entity::GetMoveTriangleId() const{return move_triangle_id_;}
  347. void Entity::SetMoveAutoRotation(const bool rotate){move_auto_rotation_ = rotate;}
  348. bool Entity::GetMoveAutoRotation() const{return move_auto_rotation_;}
  349. void Entity::SetMoveAutoAnimation(const bool animate){move_auto_animation_ = animate;}
  350. bool Entity::GetMoveAutoAnimation() const{return move_auto_animation_;}
  351. void Entity::SetMoveAnimations(const char* walk, const char* run){
  352. move_animation_walk_ = walk;
  353. move_animation_run_ = run;
  354. }
  355. const Ogre::String& Entity::GetMoveAnimationWalkName() const{return move_animation_walk_;}
  356. const Ogre::String& Entity::GetMoveAnimationRunName() const{return move_animation_run_;}
  357. void Entity::ScriptMoveToPosition(const float x, const float y){
  358. state_ = Entity::WALKMESH;
  359. move_position_ = Ogre::Vector3(x, y, 0);
  360. move_entity_ = nullptr;
  361. move_stop_distance_ = 0;
  362. scripted_move_ = true;
  363. LOG_TRIVIAL(
  364. "Entity '" + name_ + "' set move to walkmesh position '"
  365. + Ogre::StringConverter::toString(move_position_) + "'."
  366. );
  367. }
  368. void Entity::ScriptMoveToEntity(Entity* entity){
  369. state_ = Entity::WALKMESH;
  370. move_position_ = entity->GetPosition();
  371. move_entity_ = entity;
  372. move_stop_distance_ = GetSolidRadius() + entity->GetSolidRadius();
  373. scripted_move_ = true;
  374. LOG_TRIVIAL("Entity '" + name_ + "' set move to entity '" + entity->GetName() + "'.");
  375. }
  376. int Entity::ScriptMoveSync(){
  377. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  378. sync_.push_back(script);
  379. LOG_TRIVIAL(
  380. "Wait entity '" + name_ + "' move for function '"
  381. + script.function + "' in script entity '" + script.entity + "'."
  382. );
  383. return -1;
  384. }
  385. void Entity::UnsetMove(){
  386. state_ = Entity::NONE;
  387. move_entity_ = nullptr;
  388. move_stop_distance_ = 0;
  389. scripted_move_ = false;
  390. for (const auto &script : sync_) ScriptManager::getSingleton().ContinueScriptExecution(script);
  391. sync_.clear();
  392. }
  393. void Entity::ScriptLinearToPosition(
  394. const float x, const float y, const float z, const LinearMovement movement,
  395. const char* animation, const float orientation, const int dest_triangle
  396. ){
  397. Ogre::Vector3 pos = Ogre::Vector3(x, y, z);
  398. SetLinear(pos, movement, animation, orientation, dest_triangle);
  399. LOG_TRIVIAL(
  400. "Entity '" + name_ + "' set linear move to position '"
  401. + Ogre::StringConverter::toString(pos) + "' with animation '" + animation + "'."
  402. );
  403. }
  404. int Entity::ScriptLinearSync(){
  405. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  406. LOG_TRIVIAL(
  407. "Wait entity '" + name_ + "' linear move for function '"
  408. + script.function + "' in script entity '" + script.entity + "'."
  409. );
  410. sync_.push_back(script);
  411. return -1;
  412. }
  413. void Entity::SetLinear(
  414. const Ogre::Vector3& end, const LinearMovement movement,
  415. const Ogre::String& animation, const float orientation, const int dest_triangle
  416. ){
  417. SetRotation(Ogre::Angle(orientation));
  418. state_ = Entity::LINEAR;
  419. linear_movement_ = movement;
  420. linear_start_ = GetPosition();
  421. linear_end_ = end;
  422. linear_dest_triangle_ = dest_triangle;
  423. // Linear animation.
  424. animation_auto_play_ = false;
  425. PlayAnimation(animation, Entity::AUTO_ANIMATION, Entity::PLAY_LOOPED, 0, -1);
  426. // While moving, the entity is not in a triangle.
  427. move_triangle_id_ = -1;
  428. }
  429. void Entity::UnsetLinear(){
  430. state_ = Entity::NONE;
  431. move_triangle_id_ = linear_dest_triangle_; // Set for NPCs. Playable can end up in two places.
  432. animation_auto_play_ = true;
  433. PlayAnimation(animation_default_, Entity::AUTO_ANIMATION, Entity::PLAY_LOOPED, 0, -1);
  434. for (size_t i = 0; i < sync_.size(); ++ i)
  435. ScriptManager::getSingleton().ContinueScriptExecution(sync_[i]);
  436. sync_.clear();
  437. }
  438. LinearMovement Entity::GetLinearMovement() const{return linear_movement_;}
  439. const Ogre::Vector3& Entity::GetLinearStart() const{return linear_start_;}
  440. const Ogre::Vector3& Entity::GetLinearEnd() const{return linear_end_;}
  441. const int Entity::GetLinearDestTriangle() const{return linear_dest_triangle_;}
  442. void Entity::ScriptJumpToPosition(
  443. const float x, const float y, const float z, const float seconds, const int dest_triangle
  444. ){
  445. float new_z = z;
  446. // If z not specified (-1), asume same Z.
  447. if (z < 0) new_z = GetPosition().z;
  448. Ogre::Vector3 jump_to(x, y, new_z);
  449. SetJump(jump_to, seconds, dest_triangle);
  450. LOG_TRIVIAL(
  451. "Entity '" + name_ + "' set jump to position '" + Ogre::StringConverter::toString(jump_to)
  452. + "' in " + Ogre::StringConverter::toString(seconds) + " seconds."
  453. );
  454. }
  455. int Entity::ScriptJumpSync(){
  456. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  457. LOG_TRIVIAL(
  458. "Wait entity '" + name_ + "' jump for function '"
  459. + script.function + "' in script entity '" + script.entity + "'."
  460. );
  461. sync_.push_back(script);
  462. return -1;
  463. }
  464. void Entity::SetJump(const Ogre::Vector3& jump_to, const float seconds, const int dest_triangle){
  465. state_ = Entity::JUMP;
  466. jump_start_ = GetPosition();
  467. jump_end_ = jump_to;
  468. jump_seconds_ = seconds;
  469. jump_current_seconds_ = 0;
  470. jump_was_solid_ = IsSolid();
  471. jump_dest_triangle_ = dest_triangle;
  472. SetSolid(false);
  473. // After moving the entity needs to be reattached to the walkmesh.
  474. move_triangle_id_ = -1;
  475. }
  476. void Entity::UnsetJump(){
  477. SetSolid(jump_was_solid_);
  478. move_triangle_id_ = linear_dest_triangle_;
  479. state_ = Entity::NONE;
  480. for (size_t i = 0; i < sync_.size(); ++ i)
  481. ScriptManager::getSingleton().ContinueScriptExecution(sync_[i]);
  482. sync_.clear();
  483. }
  484. const Ogre::Vector3& Entity::GetJumpStart() const{return jump_start_;}
  485. const Ogre::Vector3& Entity::GetJumpEnd() const{return jump_end_;}
  486. float Entity::GetJumpSeconds() const{return jump_seconds_;}
  487. void Entity::SetJumpCurrentSeconds(const float seconds){jump_current_seconds_ = seconds;}
  488. float Entity::GetJumpCurrentSeconds() const{ return jump_current_seconds_;}
  489. const int Entity::GetJumpDestTriangle() const{return jump_dest_triangle_;}
  490. void Entity::ScriptOffsetToPosition(
  491. const float x, const float y, const float z, const ActionType type, const float seconds
  492. ){
  493. LOG_TRIVIAL(
  494. "Entity '" + name_ + "' set offset to position '"
  495. + Ogre::StringConverter::toString(Ogre::Vector3(x, y, z)) + "'."
  496. );
  497. Ogre::Vector3 position = Ogre::Vector3(x, y, z);
  498. if (type == AT_NONE){
  499. this->SetOffset(position);
  500. return;
  501. }
  502. offset_position_start_ = GetOffset();
  503. offset_position_end_ = position;
  504. offset_type_ = type;
  505. offset_seconds_ = seconds;
  506. offset_current_seconds_ = 0;
  507. }
  508. int Entity::ScriptOffsetSync(){
  509. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  510. LOG_TRIVIAL(
  511. "Wait entity '" + name_ + "' offset for function '"
  512. + script.function + "' in script entity '" + script.entity + "'."
  513. );
  514. offset_sync_.push_back(script);
  515. return -1;
  516. }
  517. void Entity::UnsetOffset(){
  518. offset_type_ = AT_NONE;
  519. for (unsigned int i = 0; i < offset_sync_.size(); ++ i)
  520. ScriptManager::getSingleton().ContinueScriptExecution(offset_sync_[i]);
  521. offset_sync_.clear();
  522. }
  523. const Ogre::Vector3& Entity::GetOffsetPositionStart() const{return offset_position_start_;}
  524. const Ogre::Vector3& Entity::GetOffsetPositionEnd() const{return offset_position_end_;}
  525. ActionType Entity::GetOffsetType() const{return offset_type_;}
  526. float Entity::GetOffsetSeconds() const{return offset_seconds_;}
  527. void Entity::SetOffsetCurrentSeconds(const float seconds){offset_current_seconds_ = seconds;}
  528. float Entity::GetOffsetCurrentSeconds() const{return offset_current_seconds_;}
  529. void Entity::ScriptTurnToDirection(
  530. const float direction, const TurnDirection turn_direction,
  531. const ActionType turn_type, const float seconds
  532. ){
  533. SetTurn(Ogre::Degree(direction), nullptr, turn_direction, turn_type, seconds);
  534. LOG_TRIVIAL(
  535. "Entity '" + name_ + "' turn to angle '" + Ogre::StringConverter::toString(direction) + "'."
  536. );
  537. }
  538. void
  539. Entity::ScriptTurnToEntity(Entity* entity, const TurnDirection turn_direction, const float seconds){
  540. if (entity == nullptr || entity == this){
  541. LOG_ERROR("Turn to entity: Invalid entity pointer (NULL or self).");
  542. return;
  543. }
  544. Ogre::Degree angle = GetDirectionToEntity(entity);
  545. SetTurn(angle, entity, turn_direction, AT_SMOOTH, seconds);
  546. LOG_TRIVIAL("Entity '" + name_ + "' turn to entity '" + entity->GetName() + ".");
  547. }
  548. void Entity::ScriptFaceEntity(Entity* entity){
  549. if (entity == nullptr || entity == this){
  550. LOG_ERROR("Face entity: Invalid entity pointer (NULL or self).");
  551. return;
  552. }
  553. SetRotation(GetDirectionToEntity(entity));
  554. LOG_TRIVIAL("Entity '" + name_ + "' faces entity '" + entity->GetName() + "'.");
  555. }
  556. int Entity::ScriptTurnSync(){
  557. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  558. LOG_TRIVIAL(
  559. "Wait entity '" + name_ + "' turn for function '"
  560. + script.function + "' in script entity '" + script.entity + "'."
  561. );
  562. turn_sync_.push_back(script);
  563. return -1;
  564. }
  565. void Entity::SetTurn(
  566. const Ogre::Degree& direction_to, Entity* entity, const TurnDirection turn_direction,
  567. const ActionType turn_type, const float seconds
  568. ){
  569. if (turn_type == AT_NONE){
  570. SetRotation(direction_to);
  571. return;
  572. }
  573. turn_direction_ = turn_direction;
  574. Ogre::Degree angle_start = GetRotation();
  575. Ogre::Degree angle_end = CalculateTurnAngle(angle_start, direction_to);
  576. turn_entity_ = entity;
  577. turn_direction_start_ = angle_start;
  578. turn_direction_end_ = angle_end;
  579. turn_type_ = turn_type;
  580. turn_seconds_ = seconds;
  581. turn_current_seconds_ = 0;
  582. }
  583. void Entity::UnsetTurn(){
  584. turn_type_ = AT_NONE;
  585. for (unsigned int i = 0; i < turn_sync_.size(); ++ i)
  586. ScriptManager::getSingleton().ContinueScriptExecution(turn_sync_[i]);
  587. turn_sync_.clear();
  588. }
  589. Ogre::Degree Entity::CalculateTurnAngle(const Ogre::Degree& start, const Ogre::Degree& end) const{
  590. Ogre::Degree ret = end;
  591. switch(turn_direction_){
  592. case TD_CLOCKWISE:
  593. if (end <= start) ret = end + Ogre::Degree(360);
  594. break;
  595. case TD_ANTICLOCKWISE:
  596. if (end >= start) ret = end - Ogre::Degree(360);
  597. break;
  598. case TD_CLOSEST:
  599. Ogre::Degree delta = end - start;
  600. delta = (delta < Ogre::Degree(0)) ? -delta : delta;
  601. if (delta > Ogre::Degree(180)){
  602. if (start < end) ret = end - Ogre::Degree(360);
  603. else ret = end + Ogre::Degree(360);
  604. }
  605. break;
  606. }
  607. return ret;
  608. }
  609. Ogre::Degree Entity::GetTurnDirectionStart() const{return turn_direction_start_;}
  610. Ogre::Degree Entity::GetTurnDirectionEnd() const{return turn_direction_end_;}
  611. ActionType Entity::GetTurnType() const{return turn_type_;}
  612. float Entity::GetTurnSeconds() const{return turn_seconds_;}
  613. void Entity::SetTurnCurrentSeconds(const float seconds){turn_current_seconds_ = seconds;}
  614. float Entity::GetTurnCurrentSeconds() const{return turn_current_seconds_;}
  615. void Entity::ScriptSetAnimationSpeed(const float speed){animation_speed_ = speed;}
  616. const Ogre::String& Entity::GetDefaultAnimationName() const{return animation_default_;}
  617. const Ogre::String& Entity::GetCurrentAnimationName() const{return animation_current_name_;}
  618. Entity::AnimationState Entity::GetAnimationState() const{return animation_state_;}
  619. void Entity::ScriptPlayAnimation(const char* name){
  620. PlayAnimation(Ogre::String(name), Entity::REQUESTED_ANIMATION, Entity::PLAY_DEFAULT, 0, -1);
  621. }
  622. void Entity::ScriptPlayAnimationStop(const char* name){
  623. PlayAnimation(Ogre::String(name), Entity::REQUESTED_ANIMATION, Entity::PLAY_ONCE, 0, -1);
  624. }
  625. void Entity::ScriptPlayAnimation(const char* name, const float start, const float end){
  626. PlayAnimation(
  627. Ogre::String(name), Entity::REQUESTED_ANIMATION, Entity::PLAY_DEFAULT, start, end
  628. );
  629. }
  630. void Entity::ScriptPlayAnimationStop(const char* name, const float start, const float end){
  631. PlayAnimation(Ogre::String(name), Entity::REQUESTED_ANIMATION, Entity::PLAY_ONCE, start, end);
  632. }
  633. float Entity::GetAnimationLength(const Ogre::String& animation) const{return 0.0f;}
  634. unsigned int Entity::GetAnimationCount() const{return 0;}
  635. float Entity::ScriptGetAnimationLength(const char* animation) const{
  636. return GetAnimationLength(Ogre::String(animation));
  637. }
  638. void Entity::ScriptSetDefaultAnimation(const char* animation){
  639. animation_default_ = Ogre::String(animation);
  640. }
  641. int Entity::ScriptAnimationSync(){
  642. ScriptId script = ScriptManager::getSingleton().GetCurrentScriptId();
  643. LOG_TRIVIAL(
  644. "Wait entity '" + name_ + "' animation for function '"
  645. + script.function + "' in script entity '" + script.entity + "'."
  646. );
  647. animation_sync_.push_back(script);
  648. return -1;
  649. }
  650. void Entity::SetCharacter(int char_id){
  651. if (char_id >= 0) is_character_ = true;
  652. else is_character_ = false;
  653. character_id_ = char_id;
  654. }
  655. bool Entity::IsCharacter(){return is_character_;}
  656. int Entity::GetCharacterId(){return character_id_;}
  657. bool Entity::IsLine(){return is_line_;}
  658. Ogre::Degree Entity::GetDirectionToEntity(Entity* entity) const{
  659. Ogre::Vector3 current_point = GetPosition();
  660. Ogre::Vector3 direction_point = entity->GetPosition();
  661. Ogre::Vector2 up(0.0f, -1.0f);
  662. Ogre::Vector2 dir(direction_point.x - current_point.x, direction_point.y - current_point.y);
  663. // Angle between vectors
  664. Ogre::Degree angle(Ogre::Radian(acosf(dir.dotProduct(up) / (dir.length() * up.length()))));
  665. return (dir.x < 0) ? Ogre::Degree(360) - angle : angle;
  666. }