Advertisement
cgprojectsfx

Untitled

May 8th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.06 KB | None | 0 0
  1. /*
  2.  * ComputerCore.cpp
  3.  *
  4.  *  Created on: Feb 22, 2018
  5.  *      Author: vivienne
  6.  */
  7.  
  8. #include "HangarsClientStd.h"
  9.  
  10. #include "GameAsset/GAFactory.h"
  11.  
  12. #include "AlphaEngine/GameAsset/Components/PhysicsComponent/RigidBodyComponent/RigidBodyComponent.h"
  13.  
  14. // Navigation Module
  15. #include "GameAssets/Components/ComputerCore/ComputerCoreComponent.h"
  16. #include "GameAssets/Components/PropulsionThruster/PropulsionThrusterComponent.h"
  17. #include "GameAssets/Components/NavigationModule/NavigationModuleComponent.h"
  18.  
  19. #include "GameAsset/Components/CharacterComponent/CharacterComponent.h"
  20.  
  21. ComputerCoreComponent::ComputerCoreComponent(Context* context) :
  22.         LogicComponent(context), m_ComputerCoreType(ComputerCore_NULL), m_Name(
  23.                 ""), m_pNavigationModule(nullptr), m_Initialized(false), m_ThrustLevel(
  24.                 0.5f), m_ThrustActive(false), m_pAutomatedVehicle(nullptr), m_MovementLongitude(
  25.                 0.0f), m_MovementLatitude(0.0f), m_MovementPitch(0.0f), m_MovementVertical(
  26.                 0.0f), m_AutomatedVehicleMode(AutomatedVehicleMode_NULL), m_Yaw(
  27.                 0.0f), m_qRotation(Quaternion::IDENTITY), m_pCharacter(nullptr) {
  28.     m_CommandBuffer.clear();
  29.  
  30. }
  31.  
  32. ComputerCoreComponent::~ComputerCoreComponent() {
  33.  
  34. }
  35.  
  36. void ComputerCoreComponent::RegisterObject(Context* context) {
  37.     context->RegisterFactory<ComputerCoreComponent>();
  38. }
  39.  
  40. bool ComputerCoreComponent::LoadXML(const XMLElement& source,
  41.         bool setInstanceDefault) {
  42.     ResourceCache* cache = g_pApp->GetConstantResCache();
  43.  
  44.     // Get Name
  45.     XMLElement node = source.GetChild("UniqueID");
  46.  
  47.     if (node) {
  48.         m_Name = node.GetAttribute("value").CString();
  49.     }
  50.  
  51.     // NEED TO CONVERT
  52.     // Get Computer Core Type
  53.     node = source.GetChild("ComputerCoreType");
  54.  
  55.     if (node) {
  56.         m_ComputerCoreType = (ComputerCoreType) node.GetUInt("value");
  57.     }
  58.  
  59.     // Set Loaded XML to true
  60.     m_bLoadedXML = true;
  61.  
  62.     return true;
  63. }
  64.  
  65. bool ComputerCoreComponent::SaveXML(XMLElement& dest) const {
  66.     XMLElement componentNode = GAFactory::SaveComponentToXML(dest,
  67.             GetTypeName(), node_);
  68.  
  69.     // Create name
  70.     XMLElement childNode = componentNode.CreateChild("UniqueID");
  71.  
  72.     // Set the name
  73.     childNode.SetString("value", String(m_Name.c_str()));
  74.  
  75.     // Set vehicle type
  76.     childNode = componentNode.CreateChild("ComputerCoreType");
  77.  
  78.     // NEED TO CONVERT
  79.     childNode.SetUInt("value", m_ComputerCoreType);
  80.  
  81.     return true;
  82. }
  83.  
  84. // Process per frame
  85. void ComputerCoreComponent::FixedUpdate(float timeStep) {
  86.     // Do nothing--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  87. }
  88.  
  89. // Initialize
  90. void ComputerCoreComponent::Initialize() {
  91.     // Debug
  92.     ALPHAENGINE_LOGINFO("Initialize Called");
  93.  
  94.     // Get the Navigation Module
  95.     m_pNavigationModule = GetComponent<NavigationModuleComponent>();
  96.  
  97.     // Get Automated Vechicle Component
  98.     m_pAutomatedVehicle = GetComponent<AutomatedVehicleComponent>();
  99.  
  100.     // Get attached propulsion thrusters
  101.     PODVector<PropulsionThrusterComponent *> pPropulsionThrusters;
  102.  
  103.     // Get Thrusters
  104.     GetNode()->GetComponents<PropulsionThrusterComponent>(pPropulsionThrusters,
  105.             true);
  106.  
  107.     // Copy thrusters into memory
  108.     if (pPropulsionThrusters.Size() == 0) {
  109.         return;
  110.     } else {
  111.         if (m_pPropulsionThrusters.Size() == 0) {
  112.             // loop thoop each thruster and update
  113.             for (unsigned int i = 0; i < pPropulsionThrusters.Size(); i++) {
  114.                 // copy each thruster
  115.                 m_pPropulsionThrusters.Push(
  116.                         (SharedPtr<PropulsionThrusterComponent> ) pPropulsionThrusters[i]);
  117.             }
  118.  
  119.         }
  120.     }
  121.  
  122.     // Subscribe to event
  123.     SubscribeToEvent(E_POSTUPDATE,
  124.             URHO3D_HANDLER(ComputerCoreComponent, HandlePostUpdate));
  125.  
  126.     // Create a cardinal table
  127.     CreateCardinalTable();
  128.  
  129. }
  130.  
  131. // All Updates
  132. void ComputerCoreComponent::Update(float timeStep) {
  133.     // If command buffer
  134.     if (m_CommandBuffer.size()) {
  135.         ComputerCoreCommand currentCommand = m_CommandBuffer.front();
  136.  
  137.         unsigned int command = 0;
  138.  
  139.         // Output command
  140.         if (currentCommand.commandCode != 0) {
  141.  
  142.             // conver tcommand code to
  143.             command = currentCommand.commandCode;
  144.  
  145.             // erase data
  146.             m_CommandBuffer.erase(m_CommandBuffer.begin());
  147.         }
  148.  
  149.         // Ship Controls
  150.         if (command == ShipControl_Pitch_Increase) {
  151.             m_MovementPitch += .10;
  152.             if (m_MovementPitch > 1.0f)
  153.                 m_MovementPitch = 1.0f;
  154.         }
  155.  
  156.         if (command == ShipControl_Pitch_Decrease) {
  157.             m_MovementPitch -= .10;
  158.             if (m_MovementPitch < -1.0f)
  159.                 m_MovementPitch = -1.0f;
  160.         }
  161.  
  162.         if (command == ShipControl_Longitude_Increase) {
  163.             m_MovementLongitude += .10;
  164.             if (m_MovementLongitude > 1.0f)
  165.                 m_MovementLongitude = 1.0f;
  166.         }
  167.  
  168.         if (command == ShipControl_Longitude_Decrease) {
  169.             m_MovementLongitude -= .10;
  170.             if (m_MovementLongitude < -1.0f)
  171.                 m_MovementLongitude = -1.0f;
  172.         }
  173.  
  174.         if (command == ShipControl_Vertical_Increase) {
  175.             m_MovementVertical += .10;
  176.             if (m_MovementVertical > 1.0f)
  177.                 m_MovementVertical = 1.0f;
  178.         }
  179.  
  180.         if (command == ShipControl_Vertical_Decrease) {
  181.             m_MovementVertical -= .10;
  182.             if (m_MovementVertical < -1.0f)
  183.                 m_MovementVertical = -1.0f;
  184.         }
  185.  
  186.         if (command == ShipControl_Latitude_Increase) {
  187.             m_MovementLatitude += .10;
  188.             if (m_MovementLatitude > 1.0f)
  189.                 m_MovementLatitude = 1.0f;
  190.         }
  191.  
  192.         if (command == ShipControl_Latitude_Decrease) {
  193.             m_MovementLatitude -= .10;
  194.             if (m_MovementLatitude < -1.0f)
  195.                 m_MovementLatitude = -1.0f;
  196.         }
  197.  
  198.         if (command == ShipControl_Longitude_Increase) {
  199.             m_MovementLongitude += .10;
  200.             if (m_MovementLongitude > 1.0f)
  201.                 m_MovementLongitude = 1.0f;
  202.         }
  203.  
  204.         if (command == ShipControl_Longitude_Decrease) {
  205.             m_MovementLongitude -= .10;
  206.             if (m_MovementLongitude < -1.0f)
  207.                 m_MovementLongitude = -1.0f;
  208.         }
  209.  
  210.         if (command == ShipControl_Vertical_Increase) {
  211.             m_MovementVertical += .10;
  212.             if (m_MovementVertical > 1.0f)
  213.                 m_MovementVertical = 1.0f;
  214.         }
  215.  
  216.         if (command == ShipControl_Vertical_Decrease) {
  217.             m_MovementVertical -= .10;
  218.             if (m_MovementVertical < -1.0f)
  219.                 m_MovementVertical = -1.0f;
  220.         }
  221.  
  222.         if (command == ShipControl_Yaw_Decrease) {
  223.             m_Yaw -= .10;
  224.             if (m_Yaw < -1.0f)
  225.                 m_Yaw = -1.0f;
  226.         }
  227.  
  228.         if (command == ShipControl_Yaw_Increase) {
  229.             m_Yaw += .10;
  230.             if (m_Yaw > 1.0f)
  231.                 m_Yaw = 1.0f;
  232.         }
  233.  
  234.         if (command == ShipControl_AutomatedMode) {
  235.             if (m_AutomatedVehicleMode == AutomatedVehicleMode_NULL) {
  236.                 //ALPHAENGINE_LOGINFO("AutomatedVehicleMode Enabled");
  237.                 m_AutomatedVehicleMode = AutomatedVehicleMode_Enable;
  238.             } else {
  239.                 //ALPHAENGINE_LOGINFO("AutomatedVehicleMode Null");
  240.                 m_AutomatedVehicleMode = AutomatedVehicleMode_NULL;
  241.             }
  242.         }
  243.  
  244.         if (command == ShipControl_FullStop) {
  245.             // Reset all forces
  246.             m_MovementLatitude = -1.0f;
  247.             m_MovementLongitude = -1.0f;
  248.             m_MovementVertical = -1.0f;
  249.         }
  250.  
  251.         // Clear Command Buffer
  252.         //m_CommandBuffer.clear();
  253.     }
  254.  
  255.     // Convert Movement to Velocity
  256.     ConvertMovementToVelocity();
  257. }
  258.  
  259. // Set State of modules
  260. void ComputerCoreComponent::SetState(bool state) {
  261.     // If navigation module
  262.     if (m_pNavigationModule) {
  263.         m_pNavigationModule->SetState(state);
  264.     }
  265. }
  266.  
  267. // Test Key Press message
  268. void ComputerCoreComponent::KeyPress(String message) {
  269.     // output message
  270. }
  271.  
  272. // Issue Command
  273. void ComputerCoreComponent::IssueCommand(ComputerCoreCommand command) {
  274.     // Add command
  275.     m_CommandBuffer.push_back(command);
  276. }
  277.  
  278. void ComputerCoreComponent::HandlePostUpdate(StringHash eventType,
  279.         VariantMap& eventData) {
  280.     if (m_pAutomatedVehicle) {
  281.         float timeStep = eventData[PostUpdate::P_TIMESTEP].GetFloat();
  282.  
  283.         if (m_LinearVelocity != Vector3::ZERO) {
  284.             // Apply Linear Velocity
  285.  
  286.             if (m_LinearVelocity.x_ == -1.0f && m_LinearVelocity.y_ == -1.0f
  287.                     && m_LinearVelocity.z_ == -1.0f) {
  288.  
  289.                 Vector3 currentLinear =
  290.                         m_pAutomatedVehicle->GetLinearVelocity();
  291.  
  292.                 if (currentLinear != Vector3::ZERO) {
  293.                     m_pAutomatedVehicle->GetNode()->GetComponent<
  294.                             RigidBodyComponent>()->SetLinearVelocity(
  295.                             Vector3::ZERO);
  296.                     m_pAutomatedVehicle->GetNode()->GetComponent<
  297.                             RigidBodyComponent>()->SetAngularVelocity(
  298.                             Vector3::ZERO);
  299.  
  300.                 }
  301.             } else {
  302.                 // Apply impulse to vehicle linear velocity
  303.                 m_pAutomatedVehicle->ApplyImpulse(m_LinearVelocity * 4);
  304.             }
  305.  
  306.             // Reset all forces
  307.             m_MovementLatitude = m_MovementLongitude = m_MovementVertical =
  308.                     0.0f;
  309.         }
  310.  
  311.         // Enable Vehicle Mode Balance
  312.         if (m_AutomatedVehicleMode == AutomatedVehicleMode_Enable
  313.                 || m_LinearVelocity != Vector3::ZERO || m_Yaw != 0.0f) {
  314.  
  315.             // If autoenabled buggie
  316.             if (m_AutomatedVehicleMode == AutomatedVehicleMode_Enable) {
  317.                 // Get Rotation in Quaternion
  318.                 Quaternion rotation = m_pAutomatedVehicle->GetRotation();
  319.  
  320.                 // Create a Identity which is one
  321.                 Quaternion test = Quaternion::IDENTITY;
  322.  
  323.                 // Create a angle axis using the current vehicle rotation specifically the rigidbody
  324.                 test.FromAngleAxis(rotation.YawAngle(), Vector3::UP);
  325.  
  326.                 // Times the rotations by the inverse
  327.                 Quaternion delta = test
  328.                         * m_pAutomatedVehicle->GetRotation().Inverse();
  329.  
  330.                 // Create blank angle and axis
  331.                 float angle = 0.0f;
  332.                 Vector3 axis = Vector3::ZERO;
  333.  
  334.                 // Convert delta time to axis and angle
  335.                 ToAngleAxis(delta, angle, axis);
  336.  
  337.                 // If axis x and axiz z equal to aero skip adjustment
  338.                 if (axis.x_ == 0.0f) {
  339.                     // do nothing
  340.                 } else {
  341.                     // Multiply a normalized axis using timestep and amount
  342.                     Vector3 test = axis.Normalized() * 0.95f * timeStep;
  343.  
  344.                     // Apply angular velocity to vehicle
  345.                     m_pAutomatedVehicle->ApplyAngularVelocity(test);
  346.                 }
  347.             }
  348.  
  349.             //  If player control yaw is not zero
  350.             if (m_Yaw != 0.0f) {
  351.                 // Apply Rotation
  352.                 // Generate Quaternion from Yaw multiply by 10
  353.                 m_pAutomatedVehicle->ApplyRotate(
  354.                         Quaternion(m_Yaw * 10, Vector3::UP));
  355.  
  356.                 // Reset player control yaw to zero
  357.                 m_Yaw = 0.0f;
  358.             }
  359.         }
  360.     } else {
  361.         //ALPHAENGINE_LOGINFO("Post Update Failed. No Automated Vehicle");
  362.     }
  363. }
  364.  
  365. void ComputerCoreComponent::CreateCardinalTable() {
  366. // Sum of available thrust
  367.     float SumAvailableThrust = 0;
  368.  
  369. // Cardinal Table for propulsion
  370.     for (unsigned int i = CardinalDirectionVert_UP;
  371.             i < CardinalDirectionVert_MAX; i++) {
  372.         SumAvailableThrust = 0;
  373.  
  374.         for (unsigned int j = CardinalDirectionHorz_NORTH;
  375.                 j < CardinalDirectionHorz_MAX; j++) {
  376.  
  377.             // Clear all current data
  378.             m_CardinalTable[i][j].Thrusters.Clear();
  379.             m_CardinalTable[i][j].AvailableThrust = 0;
  380.  
  381.             // Loop through each thruster
  382.             for (unsigned int thruster = 0;
  383.                     thruster < m_pPropulsionThrusters.Size(); thruster++) {
  384.                 SumAvailableThrust +=
  385.                         m_pPropulsionThrusters[j]->GetForceAvailable(
  386.                                 (CardinalDirectionVetical) i,
  387.                                 (CardinalDirectionHorizontal) j);
  388.             }
  389.  
  390.         }
  391.     }
  392. }
  393.  
  394. // Convert Movement to Velocity
  395. void ComputerCoreComponent::ConvertMovementToVelocity() {
  396. // Create a zero vector
  397.     Vector3 LinearVelocity = Vector3::ZERO;
  398.  
  399. // Calculate impulse amount by Latitiude, Vertical, and Longititude
  400.     LinearVelocity = Vector3(m_MovementLatitude * 2, m_MovementVertical * 2,
  401.             m_MovementLongitude * 2);
  402.  
  403. // Set Linear Velocity Impulse
  404.     m_LinearVelocity = LinearVelocity;
  405.  
  406.     if (m_MovementLatitude == -1.0f && m_MovementVertical == -1.0f
  407.             && m_MovementLongitude == -1.0f) {
  408.         m_LinearVelocity = Vector3(-1.0f, -1.0f, -1.0f);
  409.     }
  410. }
  411.  
  412. void ComputerCoreComponent::ToAngleAxis(Quaternion q1, float & angle,
  413.         Vector3 & axis) {
  414.     float retAngle = 2 * std::acos(q1.w_);
  415.     double s = std::sqrt(1 - q1.w_ * q1.w_);
  416.  
  417.     Vector3 retAxis;
  418.  
  419.     if (s < 0.001) {
  420.         retAxis.x_ = q1.x_;
  421.         retAxis.y_ = q1.y_;
  422.         retAxis.z_ = q1.z_;
  423.     } else {
  424.         retAxis.x_ = q1.x_ / s;
  425.         retAxis.y_ = q1.y_ / s;
  426.         retAxis.z_ = q1.z_ / s;
  427.     }
  428.  
  429.     angle = retAngle;
  430.     axis = retAxis;
  431. }
  432.  
  433. void ComputerCoreComponent::SetCharacter(CharacterComponent * character) {
  434.     m_pCharacter = character;
  435. }
  436.  
  437. // Convert input to thrust per amount
  438. /*for (unsigned int i = CardinalDirectionVert_UP;
  439.  i < CardinalDirectionVert_MAX; i++) {
  440.  
  441.  if (i == CardinalDirectionVert_UP || CardinalDirectionVert_DOWN) {
  442.  continue;
  443.  }
  444.  
  445.  for (unsigned int j = CardinalDirectionHorz_NORTH;
  446.  j < CardinalDirectionHorz_MAX; j++) {
  447.  
  448.  // Calculate per direciton
  449.  if (j == CardinalDirectionHorz_NORTH
  450.  && m_MovementLongitude > 0.0f) {
  451.  LinearVelocity += Vector3::FORWARD
  452.  * m_CardinalTable[i][j].AvailableThrust
  453.  * m_MovementLongitude;
  454.  }
  455.  
  456.  // Calculate per direciton
  457.  if (j == CardinalDirectionHorz_SOUTH
  458.  && m_MovementLongitude < 0.0f) {
  459.  LinearVelocity += Vector3::BACK
  460.  * m_CardinalTable[i][j].AvailableThrust
  461.  * -(m_MovementLongitude);
  462.  
  463.  }
  464.  
  465.  // Calculate per direciton
  466.  if (j == CardinalDirectionHorz_EAST && m_MovementLatitude > 0.0f) {
  467.  LinearVelocity += Vector3::RIGHT
  468.  * m_CardinalTable[i][j].AvailableThrust
  469.  * m_MovementLongitude;
  470.  
  471.  }
  472.  
  473.  // Calculate per direciton
  474.  if (j == CardinalDirectionHorz_WEST && m_MovementLatitude < 0.0f) {
  475.  LinearVelocity += Vector3::LEFT
  476.  * m_CardinalTable[i][j].AvailableThrust
  477.  * -(m_MovementLongitude);
  478.  }
  479.  }
  480.  }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement