Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Common.h"
- class MyApp : public Application
- {
- public:
- SharedPtr<Camera> camera;
- SharedPtr<Node> cameraNode;
- SharedPtr<Viewport> vp;
- SharedPtr<Node> playerNode;
- SharedPtr<RigidBody> playerRigidbody;
- SharedPtr<Scene> scene_;
- SharedPtr<Node> sbNode_;
- DebugHud* debugHud = 0;
- Console* console = 0;
- MyApp(Context* context) :
- Application(context)
- {
- }
- virtual void Setup()
- {
- // Called before engine initialization. engineParameters_ member variable can be modified here
- engineParameters_["WindowTitle"] = "SoftBody Test";
- engineParameters_["FullScreen"] = false;
- engineParameters_["WindowWidth"] = 1280;
- engineParameters_["WindowHeight"] = 720;
- //engineParameters_["VSync"] = true;
- engineParameters_["LogName"] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("urho3d", "logs") + GetTypeName() + ".log";
- }
- virtual void Start()
- {
- CreateDubugHud();
- LoadScene("GameScene.xml");
- SetupPlayer();
- sbNode_ = scene_->GetChild("SoftBody", true);
- SoftBody* sb = sbNode_->CreateComponent<SoftBody>();
- //sb->SetCollisionLayerAndMask(1, 3);
- sb->UpdateSoftBodyFromStaticModel();
- // Called after engine initialization. Setup application & subscribe to events here
- SubscribeToEvent(E_KEYDOWN, HANDLER(MyApp, HandleKeyDown));
- SubscribeToEvent(E_UPDATE, HANDLER(MyApp, HandleUpdate));
- SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(MyApp, HandleMouseButton));
- }
- void CreateDubugHud()
- {
- // Get default style
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
- // Create console
- Console* console = engine_->CreateConsole();
- console->SetDefaultStyle(xmlFile);
- console->GetBackground()->SetOpacity(0.8f);
- // Create debug HUD.
- DebugHud* debugHud = engine_->CreateDebugHud();
- debugHud->SetDefaultStyle(xmlFile);
- }
- virtual void Stop()
- {
- // Perform optional cleanup after main loop has terminated
- }
- void HandleMouseButton(StringHash eventType, VariantMap& eventData)
- {
- using namespace MouseButtonDown;
- int button = eventData[P_BUTTON].GetInt();
- if (button == MOUSEB_LEFT)
- {
- static ResourceCache* cache = GetSubsystem<ResourceCache>();
- Node* node = scene_->CreateChild("bullet");
- Vector3 pos = cameraNode->GetWorldPosition();
- node->SetWorldPosition(pos);
- StaticModel* sm = node->CreateComponent<StaticModel>();
- sm->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
- RigidBody* rb = node->CreateComponent<RigidBody>();
- rb->SetMass(10.0f);
- rb->SetCollisionMask(1);
- CollisionShape* c = node->CreateComponent<CollisionShape>();
- c->SetModel(sm->GetModel());
- c->SetShapeType(ShapeType::SHAPE_BOX);
- c->SetBox(Vector3::ONE);
- Vector3 vel = cameraNode->GetWorldRotation() * cameraNode->GetWorldDirection().FORWARD * 30.0f;
- rb->SetLinearVelocity(vel);
- }
- }
- void HandleKeyDown(StringHash eventType, VariantMap& eventData)
- {
- using namespace KeyDown;
- // Check for pressing ESC. Note the engine_ member variable for convenience access to the Engine object
- int key = eventData[P_KEY].GetInt();
- // Close console (if open) or exit when ESC is pressed
- if (key == KEY_ESC)
- {
- Console* console = GetSubsystem<Console>();
- if (console->IsVisible())
- console->SetVisible(false);
- else
- engine_->Exit();
- }
- // Toggle console with F1
- else if (key == KEY_F1)
- GetSubsystem<Console>()->Toggle();
- // Toggle debug HUD with F2
- else if (key == KEY_F2)
- GetSubsystem<DebugHud>()->ToggleAll();
- }
- void HandleUpdate(StringHash eventType, VariantMap& eventData)
- {
- using namespace Update;
- // Take the frame time step, which is stored as a float
- float timeStep = eventData[P_TIMESTEP].GetFloat();
- #ifdef URHO3D_PROFILING
- Profiler* profiler = GetSubsystem<Profiler>();
- if (profiler)
- profiler->BeginBlock("MoveCamera");
- #endif
- MoveCamera(timeStep);
- #ifdef URHO3D_PROFILING
- profiler->EndBlock();
- #endif
- }
- void LoadScene(String sceneFileName)
- {
- scene_ = SharedPtr<Scene>(new Scene(context_));
- File sceneFile(context_, context_->GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/" + sceneFileName, FILE_READ);
- scene_->LoadXML(sceneFile);
- }
- void SetupPlayer()
- {
- cameraNode = scene_->GetChild("cameraNode", true);
- if (cameraNode)
- {
- camera = cameraNode->GetComponent<Camera>();
- if (camera)
- {
- vp = SharedPtr<Viewport>(new Viewport(context_, scene_, camera));
- Renderer* renderer = GetSubsystem<Renderer>();
- renderer->SetViewport(0, vp);
- }
- }
- playerNode = scene_->GetChild("playerNode", true);
- playerRigidbody = playerNode->GetComponent<RigidBody>();
- playerRigidbody->SetMass(0);
- }
- void MoveCamera(float timeStep)
- {
- static float yaw_ = 0.0f;
- static float pitch_ = 0.0f;
- Input* input = GetSubsystem<Input>();
- const float MOVE_SPEED = 20.0f;
- const float MOUSE_SENSITIVITY = 0.1f;
- IntVector2 mouseMove = input->GetMouseMove();
- yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
- pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
- pitch_ = Clamp(pitch_, -90.0f, 90.0f);
- cameraNode->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
- Quaternion camRotation = cameraNode->GetWorldRotation();
- camRotation.z_ = 0.0f;
- Vector3 dir = Vector3::ZERO;
- if (input->GetKeyDown('W'))
- dir += Vector3::FORWARD;
- if (input->GetKeyDown('S'))
- dir += Vector3::BACK;
- if (input->GetKeyDown('A'))
- dir += Vector3::LEFT;
- if (input->GetKeyDown('D'))
- dir += Vector3::RIGHT;
- if (input->GetKeyDown(KEY_SPACE))
- {
- dir += Vector3::UP;
- }
- if (dir.Length() > 0)
- {
- Vector3 curVel = playerRigidbody->GetLinearVelocity();
- if (curVel.Length() < 5.0f)
- {
- float multipler = 1.0f;
- //if (input->GetQualifierDown(KEY_SHIFT))
- if (input->GetKeyDown(KEY_LSHIFT))
- {
- multipler = 2.0f;
- }
- //playerRigidbody->SetLinearVelocity(multipler * 500.0f * (camRotation * dir) * timeStep);
- playerNode->Translate(camRotation * dir * timeStep * 10.0f);
- }
- }
- }
- };
- DEFINE_APPLICATION_MAIN(MyApp)
Advertisement
Add Comment
Please, Sign In to add comment