Guest User

Untitled

a guest
Feb 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "PongScene.h"
  3.  
  4. #include "PhysxManager.h"
  5. #include "GameObject.h"
  6. #include "Components.h"
  7. #include "PhysxProxy.h"
  8. #include "SpherePrefab.h"
  9. #include "CubePrefab.h"
  10. #include "FixedCamera.h"
  11.  
  12.  
  13. PongScene::PongScene()
  14. :GameScene(L"PongScene")
  15. {}
  16.  
  17.  
  18.  
  19.  
  20.  
  21. void PongScene::Initialize() {
  22.  
  23. //GameContext
  24. const auto gameContext = GetGameContext();
  25. GetPhysxProxy()->EnablePhysxDebugRendering(true);
  26.  
  27. //PhysX
  28. auto pPhysx = PhysxManager::GetInstance()->GetPhysics();
  29. auto pDefaultMaterial = pPhysx->createMaterial(0, 0, 1.f);
  30.  
  31.  
  32. //scene
  33. GetPhysxProxy()->GetPhysxScene()->setGravity(physx::PxVec3(0, 0, 0));
  34.  
  35. //camera
  36. GameObject* cam = new GameObject();
  37. cam->GetTransform()->Translate(0, 70, 0);
  38. cam->GetTransform()->Rotate(90, 0, 0);
  39. CameraComponent* camComponent = new CameraComponent();
  40. cam->AddComponent(camComponent);
  41.  
  42. //camComponent->UseOrthographicProjection();
  43. SetActiveCamera(cam->GetComponent<CameraComponent>());
  44. AddChild(cam);
  45.  
  46.  
  47.  
  48. //Sphere
  49. m_Ball = new SpherePrefab();
  50. m_Ball->GetTransform()->Translate(0, 0, 0);
  51.  
  52. auto pRigidbody = new RigidBodyComponent();
  53. ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
  54. pRigidbody->SetConstraint(RigidBodyConstraintFlag::TransY, true);
  55. pRigidbody->SetConstraint(RigidBodyConstraintFlag::RotX, true);
  56. pRigidbody->SetConstraint(RigidBodyConstraintFlag::RotY, true);
  57. pRigidbody->SetConstraint(RigidBodyConstraintFlag::RotZ, true);
  58. //pRigidbody->SetKinematic(false);
  59.  
  60. m_Ball->AddComponent(pRigidbody);
  61.  
  62.  
  63. std::shared_ptr<physx::PxGeometry> pSphereGeometry(new physx::PxSphereGeometry(1.f));
  64. m_Ball->AddComponent(new ColliderComponent(pSphereGeometry, *pDefaultMaterial));
  65.  
  66. AddChild(m_Ball);
  67. pRigidbody->AddForce(physx::PxVec3(20000000, 0, 20000000));
  68.  
  69. //LeftBox
  70. m_BoxLeft = new CubePrefab(4, 4, 10);
  71. m_BoxLeft->GetTransform()->Translate(-30, 0, 0);
  72.  
  73. pRigidbody = new RigidBodyComponent();
  74. ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
  75. ///constraintflag maybe
  76. pRigidbody->SetKinematic(true);
  77. m_BoxLeft->AddComponent(pRigidbody);
  78.  
  79. std::shared_ptr<physx::PxGeometry> pBoxGeometry(new physx::PxBoxGeometry(2.f,2.f,5.f));
  80. m_BoxLeft->AddComponent(new ColliderComponent(pBoxGeometry, *pDefaultMaterial));
  81.  
  82. AddChild(m_BoxLeft);
  83.  
  84. //RightBox
  85. m_BoxRight = new CubePrefab(4,4,10);
  86. m_BoxRight->GetTransform()->Translate(30, 0, 0);
  87.  
  88.  
  89.  
  90. pRigidbody = new RigidBodyComponent();
  91. ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
  92. ///constraintflag maybe
  93. pRigidbody->SetKinematic(true);
  94. m_BoxRight->AddComponent(pRigidbody);
  95.  
  96. m_BoxRight->AddComponent(new ColliderComponent(pBoxGeometry, *pDefaultMaterial));
  97.  
  98. AddChild(m_BoxRight);
  99.  
  100.  
  101. //arena
  102. //top
  103. auto arenaTop = new CubePrefab(80, 4, 4);
  104. arenaTop->GetTransform()->Translate(0, 0,25);
  105.  
  106. pRigidbody = new RigidBodyComponent();
  107. pRigidbody->SetKinematic(true);
  108. ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
  109. ///constraintflag maybe
  110. arenaTop->AddComponent(pRigidbody);
  111. std::shared_ptr<physx::PxGeometry> pArenaTop(new physx::PxBoxGeometry(40.f, 2.f, 2.f));
  112. arenaTop->AddComponent(new ColliderComponent(pArenaTop, *pDefaultMaterial));
  113.  
  114. AddChild(arenaTop);
  115.  
  116. //bottom
  117. auto arenaBottom = new CubePrefab(80, 4, 4);
  118. arenaBottom->GetTransform()->Translate(0, 0, -25);
  119.  
  120. pRigidbody = new RigidBodyComponent();
  121. pRigidbody->SetKinematic(true);
  122. ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
  123. ///constraintflag maybe
  124. arenaBottom->AddComponent(pRigidbody);
  125. arenaBottom->AddComponent(new ColliderComponent(pArenaTop, *pDefaultMaterial));
  126.  
  127. AddChild(arenaBottom);
  128.  
  129. //right
  130. auto arenaRight = new CubePrefab(4, 4, 50);
  131. arenaRight->GetTransform()->Translate(40, 0, 0);
  132.  
  133. pRigidbody = new RigidBodyComponent();
  134. pRigidbody->SetKinematic(true);
  135. ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
  136. ///constraintflag maybe
  137. arenaRight->AddComponent(pRigidbody);
  138. std::shared_ptr<physx::PxGeometry> pArenaSide(new physx::PxBoxGeometry(2.f, 2.f, 25.f));
  139. arenaRight->AddComponent(new ColliderComponent(pArenaSide, *pDefaultMaterial));
  140.  
  141. AddChild(arenaRight);
  142.  
  143. //left
  144. auto arenaLeft = new CubePrefab(4, 4, 50);
  145. arenaLeft->GetTransform()->Translate(-40, 0, 0);
  146.  
  147. pRigidbody = new RigidBodyComponent();
  148. pRigidbody->SetKinematic(true);
  149. ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
  150. ///constraintflag maybe
  151. arenaLeft->AddComponent(pRigidbody);
  152. arenaLeft->AddComponent(new ColliderComponent(pArenaSide, *pDefaultMaterial));
  153.  
  154. AddChild(arenaLeft);
  155.  
  156.  
  157. //Triggers
  158. m_TriggerCall.SetBall(m_Ball);
  159.  
  160. //left
  161. m_LeftTriggerBox = new GameObject;
  162. m_LeftTriggerBox->GetTransform()->Translate(-30, 0, 0);
  163. m_LeftTriggerBox->AddComponent(new RigidBodyComponent(true));
  164. std::shared_ptr<physx::PxGeometry> pTriggerBoxGeometry(new physx::PxBoxGeometry(2.f, 2.f, 20.f));
  165. auto colliderComp = new ColliderComponent(pTriggerBoxGeometry, *pDefaultMaterial);
  166. colliderComp->EnableTrigger(true);
  167. m_LeftTriggerBox->SetOnTriggerCallBack(m_TriggerCall());
  168. m_LeftTriggerBox->AddComponent(colliderComp);
  169.  
  170.  
  171.  
  172.  
  173. AddChild(m_LeftTriggerBox);
  174.  
  175.  
  176.  
  177. //input
  178. auto inputActionPlayer1Up = InputAction(Player1Up, InputTriggerState::Down, 'W');
  179. auto inputActionPlayer1Down = InputAction(Player1Down, InputTriggerState::Down, 'S');
  180.  
  181. auto inputActionPlayer2Up = InputAction(Player2Up, InputTriggerState::Down, VK_UP);
  182. auto inputActionPlayer2Down = InputAction(Player2Down, InputTriggerState::Down, VK_DOWN);
  183.  
  184. auto inputActionReset = InputAction(Reset, InputTriggerState::Down, 'R');
  185.  
  186. gameContext.pInput->AddInputAction(inputActionPlayer1Up);
  187. gameContext.pInput->AddInputAction(inputActionPlayer1Down);
  188. gameContext.pInput->AddInputAction(inputActionPlayer2Up);
  189. gameContext.pInput->AddInputAction(inputActionPlayer2Down);
  190. gameContext.pInput->AddInputAction(inputActionReset);
  191.  
  192. };
  193.  
  194.  
  195. void PongScene::Update() {
  196.  
  197. const auto gameContext = GetGameContext();
  198.  
  199. m_Ball->GetComponent<RigidBodyComponent>()->GetPxRigidBody()->setActorFlag(physx::PxActorFlag::eDISABLE_GRAVITY, true);
  200.  
  201. float speed{ 30.f*gameContext.pGameTime->GetElapsed() };
  202. const float limit{19 };
  203.  
  204. if (gameContext.pInput->IsActionTriggered(Reset)) {
  205. m_Reset = true;
  206. }
  207.  
  208. if (m_Reset) {
  209. m_Ball->GetTransform()->Translate(0, 0, 0);
  210. m_Ball->GetComponent<RigidBodyComponent>()->ClearForce(physx::PxForceMode::eFORCE);
  211. m_Ball->GetComponent<RigidBodyComponent>()->AddForce(physx::PxVec3(300, 0, 100));
  212.  
  213.  
  214. m_BoxLeft->GetTransform()->Translate(-20, 0, 0);
  215. m_BoxRight->GetTransform()->Translate(20, 0, 0);
  216.  
  217. m_Reset = false;
  218. }
  219.  
  220.  
  221. auto posBoxLeft = m_BoxLeft->GetTransform()->GetPosition();
  222. if (gameContext.pInput->IsActionTriggered(Player1Up)) {
  223. m_BoxLeft->GetTransform()->Translate(posBoxLeft.x, posBoxLeft.y, std::min(posBoxLeft.z + speed, limit));
  224. }
  225. if (gameContext.pInput->IsActionTriggered(Player1Down)) {
  226. m_BoxLeft->GetTransform()->Translate(posBoxLeft.x, posBoxLeft.y, std::max(posBoxLeft.z - speed, -limit));
  227. }
  228.  
  229. auto posBoxRight = m_BoxRight->GetTransform()->GetPosition();
  230. if (gameContext.pInput->IsActionTriggered(Player2Up)) {
  231. m_BoxRight->GetTransform()->Translate(posBoxRight.x, posBoxRight.y, std::min(posBoxRight.z + speed, limit));
  232. }
  233. if (gameContext.pInput->IsActionTriggered(Player2Down)) {
  234. m_BoxRight->GetTransform()->Translate(posBoxRight.x, posBoxRight.y, std::max(posBoxRight.z - speed, -limit));
  235. }
  236.  
  237.  
  238.  
  239. }
  240.  
  241.  
  242.  
  243. void PongScene::Draw()
  244. {
  245.  
  246. }
Add Comment
Please, Sign In to add comment