[Gameplay] Reduce loom cost
[0ad.git] / source / graphics / CinemaManager.cpp
blob02723fdb8bdce8a8e067317354ed5bc83db7109c
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "graphics/CinemaManager.h"
22 #include "graphics/Camera.h"
23 #include "graphics/Color.h"
24 #include "graphics/GameView.h"
25 #include "maths/MathUtil.h"
26 #include "maths/Quaternion.h"
27 #include "maths/Vector3D.h"
28 #include "maths/Vector4D.h"
29 #include "ps/CLogger.h"
30 #include "ps/ConfigDB.h"
31 #include "ps/CStr.h"
32 #include "ps/Game.h"
33 #include "ps/GameSetup/Config.h"
34 #include "ps/Hotkey.h"
35 #include "ps/World.h"
36 #include "renderer/DebugRenderer.h"
37 #include "renderer/Renderer.h"
38 #include "simulation2/components/ICmpCinemaManager.h"
39 #include "simulation2/components/ICmpOverlayRenderer.h"
40 #include "simulation2/components/ICmpRangeManager.h"
41 #include "simulation2/components/ICmpSelectable.h"
42 #include "simulation2/components/ICmpTerritoryManager.h"
43 #include "simulation2/helpers/CinemaPath.h"
44 #include "simulation2/MessageTypes.h"
45 #include "simulation2/system/ComponentManager.h"
46 #include "simulation2/Simulation2.h"
48 CCinemaManager::CCinemaManager()
49 : m_DrawPaths(false)
53 void CCinemaManager::Update(const float deltaRealTime) const
55 CmpPtr<ICmpCinemaManager> cmpCinemaManager(g_Game->GetSimulation2()->GetSimContext().GetSystemEntity());
56 if (!cmpCinemaManager)
57 return;
59 if (IsPlaying())
60 cmpCinemaManager->PlayQueue(deltaRealTime, g_Game->GetView()->GetCamera());
63 void CCinemaManager::Render() const
65 if (!IsEnabled() && m_DrawPaths)
66 DrawPaths();
69 void CCinemaManager::DrawPaths() const
71 CmpPtr<ICmpCinemaManager> cmpCinemaManager(g_Game->GetSimulation2()->GetSimContext().GetSystemEntity());
72 if (!cmpCinemaManager)
73 return;
75 for (const std::pair<const CStrW, CCinemaPath>& p : cmpCinemaManager->GetPaths())
77 DrawSpline(p.second, CColor(0.2f, 0.2f, 1.f, 0.9f), 128);
78 DrawNodes(p.second, CColor(0.1f, 1.f, 0.f, 1.f));
80 if (p.second.GetTargetSpline().GetAllNodes().empty())
81 continue;
83 DrawSpline(p.second.GetTargetSpline(), CColor(1.f, 0.3f, 0.4f, 0.9f), 128);
84 DrawNodes(p.second.GetTargetSpline(), CColor(1.f, 0.1f, 0.f, 1.f));
88 void CCinemaManager::DrawSpline(const RNSpline& spline, const CColor& splineColor, int smoothness) const
90 if (spline.GetAllNodes().size() < 2)
91 return;
92 if (spline.GetAllNodes().size() == 2)
93 smoothness = 2;
95 const float start = spline.MaxDistance.ToFloat() / smoothness;
97 std::vector<CVector3D> line;
98 for (int i = 0; i <= smoothness; ++i)
100 const float time = start * i / spline.MaxDistance.ToFloat();
101 line.emplace_back(spline.GetPosition(time));
103 g_Renderer.GetDebugRenderer().DrawLine(line, splineColor, 0.2f, false);
105 // Height indicator
106 if (g_Game && g_Game->GetWorld() && g_Game->GetWorld()->GetTerrain())
108 for (int i = 0; i <= smoothness; ++i)
110 const float time = start * i / spline.MaxDistance.ToFloat();
111 const CVector3D tmp = spline.GetPosition(time);
112 const float groundY = g_Game->GetWorld()->GetTerrain()->GetExactGroundLevel(tmp.X, tmp.Z);
113 g_Renderer.GetDebugRenderer().DrawLine(tmp, CVector3D(tmp.X, groundY, tmp.Z), splineColor, 0.1f, false);
118 void CCinemaManager::DrawNodes(const RNSpline& spline, const CColor& nodeColor) const
120 for (const SplineData& node : spline.GetAllNodes())
122 g_Renderer.GetDebugRenderer().DrawCircle(
123 CVector3D(node.Position.X.ToFloat(), node.Position.Y.ToFloat(), node.Position.Z.ToFloat()),
124 0.5f, nodeColor);
128 bool CCinemaManager::IsEnabled() const
130 CmpPtr<ICmpCinemaManager> cmpCinemaManager(g_Game->GetSimulation2()->GetSimContext().GetSystemEntity());
131 return cmpCinemaManager && cmpCinemaManager->IsEnabled();
134 bool CCinemaManager::IsPlaying() const
136 return IsEnabled() && g_Game && !g_Game->m_Paused;
139 bool CCinemaManager::GetPathsDrawing() const
141 return m_DrawPaths;
144 void CCinemaManager::SetPathsDrawing(const bool drawPath)
146 m_DrawPaths = drawPath;