GaelVanhalst

Infiltration: Enemy

Jan 25th, 2016
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.85 KB | None | 0 0
  1. #include "Base\stdafx.h"
  2. #include "Enemy.h"
  3. #include "Components\Components.h"
  4. #include "Prefabs\Prefabs.h"
  5. #include "Scenegraph\GameScene.h"
  6. #include "Physx/PhysxManager.h"
  7. #include "Physx/PhysxProxy.h"
  8. #include "Diagnostics/Logger.h"
  9. #include "Components\ModelComponent.h"
  10. #include "Components\ParticleEmitterComponent.h"
  11. #include "Graphics\ModelAnimator.h"
  12.  
  13. #include "LevelScene.h"
  14. #include "Character.h"
  15.  
  16. static const XMFLOAT3 MATERIAL(0.5f, 0.5f, 0.2f);
  17.  
  18. static const XMFLOAT3 GUNROTATE(0.f,180.f,0.f);
  19. static const XMFLOAT3 GUNTRANSLATE(12.f,6.f,-12.f);
  20.  
  21. static const float HEIGHT=0.8f;
  22. static const float GUNSCALE=0.075f;
  23. static const float MODELSCALE=0.25f;
  24. static const float HEIGHTHEAD=0.60f;
  25. static const float SHOOTDISTANCE=20.f;
  26. static const float SPRINTING=4.f;
  27. static const float LOOKALARMLEVEL=0.25f;
  28. static const float SHOOTALARMLEVEL=0.75f;
  29. static const float FLARETIMER=0.03f;
  30. static const float SHOOTTIMER=0.75f;
  31. static const float ALARMLEVELSTARTINPACT=1.f;
  32. static const float ALARMLEVELSPEEDCAP=3.f;
  33. static const float ALARMLEVELRANDOMROTATION=2.f;
  34. static const float ALARMLEVELRANDOMROTATIONCAP=1.5f;
  35. static const float ANIMATIONSPEED=0.02f;
  36. static const float OTHERENEMYDISTANCE=250.f;
  37.  
  38. static const int ASTANDING=0;
  39. static const int ARUNNING=1;
  40. static const int AROTATING=2;
  41.  
  42. Enemy::Enemy(LevelScene* levelScene,float* alarmLevel, bool doesLoop,float radius, float height, float moveSpeed) :
  43.  
  44.     m_Velocity(0,0,0),
  45.     m_pController(nullptr),
  46.     m_pModel(nullptr),
  47.     m_pLevelScene(levelScene),
  48.     m_pEmitter(nullptr),
  49.     m_pAlarmLevel(alarmLevel),
  50.     m_TotalYaw(0),
  51.     m_RotationSpeed(90.f),
  52.     m_MoveSpeed(moveSpeed),
  53.     m_Radius(radius),
  54.     m_Height(height),
  55.     m_WatchAngle(45.f),
  56.     m_RotateAngle(0.f),
  57.     m_RandomTurningMeter(0),
  58.     m_ShootTimer(0),
  59.     m_FlareTimer(0),
  60.     m_MaxRunVelocity(55.0f),
  61.     m_TerminalVelocity(50),
  62.     m_Gravity(100.f),
  63.     m_RunAccelerationTime(0.3f),
  64.     m_JumpAccelerationTime(0.8f),
  65.     m_RunAcceleration(m_MaxRunVelocity/m_RunAccelerationTime),
  66.     m_JumpAcceleration(m_Gravity/m_JumpAccelerationTime),
  67.     m_RunVelocity(0),
  68.     m_JumpVelocity(0),
  69.     m_CurrentGoal(0),
  70.     m_GotCollisionWithOther(false),
  71.     m_PositiveDirection(true),
  72.     m_Detected(false),
  73.     m_GotTarget(false),
  74.     m_DoesLoop(doesLoop),
  75.     m_RunIntoOtherEnemy(false),
  76.     m_AlignedWithCheckPoint(true),
  77.     m_SetToBeDeleted(false)
  78. {
  79. }
  80.  
  81.  
  82. Enemy::~Enemy(void)
  83. {
  84. }
  85.  
  86. void Enemy::Initialize(const GameContext& gameContext)
  87. {
  88.     UNREFERENCED_PARAMETER(gameContext);
  89.     auto physX = PhysxManager::GetInstance()->GetPhysics();
  90.     auto bouncyMaterial = physX->createMaterial(MATERIAL.x,MATERIAL.y,MATERIAL.z);
  91.     // Create controller
  92.     m_pController = new ControllerComponent(bouncyMaterial,m_Radius,m_Height);
  93.     AddComponent(m_pController);
  94.  
  95.     auto modelObject= new GameObject();
  96.    
  97.     m_pModel = new ModelComponent(L"./Resources/Meshes/EnemyAnimated.ovm");
  98.     m_pModel->SetMaterial(LevelScene::Materials::ENEMY);
  99.  
  100.     modelObject->AddComponent(m_pModel);
  101.     modelObject->GetTransform()->Translate(0,-m_Height*HEIGHT,0);
  102.     modelObject->GetTransform()->Rotate(0,-90,0);
  103.     AddChild(modelObject);
  104.  
  105.     auto gunObject = new GameObject();
  106.  
  107.     auto gunModel= new ModelComponent(L"./Resources/Meshes/GunEnemy.ovm");
  108.     gunModel->SetMaterial(LevelScene::Materials::ENEMYGUN);
  109.     gunObject->AddComponent(gunModel);
  110.  
  111.     gunModel->GetTransform()->Scale(GUNSCALE,GUNSCALE,GUNSCALE);
  112.     gunModel->GetTransform()->Rotate(GUNROTATE);
  113.     gunModel->GetTransform()->Translate(GUNTRANSLATE);
  114.  
  115.     AddChild(gunObject);
  116.    
  117.     //Setting direction right
  118.     XMFLOAT3 direction(m_CheckPoints[m_CurrentGoal].Point.x,0.f,m_CheckPoints[m_CurrentGoal].Point.y);
  119.     direction.x-=this->GetTransform()->GetPosition().x;
  120.     direction.z-=this->GetTransform()->GetPosition().z;
  121.  
  122.     m_TotalYaw=-90;
  123.     GetTransform()->Rotate(0,m_TotalYaw,0,true);
  124.    
  125.     m_pModel->GetTransform()->Scale(MODELSCALE,MODELSCALE,MODELSCALE);
  126.  
  127.     auto emiterObject= new GameObject();
  128.     m_pEmitter= new ParticleEmitterComponent(L"./Resources/Textures/Flare.png",5);
  129.     m_pEmitter->SetVelocity(XMFLOAT3(0.f,0.f,0.f));
  130.     m_pEmitter->SetMinSize(0.5f);
  131.     m_pEmitter->SetMaxSize(1.f);
  132.     m_pEmitter->SetMinEnergy(0.05f);
  133.     m_pEmitter->SetMaxEnergy(0.1f);
  134.     m_pEmitter->SetMinSizeGrow(-0.5f);
  135.     m_pEmitter->SetMaxSizeGrow(-0.1f);
  136.     m_pEmitter->SetMinEmitterRange(0.1f);
  137.     m_pEmitter->SetMaxEmitterRange(0.2f);
  138.     m_pEmitter->SetColor(XMFLOAT4(1.f, 1.f, 1.f, 0.1f));
  139.     m_pEmitter->CanEmitParticles(false);
  140.     emiterObject->AddComponent(m_pEmitter);
  141.     gunObject->AddChild(emiterObject);
  142.     emiterObject->GetTransform()->Translate(-120.f,50.f,0);
  143. }
  144.  
  145. void Enemy::PostInitialize(const GameContext& gameContext)
  146. {
  147.     UNREFERENCED_PARAMETER(gameContext);
  148.     m_pModel->GetAnimator()->SetAnimation(AROTATING);
  149. }
  150.  
  151. void Enemy::Update(const GameContext& gameContext)
  152. {
  153.     //Check if needs to update
  154.     if(m_pLevelScene->IsPaused())return;
  155.     if(m_pLevelScene->IsGameOver())
  156.     {
  157.         m_pModel->GetAnimator()->Pause();
  158.         return;
  159.     }
  160.     if(m_SetToBeDeleted)
  161.     {
  162.         return;
  163.     }
  164.  
  165.     float dTime = gameContext.pGameTime->GetElapsed();
  166.  
  167.     //Fire particles
  168.     if(m_FlareTimer>0)
  169.     {
  170.         m_FlareTimer-=dTime;
  171.         if(m_FlareTimer<=0)
  172.         {
  173.             m_pEmitter->CanEmitParticles(false);
  174.             m_FlareTimer=0;
  175.         }
  176.     }
  177.  
  178.     //Set rotationspeed
  179.     float rotateSpeed = m_RotationSpeed;
  180.  
  181.     //Check if player is in visible range
  182.     XMFLOAT3 forward=GetTransform()->GetForward();
  183.     XMFLOAT3 vectorToPlayer=m_pLevelScene->GetHero()->GetTransform()->GetPosition();
  184.     vectorToPlayer.x-=GetTransform()->GetPosition().x;
  185.     vectorToPlayer.y-=GetTransform()->GetPosition().y;
  186.     vectorToPlayer.z-=GetTransform()->GetPosition().z;
  187.    
  188.     float angleX=(atan2(vectorToPlayer.y, sqrt((vectorToPlayer.x * vectorToPlayer.x) + (vectorToPlayer.z * vectorToPlayer.z)))-atan2(forward.y, sqrt((forward.x * forward.x) + (forward.z * forward.z))))/XM_PI*180;
  189.    
  190.     float angleY=(atan2(vectorToPlayer.x,vectorToPlayer.z)-atan2(forward.x,forward.z)-XM_PIDIV2)/XM_PI*180;
  191.     while(angleY<-180.f)angleY+=360;
  192.     while(angleY>180.f)angleY-=360;
  193.     while(angleX<-180.f)angleX+=360;
  194.     while(angleX>180.f)angleX-=360;
  195.     bool playerDetected=false;
  196.    
  197.     float watchAngle=m_WatchAngle;
  198.     float alarmLevel=*m_pAlarmLevel;
  199.     watchAngle+=5.f*(alarmLevel);
  200.  
  201.     //If player is in visible range do raycast to detect if player isn't hidden behind object
  202.     if(abs(angleX)<=watchAngle&&abs(angleY)<=watchAngle)
  203.     {
  204.         PxVec3 rayDirection=PxVec3(vectorToPlayer.x,vectorToPlayer.y,vectorToPlayer.z);
  205.         PxVec3 rayOrigin=PxVec3(GetTransform()->GetPosition().x,GetTransform()->GetPosition().y+(m_Height*HEIGHTHEAD),GetTransform()->GetPosition().z);
  206.         rayDirection.normalize();
  207.         rayOrigin+=rayDirection*SHOOTDISTANCE;
  208.    
  209.         PxRaycastBuffer buf;
  210.    
  211.         if(GetScene()->GetPhysxProxy()->Raycast(rayOrigin, rayDirection, PX_MAX_F32, buf))
  212.         {
  213.             if(buf.block.actor!=nullptr)
  214.             {
  215.                 if(buf.block.actor->userData!=nullptr)
  216.                 {
  217.                     BaseComponent* component=static_cast<BaseComponent*>(buf.block.actor->userData);
  218.                     //Check if player is seen
  219.                     if(component->GetGameObject()->WhatKindOfObject()==L"Character")
  220.                     {
  221.                         playerDetected=true;
  222.                         m_RotateAngle=angleY;
  223.                         rotateSpeed*=SPRINTING;
  224.                         m_pLevelScene->RaiseAlarmLevel(dTime*LOOKALARMLEVEL);
  225.                     }
  226.                 }
  227.             }
  228.             if(playerDetected)
  229.             {
  230.                 //Shoot and reload
  231.                 if(!m_Detected)
  232.                 {
  233.                     m_pModel->GetAnimator()->SetAnimation(ASTANDING);
  234.                     m_ShootTimer=SHOOTALARMLEVEL;
  235.                     m_Detected=true;
  236.                 }
  237.                 else
  238.                 {
  239.                     m_ShootTimer-=dTime;
  240.                     if(m_ShootTimer<=0)
  241.                     {
  242.                         m_ShootTimer=SHOOTTIMER;
  243.                         PxVec3 aimVector(forward.x,rayDirection.y,forward.z);
  244.                         aimVector.normalize();
  245.  
  246.                         XMFLOAT3 directionFlare=GetTransform()->GetRight();
  247.                         directionFlare.x*=20;
  248.                         directionFlare.y*=20;
  249.                         directionFlare.z*=20;
  250.                         m_FlareTimer=FLARETIMER;
  251.                         m_pEmitter->SetVelocity(directionFlare);
  252.                         m_pEmitter->CanEmitParticles(true);
  253.                        
  254.                         if(GetScene()->GetPhysxProxy()->Raycast(rayOrigin, rayDirection, PX_MAX_F32, buf))
  255.                         {
  256.                             if(buf.block.actor!=nullptr)
  257.                             {
  258.                                 if(buf.block.actor->userData!=nullptr)
  259.                                 {
  260.                                     BaseComponent* component=static_cast<BaseComponent*>(buf.block.actor->userData);
  261.                                     if(component->GetGameObject()->WhatKindOfObject()==L"Character")
  262.                                     {
  263.                                         wcout<<"Hit"<<endl;
  264.                                         m_pLevelScene->GetHero()->GetsShot();
  265.                                     }
  266.                                     else wcout<<"Miss"<<endl;
  267.                                 }
  268.                             }
  269.                             m_pLevelScene->RaiseAlarmLevel(SHOOTALARMLEVEL);
  270.                         }
  271.                     }
  272.                 }
  273.             }
  274.         }
  275.     }
  276.    
  277.     if(!playerDetected)
  278.     {
  279.         if(m_Detected)m_pModel->GetAnimator()->SetAnimation(ARUNNING);
  280.         m_Detected=false;
  281.         float curVelY = m_Velocity.y;
  282.         m_Velocity.y = 0;
  283.    
  284.         float maxRunVelocity=m_MaxRunVelocity;
  285.         float runAcceleration=m_RunAcceleration;
  286.        
  287.         //Alarmlevel behaviour
  288.         if(alarmLevel>=ALARMLEVELSTARTINPACT)
  289.         {
  290.             //Higher speed
  291.             float value(alarmLevel);
  292.             if(value>ALARMLEVELSPEEDCAP)value=ALARMLEVELSPEEDCAP;
  293.             maxRunVelocity*=value;
  294.             runAcceleration*=value;
  295.             rotateSpeed*=value;
  296.    
  297.             //Randomly rotate
  298.             if(alarmLevel>=2&&m_AlignedWithCheckPoint)
  299.             {
  300.                 value=alarmLevel-ALARMLEVELRANDOMROTATION;
  301.                 if(value>ALARMLEVELRANDOMROTATIONCAP)value=ALARMLEVELRANDOMROTATIONCAP;
  302.                 value=(float)(rand()%(((UINT)((value)*100)/25)+1));
  303.                 m_RandomTurningMeter+=value*dTime;
  304.    
  305.                 if(m_RandomTurningMeter>=1)
  306.                 {
  307.                     m_RandomTurningMeter--;
  308.                     m_AlignedWithCheckPoint=false;
  309.                     if(rand()%2==0)m_RotateAngle=-360.f;
  310.                     else m_RotateAngle=360.f;
  311.                 }
  312.             }
  313.         }
  314.         //Movement
  315.         if(!m_GotTarget&&m_AlignedWithCheckPoint)
  316.         {
  317.             m_RunVelocity+=runAcceleration*dTime;
  318.             if(m_RunVelocity > maxRunVelocity) m_RunVelocity = maxRunVelocity;
  319.             m_pModel->GetAnimator()->SetAnimationSpeed(m_RunVelocity*ANIMATIONSPEED);
  320.  
  321.             XMFLOAT3 direction(m_CheckPoints[m_CurrentGoal].Point.x,0.f,m_CheckPoints[m_CurrentGoal].Point.y);
  322.             direction.x-=this->GetTransform()->GetPosition().x;
  323.             direction.z-=this->GetTransform()->GetPosition().z;
  324.             XMVECTOR tempVec = XMLoadFloat3(&direction);
  325.    
  326.             float angleA = (float)atan2(1,0);
  327.             float angleB = (float)atan2(direction.x,direction.z);
  328.             float angle= angleB - angleA;
  329.             m_TotalYaw=angle/XM_PI*180;
  330.    
  331.             this->GetTransform()->Rotate(0,m_TotalYaw,0,true);
  332.    
  333.             //check if a direction is pressed
  334.    
  335.             XMVECTOR velVec = XMLoadFloat3(&m_Velocity);
  336.             tempVec = XMVector3Normalize(tempVec);
  337.             //apply scale
  338.             tempVec = XMVectorScale(tempVec,m_RunVelocity);
  339.             //smooth out by adding current velocity and normalizing
  340.            
  341.             tempVec = XMVectorAdd(tempVec,velVec);
  342.             tempVec = XMVector3Normalize(tempVec);
  343.             //scaling again since we normalized the velocity
  344.             tempVec = XMVectorScale(tempVec,m_RunVelocity);
  345.            
  346.             //THIS WILL ONLY STORE X AND Z, Y APPLIED AFTERWARDS IN THE JUMP FUNC;
  347.             XMStoreFloat3(&m_Velocity, tempVec);
  348.            
  349.             //restore Y velocity
  350.             m_Velocity.y = curVelY;
  351.    
  352.             //Gravity
  353.             //*******
  354.             if(!m_pController->GetCollisionFlags().isSet(PxControllerCollisionFlag::eCOLLISION_DOWN))
  355.             {
  356.                 m_Velocity.y -= m_JumpAcceleration*dTime;
  357.                 if(m_Velocity.y < -m_TerminalVelocity)
  358.                 {
  359.                     m_Velocity.y = -m_TerminalVelocity;
  360.                 }
  361.             }
  362.             else
  363.             {
  364.                 m_Velocity.y = 0;
  365.             }
  366.            
  367.             tempVec = XMLoadFloat3(&m_Velocity);
  368.            
  369.             tempVec = XMVectorScale(tempVec, dTime);
  370.    
  371.             XMFLOAT3 distanceToMove;
  372.    
  373.             //Check if it doesn't go over the point
  374.    
  375.             XMStoreFloat3(&distanceToMove,tempVec);
  376.    
  377.             if((direction.x*direction.x)+(direction.z*direction.z)<=(distanceToMove.x*distanceToMove.x)+(distanceToMove.z*distanceToMove.z))
  378.             {
  379.                 distanceToMove.x=direction.x;
  380.                 distanceToMove.z=direction.z;
  381.                 m_GotTarget=true;
  382.             }
  383.             m_pController->Move(distanceToMove);
  384.            
  385.             //Check if sees other enemy and change direction if so
  386.             PxVec3 rayDirection=PxVec3(GetTransform()->GetRight().x,GetTransform()->GetRight().y,GetTransform()->GetRight().z);
  387.             PxVec3 rayOrigin=PxVec3(GetTransform()->GetPosition().x,GetTransform()->GetPosition().y,GetTransform()->GetPosition().z);
  388.             rayDirection.normalize();
  389.             rayOrigin+=rayDirection*20;
  390.             rayOrigin.y-=25;
  391.    
  392.             PxRaycastBuffer buf;
  393.    
  394.             if(GetScene()->GetPhysxProxy()->Raycast(rayOrigin, rayDirection, OTHERENEMYDISTANCE, buf,PxHitFlags(PxHitFlag::eDEFAULT)))
  395.             {
  396.                 if(buf.block.actor!=nullptr)
  397.                 {
  398.                     if(buf.block.actor->userData!=nullptr)
  399.                     {
  400.                         BaseComponent* component=static_cast<BaseComponent*>(buf.block.actor->userData);
  401.                         if(component->GetGameObject()->WhatKindOfObject()==WhatKindOfObject())
  402.                         {
  403.                             m_GotTarget=true;
  404.                             m_PositiveDirection=!m_PositiveDirection;
  405.                             m_RunIntoOtherEnemy=true;
  406.                         }
  407.                     }
  408.                 }
  409.             }
  410.            
  411.         }
  412.         else
  413.         {
  414.             if(!m_pController->GetCollisionFlags().isSet(PxControllerCollisionFlag::eCOLLISION_DOWN))
  415.             {
  416.                 m_Velocity.y -= m_JumpAcceleration*dTime;
  417.                 if(m_Velocity.y < -m_TerminalVelocity)
  418.                 {
  419.                     m_Velocity.y = -m_TerminalVelocity;
  420.                 }
  421.            
  422.             }
  423.             else
  424.             {
  425.                 m_Velocity.y = 0;
  426.             }
  427.             m_Velocity.x=0;
  428.             m_Velocity.z=0;
  429.    
  430.             auto tempVec = XMLoadFloat3(&m_Velocity);
  431.            
  432.             tempVec = XMVectorScale(tempVec, dTime);
  433.    
  434.             XMFLOAT3 distanceToMove;
  435.    
  436.             //Check if it doesn't go over the point
  437.    
  438.             XMStoreFloat3(&distanceToMove,tempVec);
  439.    
  440.             m_pController->Move(distanceToMove);
  441.         }
  442.     }
  443.     if(!m_AlignedWithCheckPoint||playerDetected)
  444.     {
  445.         //Rotate to wanted direction
  446.  
  447.         float rotateAngle=rotateSpeed*dTime;
  448.         if(abs(m_RotateAngle)<=rotateAngle)
  449.         {
  450.             m_TotalYaw+=m_RotateAngle;
  451.             GetTransform()->Rotate(0,m_TotalYaw,0);
  452.             m_AlignedWithCheckPoint=true;
  453.             m_RotateAngle=0;
  454.             m_pModel->GetAnimator()->SetAnimation(1);
  455.         }
  456.         else
  457.         {
  458.             if(m_RotateAngle<0)rotateAngle*=-1;
  459.             m_TotalYaw+=rotateAngle;
  460.    
  461.             GetTransform()->Rotate(0,m_TotalYaw,0);
  462.             m_RotateAngle-=rotateAngle;
  463.         }
  464.     }
  465.    
  466.    
  467.     if(m_GotTarget)
  468.     {
  469.         //Reached checkpoint and need to go to next point
  470.         ChooseCheckPoint();
  471.     }
  472.  
  473. }
  474.  
  475. void Enemy::AddingCheckPoint(const XMFLOAT2 point,const bool turnPoint)
  476. {
  477.     m_CheckPoints.push_back(CheckPoint(point,turnPoint));
  478. }
  479.  
  480. wstring Enemy::WhatKindOfObject()
  481. {
  482.     return L"enemy";
  483. }
  484.  
  485. float Enemy::GetYaw()
  486. {
  487.     return m_TotalYaw+90.f;
  488. }
  489.  
  490. void Enemy::PauseAnimations(bool pause)
  491. {
  492.     if(pause)m_pModel->GetAnimator()->Pause();
  493.     else m_pModel->GetAnimator()->Play();
  494. }
  495.  
  496. void Enemy::StopParticles()
  497. {
  498.     m_pEmitter->CanEmitParticles(false);
  499. }
  500.  
  501. void Enemy::GetsKilled()
  502. {
  503.     m_SetToBeDeleted=true;
  504.     m_pLevelScene->RequestObjectForDeletion(this);
  505. }
  506.  
  507. bool Enemy::WillBeDeleted()
  508. {
  509.     return m_SetToBeDeleted;
  510. }
  511.  
  512. //Choose next checkpoint, based on path behaviour and direction
  513. void Enemy::ChooseCheckPoint()
  514. {
  515.     m_pModel->GetAnimator()->SetAnimation(AROTATING);
  516.     if(m_PositiveDirection)
  517.     {
  518.         if((UINT)m_CurrentGoal+1>=m_CheckPoints.size())
  519.         {
  520.             if(m_DoesLoop)
  521.             {
  522.                 m_CurrentGoal=0;
  523.             }
  524.             else
  525.             {
  526.                 m_PositiveDirection=false;
  527.                 m_CurrentGoal--;
  528.                 if(m_CurrentGoal>=m_CheckPoints.size())m_CurrentGoal=0;
  529.             }
  530.         }
  531.         else m_CurrentGoal++;
  532.     }
  533.     else
  534.     {
  535.         if((int)m_CurrentGoal-1<0)
  536.         {
  537.             if(m_DoesLoop)
  538.             {
  539.                 m_CurrentGoal=m_CheckPoints.size()-1;
  540.             }
  541.             else
  542.             {
  543.                 m_PositiveDirection=true;
  544.                 m_CurrentGoal++;
  545.                 if(m_CurrentGoal>=m_CheckPoints.size())m_CurrentGoal=0;
  546.             }
  547.         }
  548.         else m_CurrentGoal--;
  549.     }
  550.     m_GotTarget=false;
  551.     m_AlignedWithCheckPoint=false;
  552.  
  553.  
  554.     XMFLOAT3 direction(m_CheckPoints[m_CurrentGoal].Point.x,0.f,m_CheckPoints[m_CurrentGoal].Point.y);
  555.     direction.x-=this->GetTransform()->GetPosition().x;
  556.     direction.z-=this->GetTransform()->GetPosition().z;
  557.     XMFLOAT3 forward(GetTransform()->GetForward());
  558.     float angleA = (float)atan2(forward.x,forward.z);
  559.     float angleB = (float)(atan2(direction.x,direction.z)-XM_PIDIV2);
  560.     float angle= angleB - angleA;
  561.  
  562.     if(angle<0)angle+=XM_2PI;
  563.  
  564.     if(angle<=XM_PI)
  565.     {
  566.         if(m_CheckPoints[m_CurrentGoal].TurnPoint&&!m_RunIntoOtherEnemy)angle+=XM_2PI;
  567.     }
  568.     else
  569.     {
  570.         angle-=XM_2PI;
  571.         if(m_CheckPoints[m_CurrentGoal].TurnPoint&&!m_RunIntoOtherEnemy)angle-=XM_2PI;
  572.     }
  573.     m_RunIntoOtherEnemy=false;
  574.     m_RotateAngle=angle/XM_PI*180;
  575. }
Advertisement
Add Comment
Please, Sign In to add comment