Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Bloke.h"
- #include "MyUtils.hpp"
- #include <tyga/ActorWorld.hpp>
- #include <tyga\GraphicsCentre.hpp>
- #include <tyga/Actor.hpp>
- #include <tyga/BasicWorldClock.hpp>
- #include <tyga\Math.hpp>
- Bloke::Bloke()
- {
- }
- Bloke::~Bloke()
- {
- }
- void Bloke::AddBadger(std::shared_ptr<Badger> _badger)
- {
- badger = _badger;
- }
- std::shared_ptr<tyga::Actor> Bloke::CreateActor(std::string _name)
- {
- auto world = tyga::ActorWorld::defaultWorld();
- auto graphics = tyga::GraphicsCentre::defaultCentre();
- auto material = graphics->newMaterial();
- material->colour = tyga::Vector3(0.f, 0.5f, 0.f);
- auto mesh = graphics->newMeshWithIdentifier("bloke.tcf/" + _name);
- auto model = graphics->newModel();
- model->material = material;
- model->mesh = mesh;
- auto actor = std::make_shared<tyga::Actor>();
- actor->attachComponent(model);
- world->addActor(actor);
- return actor;
- }
- void Bloke::CreateJoints()
- {
- doc.load_file("bloke.xml");
- std::string query = "/entity/pose[@name=sitting]/actor";
- pugi::xpath_node_set actors = doc.select_nodes(query.c_str());
- for (pugi::xpath_node n : actors)
- {
- auto name = n.node().attribute("name").value();
- Joint joint;
- joint.actor_comp = HashString(name);
- joint.actor = CreateActor(name);
- ActorPose& actor_pose = ActorPose();
- pugi::xml_text euler = n.node().child("euler").text();
- if (euler)
- {
- std::stringstream ss(euler.as_string());
- tyga::Vector3 v;
- ss >> v.x >> v.y >> v.z;
- if (ss)
- {
- actor_pose.rotation = v;
- }
- }
- auto translate = n.node().child("translate").text();
- if (translate)
- {
- std::stringstream ss(translate.as_string());
- tyga::Vector3 v;
- ss >> v.x >> v.y >> v.z;
- if (ss)
- {
- actor_pose.position = v;
- }
- }
- }
- }
- void Bloke::LoadPose(std::string _poseName)
- {
- doc.load_file("bloke.xml");
- std::string query = "/entity/pose[@name='" + _poseName + "']/actor";
- pugi::xpath_node_set actors = doc.select_nodes(query.c_str());
- int i = 0;
- for (pugi::xpath_node n : actors)
- {
- auto name = n.node().attribute("name").value();
- ActorPose& actor_pose = ActorPose();
- pugi::xml_text euler = n.node().child("euler").text();
- if (euler)
- {
- std::stringstream ss(euler.as_string());
- tyga::Vector3 v;
- ss >> v.x >> v.y >> v.z;
- if (ss)
- {
- actor_pose.rotation = v;
- }
- }
- auto translate = n.node().child("translate").text();
- if (translate)
- {
- std::stringstream ss(translate.as_string());
- tyga::Vector3 v;
- ss >> v.x >> v.y >> v.z;
- if (ss)
- {
- actor_pose.position = v;
- }
- }
- joints[i].actor_poses[_poseName] = actor_pose;
- i++;
- }
- }
- Component Bloke::HashString(std::string _name)
- {
- if (_name == "pelvis")
- {
- return _Pelvis;
- }
- else if (_name == "torso")
- {
- return _Torso;
- }
- else if (_name == "helmet")
- {
- return _Helmet;
- }
- else if (_name == "arm_left")
- {
- return _Arm_Left;
- }
- else if (_name == "forearm_left")
- {
- return _Forearm_Left;
- }
- else if (_name == "hand_left")
- {
- return _Hand_Left;
- }
- else if (_name == "arm_right")
- {
- return _Arm_Right;
- }
- else if (_name == "forearm_right")
- {
- return _Forearm_Right;
- }
- else if (_name == "hand_right")
- {
- return _Hand_Right;
- }
- else if (_name == "leg_left")
- {
- return _Leg_Left;
- }
- else if (_name == "shin_left")
- {
- return _Shin_Left;
- }
- else if (_name == "foot_left")
- {
- return _Foot_Left;
- }
- else if (_name == "leg_right")
- {
- return _Leg_Right;
- }
- else if (_name == "shin_right")
- {
- return _Shin_Right;
- }
- else if (_name == "foot_right")
- {
- return _Foot_Right;
- }
- else
- {
- std::cout << "Part " + _name + " not found." << std::endl;
- return _None;
- }
- }
- void Bloke::actorDidEnterWorld(std::shared_ptr<tyga::Actor> _actor)
- {
- auto world = tyga::ActorWorld::defaultWorld();
- auto graphics = tyga::GraphicsCentre::defaultCentre();
- actor = std::make_shared<tyga::Actor>();
- world->addActor(actor);
- CreateJoints();
- LoadPose("sitting_straight");
- }
- void Bloke::actorWillLeaveWorld(std::shared_ptr<tyga::Actor> _actor)
- {
- auto world = tyga::ActorWorld::defaultWorld();
- }
- void Bloke::actorClockTick(std::shared_ptr<tyga::Actor> _actor)
- {
- int current = 0;
- for (auto& Joint : joints)
- {
- ActorPose& poseA = Joint.actor_poses["sitting"];
- tyga::Matrix4x4 xform = jackL::Pose(poseA.position, poseA.rotation);
- switch (current)
- {
- case _Pelvis:
- Joint.actor->setTransformation(xform * badger->GetParentActor()->Transformation());
- break;
- case _Torso:
- Joint.actor->setTransformation(xform * joints[_Pelvis].actor->Transformation());
- break;
- case _Helmet:
- Joint.actor->setTransformation(xform * joints[_Torso].actor->Transformation());
- break;
- case _Arm_Left:
- Joint.actor->setTransformation(xform * joints[_Torso].actor->Transformation());
- break;
- case _Forearm_Left:
- Joint.actor->setTransformation(xform * joints[_Arm_Left].actor->Transformation());
- break;
- case _Hand_Left:
- Joint.actor->setTransformation(xform * joints[_Forearm_Left].actor->Transformation());
- break;
- case _Arm_Right:
- Joint.actor->setTransformation(xform * joints[_Torso].actor->Transformation());
- break;
- case _Forearm_Right:
- Joint.actor->setTransformation(xform * joints[_Arm_Right].actor->Transformation());
- break;
- case _Hand_Right:
- Joint.actor->setTransformation(xform * joints[_Forearm_Right].actor->Transformation());
- break;
- case _Leg_Left:
- Joint.actor->setTransformation(xform * joints[_Pelvis].actor->Transformation());
- break;
- case _Shin_Left:
- Joint.actor->setTransformation(xform * joints[_Leg_Left].actor->Transformation());
- break;
- case _Foot_Left:
- Joint.actor->setTransformation(xform * joints[_Shin_Left].actor->Transformation());
- break;
- case _Leg_Right:
- Joint.actor->setTransformation(xform * joints[_Pelvis].actor->Transformation());
- break;
- case _Shin_Right:
- Joint.actor->setTransformation(xform * joints[_Leg_Right].actor->Transformation());
- break;
- case _Foot_Right:
- Joint.actor->setTransformation(xform * joints[_Shin_Right].actor->Transformation());
- break;
- }
- current++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement