Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. #include "Bloke.h"
  2. #include "MyUtils.hpp"
  3.  
  4. #include <tyga/ActorWorld.hpp>
  5. #include <tyga\GraphicsCentre.hpp>
  6. #include <tyga/Actor.hpp>
  7. #include <tyga/BasicWorldClock.hpp>
  8. #include <tyga\Math.hpp>
  9.  
  10. Bloke::Bloke()
  11. {
  12. }
  13.  
  14.  
  15. Bloke::~Bloke()
  16. {
  17. }
  18.  
  19. void Bloke::AddBadger(std::shared_ptr<Badger> _badger)
  20. {
  21. badger = _badger;
  22. }
  23.  
  24. std::shared_ptr<tyga::Actor> Bloke::CreateActor(std::string _name)
  25. {
  26. auto world = tyga::ActorWorld::defaultWorld();
  27. auto graphics = tyga::GraphicsCentre::defaultCentre();
  28.  
  29. auto material = graphics->newMaterial();
  30. material->colour = tyga::Vector3(0.f, 0.5f, 0.f);
  31.  
  32. auto mesh = graphics->newMeshWithIdentifier("bloke.tcf/" + _name);
  33.  
  34. auto model = graphics->newModel();
  35. model->material = material;
  36. model->mesh = mesh;
  37.  
  38. auto actor = std::make_shared<tyga::Actor>();
  39. actor->attachComponent(model);
  40. world->addActor(actor);
  41. return actor;
  42. }
  43.  
  44. void Bloke::CreateJoints()
  45. {
  46. doc.load_file("bloke.xml");
  47. std::string query = "/entity/pose[@name=sitting]/actor";
  48. pugi::xpath_node_set actors = doc.select_nodes(query.c_str());
  49. for (pugi::xpath_node n : actors)
  50. {
  51. auto name = n.node().attribute("name").value();
  52. Joint joint;
  53. joint.actor_comp = HashString(name);
  54. joint.actor = CreateActor(name);
  55.  
  56. ActorPose& actor_pose = ActorPose();
  57.  
  58. pugi::xml_text euler = n.node().child("euler").text();
  59. if (euler)
  60. {
  61. std::stringstream ss(euler.as_string());
  62. tyga::Vector3 v;
  63. ss >> v.x >> v.y >> v.z;
  64. if (ss)
  65. {
  66. actor_pose.rotation = v;
  67. }
  68. }
  69.  
  70. auto translate = n.node().child("translate").text();
  71. if (translate)
  72. {
  73. std::stringstream ss(translate.as_string());
  74. tyga::Vector3 v;
  75. ss >> v.x >> v.y >> v.z;
  76. if (ss)
  77. {
  78. actor_pose.position = v;
  79. }
  80. }
  81. }
  82. }
  83.  
  84. void Bloke::LoadPose(std::string _poseName)
  85. {
  86. doc.load_file("bloke.xml");
  87.  
  88. std::string query = "/entity/pose[@name='" + _poseName + "']/actor";
  89. pugi::xpath_node_set actors = doc.select_nodes(query.c_str());
  90.  
  91. int i = 0;
  92. for (pugi::xpath_node n : actors)
  93. {
  94. auto name = n.node().attribute("name").value();
  95.  
  96. ActorPose& actor_pose = ActorPose();
  97.  
  98. pugi::xml_text euler = n.node().child("euler").text();
  99. if (euler)
  100. {
  101. std::stringstream ss(euler.as_string());
  102. tyga::Vector3 v;
  103. ss >> v.x >> v.y >> v.z;
  104. if (ss)
  105. {
  106. actor_pose.rotation = v;
  107. }
  108. }
  109.  
  110. auto translate = n.node().child("translate").text();
  111. if (translate)
  112. {
  113. std::stringstream ss(translate.as_string());
  114. tyga::Vector3 v;
  115. ss >> v.x >> v.y >> v.z;
  116. if (ss)
  117. {
  118. actor_pose.position = v;
  119. }
  120. }
  121.  
  122. joints[i].actor_poses[_poseName] = actor_pose;
  123. i++;
  124. }
  125. }
  126.  
  127. Component Bloke::HashString(std::string _name)
  128. {
  129. if (_name == "pelvis")
  130. {
  131. return _Pelvis;
  132. }
  133. else if (_name == "torso")
  134. {
  135. return _Torso;
  136. }
  137. else if (_name == "helmet")
  138. {
  139. return _Helmet;
  140. }
  141. else if (_name == "arm_left")
  142. {
  143. return _Arm_Left;
  144. }
  145. else if (_name == "forearm_left")
  146. {
  147. return _Forearm_Left;
  148. }
  149. else if (_name == "hand_left")
  150. {
  151. return _Hand_Left;
  152. }
  153. else if (_name == "arm_right")
  154. {
  155. return _Arm_Right;
  156. }
  157. else if (_name == "forearm_right")
  158. {
  159. return _Forearm_Right;
  160. }
  161. else if (_name == "hand_right")
  162. {
  163. return _Hand_Right;
  164. }
  165. else if (_name == "leg_left")
  166. {
  167. return _Leg_Left;
  168. }
  169. else if (_name == "shin_left")
  170. {
  171. return _Shin_Left;
  172. }
  173. else if (_name == "foot_left")
  174. {
  175. return _Foot_Left;
  176. }
  177. else if (_name == "leg_right")
  178. {
  179. return _Leg_Right;
  180. }
  181. else if (_name == "shin_right")
  182. {
  183. return _Shin_Right;
  184. }
  185. else if (_name == "foot_right")
  186. {
  187. return _Foot_Right;
  188. }
  189. else
  190. {
  191. std::cout << "Part " + _name + " not found." << std::endl;
  192. return _None;
  193. }
  194. }
  195.  
  196. void Bloke::actorDidEnterWorld(std::shared_ptr<tyga::Actor> _actor)
  197. {
  198. auto world = tyga::ActorWorld::defaultWorld();
  199. auto graphics = tyga::GraphicsCentre::defaultCentre();
  200.  
  201. actor = std::make_shared<tyga::Actor>();
  202. world->addActor(actor);
  203.  
  204. CreateJoints();
  205. LoadPose("sitting_straight");
  206. }
  207.  
  208. void Bloke::actorWillLeaveWorld(std::shared_ptr<tyga::Actor> _actor)
  209. {
  210. auto world = tyga::ActorWorld::defaultWorld();
  211. }
  212.  
  213. void Bloke::actorClockTick(std::shared_ptr<tyga::Actor> _actor)
  214. {
  215. int current = 0;
  216. for (auto& Joint : joints)
  217. {
  218. ActorPose& poseA = Joint.actor_poses["sitting"];
  219.  
  220. tyga::Matrix4x4 xform = jackL::Pose(poseA.position, poseA.rotation);
  221.  
  222. switch (current)
  223. {
  224. case _Pelvis:
  225. Joint.actor->setTransformation(xform * badger->GetParentActor()->Transformation());
  226. break;
  227. case _Torso:
  228. Joint.actor->setTransformation(xform * joints[_Pelvis].actor->Transformation());
  229. break;
  230. case _Helmet:
  231. Joint.actor->setTransformation(xform * joints[_Torso].actor->Transformation());
  232. break;
  233. case _Arm_Left:
  234. Joint.actor->setTransformation(xform * joints[_Torso].actor->Transformation());
  235. break;
  236. case _Forearm_Left:
  237. Joint.actor->setTransformation(xform * joints[_Arm_Left].actor->Transformation());
  238. break;
  239. case _Hand_Left:
  240. Joint.actor->setTransformation(xform * joints[_Forearm_Left].actor->Transformation());
  241. break;
  242. case _Arm_Right:
  243. Joint.actor->setTransformation(xform * joints[_Torso].actor->Transformation());
  244. break;
  245. case _Forearm_Right:
  246. Joint.actor->setTransformation(xform * joints[_Arm_Right].actor->Transformation());
  247. break;
  248. case _Hand_Right:
  249. Joint.actor->setTransformation(xform * joints[_Forearm_Right].actor->Transformation());
  250. break;
  251. case _Leg_Left:
  252. Joint.actor->setTransformation(xform * joints[_Pelvis].actor->Transformation());
  253. break;
  254. case _Shin_Left:
  255. Joint.actor->setTransformation(xform * joints[_Leg_Left].actor->Transformation());
  256. break;
  257. case _Foot_Left:
  258. Joint.actor->setTransformation(xform * joints[_Shin_Left].actor->Transformation());
  259. break;
  260. case _Leg_Right:
  261. Joint.actor->setTransformation(xform * joints[_Pelvis].actor->Transformation());
  262. break;
  263. case _Shin_Right:
  264. Joint.actor->setTransformation(xform * joints[_Leg_Right].actor->Transformation());
  265. break;
  266. case _Foot_Right:
  267. Joint.actor->setTransformation(xform * joints[_Shin_Right].actor->Transformation());
  268. break;
  269. }
  270. current++;
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement