Guest User

Untitled

a guest
Aug 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.63 KB | None | 0 0
  1. #include "GameProject_precompiled.h"
  2.  
  3. #include <ISystem.h>
  4.  
  5. #include <AzCore/Component/Entity.h>
  6. #include <AzCore/Serialization/SerializeContext.h>
  7. #include <AzCore/Serialization/EditContext.h>
  8.  
  9. #include <AzFramework/Input/Devices/Mouse/InputDeviceMouse.h>
  10. #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
  11.  
  12. #include <IActionMapManager.h>
  13. #include <IGameObjectSystem.h>
  14.  
  15. // for DrawDebug
  16. #include <IProximityTriggerSystem.h>
  17. #include <IRenderAuxGeom.h>
  18.  
  19. #include <AzFramework/Input/Events/InputChannelEventListener.h>
  20.  
  21.  
  22. //BUS
  23. #include <AzCore/Component/EntityBus.h>
  24. #include <AzCore/Component/ComponentApplicationBus.h>
  25. #include <AzCore/Component/TickBus.h>
  26. #include <AzCore/Component/TransformBus.h>
  27. #include <AzFramework/Physics/PhysicsSystemComponentBus.h>
  28.  
  29. #include <Terrain/Bus/LegacyTerrainBus.h>
  30.  
  31. #include <Maestro/Bus/SequenceComponentBus.h>
  32. #include "SimpleFPSController.h"
  33.  
  34. //PhysX
  35. #include <AzFramework\Physics\World.h>
  36. #include <AzFramework\Physics\ShapeConfiguration.h>
  37. //#include PhysX/SystemComponentBus.h>
  38.  
  39. #include <..\..\..\Gems\PhysX\Code\Include\PhysX\ConfigurationBus.h>
  40. #include <..\..\..\Gems\PhysX\Code\Include\PhysX\MathConversion.h>
  41. #include <..\..\..\Gems\PhysX\Code\Include\PhysX\SystemComponentBus.h>
  42.  
  43. #include "NavigationActorBus.h"
  44.  
  45.  
  46. using namespace GameProject;
  47.  
  48. SimpleFPSContoller::SimpleFPSContoller()
  49. {
  50.  
  51. }
  52.  
  53. SimpleFPSContoller::~SimpleFPSContoller()
  54. {
  55.  
  56. }
  57.  
  58. void SimpleFPSContoller::Reflect(AZ::ReflectContext *context)
  59. {
  60.     AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  61.  
  62.     if (serialize)
  63.     {
  64.         serialize->Class<SimpleFPSContoller, AZ::Component >()
  65.             ->Version(1)
  66.             ->Field("yawRotationSpeed", &SimpleFPSContoller::m_yawRotationSpeed)
  67.             ->Field("pitchRotationSpeed", &SimpleFPSContoller::m_pitchRotationSpeed)
  68.             ;
  69.  
  70.         AZ::EditContext* edit = serialize->GetEditContext();
  71.         if (edit)
  72.         {
  73.             edit->Class<SimpleFPSContoller>("SimpleFPSContoller", "")
  74.                 ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  75.                 ->Attribute(AZ::Edit::Attributes::Category, CATEGORY)
  76.                 ->Attribute("Icon", "Editor/Icons/Components/ComponentPlaceholder.png")
  77.                 ->Attribute("ViewportIcon", "Editor/Icons/Components/Viewport/ComponentPlaceholder.png")
  78.                 ->Attribute("AutoExpand", true)
  79.                 ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game", 0x232b318c))
  80.                 ->DataElement(AZ::Edit::UIHandlers::Slider, &SimpleFPSContoller::m_yawRotationSpeed, "yawRotationSpeed", "[desc]")
  81.                 ->Attribute(AZ::Edit::Attributes::Min, 0.1f)
  82.                 ->Attribute(AZ::Edit::Attributes::Max, 10.0f)
  83.                 ->DataElement(AZ::Edit::UIHandlers::Slider, &SimpleFPSContoller::m_pitchRotationSpeed, "pitchRotationSpeed", "[desc]")
  84.                 ->Attribute(AZ::Edit::Attributes::Min, 0.1f)
  85.                 ->Attribute(AZ::Edit::Attributes::Max, 10.0f)
  86.                 ;
  87.         }
  88.     }
  89. }
  90.  
  91. void SimpleFPSContoller::Init()
  92. {
  93.  
  94. }
  95.  
  96. void SimpleFPSContoller::Activate()
  97. {
  98.     InputChannelEventListener::Connect();
  99.     AZ::TickBus::Handler::BusConnect();
  100.     AzFramework::GameEntityContextEventBus::Handler::BusConnect();
  101. }
  102.  
  103. void SimpleFPSContoller::Deactivate()
  104. {
  105.     InputChannelEventListener::Disconnect();
  106.     AZ::TickBus::Handler::BusDisconnect();
  107.     AzFramework::GameEntityContextEventBus::Handler::BusDisconnect();
  108. }
  109.  
  110. bool SimpleFPSContoller::OnInputChannelEventFiltered(const AzFramework::InputChannel & inputChannel)
  111. {
  112.     const AzFramework::InputDeviceId& deviceId = inputChannel.GetInputDevice().GetInputDeviceId();
  113.    
  114.     if (AzFramework::InputDeviceMouse::IsMouseDevice(deviceId))
  115.     {
  116.         OnMouseEvent(inputChannel);
  117.     }
  118.     else if (AzFramework::InputDeviceKeyboard::IsKeyboardDevice(deviceId))
  119.     {
  120.         OnKeyboardEvent(inputChannel);
  121.     }
  122.  
  123.     return false;
  124. }
  125.  
  126. void SimpleFPSContoller::OnTick(float deltaTime, AZ::ScriptTimePoint time)
  127. {
  128.     DrawDebug();
  129.  
  130.     const float speed = 10.0f;
  131.  
  132.     AZ::Vector3 velocity(0.0f,0.0f,0.0f);
  133.    
  134.     if (m_MovementMasks & eINPUT_FORWARD)
  135.         velocity.SetY(speed * deltaTime);
  136.  
  137.     if (m_MovementMasks & eINPUT_BACK)
  138.         velocity.SetY(-speed * deltaTime);
  139.  
  140.     if (m_MovementMasks & eINPUT_LEFT)
  141.         velocity.SetX(-speed * deltaTime);
  142.  
  143.     if (m_MovementMasks & eINPUT_RIGHT)
  144.         velocity.SetX(speed * deltaTime);
  145.  
  146.     if (m_MovementMasks & eINPUT_UP)
  147.         velocity.SetZ(speed * deltaTime);
  148.  
  149.     if (m_MovementMasks & eINPUT_DOWN)
  150.         velocity.SetZ(-speed * deltaTime);
  151.  
  152.     AZ::Transform tm = m_entity->GetTransform()->GetWorldTM().GetInverseFast();
  153.     AZ::Vector3 move = velocity * tm;
  154.     AZ::Vector3 prevWorldPos = m_entity->GetTransform()->GetWorldTranslation();
  155.  
  156.     if (move.GetLength() > 0.001f)
  157.         m_entity->GetTransform()->SetWorldTranslation(prevWorldPos + (move * m_boost));
  158.  
  159.     if (m_MovementMasks & eINPUT_MOUSE_X)
  160.     {
  161.         m_yaw -= (m_yawRotationSpeed * m_yawChange * deltaTime);
  162.         //m_yaw = AZ::GetClamp(m_yaw, 0.0f, 360.0f);
  163.         m_yawChange = 0.0f;
  164.         m_MovementMasks &= ~eINPUT_MOUSE_X;
  165.     }
  166.    
  167.     if (m_MovementMasks & eINPUT_MOUSE_Y)
  168.     {
  169.         m_pitch += (m_pitchRotationSpeed * m_pitchChange * deltaTime);
  170.         m_pitch = AZ::GetClamp(m_pitch, -90.0f, 90.0f);
  171.         m_pitchChange = 0.0f;
  172.         m_MovementMasks &= ~eINPUT_MOUSE_Y;
  173.     }
  174.    
  175.     AZ::Quaternion yawRotation = AZ::Quaternion::CreateRotationZ(AZ::DegToRad(m_yaw));
  176.     AZ::Quaternion pitchRotation = AZ::Quaternion::CreateRotationX(AZ::DegToRad(m_pitch));
  177.     AZ::Quaternion finalRotation = (yawRotation * pitchRotation);
  178.  
  179.     m_entity->GetTransform()->SetLocalRotationQuaternion(finalRotation);
  180. }
  181.  
  182. void SimpleFPSContoller::OnGameEntitiesStarted()
  183. {
  184.     // TODO: CAMERA добавать серизлизацию в файл позиции камеры
  185.  
  186. }
  187.  
  188. bool SimpleFPSContoller::IsPointInView(AZ::Vector3 pos)
  189. {
  190.     const CCamera pCamera = gEnv->pRenderer->GetCamera();
  191.  
  192.     return pCamera.IsPointVisible(AZVec3ToLYVec3(pos));
  193. }
  194.  
  195. void SimpleFPSContoller::OnMouseEvent(const AzFramework::InputChannel & inputChannel)
  196. {
  197.     using namespace AzFramework;
  198.  
  199.     const InputChannelId& channelId = inputChannel.GetInputChannelId();
  200.     const InputChannel::State state = inputChannel.GetState();
  201.  
  202.     if (channelId == InputDeviceMouse::Movement::X)
  203.     {
  204.  
  205.         m_MovementMasks |= eINPUT_MOUSE_X;
  206.         m_yawChange += inputChannel.GetValue();
  207.     }
  208.     else if (channelId == InputDeviceMouse::Movement::Y)
  209.     {
  210.         m_MovementMasks |= eINPUT_MOUSE_Y;
  211.         m_pitchChange += inputChannel.GetValue();
  212.     }
  213.     else if (channelId == InputDeviceMouse::Button::Left)
  214.     {
  215.         if (state == InputChannel::State::Began)
  216.         {
  217.             //RayCastForward();
  218.             RayCastForward2();
  219.             NavigationActorRequestBus::Broadcast(&NavigationActorRequestBus::Events::SetDestination, m_hitPosition);
  220.             //UnitNavigationBus::Broadcast(&UnitNavigationBus::Events::SetDestinationPoint, m_hitPosition);
  221.         }
  222.     }
  223.     else if (channelId == InputDeviceMouse::Button::Right)
  224.     {
  225.         GatherAroundHit();
  226.         //TerraformingBus::Broadcast(&TerraformingBus::Events::UpdateTerrainCutout, m_hitPosition, m_gatherRadius, true);
  227.     }
  228. }
  229.  
  230. void SimpleFPSContoller::OnKeyboardEvent(const AzFramework::InputChannel & inputChannel)
  231. {
  232.     if (gEnv && gEnv->pConsole && gEnv->pConsole->IsOpened())
  233.     {
  234.         return;
  235.     }
  236.  
  237.     const AzFramework::InputChannelId& channelId = inputChannel.GetInputChannelId();
  238.     const AzFramework::InputChannel::State state = inputChannel.GetState();
  239.  
  240.    
  241.     if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericW)
  242.     {
  243.         if (state == AzFramework::InputChannel::State::Began)
  244.             m_MovementMasks |= eINPUT_FORWARD;
  245.         else if (state == AzFramework::InputChannel::State::Ended)
  246.             m_MovementMasks &= ~eINPUT_FORWARD;
  247.     }
  248.     else if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericA)
  249.     {
  250.         if (state == AzFramework::InputChannel::State::Began)
  251.             m_MovementMasks |= eINPUT_LEFT;
  252.         else if(state == AzFramework::InputChannel::State::Ended)
  253.             m_MovementMasks &= ~eINPUT_LEFT;
  254.     }
  255.     else if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericS)
  256.     {
  257.         if (state == AzFramework::InputChannel::State::Began)
  258.             m_MovementMasks |= eINPUT_BACK;
  259.         else if (state == AzFramework::InputChannel::State::Ended)
  260.             m_MovementMasks &= ~eINPUT_BACK;
  261.     }
  262.     else if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericD)
  263.     {
  264.         if (state == AzFramework::InputChannel::State::Began)
  265.             m_MovementMasks |= eINPUT_RIGHT;
  266.         else if (state == AzFramework::InputChannel::State::Ended)
  267.             m_MovementMasks &= ~eINPUT_RIGHT;
  268.     }
  269.     else if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericZ)
  270.     {
  271.         if (state == AzFramework::InputChannel::State::Began)
  272.             m_MovementMasks |= eINPUT_DOWN;
  273.         else if (state == AzFramework::InputChannel::State::Ended)
  274.             m_MovementMasks &= ~eINPUT_DOWN;
  275.     }
  276.     else if (channelId == AzFramework::InputDeviceKeyboard::Key::EditSpace)
  277.     {
  278.         if (state == AzFramework::InputChannel::State::Began)
  279.             m_MovementMasks |= eINPUT_UP;
  280.         else if (state == AzFramework::InputChannel::State::Ended)
  281.             m_MovementMasks &= ~eINPUT_UP;
  282.     }
  283.     else if (channelId == AzFramework::InputDeviceKeyboard::Key::ModifierShiftL)
  284.     {
  285.         if (state == AzFramework::InputChannel::State::Began)
  286.             m_boost = 8.0f;
  287.         else if (state == AzFramework::InputChannel::State::Ended)
  288.             m_boost = 1.0f;
  289.  
  290.         //Maestro::SequenceComponentRequestBus::Broadcast(&Maestro::SequenceComponentRequestBus::Events::Play);
  291.     }
  292. }
  293.  
  294. void SimpleFPSContoller::DrawDebug()
  295. {
  296.     if (!gEnv) return;
  297.  
  298.     // hit
  299.     AZ::Aabb encompassingAABB = AZ::Aabb::CreateFromPoint(m_hitPosition);
  300.     encompassingAABB.Expand(AZ::Vector3(0.1, 0.1, 0.1));
  301.     AABB m_cachedAABB;
  302.  
  303.     m_cachedAABB = AZAabbToLyAABB(encompassingAABB);
  304.  
  305.     IRenderAuxGeom* renderAuxGeom = gEnv->pRenderer->GetIRenderAuxGeom();
  306.     SAuxGeomRenderFlags flags;
  307.     flags.SetAlphaBlendMode(e_AlphaNone);
  308.     flags.SetCullMode(e_CullModeBack);
  309.     flags.SetDepthTestFlag(e_DepthTestOn);
  310.     flags.SetFillMode(e_FillModeWireframe);
  311.     renderAuxGeom->SetRenderFlags(flags);
  312.  
  313.     renderAuxGeom->DrawAABB(m_cachedAABB, true, Col_Red, eBBD_Faceted);
  314.  
  315.     // ray
  316.     auto cryBegin = AZVec3ToLYVec3(m_rayStartPosition);
  317.     auto cryEnd = AZVec3ToLYVec3(m_rayEndPosition);
  318.     const ColorF& color = ColorF(1, 0, 0);
  319.     gEnv->pRenderer->GetIRenderAuxGeom()->DrawLine(cryBegin, color, cryEnd, color);
  320.  
  321.     // normal
  322.     auto normalBegin = AZVec3ToLYVec3(m_hitPosition);
  323.     auto normalEnd = AZVec3ToLYVec3(m_hitPosition) + (AZVec3ToLYVec3(m_hitNormal) * 3.0f);
  324.     const ColorF& normalColor = ColorF(1, 1, 1);
  325.     gEnv->pRenderer->GetIRenderAuxGeom()->DrawLine(normalBegin, normalColor, normalEnd, normalColor);
  326.  
  327.     // text label
  328.     //m_label = "hit entity: "; //+ m_hitName;
  329.     //gEnv->pRenderer->GetIRenderAuxGeom()->Draw2dLabel(20, 10, 2, ColorF(1, 1, 1), false, m_label.c_str());
  330.     // gather sphere
  331.     Vec3 pos = AZVec3ToLYVec3(m_gatherPosition);
  332.     renderAuxGeom->DrawSphere(pos, m_gatherRadius, ColorF(1,1,0), true);
  333. }
  334.  
  335. void SimpleFPSContoller::RayCastForward()
  336. {
  337.     //AzFramework::PhysicsSystemRequest
  338.     AzFramework::PhysicsSystemRequests::RayCastConfiguration rayCastConfiguration;
  339.     rayCastConfiguration.m_origin = m_entity->GetTransform()->GetWorldTranslation();
  340.     //AZ::Transform tm = m_entity->GetTransform()->GetWorldTM().GetInverseFast();
  341.     //AZ::Vector3 forward = (AZ::Vector3(0, 1, 0) * tm).GetNormalized();
  342.     AZ::Vector3 forward = m_entity->GetTransform()->GetWorldTM().GetBasisY();
  343.  
  344.     rayCastConfiguration.m_direction = forward;
  345.     rayCastConfiguration.m_maxHits = 1;
  346.     rayCastConfiguration.m_ignoreEntityIds = AZStd::vector<AZ::EntityId>{ m_entity->GetId() };
  347.     rayCastConfiguration.m_maxDistance = 200.0f;
  348.     rayCastConfiguration.m_piercesSurfacesGreaterThan = 10000;
  349.  
  350.     m_rayStartPosition = rayCastConfiguration.m_origin;
  351.     m_rayEndPosition = rayCastConfiguration.m_origin + (rayCastConfiguration.m_direction * rayCastConfiguration.m_maxDistance);
  352.     AzFramework::PhysicsSystemRequests::RayCastResult result;
  353.     EBUS_EVENT_RESULT(result, AzFramework::PhysicsSystemRequestBus, RayCast, rayCastConfiguration);
  354.  
  355.     if (result.GetHitCount() > 0)
  356.     {
  357.         //AZ_Printf("RayCastResult", "We hit something!", true);
  358.        
  359.         const AzFramework::PhysicsSystemRequests::RayCastHit* hit = result.GetHit(0);
  360.         if (hit->IsValid())
  361.         {
  362.             m_hitPosition = hit->m_position;
  363.             m_hitNormal = hit->m_normal;
  364.             m_hitEntityId = hit->m_entityId;
  365.  
  366.             EBUS_EVENT_RESULT(m_hitEntity, AZ::ComponentApplicationBus, FindEntity, hit->m_entityId);
  367.  
  368.             if (m_hitEntity)
  369.                 m_hitName = m_hitEntity->GetName();
  370.  
  371.         }
  372.     }
  373. }
  374.  
  375. void SimpleFPSContoller::RayCastForward2()
  376. {
  377.    
  378.     // Code examples: dev\Code\Framework\Tests\Physics\PhysicsGenericInterfaceTests
  379.  
  380.     Physics::RayCastRequest request;
  381.  
  382.     request.m_start = m_entity->GetTransform()->GetWorldTranslation();
  383.     request.m_direction = m_entity->GetTransform()->GetWorldTM().GetBasisY();
  384.     request.m_distance = 200.0f;
  385.  
  386.     // Single world setup
  387.     Physics::RayCastHit hit;
  388.     Physics::WorldRequestBus::BroadcastResult(hit, &Physics::WorldRequests::RayCast, request);
  389.    
  390.     if (hit.m_body != nullptr)
  391.     {
  392.         AZ::EntityId id = hit.m_body->GetEntityId();
  393.         m_hitPosition = hit.m_position;
  394.         m_hitNormal = hit.m_normal;
  395.         m_hitEntityId = hit.m_body->GetEntityId();
  396.  
  397.         EBUS_EVENT_RESULT(m_hitEntity, AZ::ComponentApplicationBus, FindEntity, m_hitEntityId);
  398.  
  399.         if (m_hitEntity)
  400.             m_hitName = m_hitEntity->GetName();
  401.  
  402.     }
  403.  
  404.     //// #include <AzFramework\Components\CameraBus.h>
  405.     //Camera::CameraRequestBus::Event(GetEntityId(), Camera::CameraRequestBus::Events::MakeActiveView);
  406.    
  407.  
  408. }
  409.  
  410. void SimpleFPSContoller::GatherAroundHit()
  411. {
  412.     //virtual AZStd::vector<AZ::EntityId> GatherPhysicalEntitiesAroundPoint(const AZ::Vector3& center, float radius, AZ::u32 query)
  413.     AZStd::vector<AZ::EntityId> result;
  414.     m_gatherPosition = m_hitPosition;
  415.     AZ::Vector3& center = m_gatherPosition;
  416.     float radius = m_gatherRadius;
  417.     AZ::u32 query = AzFramework::PhysicalEntityTypes::All;
  418.  
  419.     EBUS_EVENT_RESULT(result, AzFramework::PhysicsSystemRequestBus, GatherPhysicalEntitiesAroundPoint, center, radius, query);
  420.  
  421.     if (result.size())
  422.     {
  423.  
  424.     }
  425. }
Add Comment
Please, Sign In to add comment