#include "Urho3DAll.h" #include "Game.h" #include "SmoothFocus.h" URHO3D_DEFINE_APPLICATION_MAIN(Game) Game::Game(Context* context) : Application(context), yaw_(0.0f), pitch_(0.0f) { SmoothFocus::RegisterObject(context); } void Game::Setup() { engineParameters_["WindowTitle"] = GetTypeName(); // engineParameters_["LogName"] = GetSubsystem()->GetAppPreferencesDir("urho3d", "logs") + GetTypeName() + ".log"; engineParameters_["FullScreen"] = false; engineParameters_["Headless"] = false; engineParameters_["WindowWidth"] = 800; engineParameters_["WindowHeight"] = 600; // engineParameters_["ResourcePaths"] = "Data;CoreData;MyData"; } void Game::Start() { CreateScene(); SetupViewport(); SubscribeToEvents(); ResourceCache* cache = GetSubsystem(); XMLFile* xmlFile = cache->GetResource("UI/DefaultStyle.xml"); DebugHud* debugHud = engine_->CreateDebugHud(); debugHud->SetDefaultStyle(xmlFile); } void Game::SetupViewport() { ResourceCache* cache = GetSubsystem(); Renderer* renderer = GetSubsystem(); SharedPtr viewport(new Viewport(context_, scene_, cameraNode_->GetComponent())); viewport->SetRenderPath(cache->GetResource("CoreData/RenderPaths/ForwardHWDepthDOF.xml")); SmoothFocus* sf = cameraNode_->CreateComponent(); sf->SetViewport(viewport); sf->SetFocusTime(0.5f); renderer->SetViewport(0, viewport); } void Game::CreateScene() { ResourceCache* cache = GetSubsystem(); scene_ = new Scene(context_); scene_->CreateComponent(); Node* planeNode = scene_->CreateChild("Plane"); planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent(); planeObject->SetModel(cache->GetResource("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource("Materials/StoneTiled.xml")); Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent(); light->SetColor(Color(0.5f, 0.5f, 0.5f)); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); //light->SetShadowIntensity(0.5f); Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.5f, 0.5f, 0.5f)); zone->SetFogColor(Color(0.4f, 0.5f, 0.8f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); const unsigned NUM_OBJECTS = 200; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* mushroomNode = scene_->CreateChild("Mushroom"); mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); mushroomNode->SetScale(0.5f + Random(2.0f)); StaticModel* mushroomObject = mushroomNode->CreateComponent(); mushroomObject->SetModel(cache->GetResource("Models/Mushroom.mdl")); mushroomObject->SetMaterial(cache->GetResource("Materials/Mushroom.xml")); mushroomObject->SetCastShadows(true); } cameraNode_ = scene_->CreateChild("Camera"); cameraNode_->CreateComponent(); cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); } void Game::MoveCamera(float timeStep) { Input* input = GetSubsystem(); 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)); if (input->GetKeyDown('W')) cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep); if (input->GetKeyDown('S')) cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep); if (input->GetKeyDown('A')) cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep); if (input->GetKeyDown('D')) cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep); if (input->GetKeyPress(KEY_F2)) GetSubsystem()->ToggleAll(); } void Game::SubscribeToEvents() { SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Game, HandleUpdate)); } void Game::HandleUpdate(StringHash eventType, VariantMap& eventData) { using namespace Update; float timeStep = eventData[P_TIMESTEP].GetFloat(); MoveCamera(timeStep); }