Advertisement
Guest User

Untitled

a guest
Aug 17th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.63 KB | None | 0 0
  1. #include <string>
  2. #include <sstream>
  3.  
  4. #include <Urho3D/Core/CoreEvents.h>
  5. #include <Urho3D/Engine/Application.h>
  6. #include <Urho3D/Engine/Engine.h>
  7. #include <Urho3D/Engine/EngineDefs.h>
  8. #include <Urho3D/Input/Input.h>
  9. #include <Urho3D/Input/InputEvents.h>
  10. #include <Urho3D/Resource/ResourceCache.h>
  11. #include <Urho3D/Resource/XMLFile.h>
  12. #include <Urho3D/IO/Log.h>
  13. #include <Urho3D/UI/UI.h>
  14. #include <Urho3D/UI/Text.h>
  15. #include <Urho3D/UI/Font.h>
  16. #include <Urho3D/UI/Button.h>
  17. #include <Urho3D/UI/UIEvents.h>
  18. #include <Urho3D/Scene/Scene.h>
  19. #include <Urho3D/Scene/SceneEvents.h>
  20. #include <Urho3D/Graphics/Graphics.h>
  21. #include <Urho3D/Graphics/Camera.h>
  22. #include <Urho3D/Graphics/Geometry.h>
  23. #include <Urho3D/Graphics/Renderer.h>
  24. #include <Urho3D/Graphics/DebugRenderer.h>
  25. #include <Urho3D/Graphics/Octree.h>
  26. #include <Urho3D/Graphics/Light.h>
  27. #include <Urho3D/Graphics/Model.h>
  28. #include <Urho3D/Graphics/StaticModel.h>
  29. #include <Urho3D/Graphics/Material.h>
  30. #include <Urho3D/Graphics/Skybox.h>
  31.  
  32. // Alternatively, you can replace all above Urho3D include statements by the single following one:
  33. // #include <Urho3D/Urho3DAll.h>
  34.  
  35. #include <Urho3D/Graphics/AnimatedModel.h>
  36. #include <Urho3D/Graphics/AnimationController.h>
  37. #include <Urho3D/Physics/CollisionShape.h>
  38. #include <Urho3D/Physics/PhysicsWorld.h>
  39. #include <Urho3D/Physics/RigidBody.h>
  40.  
  41. using namespace Urho3D;
  42. /**
  43. * Using the convenient Application API we don't have
  44. * to worry about initializing the engine or writing a main.
  45. * You can probably mess around with initializing the engine
  46. * and running a main manually, but this is convenient and portable.
  47. */
  48. class PlatfTest : public Application
  49. {
  50.     SharedPtr<Text> text_;
  51.     SharedPtr<Scene> scene_;
  52.     SharedPtr<Node> cameraNode_;
  53.     SharedPtr<Node> chara_;
  54.     bool drawDebug_=true;
  55.     int cameraMode_=1;
  56.  
  57. public:
  58.  
  59.     /**
  60.     * This happens before the engine has been initialized
  61.     * so it's usually minimal code setting defaults for
  62.     * whatever instance variables you have.
  63.     * You can also do this in the Setup method.
  64.     */
  65.     PlatfTest(Context * context) : Application(context)
  66.     {
  67.     }
  68.  
  69.     /**
  70.     * This method is called before the engine has been initialized.
  71.     * Thusly, we can setup the engine parameters before anything else
  72.     * of engine importance happens (such as windows, search paths,
  73.     * resolution and other things that might be user configurable).
  74.     */
  75.     virtual void Setup()
  76.     {
  77.         engineParameters_["FullScreen"] = false;
  78.         engineParameters_["WindowWidth"]=800;
  79.         engineParameters_["WindowHeight"]=600;
  80.     }
  81.     /**
  82.     * This method is called after the engine has been initialized.
  83.     * This is where you set up your actual content, such as scenes,
  84.     * models, controls and what not. Basically, anything that needs
  85.     * the engine initialized and ready goes in here.
  86.     */
  87.     virtual void Start()
  88.     {
  89.         //SubscribeToEvent(E_KEYDOWN,URHO3D_HANDLER(PlatfTest,HandleKeyDown));
  90.         GetSubsystem<Input>()->SetMouseVisible(true);
  91.  
  92.         ResourceCache* cache=GetSubsystem<ResourceCache>();
  93.         //GetSubsystem<UI>()->GetRoot()->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  94.  
  95.         text_=new Text(context_);
  96.         text_->SetText("Text is doing its best now and is preparing. Please watch warmly until it is ready.");
  97.         text_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 13); // SetFont apparently NEEDS to be set.
  98.         text_->SetColor(Color(1, 0, 0));
  99.         GetSubsystem<UI>()->GetRoot()->AddChild(text_);
  100.  
  101.         /** Start of scene preparation. */
  102.         scene_=new Scene(context_);
  103.         scene_->CreateComponent<Octree>();
  104.         scene_->CreateComponent<PhysicsWorld>();
  105.         scene_->CreateComponent<DebugRenderer>();
  106.  
  107.         Node* floorNode=scene_->CreateChild("Floor");
  108.         floorNode->SetWorldPosition(Vector3(140,-6,15)); // X and Z = lateral position, Y = altitude
  109.         floorNode->SetScale(Vector3(300,3,30));
  110.         StaticModel* floorModel=floorNode->CreateComponent<StaticModel>();
  111.         floorModel->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  112.         floorModel->SetMaterial(cache->GetResource<Material>("Materials/Terrain.xml"));
  113.         /*RigidBody* floorBody = */floorNode->CreateComponent<RigidBody>(); // RigidBody can stay at default values.
  114.         CollisionShape* floorShape = floorNode->CreateComponent<CollisionShape>();
  115.         floorShape->SetBox(Vector3::ONE); // Box automatically scales to fit the scale of the object so just leave this at 1.
  116.  
  117.         Node* skyNode=scene_->CreateChild("Sky");
  118.         Skybox* skybox=skyNode->CreateComponent<Skybox>();
  119.         skybox->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  120.         skybox->SetMaterial(cache->GetResource<Material>("Materials/Skybox.xml"));
  121.  
  122.         Node* lightNode=scene_->CreateChild("DirectionalLight");
  123.         lightNode->SetDirection(Vector3(0,-5,2)); // The direction of the shade.
  124.         Light* light=lightNode->CreateComponent<Light>();
  125.         light->SetLightType(LIGHT_DIRECTIONAL);
  126.  
  127.         cameraNode_=scene_->CreateChild("Camera");
  128.         Camera* camera=cameraNode_->CreateComponent<Camera>();
  129.  
  130.         Renderer* renderer=GetSubsystem<Renderer>();
  131.         SharedPtr<Viewport> viewport(new Viewport(context_,scene_,cameraNode_->GetComponent<Camera>()));
  132.         renderer->SetViewport(0, viewport);
  133.  
  134.         SubscribeToEvent(E_UPDATE,URHO3D_HANDLER(PlatfTest,HandleUpdate));
  135.         SubscribeToEvent(E_POSTRENDERUPDATE,URHO3D_HANDLER(PlatfTest,HandlePostRenderUpdate));
  136.         /** End of scene preparation. */
  137.  
  138.         /** Start of code for our good old platformer bopper. */
  139.         Node* charaNode=scene_->CreateChild("Bopper");
  140.         charaNode->SetWorldPosition(Vector3(0,1,2.5));
  141.         // This turns the model in case it needs to be done, as it is in this case.
  142.         Node* adjustNode = charaNode->CreateChild("AdjNode");
  143.         adjustNode->SetRotation(Quaternion(180, Vector3(0,1,0))); // Turn it 180 degrees.
  144.         // Prepare model and enable animations
  145.         AnimatedModel* animChara = adjustNode->CreateComponent<AnimatedModel>();
  146.         animChara->SetModel(cache->GetResource<Model>("Models/BetaLowpoly/Beta.mdl"));
  147.         animChara->SetMaterial(0, cache->GetResource<Material>("BetaBody_MAT.xml")); // Material settings, as seen on the model's Readme file
  148.         animChara->SetMaterial(1, cache->GetResource<Material>("BetaBody_MAT.xml"));
  149.         animChara->SetMaterial(2, cache->GetResource<Material>("BetaJoints_MAT.xml"));
  150.         adjustNode->CreateComponent<AnimationController>();
  151.         // Prepare physics
  152.         RigidBody* body = charaNode->CreateComponent<RigidBody>();
  153.         body->SetMass(1); // Or it won't be affected by gravity.
  154.         body->SetFriction(10);
  155.         body->SetRestitution(0);
  156.         body->SetAngularFactor(Vector3::ZERO); // Physics alone can't rotate this body horizontally. (Here, only the code should)
  157.         body->SetCollisionEventMode(COLLISION_ALWAYS);
  158.         // Prepare collision box
  159.         CollisionShape* shape = charaNode->CreateComponent<CollisionShape>();
  160.         shape->SetBox(Vector3(0.5,2,0.5), Vector3(0,1,0));
  161.         chara_=charaNode;
  162.         /** End of bopper start-up code. */
  163.     }
  164.     /**
  165.     * Good place to get rid of any system resources that requires the
  166.     * engine still initialized. You could do the rest in the destructor,
  167.     * but there's no need, this method will get called when the engine stops,
  168.     * for whatever reason (short of a segfault).
  169.     */
  170.     virtual void Stop()
  171.     {
  172.     }
  173.     /**
  174.     * Your non-rendering logic should be handled here.
  175.     * This could be moving objects, checking collisions and reaction, etc.
  176.     */
  177.     void HandleUpdate(StringHash eventType,VariantMap& eventData)
  178.     {
  179.         Input* input=GetSubsystem<Input>();
  180.         Vector3 moveDir=Vector3::ZERO;
  181.         float cameraXPos=chara_->GetWorldPosition().x_-(chara_->GetWorldDirection().x_*(Vector3::RIGHT.x_*5));
  182.         float cameraYPoint=chara_->GetWorldPosition().y_+chara_->GetComponent<CollisionShape>()->GetSize().y_;
  183.         float cameraZPos=chara_->GetWorldPosition().z_-(chara_->GetWorldDirection().z_*(Vector3::FORWARD.z_*5));
  184.  
  185.         /** Camera controls */
  186.         if(cameraMode_ == 0)
  187.         {
  188.             if(input->GetKeyDown(KEY_KP_8))
  189.                 cameraNode_->Translate(Vector3::FORWARD / 2);
  190.             if(input->GetKeyDown(KEY_KP_5))
  191.                 cameraNode_->Translate(Vector3::BACK / 2);
  192.             if(input->GetKeyDown(KEY_KP_4))
  193.                 cameraNode_->Yaw(-0.5); // Remove from yaw to rotate counterclockwise
  194.             if(input->GetKeyDown(KEY_KP_6))
  195.                 cameraNode_->Yaw(+0.5); // Add to yaw to rotate clockwise
  196.             if(input->GetKeyDown(KEY_KP_DIVIDE))
  197.                 cameraNode_->Translate(Vector3::UP / 20);
  198.             if(input->GetKeyDown(KEY_KP_2))
  199.                 cameraNode_->Translate(Vector3::DOWN / 20);
  200.             if(input->GetKeyDown(KEY_KP_7))
  201.                 cameraNode_->Translate(Vector3::LEFT / 5);
  202.             if(input->GetKeyDown(KEY_KP_9))
  203.                 cameraNode_->Translate(Vector3::RIGHT / 5);
  204.         } else if (cameraMode_ == 1) {
  205.             // look at the player character!
  206.             cameraNode_->LookAt(Vector3(chara_->GetWorldPosition().x_,cameraYPoint,chara_->GetWorldPosition().z_));
  207.             // orbit around with the camera keys
  208.             if(input->GetKeyDown(KEY_KP_4))
  209.                 cameraNode_->Yaw(-2.0);
  210.             if(input->GetKeyDown(KEY_KP_6))
  211.                 cameraNode_->Yaw(+2.0);
  212.             // don't stray too far from your focal point now
  213.             float analogXPos=chara_->GetWorldPosition().x_-(cameraNode_->GetWorldDirection().x_*(Vector3::RIGHT.x_*5));
  214.             float analogZPos=chara_->GetWorldPosition().z_-(cameraNode_->GetWorldDirection().z_*(Vector3::FORWARD.z_*5));
  215.             cameraNode_->SetWorldPosition(Vector3(analogXPos,cameraYPoint,analogZPos));
  216.         }
  217.  
  218.         /** Bopper controls */
  219.         if(input->GetKeyDown(KEY_W))
  220.             moveDir += Vector3::FORWARD/5;
  221.         if(input->GetKeyDown(KEY_A))
  222.             moveDir += Vector3::LEFT/5;
  223.         if(input->GetKeyDown(KEY_S))
  224.             moveDir += Vector3::BACK/5;
  225.         if(input->GetKeyDown(KEY_D))
  226.             moveDir += Vector3::RIGHT/5;
  227.         if(input->GetKeyDown(KEY_UP))
  228.         {
  229.             //if(chara_->GetComponent<RigidBody>()->GetLinearVelocity().y_ == 0)
  230.                 chara_->GetComponent<RigidBody>()->ApplyImpulse(Vector3::UP);
  231.         }
  232.         if(input->GetKeyDown(KEY_LEFT))
  233.             chara_->Yaw(-2.0);
  234.         if(input->GetKeyDown(KEY_RIGHT))
  235.             chara_->Yaw(+2.0);
  236.  
  237.         // let's not be faster with diagonal movement
  238.         if(moveDir.LengthSquared() > 0)
  239.             moveDir.Normalize();
  240.  
  241.         // move!
  242.         chara_->GetComponent<RigidBody>()->ApplyImpulse(cameraNode_->GetRotation() * moveDir);
  243.  
  244.         /** Debug controls */
  245.         if(input->GetKeyDown(KEY_ESCAPE))
  246.             engine_->Exit();
  247.         if(input->GetKeyDown(KEY_TAB))
  248.             GetSubsystem<Input>()->SetMouseVisible(!GetSubsystem<Input>()->IsMouseVisible());
  249.         if(input->GetKeyPress(KEY_SPACE))
  250.             drawDebug_=!drawDebug_;
  251.         if(input->GetKeyPress(KEY_KP_0))
  252.         {
  253.             if (cameraMode_==1)
  254.             {
  255.                 cameraMode_-=1;
  256.             } else {
  257.                 cameraMode_+=1;
  258.             }
  259.         }
  260.  
  261.         std::string str;
  262.         str.append("Camera mode: ");
  263.         {
  264.             std::ostringstream ss;
  265.             ss<<cameraMode_;
  266.             std::string s(ss.str());
  267.             str.append(s);
  268.         }
  269.         String s(str.c_str(),str.size());
  270.         text_->SetText(s);
  271.     }
  272.     /**
  273.     * After everything is rendered, there might still be things you wish
  274.     * to add to the rendering. At this point you cannot modify the scene,
  275.     * only post rendering is allowed. Good for adding things like debug
  276.     * artifacts on screen or brush up lighting, etc.
  277.     */
  278.     void HandlePostRenderUpdate(StringHash eventType, VariantMap & eventData)
  279.     {
  280.         if (drawDebug_)
  281.             scene_->GetComponent<PhysicsWorld>()->DrawDebugGeometry(true);
  282.     }
  283. };
  284.  
  285. /**
  286. * This macro is expanded to (roughly, depending on OS) this:
  287. *
  288. * > int RunApplication()
  289. * > {
  290. * > Urho3D::SharedPtr<Urho3D::Context> context(new Urho3D::Context());
  291. * > Urho3D::SharedPtr<className> application(new className(context));
  292. * > return application->Run();
  293. * > }
  294. * >
  295. * > int main(int argc, char** argv)
  296. * > {
  297. * > Urho3D::ParseArguments(argc, argv);
  298. * > return function;
  299. * > }
  300. */
  301. URHO3D_DEFINE_APPLICATION_MAIN(PlatfTest)
  302.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement