Guest User

Untitled

a guest
Sep 6th, 2015
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. #include "Common.h"
  2.  
  3. class MyApp : public Application
  4. {
  5. public:
  6.     SharedPtr<Camera> camera;
  7.     SharedPtr<Node> cameraNode;
  8.     SharedPtr<Viewport> vp;
  9.     SharedPtr<Node> playerNode;
  10.     SharedPtr<RigidBody> playerRigidbody;
  11.     SharedPtr<Scene> scene_;
  12.     SharedPtr<Node> sbNode_;
  13.  
  14.     DebugHud* debugHud = 0;
  15.     Console* console = 0;
  16.  
  17.     MyApp(Context* context) :
  18.         Application(context)
  19.     {
  20.  
  21.     }
  22.  
  23.     virtual void Setup()
  24.     {
  25.         // Called before engine initialization. engineParameters_ member variable can be modified here
  26.         engineParameters_["WindowTitle"] = "SoftBody Test";
  27.         engineParameters_["FullScreen"] = false;
  28.         engineParameters_["WindowWidth"] = 1280;
  29.         engineParameters_["WindowHeight"] = 720;
  30.         //engineParameters_["VSync"] = true;
  31.         engineParameters_["LogName"] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("urho3d", "logs") + GetTypeName() + ".log";
  32.     }
  33.  
  34.     virtual void Start()
  35.     {
  36.  
  37.  
  38.         CreateDubugHud();
  39.         LoadScene("GameScene.xml");
  40.         SetupPlayer();
  41.  
  42.         sbNode_ = scene_->GetChild("SoftBody", true);
  43.         SoftBody* sb = sbNode_->CreateComponent<SoftBody>();
  44.         //sb->SetCollisionLayerAndMask(1, 3);
  45.         sb->UpdateSoftBodyFromStaticModel();
  46.  
  47.  
  48.         // Called after engine initialization. Setup application & subscribe to events here
  49.         SubscribeToEvent(E_KEYDOWN, HANDLER(MyApp, HandleKeyDown));
  50.         SubscribeToEvent(E_UPDATE, HANDLER(MyApp, HandleUpdate));
  51.         SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(MyApp, HandleMouseButton));
  52.  
  53.     }
  54.  
  55.     void CreateDubugHud()
  56.     {
  57.         // Get default style
  58.         ResourceCache* cache = GetSubsystem<ResourceCache>();
  59.         XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  60.  
  61.         // Create console
  62.         Console* console = engine_->CreateConsole();
  63.         console->SetDefaultStyle(xmlFile);
  64.         console->GetBackground()->SetOpacity(0.8f);
  65.  
  66.         // Create debug HUD.
  67.         DebugHud* debugHud = engine_->CreateDebugHud();
  68.         debugHud->SetDefaultStyle(xmlFile);
  69.  
  70.        
  71.     }
  72.  
  73.     virtual void Stop()
  74.     {
  75.         // Perform optional cleanup after main loop has terminated
  76.     }
  77.  
  78.     void HandleMouseButton(StringHash eventType, VariantMap& eventData)
  79.     {
  80.         using namespace MouseButtonDown;
  81.         int button = eventData[P_BUTTON].GetInt();
  82.         if (button == MOUSEB_LEFT)
  83.         {
  84.             static ResourceCache* cache = GetSubsystem<ResourceCache>();
  85.             Node* node = scene_->CreateChild("bullet");
  86.             Vector3 pos = cameraNode->GetWorldPosition();
  87.             node->SetWorldPosition(pos);
  88.  
  89.             StaticModel* sm = node->CreateComponent<StaticModel>();
  90.             sm->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  91.             RigidBody* rb = node->CreateComponent<RigidBody>();
  92.             rb->SetMass(10.0f);
  93.             rb->SetCollisionMask(1);
  94.             CollisionShape* c = node->CreateComponent<CollisionShape>();
  95.             c->SetModel(sm->GetModel());
  96.             c->SetShapeType(ShapeType::SHAPE_BOX);
  97.             c->SetBox(Vector3::ONE);
  98.  
  99.             Vector3 vel = cameraNode->GetWorldRotation() * cameraNode->GetWorldDirection().FORWARD * 30.0f;
  100.             rb->SetLinearVelocity(vel);
  101.         }
  102.    
  103.     }
  104.  
  105.     void HandleKeyDown(StringHash eventType, VariantMap& eventData)
  106.     {
  107.         using namespace KeyDown;
  108.  
  109.         // Check for pressing ESC. Note the engine_ member variable for convenience access to the Engine object
  110.         int key = eventData[P_KEY].GetInt();
  111.        
  112.         // Close console (if open) or exit when ESC is pressed
  113.         if (key == KEY_ESC)
  114.         {
  115.             Console* console = GetSubsystem<Console>();
  116.             if (console->IsVisible())
  117.                 console->SetVisible(false);
  118.             else
  119.                 engine_->Exit();
  120.         }
  121.         // Toggle console with F1
  122.         else if (key == KEY_F1)
  123.             GetSubsystem<Console>()->Toggle();
  124.  
  125.         // Toggle debug HUD with F2
  126.         else if (key == KEY_F2)
  127.             GetSubsystem<DebugHud>()->ToggleAll();
  128.  
  129.     }
  130.  
  131.     void HandleUpdate(StringHash eventType, VariantMap& eventData)
  132.     {
  133.         using namespace Update;
  134.  
  135.         // Take the frame time step, which is stored as a float
  136.         float timeStep = eventData[P_TIMESTEP].GetFloat();
  137.  
  138. #ifdef URHO3D_PROFILING
  139.         Profiler* profiler = GetSubsystem<Profiler>();
  140.         if (profiler)
  141.             profiler->BeginBlock("MoveCamera");
  142. #endif
  143.  
  144.         MoveCamera(timeStep);
  145.  
  146.  
  147. #ifdef URHO3D_PROFILING
  148.         profiler->EndBlock();
  149. #endif
  150. }
  151.  
  152.     void LoadScene(String sceneFileName)
  153.     {
  154.         scene_ = SharedPtr<Scene>(new Scene(context_));
  155.         File sceneFile(context_, context_->GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/" + sceneFileName, FILE_READ);
  156.         scene_->LoadXML(sceneFile);
  157.     }
  158.  
  159.     void SetupPlayer()
  160.     {
  161.         cameraNode = scene_->GetChild("cameraNode", true);
  162.         if (cameraNode)
  163.         {
  164.             camera = cameraNode->GetComponent<Camera>();
  165.             if (camera)
  166.             {
  167.                 vp = SharedPtr<Viewport>(new Viewport(context_, scene_, camera));
  168.                 Renderer* renderer = GetSubsystem<Renderer>();
  169.                 renderer->SetViewport(0, vp);
  170.             }
  171.         }
  172.  
  173.         playerNode = scene_->GetChild("playerNode", true);
  174.         playerRigidbody = playerNode->GetComponent<RigidBody>();
  175.         playerRigidbody->SetMass(0);
  176.     }
  177.  
  178.     void MoveCamera(float timeStep)
  179.     {
  180.         static float yaw_ = 0.0f;
  181.         static float pitch_ = 0.0f;
  182.  
  183.         Input* input = GetSubsystem<Input>();
  184.  
  185.         const float MOVE_SPEED = 20.0f;
  186.         const float MOUSE_SENSITIVITY = 0.1f;
  187.  
  188.         IntVector2 mouseMove = input->GetMouseMove();
  189.         yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  190.         pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  191.         pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  192.  
  193.         cameraNode->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  194.  
  195.         Quaternion camRotation = cameraNode->GetWorldRotation();
  196.         camRotation.z_ = 0.0f;
  197.  
  198.         Vector3 dir = Vector3::ZERO;
  199.  
  200.  
  201.         if (input->GetKeyDown('W'))
  202.             dir += Vector3::FORWARD;
  203.  
  204.         if (input->GetKeyDown('S'))
  205.             dir += Vector3::BACK;
  206.  
  207.         if (input->GetKeyDown('A'))
  208.             dir += Vector3::LEFT;
  209.  
  210.         if (input->GetKeyDown('D'))
  211.             dir += Vector3::RIGHT;
  212.  
  213.         if (input->GetKeyDown(KEY_SPACE))
  214.         {
  215.             dir += Vector3::UP;
  216.         }
  217.  
  218.         if (dir.Length() > 0)
  219.         {
  220.             Vector3 curVel = playerRigidbody->GetLinearVelocity();
  221.             if (curVel.Length() < 5.0f)
  222.             {
  223.                 float multipler = 1.0f;
  224.                 //if (input->GetQualifierDown(KEY_SHIFT))
  225.                 if (input->GetKeyDown(KEY_LSHIFT))
  226.                 {
  227.                     multipler = 2.0f;
  228.                 }
  229.                 //playerRigidbody->SetLinearVelocity(multipler * 500.0f * (camRotation * dir) * timeStep);
  230.                 playerNode->Translate(camRotation * dir * timeStep * 10.0f);
  231.             }
  232.         }
  233.     }
  234. };
  235.  
  236. DEFINE_APPLICATION_MAIN(MyApp)
Advertisement
Add Comment
Please, Sign In to add comment