Line data Source code
1 : /* Copyright (C) 2022 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
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.
8 : *
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.
13 : *
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/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "graphics/CinemaManager.h"
21 :
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"
47 :
48 0 : CCinemaManager::CCinemaManager()
49 0 : : m_DrawPaths(false)
50 : {
51 0 : }
52 :
53 0 : void CCinemaManager::Update(const float deltaRealTime) const
54 : {
55 0 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(g_Game->GetSimulation2()->GetSimContext().GetSystemEntity());
56 0 : if (!cmpCinemaManager)
57 0 : return;
58 :
59 0 : if (IsPlaying())
60 0 : cmpCinemaManager->PlayQueue(deltaRealTime, g_Game->GetView()->GetCamera());
61 : }
62 :
63 0 : void CCinemaManager::Render() const
64 : {
65 0 : if (!IsEnabled() && m_DrawPaths)
66 0 : DrawPaths();
67 0 : }
68 :
69 0 : void CCinemaManager::DrawPaths() const
70 : {
71 0 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(g_Game->GetSimulation2()->GetSimContext().GetSystemEntity());
72 0 : if (!cmpCinemaManager)
73 0 : return;
74 :
75 0 : for (const std::pair<const CStrW, CCinemaPath>& p : cmpCinemaManager->GetPaths())
76 : {
77 0 : DrawSpline(p.second, CColor(0.2f, 0.2f, 1.f, 0.9f), 128);
78 0 : DrawNodes(p.second, CColor(0.1f, 1.f, 0.f, 1.f));
79 :
80 0 : if (p.second.GetTargetSpline().GetAllNodes().empty())
81 0 : continue;
82 :
83 0 : DrawSpline(p.second.GetTargetSpline(), CColor(1.f, 0.3f, 0.4f, 0.9f), 128);
84 0 : DrawNodes(p.second.GetTargetSpline(), CColor(1.f, 0.1f, 0.f, 1.f));
85 : }
86 : }
87 :
88 0 : void CCinemaManager::DrawSpline(const RNSpline& spline, const CColor& splineColor, int smoothness) const
89 : {
90 0 : if (spline.GetAllNodes().size() < 2)
91 0 : return;
92 0 : if (spline.GetAllNodes().size() == 2)
93 0 : smoothness = 2;
94 :
95 0 : const float start = spline.MaxDistance.ToFloat() / smoothness;
96 :
97 0 : std::vector<CVector3D> line;
98 0 : for (int i = 0; i <= smoothness; ++i)
99 : {
100 0 : const float time = start * i / spline.MaxDistance.ToFloat();
101 0 : line.emplace_back(spline.GetPosition(time));
102 : }
103 0 : g_Renderer.GetDebugRenderer().DrawLine(line, splineColor, 0.2f, false);
104 :
105 : // Height indicator
106 0 : if (g_Game && g_Game->GetWorld() && g_Game->GetWorld()->GetTerrain())
107 : {
108 0 : for (int i = 0; i <= smoothness; ++i)
109 : {
110 0 : const float time = start * i / spline.MaxDistance.ToFloat();
111 0 : const CVector3D tmp = spline.GetPosition(time);
112 0 : const float groundY = g_Game->GetWorld()->GetTerrain()->GetExactGroundLevel(tmp.X, tmp.Z);
113 0 : g_Renderer.GetDebugRenderer().DrawLine(tmp, CVector3D(tmp.X, groundY, tmp.Z), splineColor, 0.1f, false);
114 : }
115 : }
116 : }
117 :
118 0 : void CCinemaManager::DrawNodes(const RNSpline& spline, const CColor& nodeColor) const
119 : {
120 0 : for (const SplineData& node : spline.GetAllNodes())
121 : {
122 0 : g_Renderer.GetDebugRenderer().DrawCircle(
123 0 : CVector3D(node.Position.X.ToFloat(), node.Position.Y.ToFloat(), node.Position.Z.ToFloat()),
124 : 0.5f, nodeColor);
125 : }
126 0 : }
127 :
128 0 : bool CCinemaManager::IsEnabled() const
129 : {
130 0 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(g_Game->GetSimulation2()->GetSimContext().GetSystemEntity());
131 0 : return cmpCinemaManager && cmpCinemaManager->IsEnabled();
132 : }
133 :
134 0 : bool CCinemaManager::IsPlaying() const
135 : {
136 0 : return IsEnabled() && g_Game && !g_Game->m_Paused;
137 : }
138 :
139 0 : bool CCinemaManager::GetPathsDrawing() const
140 : {
141 0 : return m_DrawPaths;
142 : }
143 :
144 0 : void CCinemaManager::SetPathsDrawing(const bool drawPath)
145 : {
146 0 : m_DrawPaths = drawPath;
147 3 : }
|