Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <sstream>
- #include <Urho3D/Core/CoreEvents.h>
- #include <Urho3D/Engine/Application.h>
- #include <Urho3D/Engine/Engine.h>
- #include <Urho3D/Engine/EngineDefs.h>
- #include <Urho3D/Input/Input.h>
- #include <Urho3D/Input/InputEvents.h>
- #include <Urho3D/Resource/ResourceCache.h>
- #include <Urho3D/Resource/XMLFile.h>
- #include <Urho3D/IO/Log.h>
- #include <Urho3D/UI/UI.h>
- #include <Urho3D/UI/Text.h>
- #include <Urho3D/UI/Font.h>
- #include <Urho3D/UI/Button.h>
- #include <Urho3D/UI/UIEvents.h>
- #include <Urho3D/Scene/Scene.h>
- #include <Urho3D/Scene/SceneEvents.h>
- #include <Urho3D/Graphics/Graphics.h>
- #include <Urho3D/Graphics/Camera.h>
- #include <Urho3D/Graphics/Geometry.h>
- #include <Urho3D/Graphics/Renderer.h>
- #include <Urho3D/Graphics/DebugRenderer.h>
- #include <Urho3D/Graphics/Octree.h>
- #include <Urho3D/Graphics/Light.h>
- #include <Urho3D/Graphics/Model.h>
- #include <Urho3D/Graphics/StaticModel.h>
- #include <Urho3D/Graphics/Material.h>
- #include <Urho3D/Graphics/Skybox.h>
- // Alternatively, you can replace all above Urho3D include statements by the single following one:
- // #include <Urho3D/Urho3DAll.h>
- #include <Urho3D/Graphics/AnimatedModel.h>
- #include <Urho3D/Graphics/AnimationController.h>
- #include <Urho3D/Physics/CollisionShape.h>
- #include <Urho3D/Physics/PhysicsWorld.h>
- #include <Urho3D/Physics/RigidBody.h>
- using namespace Urho3D;
- /**
- * Using the convenient Application API we don't have
- * to worry about initializing the engine or writing a main.
- * You can probably mess around with initializing the engine
- * and running a main manually, but this is convenient and portable.
- */
- class PlatfTest : public Application
- {
- SharedPtr<Text> text_;
- SharedPtr<Scene> scene_;
- SharedPtr<Node> cameraNode_;
- SharedPtr<Node> chara_;
- bool drawDebug_=true;
- int cameraMode_=1;
- public:
- /**
- * This happens before the engine has been initialized
- * so it's usually minimal code setting defaults for
- * whatever instance variables you have.
- * You can also do this in the Setup method.
- */
- PlatfTest(Context * context) : Application(context)
- {
- }
- /**
- * This method is called before the engine has been initialized.
- * Thusly, we can setup the engine parameters before anything else
- * of engine importance happens (such as windows, search paths,
- * resolution and other things that might be user configurable).
- */
- virtual void Setup()
- {
- engineParameters_["FullScreen"] = false;
- engineParameters_["WindowWidth"]=800;
- engineParameters_["WindowHeight"]=600;
- }
- /**
- * This method is called after the engine has been initialized.
- * This is where you set up your actual content, such as scenes,
- * models, controls and what not. Basically, anything that needs
- * the engine initialized and ready goes in here.
- */
- virtual void Start()
- {
- //SubscribeToEvent(E_KEYDOWN,URHO3D_HANDLER(PlatfTest,HandleKeyDown));
- GetSubsystem<Input>()->SetMouseVisible(true);
- ResourceCache* cache=GetSubsystem<ResourceCache>();
- //GetSubsystem<UI>()->GetRoot()->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
- text_=new Text(context_);
- text_->SetText("Text is doing its best now and is preparing. Please watch warmly until it is ready.");
- text_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 13); // SetFont apparently NEEDS to be set.
- text_->SetColor(Color(1, 0, 0));
- GetSubsystem<UI>()->GetRoot()->AddChild(text_);
- /** Start of scene preparation. */
- scene_=new Scene(context_);
- scene_->CreateComponent<Octree>();
- scene_->CreateComponent<PhysicsWorld>();
- scene_->CreateComponent<DebugRenderer>();
- Node* floorNode=scene_->CreateChild("Floor");
- floorNode->SetWorldPosition(Vector3(140,-6,15)); // X and Z = lateral position, Y = altitude
- floorNode->SetScale(Vector3(300,3,30));
- StaticModel* floorModel=floorNode->CreateComponent<StaticModel>();
- floorModel->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
- floorModel->SetMaterial(cache->GetResource<Material>("Materials/Terrain.xml"));
- /*RigidBody* floorBody = */floorNode->CreateComponent<RigidBody>(); // RigidBody can stay at default values.
- CollisionShape* floorShape = floorNode->CreateComponent<CollisionShape>();
- floorShape->SetBox(Vector3::ONE); // Box automatically scales to fit the scale of the object so just leave this at 1.
- Node* skyNode=scene_->CreateChild("Sky");
- Skybox* skybox=skyNode->CreateComponent<Skybox>();
- skybox->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
- skybox->SetMaterial(cache->GetResource<Material>("Materials/Skybox.xml"));
- Node* lightNode=scene_->CreateChild("DirectionalLight");
- lightNode->SetDirection(Vector3(0,-5,2)); // The direction of the shade.
- Light* light=lightNode->CreateComponent<Light>();
- light->SetLightType(LIGHT_DIRECTIONAL);
- cameraNode_=scene_->CreateChild("Camera");
- Camera* camera=cameraNode_->CreateComponent<Camera>();
- Renderer* renderer=GetSubsystem<Renderer>();
- SharedPtr<Viewport> viewport(new Viewport(context_,scene_,cameraNode_->GetComponent<Camera>()));
- renderer->SetViewport(0, viewport);
- SubscribeToEvent(E_UPDATE,URHO3D_HANDLER(PlatfTest,HandleUpdate));
- SubscribeToEvent(E_POSTRENDERUPDATE,URHO3D_HANDLER(PlatfTest,HandlePostRenderUpdate));
- /** End of scene preparation. */
- /** Start of code for our good old platformer bopper. */
- Node* charaNode=scene_->CreateChild("Bopper");
- charaNode->SetWorldPosition(Vector3(0,1,2.5));
- // This turns the model in case it needs to be done, as it is in this case.
- Node* adjustNode = charaNode->CreateChild("AdjNode");
- adjustNode->SetRotation(Quaternion(180, Vector3(0,1,0))); // Turn it 180 degrees.
- // Prepare model and enable animations
- AnimatedModel* animChara = adjustNode->CreateComponent<AnimatedModel>();
- animChara->SetModel(cache->GetResource<Model>("Models/BetaLowpoly/Beta.mdl"));
- animChara->SetMaterial(0, cache->GetResource<Material>("BetaBody_MAT.xml")); // Material settings, as seen on the model's Readme file
- animChara->SetMaterial(1, cache->GetResource<Material>("BetaBody_MAT.xml"));
- animChara->SetMaterial(2, cache->GetResource<Material>("BetaJoints_MAT.xml"));
- adjustNode->CreateComponent<AnimationController>();
- // Prepare physics
- RigidBody* body = charaNode->CreateComponent<RigidBody>();
- body->SetMass(1); // Or it won't be affected by gravity.
- body->SetFriction(10);
- body->SetRestitution(0);
- body->SetAngularFactor(Vector3::ZERO); // Physics alone can't rotate this body horizontally. (Here, only the code should)
- body->SetCollisionEventMode(COLLISION_ALWAYS);
- // Prepare collision box
- CollisionShape* shape = charaNode->CreateComponent<CollisionShape>();
- shape->SetBox(Vector3(0.5,2,0.5), Vector3(0,1,0));
- chara_=charaNode;
- /** End of bopper start-up code. */
- }
- /**
- * Good place to get rid of any system resources that requires the
- * engine still initialized. You could do the rest in the destructor,
- * but there's no need, this method will get called when the engine stops,
- * for whatever reason (short of a segfault).
- */
- virtual void Stop()
- {
- }
- /**
- * Your non-rendering logic should be handled here.
- * This could be moving objects, checking collisions and reaction, etc.
- */
- void HandleUpdate(StringHash eventType,VariantMap& eventData)
- {
- Input* input=GetSubsystem<Input>();
- Vector3 moveDir=Vector3::ZERO;
- float cameraXPos=chara_->GetWorldPosition().x_-(chara_->GetWorldDirection().x_*(Vector3::RIGHT.x_*5));
- float cameraYPoint=chara_->GetWorldPosition().y_+chara_->GetComponent<CollisionShape>()->GetSize().y_;
- float cameraZPos=chara_->GetWorldPosition().z_-(chara_->GetWorldDirection().z_*(Vector3::FORWARD.z_*5));
- /** Camera controls */
- if(cameraMode_ == 0)
- {
- if(input->GetKeyDown(KEY_KP_8))
- cameraNode_->Translate(Vector3::FORWARD / 2);
- if(input->GetKeyDown(KEY_KP_5))
- cameraNode_->Translate(Vector3::BACK / 2);
- if(input->GetKeyDown(KEY_KP_4))
- cameraNode_->Yaw(-0.5); // Remove from yaw to rotate counterclockwise
- if(input->GetKeyDown(KEY_KP_6))
- cameraNode_->Yaw(+0.5); // Add to yaw to rotate clockwise
- if(input->GetKeyDown(KEY_KP_DIVIDE))
- cameraNode_->Translate(Vector3::UP / 20);
- if(input->GetKeyDown(KEY_KP_2))
- cameraNode_->Translate(Vector3::DOWN / 20);
- if(input->GetKeyDown(KEY_KP_7))
- cameraNode_->Translate(Vector3::LEFT / 5);
- if(input->GetKeyDown(KEY_KP_9))
- cameraNode_->Translate(Vector3::RIGHT / 5);
- } else if (cameraMode_ == 1) {
- // look at the player character!
- cameraNode_->LookAt(Vector3(chara_->GetWorldPosition().x_,cameraYPoint,chara_->GetWorldPosition().z_));
- // orbit around with the camera keys
- if(input->GetKeyDown(KEY_KP_4))
- cameraNode_->Yaw(-2.0);
- if(input->GetKeyDown(KEY_KP_6))
- cameraNode_->Yaw(+2.0);
- // don't stray too far from your focal point now
- float analogXPos=chara_->GetWorldPosition().x_-(cameraNode_->GetWorldDirection().x_*(Vector3::RIGHT.x_*5));
- float analogZPos=chara_->GetWorldPosition().z_-(cameraNode_->GetWorldDirection().z_*(Vector3::FORWARD.z_*5));
- cameraNode_->SetWorldPosition(Vector3(analogXPos,cameraYPoint,analogZPos));
- }
- /** Bopper controls */
- if(input->GetKeyDown(KEY_W))
- moveDir += Vector3::FORWARD/5;
- if(input->GetKeyDown(KEY_A))
- moveDir += Vector3::LEFT/5;
- if(input->GetKeyDown(KEY_S))
- moveDir += Vector3::BACK/5;
- if(input->GetKeyDown(KEY_D))
- moveDir += Vector3::RIGHT/5;
- if(input->GetKeyDown(KEY_UP))
- {
- //if(chara_->GetComponent<RigidBody>()->GetLinearVelocity().y_ == 0)
- chara_->GetComponent<RigidBody>()->ApplyImpulse(Vector3::UP);
- }
- if(input->GetKeyDown(KEY_LEFT))
- chara_->Yaw(-2.0);
- if(input->GetKeyDown(KEY_RIGHT))
- chara_->Yaw(+2.0);
- // let's not be faster with diagonal movement
- if(moveDir.LengthSquared() > 0)
- moveDir.Normalize();
- // move!
- chara_->GetComponent<RigidBody>()->ApplyImpulse(cameraNode_->GetRotation() * moveDir);
- /** Debug controls */
- if(input->GetKeyDown(KEY_ESCAPE))
- engine_->Exit();
- if(input->GetKeyDown(KEY_TAB))
- GetSubsystem<Input>()->SetMouseVisible(!GetSubsystem<Input>()->IsMouseVisible());
- if(input->GetKeyPress(KEY_SPACE))
- drawDebug_=!drawDebug_;
- if(input->GetKeyPress(KEY_KP_0))
- {
- if (cameraMode_==1)
- {
- cameraMode_-=1;
- } else {
- cameraMode_+=1;
- }
- }
- std::string str;
- str.append("Camera mode: ");
- {
- std::ostringstream ss;
- ss<<cameraMode_;
- std::string s(ss.str());
- str.append(s);
- }
- String s(str.c_str(),str.size());
- text_->SetText(s);
- }
- /**
- * After everything is rendered, there might still be things you wish
- * to add to the rendering. At this point you cannot modify the scene,
- * only post rendering is allowed. Good for adding things like debug
- * artifacts on screen or brush up lighting, etc.
- */
- void HandlePostRenderUpdate(StringHash eventType, VariantMap & eventData)
- {
- if (drawDebug_)
- scene_->GetComponent<PhysicsWorld>()->DrawDebugGeometry(true);
- }
- };
- /**
- * This macro is expanded to (roughly, depending on OS) this:
- *
- * > int RunApplication()
- * > {
- * > Urho3D::SharedPtr<Urho3D::Context> context(new Urho3D::Context());
- * > Urho3D::SharedPtr<className> application(new className(context));
- * > return application->Run();
- * > }
- * >
- * > int main(int argc, char** argv)
- * > {
- * > Urho3D::ParseArguments(argc, argv);
- * > return function;
- * > }
- */
- URHO3D_DEFINE_APPLICATION_MAIN(PlatfTest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement