Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "GameProject_precompiled.h"
- #include <ISystem.h>
- #include <AzCore/Component/Entity.h>
- #include <AzCore/Serialization/SerializeContext.h>
- #include <AzCore/Serialization/EditContext.h>
- #include <AzFramework/Input/Devices/Mouse/InputDeviceMouse.h>
- #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
- #include <IActionMapManager.h>
- #include <IGameObjectSystem.h>
- // for DrawDebug
- #include <IProximityTriggerSystem.h>
- #include <IRenderAuxGeom.h>
- #include <AzFramework/Input/Events/InputChannelEventListener.h>
- //BUS
- #include <AzCore/Component/EntityBus.h>
- #include <AzCore/Component/ComponentApplicationBus.h>
- #include <AzCore/Component/TickBus.h>
- #include <AzCore/Component/TransformBus.h>
- #include <AzFramework/Physics/PhysicsSystemComponentBus.h>
- #include <Terrain/Bus/LegacyTerrainBus.h>
- #include <Maestro/Bus/SequenceComponentBus.h>
- #include "SimpleFPSController.h"
- //PhysX
- #include <AzFramework\Physics\World.h>
- #include <AzFramework\Physics\ShapeConfiguration.h>
- //#include PhysX/SystemComponentBus.h>
- #include <..\..\..\Gems\PhysX\Code\Include\PhysX\ConfigurationBus.h>
- #include <..\..\..\Gems\PhysX\Code\Include\PhysX\MathConversion.h>
- #include <..\..\..\Gems\PhysX\Code\Include\PhysX\SystemComponentBus.h>
- #include "NavigationActorBus.h"
- using namespace GameProject;
- SimpleFPSContoller::SimpleFPSContoller()
- {
- }
- SimpleFPSContoller::~SimpleFPSContoller()
- {
- }
- void SimpleFPSContoller::Reflect(AZ::ReflectContext *context)
- {
- AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
- if (serialize)
- {
- serialize->Class<SimpleFPSContoller, AZ::Component >()
- ->Version(1)
- ->Field("yawRotationSpeed", &SimpleFPSContoller::m_yawRotationSpeed)
- ->Field("pitchRotationSpeed", &SimpleFPSContoller::m_pitchRotationSpeed)
- ;
- AZ::EditContext* edit = serialize->GetEditContext();
- if (edit)
- {
- edit->Class<SimpleFPSContoller>("SimpleFPSContoller", "")
- ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
- ->Attribute(AZ::Edit::Attributes::Category, CATEGORY)
- ->Attribute("Icon", "Editor/Icons/Components/ComponentPlaceholder.png")
- ->Attribute("ViewportIcon", "Editor/Icons/Components/Viewport/ComponentPlaceholder.png")
- ->Attribute("AutoExpand", true)
- ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game", 0x232b318c))
- ->DataElement(AZ::Edit::UIHandlers::Slider, &SimpleFPSContoller::m_yawRotationSpeed, "yawRotationSpeed", "[desc]")
- ->Attribute(AZ::Edit::Attributes::Min, 0.1f)
- ->Attribute(AZ::Edit::Attributes::Max, 10.0f)
- ->DataElement(AZ::Edit::UIHandlers::Slider, &SimpleFPSContoller::m_pitchRotationSpeed, "pitchRotationSpeed", "[desc]")
- ->Attribute(AZ::Edit::Attributes::Min, 0.1f)
- ->Attribute(AZ::Edit::Attributes::Max, 10.0f)
- ;
- }
- }
- }
- void SimpleFPSContoller::Init()
- {
- }
- void SimpleFPSContoller::Activate()
- {
- InputChannelEventListener::Connect();
- AZ::TickBus::Handler::BusConnect();
- AzFramework::GameEntityContextEventBus::Handler::BusConnect();
- }
- void SimpleFPSContoller::Deactivate()
- {
- InputChannelEventListener::Disconnect();
- AZ::TickBus::Handler::BusDisconnect();
- AzFramework::GameEntityContextEventBus::Handler::BusDisconnect();
- }
- bool SimpleFPSContoller::OnInputChannelEventFiltered(const AzFramework::InputChannel & inputChannel)
- {
- const AzFramework::InputDeviceId& deviceId = inputChannel.GetInputDevice().GetInputDeviceId();
- if (AzFramework::InputDeviceMouse::IsMouseDevice(deviceId))
- {
- OnMouseEvent(inputChannel);
- }
- else if (AzFramework::InputDeviceKeyboard::IsKeyboardDevice(deviceId))
- {
- OnKeyboardEvent(inputChannel);
- }
- return false;
- }
- void SimpleFPSContoller::OnTick(float deltaTime, AZ::ScriptTimePoint time)
- {
- DrawDebug();
- const float speed = 10.0f;
- AZ::Vector3 velocity(0.0f,0.0f,0.0f);
- if (m_MovementMasks & eINPUT_FORWARD)
- velocity.SetY(speed * deltaTime);
- if (m_MovementMasks & eINPUT_BACK)
- velocity.SetY(-speed * deltaTime);
- if (m_MovementMasks & eINPUT_LEFT)
- velocity.SetX(-speed * deltaTime);
- if (m_MovementMasks & eINPUT_RIGHT)
- velocity.SetX(speed * deltaTime);
- if (m_MovementMasks & eINPUT_UP)
- velocity.SetZ(speed * deltaTime);
- if (m_MovementMasks & eINPUT_DOWN)
- velocity.SetZ(-speed * deltaTime);
- AZ::Transform tm = m_entity->GetTransform()->GetWorldTM().GetInverseFast();
- AZ::Vector3 move = velocity * tm;
- AZ::Vector3 prevWorldPos = m_entity->GetTransform()->GetWorldTranslation();
- if (move.GetLength() > 0.001f)
- m_entity->GetTransform()->SetWorldTranslation(prevWorldPos + (move * m_boost));
- if (m_MovementMasks & eINPUT_MOUSE_X)
- {
- m_yaw -= (m_yawRotationSpeed * m_yawChange * deltaTime);
- //m_yaw = AZ::GetClamp(m_yaw, 0.0f, 360.0f);
- m_yawChange = 0.0f;
- m_MovementMasks &= ~eINPUT_MOUSE_X;
- }
- if (m_MovementMasks & eINPUT_MOUSE_Y)
- {
- m_pitch += (m_pitchRotationSpeed * m_pitchChange * deltaTime);
- m_pitch = AZ::GetClamp(m_pitch, -90.0f, 90.0f);
- m_pitchChange = 0.0f;
- m_MovementMasks &= ~eINPUT_MOUSE_Y;
- }
- AZ::Quaternion yawRotation = AZ::Quaternion::CreateRotationZ(AZ::DegToRad(m_yaw));
- AZ::Quaternion pitchRotation = AZ::Quaternion::CreateRotationX(AZ::DegToRad(m_pitch));
- AZ::Quaternion finalRotation = (yawRotation * pitchRotation);
- m_entity->GetTransform()->SetLocalRotationQuaternion(finalRotation);
- }
- void SimpleFPSContoller::OnGameEntitiesStarted()
- {
- // TODO: CAMERA добавать серизлизацию в файл позиции камеры
- }
- bool SimpleFPSContoller::IsPointInView(AZ::Vector3 pos)
- {
- const CCamera pCamera = gEnv->pRenderer->GetCamera();
- return pCamera.IsPointVisible(AZVec3ToLYVec3(pos));
- }
- void SimpleFPSContoller::OnMouseEvent(const AzFramework::InputChannel & inputChannel)
- {
- using namespace AzFramework;
- const InputChannelId& channelId = inputChannel.GetInputChannelId();
- const InputChannel::State state = inputChannel.GetState();
- if (channelId == InputDeviceMouse::Movement::X)
- {
- m_MovementMasks |= eINPUT_MOUSE_X;
- m_yawChange += inputChannel.GetValue();
- }
- else if (channelId == InputDeviceMouse::Movement::Y)
- {
- m_MovementMasks |= eINPUT_MOUSE_Y;
- m_pitchChange += inputChannel.GetValue();
- }
- else if (channelId == InputDeviceMouse::Button::Left)
- {
- if (state == InputChannel::State::Began)
- {
- //RayCastForward();
- RayCastForward2();
- NavigationActorRequestBus::Broadcast(&NavigationActorRequestBus::Events::SetDestination, m_hitPosition);
- //UnitNavigationBus::Broadcast(&UnitNavigationBus::Events::SetDestinationPoint, m_hitPosition);
- }
- }
- else if (channelId == InputDeviceMouse::Button::Right)
- {
- GatherAroundHit();
- //TerraformingBus::Broadcast(&TerraformingBus::Events::UpdateTerrainCutout, m_hitPosition, m_gatherRadius, true);
- }
- }
- void SimpleFPSContoller::OnKeyboardEvent(const AzFramework::InputChannel & inputChannel)
- {
- if (gEnv && gEnv->pConsole && gEnv->pConsole->IsOpened())
- {
- return;
- }
- const AzFramework::InputChannelId& channelId = inputChannel.GetInputChannelId();
- const AzFramework::InputChannel::State state = inputChannel.GetState();
- if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericW)
- {
- if (state == AzFramework::InputChannel::State::Began)
- m_MovementMasks |= eINPUT_FORWARD;
- else if (state == AzFramework::InputChannel::State::Ended)
- m_MovementMasks &= ~eINPUT_FORWARD;
- }
- else if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericA)
- {
- if (state == AzFramework::InputChannel::State::Began)
- m_MovementMasks |= eINPUT_LEFT;
- else if(state == AzFramework::InputChannel::State::Ended)
- m_MovementMasks &= ~eINPUT_LEFT;
- }
- else if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericS)
- {
- if (state == AzFramework::InputChannel::State::Began)
- m_MovementMasks |= eINPUT_BACK;
- else if (state == AzFramework::InputChannel::State::Ended)
- m_MovementMasks &= ~eINPUT_BACK;
- }
- else if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericD)
- {
- if (state == AzFramework::InputChannel::State::Began)
- m_MovementMasks |= eINPUT_RIGHT;
- else if (state == AzFramework::InputChannel::State::Ended)
- m_MovementMasks &= ~eINPUT_RIGHT;
- }
- else if (channelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericZ)
- {
- if (state == AzFramework::InputChannel::State::Began)
- m_MovementMasks |= eINPUT_DOWN;
- else if (state == AzFramework::InputChannel::State::Ended)
- m_MovementMasks &= ~eINPUT_DOWN;
- }
- else if (channelId == AzFramework::InputDeviceKeyboard::Key::EditSpace)
- {
- if (state == AzFramework::InputChannel::State::Began)
- m_MovementMasks |= eINPUT_UP;
- else if (state == AzFramework::InputChannel::State::Ended)
- m_MovementMasks &= ~eINPUT_UP;
- }
- else if (channelId == AzFramework::InputDeviceKeyboard::Key::ModifierShiftL)
- {
- if (state == AzFramework::InputChannel::State::Began)
- m_boost = 8.0f;
- else if (state == AzFramework::InputChannel::State::Ended)
- m_boost = 1.0f;
- //Maestro::SequenceComponentRequestBus::Broadcast(&Maestro::SequenceComponentRequestBus::Events::Play);
- }
- }
- void SimpleFPSContoller::DrawDebug()
- {
- if (!gEnv) return;
- // hit
- AZ::Aabb encompassingAABB = AZ::Aabb::CreateFromPoint(m_hitPosition);
- encompassingAABB.Expand(AZ::Vector3(0.1, 0.1, 0.1));
- AABB m_cachedAABB;
- m_cachedAABB = AZAabbToLyAABB(encompassingAABB);
- IRenderAuxGeom* renderAuxGeom = gEnv->pRenderer->GetIRenderAuxGeom();
- SAuxGeomRenderFlags flags;
- flags.SetAlphaBlendMode(e_AlphaNone);
- flags.SetCullMode(e_CullModeBack);
- flags.SetDepthTestFlag(e_DepthTestOn);
- flags.SetFillMode(e_FillModeWireframe);
- renderAuxGeom->SetRenderFlags(flags);
- renderAuxGeom->DrawAABB(m_cachedAABB, true, Col_Red, eBBD_Faceted);
- // ray
- auto cryBegin = AZVec3ToLYVec3(m_rayStartPosition);
- auto cryEnd = AZVec3ToLYVec3(m_rayEndPosition);
- const ColorF& color = ColorF(1, 0, 0);
- gEnv->pRenderer->GetIRenderAuxGeom()->DrawLine(cryBegin, color, cryEnd, color);
- // normal
- auto normalBegin = AZVec3ToLYVec3(m_hitPosition);
- auto normalEnd = AZVec3ToLYVec3(m_hitPosition) + (AZVec3ToLYVec3(m_hitNormal) * 3.0f);
- const ColorF& normalColor = ColorF(1, 1, 1);
- gEnv->pRenderer->GetIRenderAuxGeom()->DrawLine(normalBegin, normalColor, normalEnd, normalColor);
- // text label
- //m_label = "hit entity: "; //+ m_hitName;
- //gEnv->pRenderer->GetIRenderAuxGeom()->Draw2dLabel(20, 10, 2, ColorF(1, 1, 1), false, m_label.c_str());
- // gather sphere
- Vec3 pos = AZVec3ToLYVec3(m_gatherPosition);
- renderAuxGeom->DrawSphere(pos, m_gatherRadius, ColorF(1,1,0), true);
- }
- void SimpleFPSContoller::RayCastForward()
- {
- //AzFramework::PhysicsSystemRequest
- AzFramework::PhysicsSystemRequests::RayCastConfiguration rayCastConfiguration;
- rayCastConfiguration.m_origin = m_entity->GetTransform()->GetWorldTranslation();
- //AZ::Transform tm = m_entity->GetTransform()->GetWorldTM().GetInverseFast();
- //AZ::Vector3 forward = (AZ::Vector3(0, 1, 0) * tm).GetNormalized();
- AZ::Vector3 forward = m_entity->GetTransform()->GetWorldTM().GetBasisY();
- rayCastConfiguration.m_direction = forward;
- rayCastConfiguration.m_maxHits = 1;
- rayCastConfiguration.m_ignoreEntityIds = AZStd::vector<AZ::EntityId>{ m_entity->GetId() };
- rayCastConfiguration.m_maxDistance = 200.0f;
- rayCastConfiguration.m_piercesSurfacesGreaterThan = 10000;
- m_rayStartPosition = rayCastConfiguration.m_origin;
- m_rayEndPosition = rayCastConfiguration.m_origin + (rayCastConfiguration.m_direction * rayCastConfiguration.m_maxDistance);
- AzFramework::PhysicsSystemRequests::RayCastResult result;
- EBUS_EVENT_RESULT(result, AzFramework::PhysicsSystemRequestBus, RayCast, rayCastConfiguration);
- if (result.GetHitCount() > 0)
- {
- //AZ_Printf("RayCastResult", "We hit something!", true);
- const AzFramework::PhysicsSystemRequests::RayCastHit* hit = result.GetHit(0);
- if (hit->IsValid())
- {
- m_hitPosition = hit->m_position;
- m_hitNormal = hit->m_normal;
- m_hitEntityId = hit->m_entityId;
- EBUS_EVENT_RESULT(m_hitEntity, AZ::ComponentApplicationBus, FindEntity, hit->m_entityId);
- if (m_hitEntity)
- m_hitName = m_hitEntity->GetName();
- }
- }
- }
- void SimpleFPSContoller::RayCastForward2()
- {
- // Code examples: dev\Code\Framework\Tests\Physics\PhysicsGenericInterfaceTests
- Physics::RayCastRequest request;
- request.m_start = m_entity->GetTransform()->GetWorldTranslation();
- request.m_direction = m_entity->GetTransform()->GetWorldTM().GetBasisY();
- request.m_distance = 200.0f;
- // Single world setup
- Physics::RayCastHit hit;
- Physics::WorldRequestBus::BroadcastResult(hit, &Physics::WorldRequests::RayCast, request);
- if (hit.m_body != nullptr)
- {
- AZ::EntityId id = hit.m_body->GetEntityId();
- m_hitPosition = hit.m_position;
- m_hitNormal = hit.m_normal;
- m_hitEntityId = hit.m_body->GetEntityId();
- EBUS_EVENT_RESULT(m_hitEntity, AZ::ComponentApplicationBus, FindEntity, m_hitEntityId);
- if (m_hitEntity)
- m_hitName = m_hitEntity->GetName();
- }
- //// #include <AzFramework\Components\CameraBus.h>
- //Camera::CameraRequestBus::Event(GetEntityId(), Camera::CameraRequestBus::Events::MakeActiveView);
- }
- void SimpleFPSContoller::GatherAroundHit()
- {
- //virtual AZStd::vector<AZ::EntityId> GatherPhysicalEntitiesAroundPoint(const AZ::Vector3& center, float radius, AZ::u32 query)
- AZStd::vector<AZ::EntityId> result;
- m_gatherPosition = m_hitPosition;
- AZ::Vector3& center = m_gatherPosition;
- float radius = m_gatherRadius;
- AZ::u32 query = AzFramework::PhysicalEntityTypes::All;
- EBUS_EVENT_RESULT(result, AzFramework::PhysicsSystemRequestBus, GatherPhysicalEntitiesAroundPoint, center, radius, query);
- if (result.size())
- {
- }
- }
Add Comment
Please, Sign In to add comment