Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "PongScene.h"
- #include "PhysxManager.h"
- #include "GameObject.h"
- #include "Components.h"
- #include "PhysxProxy.h"
- #include "SpherePrefab.h"
- #include "CubePrefab.h"
- #include "FixedCamera.h"
- PongScene::PongScene()
- :GameScene(L"PongScene")
- {}
- void PongScene::Initialize() {
- //GameContext
- const auto gameContext = GetGameContext();
- GetPhysxProxy()->EnablePhysxDebugRendering(true);
- //PhysX
- auto pPhysx = PhysxManager::GetInstance()->GetPhysics();
- auto pDefaultMaterial = pPhysx->createMaterial(0, 0, 1.f);
- //scene
- GetPhysxProxy()->GetPhysxScene()->setGravity(physx::PxVec3(0, 0, 0));
- //camera
- GameObject* cam = new GameObject();
- cam->GetTransform()->Translate(0, 70, 0);
- cam->GetTransform()->Rotate(90, 0, 0);
- CameraComponent* camComponent = new CameraComponent();
- cam->AddComponent(camComponent);
- //camComponent->UseOrthographicProjection();
- SetActiveCamera(cam->GetComponent<CameraComponent>());
- AddChild(cam);
- //Sphere
- m_Ball = new SpherePrefab();
- m_Ball->GetTransform()->Translate(0, 0, 0);
- auto pRigidbody = new RigidBodyComponent();
- ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
- pRigidbody->SetConstraint(RigidBodyConstraintFlag::TransY, true);
- pRigidbody->SetConstraint(RigidBodyConstraintFlag::RotX, true);
- pRigidbody->SetConstraint(RigidBodyConstraintFlag::RotY, true);
- pRigidbody->SetConstraint(RigidBodyConstraintFlag::RotZ, true);
- //pRigidbody->SetKinematic(false);
- m_Ball->AddComponent(pRigidbody);
- std::shared_ptr<physx::PxGeometry> pSphereGeometry(new physx::PxSphereGeometry(1.f));
- m_Ball->AddComponent(new ColliderComponent(pSphereGeometry, *pDefaultMaterial));
- AddChild(m_Ball);
- pRigidbody->AddForce(physx::PxVec3(20000000, 0, 20000000));
- //LeftBox
- m_BoxLeft = new CubePrefab(4, 4, 10);
- m_BoxLeft->GetTransform()->Translate(-30, 0, 0);
- pRigidbody = new RigidBodyComponent();
- ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
- ///constraintflag maybe
- pRigidbody->SetKinematic(true);
- m_BoxLeft->AddComponent(pRigidbody);
- std::shared_ptr<physx::PxGeometry> pBoxGeometry(new physx::PxBoxGeometry(2.f,2.f,5.f));
- m_BoxLeft->AddComponent(new ColliderComponent(pBoxGeometry, *pDefaultMaterial));
- AddChild(m_BoxLeft);
- //RightBox
- m_BoxRight = new CubePrefab(4,4,10);
- m_BoxRight->GetTransform()->Translate(30, 0, 0);
- pRigidbody = new RigidBodyComponent();
- ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
- ///constraintflag maybe
- pRigidbody->SetKinematic(true);
- m_BoxRight->AddComponent(pRigidbody);
- m_BoxRight->AddComponent(new ColliderComponent(pBoxGeometry, *pDefaultMaterial));
- AddChild(m_BoxRight);
- //arena
- //top
- auto arenaTop = new CubePrefab(80, 4, 4);
- arenaTop->GetTransform()->Translate(0, 0,25);
- pRigidbody = new RigidBodyComponent();
- pRigidbody->SetKinematic(true);
- ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
- ///constraintflag maybe
- arenaTop->AddComponent(pRigidbody);
- std::shared_ptr<physx::PxGeometry> pArenaTop(new physx::PxBoxGeometry(40.f, 2.f, 2.f));
- arenaTop->AddComponent(new ColliderComponent(pArenaTop, *pDefaultMaterial));
- AddChild(arenaTop);
- //bottom
- auto arenaBottom = new CubePrefab(80, 4, 4);
- arenaBottom->GetTransform()->Translate(0, 0, -25);
- pRigidbody = new RigidBodyComponent();
- pRigidbody->SetKinematic(true);
- ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
- ///constraintflag maybe
- arenaBottom->AddComponent(pRigidbody);
- arenaBottom->AddComponent(new ColliderComponent(pArenaTop, *pDefaultMaterial));
- AddChild(arenaBottom);
- //right
- auto arenaRight = new CubePrefab(4, 4, 50);
- arenaRight->GetTransform()->Translate(40, 0, 0);
- pRigidbody = new RigidBodyComponent();
- pRigidbody->SetKinematic(true);
- ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
- ///constraintflag maybe
- arenaRight->AddComponent(pRigidbody);
- std::shared_ptr<physx::PxGeometry> pArenaSide(new physx::PxBoxGeometry(2.f, 2.f, 25.f));
- arenaRight->AddComponent(new ColliderComponent(pArenaSide, *pDefaultMaterial));
- AddChild(arenaRight);
- //left
- auto arenaLeft = new CubePrefab(4, 4, 50);
- arenaLeft->GetTransform()->Translate(-40, 0, 0);
- pRigidbody = new RigidBodyComponent();
- pRigidbody->SetKinematic(true);
- ///pRigidbody->SetCollisionGroup(CollisionGroupFlag::Group0);
- ///constraintflag maybe
- arenaLeft->AddComponent(pRigidbody);
- arenaLeft->AddComponent(new ColliderComponent(pArenaSide, *pDefaultMaterial));
- AddChild(arenaLeft);
- //Triggers
- m_TriggerCall.SetBall(m_Ball);
- //left
- m_LeftTriggerBox = new GameObject;
- m_LeftTriggerBox->GetTransform()->Translate(-30, 0, 0);
- m_LeftTriggerBox->AddComponent(new RigidBodyComponent(true));
- std::shared_ptr<physx::PxGeometry> pTriggerBoxGeometry(new physx::PxBoxGeometry(2.f, 2.f, 20.f));
- auto colliderComp = new ColliderComponent(pTriggerBoxGeometry, *pDefaultMaterial);
- colliderComp->EnableTrigger(true);
- m_LeftTriggerBox->SetOnTriggerCallBack(m_TriggerCall());
- m_LeftTriggerBox->AddComponent(colliderComp);
- AddChild(m_LeftTriggerBox);
- //input
- auto inputActionPlayer1Up = InputAction(Player1Up, InputTriggerState::Down, 'W');
- auto inputActionPlayer1Down = InputAction(Player1Down, InputTriggerState::Down, 'S');
- auto inputActionPlayer2Up = InputAction(Player2Up, InputTriggerState::Down, VK_UP);
- auto inputActionPlayer2Down = InputAction(Player2Down, InputTriggerState::Down, VK_DOWN);
- auto inputActionReset = InputAction(Reset, InputTriggerState::Down, 'R');
- gameContext.pInput->AddInputAction(inputActionPlayer1Up);
- gameContext.pInput->AddInputAction(inputActionPlayer1Down);
- gameContext.pInput->AddInputAction(inputActionPlayer2Up);
- gameContext.pInput->AddInputAction(inputActionPlayer2Down);
- gameContext.pInput->AddInputAction(inputActionReset);
- };
- void PongScene::Update() {
- const auto gameContext = GetGameContext();
- m_Ball->GetComponent<RigidBodyComponent>()->GetPxRigidBody()->setActorFlag(physx::PxActorFlag::eDISABLE_GRAVITY, true);
- float speed{ 30.f*gameContext.pGameTime->GetElapsed() };
- const float limit{19 };
- if (gameContext.pInput->IsActionTriggered(Reset)) {
- m_Reset = true;
- }
- if (m_Reset) {
- m_Ball->GetTransform()->Translate(0, 0, 0);
- m_Ball->GetComponent<RigidBodyComponent>()->ClearForce(physx::PxForceMode::eFORCE);
- m_Ball->GetComponent<RigidBodyComponent>()->AddForce(physx::PxVec3(300, 0, 100));
- m_BoxLeft->GetTransform()->Translate(-20, 0, 0);
- m_BoxRight->GetTransform()->Translate(20, 0, 0);
- m_Reset = false;
- }
- auto posBoxLeft = m_BoxLeft->GetTransform()->GetPosition();
- if (gameContext.pInput->IsActionTriggered(Player1Up)) {
- m_BoxLeft->GetTransform()->Translate(posBoxLeft.x, posBoxLeft.y, std::min(posBoxLeft.z + speed, limit));
- }
- if (gameContext.pInput->IsActionTriggered(Player1Down)) {
- m_BoxLeft->GetTransform()->Translate(posBoxLeft.x, posBoxLeft.y, std::max(posBoxLeft.z - speed, -limit));
- }
- auto posBoxRight = m_BoxRight->GetTransform()->GetPosition();
- if (gameContext.pInput->IsActionTriggered(Player2Up)) {
- m_BoxRight->GetTransform()->Translate(posBoxRight.x, posBoxRight.y, std::min(posBoxRight.z + speed, limit));
- }
- if (gameContext.pInput->IsActionTriggered(Player2Down)) {
- m_BoxRight->GetTransform()->Translate(posBoxRight.x, posBoxRight.y, std::max(posBoxRight.z - speed, -limit));
- }
- }
- void PongScene::Draw()
- {
- }
Add Comment
Please, Sign In to add comment