-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
297 lines (244 loc) · 7.51 KB
/
Copy pathPlayer.cpp
File metadata and controls
297 lines (244 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "template.h"
#include "renderer.h"
#include "Player.h"
#include "camera.h"
#include "Planet.h"
Player* Player::m_instance = nullptr;
static void ImplOnLMBDown()
{
if (Player::m_instance)
{
Player::m_instance->OnLMBDown();
}
}
static void ImplOnLMBUp()
{
if (Player::m_instance)
{
Player::m_instance->OnLMBUp();
}
}
static void ImplOnRMBDown()
{
if (Player::m_instance)
{
Player::m_instance->OnRMBDown();
}
}
static void ImplOnRMBUp()
{
if (Player::m_instance)
{
Player::m_instance->OnRMBUp();
}
}
static void ImplOnScroll(float delta)
{
if (Player::m_instance)
{
Player::m_instance->OnScroll(delta);
}
}
Player::Player(Camera& camera) : m_camera(camera)
{
}
Player::~Player()
{
}
void Player::Init(Renderer* renderer)
{
m_instance = this;
m_renderer = renderer;
m_renderer->OnLMBDown += ImplOnLMBDown;
m_renderer->OnLMBUp += ImplOnLMBUp;
m_renderer->OnRMBDown += ImplOnRMBDown;
m_renderer->OnRMBUp += ImplOnRMBUp;
m_renderer->OnScroll += ImplOnScroll;
const float3 center = float3(0.5f);
constexpr float radius = 2.f;
const float3 xvec = float3(1, 0, 0) * radius;
const float3 yvec = float3(0, 1, 0) * radius;
const float3 zvec = float3(0, 0, 1) * radius;
m_CMHor.m_speed = 0.04f;
m_CMHor.m_path.SetLooped(true);
m_CMHor.m_mode = Cinemachine::Mode::Velocity;
constexpr int horPointsNum{ 8 };
for (int i = 0; i < horPointsNum; ++i)
{
const float angle = (2.0f * PI * i) / horPointsNum;
const float x = cosf(angle);
const float z = sinf(angle);
m_CMHor.m_path.AppendControlPoint(center + xvec * x + zvec * z);
}
//m_CMHor.m_path.AppendControlPoint(center + xvec);
//m_CMHor.m_path.AppendControlPoint(center + zvec);
//m_CMHor.m_path.AppendControlPoint(center - xvec);
//m_CMHor.m_path.AppendControlPoint(center - zvec);
m_CMHor.m_path.SetAlpha(0.5f);
const float3 horPos = m_CMHor.Get();
m_CMVer.AddT(0.5f);
m_CMVer.m_mode = Cinemachine::Mode::Velocity;
m_CMVer.m_speed = 0.15f;
if (renderer->scene.GetSettingsR().m_isInGame)
{
renderer->camera.SetPosition(horPos); // center + xvec
renderer->camera.LookAt(center);
}
UpdateVerticalPath(horPos);
}
static float RadiusClamp(float t, float rmin, float rmax)
{
/*constexpr float scale = 1.6f;
constexpr float center = 1.5f;
constexpr float height = 1.f;
return height - scale * (t - center) * (t - center);*/
constexpr float scale = -1.f;
const float value = scale * (t - rmin) * (t - rmax);
const float vertexx = -(rmin + rmax) / (2 * scale);
const float height = scale * (vertexx - rmin) * (vertexx - rmax);
const float result = (value / height);
if (result <= 1e-5) return 0.f;
return result;
}
void Player::Tick(float /*deltaTime*/)
{
constexpr float FRICTION = 1.f - 0.1f;
m_CMHor.m_velocity *= FRICTION;
m_CMVer.m_velocity *= FRICTION;
m_zoomVelocity *= FRICTION;
float zoomDelta = m_zoomVelocity;
constexpr float zoomSoftMargin = 0.3f;
if (m_radius > MAX_ZOOM - zoomSoftMargin && m_zoomVelocity > 0.f ||
m_radius < MIN_ZOOM + zoomSoftMargin && m_zoomVelocity < 0.f)
{
zoomDelta *= RadiusClamp(m_radius, MIN_ZOOM, MAX_ZOOM);
}
m_radius += zoomDelta;
Light* light = GetFlashlight();
if (light)
{
light->Position(m_camera.camPos);
light->Direction(m_camera.GetForward());
}
if ((m_isLMBDown || m_isRMBDown) && m_drawTimer.elapsed() > MIN_TIME_BETWEEN_DRAWS)
{
m_drawTimer.reset();
Draw();
}
}
void Player::OnLMBUp()
{
m_isLMBDown = false;
}
void Player::OnLMBDown()
{
const ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureKeyboard || !m_renderer->scene.GetSettingsR().m_isInGame)
{
return;
}
m_isLMBDown = true;
}
void Player::OnRMBUp()
{
m_isRMBDown = false;
}
void Player::OnRMBDown()
{
const ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureKeyboard || !m_renderer->scene.GetSettingsR().m_isInGame)
{
return;
}
m_isRMBDown = true;
}
void Player::OnScroll(float delta)
{
const ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureMouse || !m_renderer->scene.GetSettingsR().m_isInGame)
{
return;
}
m_zoomVelocity = std::clamp(m_zoomVelocity - delta * m_zoomSpeed, -MAX_ZOOM_VELOCITY, MAX_ZOOM_VELOCITY);
}
void Player::UpdateVerticalPath(float3 horPos)
{
const float3 center(0.5f);
const float radius = 0.9f;
const float radiusend = 2.f;
const float3 up(0, 1, 0);
m_CMVer.m_path.SetLooped(false);
m_CMVer.m_path.Clear();
m_CMVer.m_path.AppendControlPoint(center - up * radiusend);
m_CMVer.m_path.AppendControlPoint(horPos - up * radius);
m_CMVer.m_path.AppendControlPoint(horPos + up * radius);
m_CMVer.m_path.AppendControlPoint(center + up * radiusend);
//m_CMVer.m_time = Cinemachine::OpenCurveTimeWrap::SoftClamp;
}
static constexpr float VerticalTimeWrap(float t)
{
//return 4.f * (t - t * t); // -4t^2 + 4t
constexpr float scale = 5.6f;
constexpr float center = 0.5f;
constexpr float height = 1.f;
return height - scale * (t - center) * (t - center);
}
bool Player::HandleInput(float deltaTime)
{
ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureKeyboard || !m_renderer->scene.GetSettingsR().m_isInGame)
{
return false;
}
const float3 prevPos = m_camera.camPos;
float deltaHor = 0;
float deltaVer = 0;
if (IsKeyDown(GLFW_KEY_A)) deltaHor -= 1.f;
if (IsKeyDown(GLFW_KEY_D)) deltaHor += 1.f;
if (IsKeyDown(GLFW_KEY_W)) deltaVer += 1.f;
if (IsKeyDown(GLFW_KEY_S)) deltaVer -= 1.f;
m_CMHor.AddT(deltaHor * deltaTime * 0.001f);
const float3 horPos = m_CMHor.Get();
UpdateVerticalPath(horPos);
float tVerSpeed = deltaVer * deltaTime * 0.001f;
const float tver = m_CMVer.GetTime();
constexpr float tVerSoftMargin = 0.2f;
if (tver > 0.5f + tVerSoftMargin && tVerSpeed > 0.f ||
tver < 0.5f - tVerSoftMargin && tVerSpeed < 0.f)
{
tVerSpeed *= VerticalTimeWrap(tver);
}
m_CMVer.AddT(tVerSpeed);
const float3 verPos = m_CMVer.Get();
const float3 scrolledPos = CENTER + normalize(verPos - CENTER) * m_radius;
m_camera.SetPosition(scrolledPos);
m_camera.LookAt(CENTER);
return length(scrolledPos - prevPos) > 0.003f;
}
void Player::Draw()
{
const int2 xy = m_renderer->mousePos;
const float2 uv = float2(static_cast<float>(xy.x), static_cast<float>(xy.y)) * SCR_SIZE_RECIP;
Ray primary = m_renderer->camera.GetPrimaryRay(uv.x, uv.y);
m_renderer->scene.FindNearest(primary);
if (!primary.IsInfinite())
{
const float3 hitPoint = primary.IntersectionPoint();
const uint x = static_cast<uint>(hitPoint.x * WORLDSIZE);
const uint y = static_cast<uint>(hitPoint.y * WORLDSIZE);
const uint z = static_cast<uint>(hitPoint.z * WORLDSIZE);
const Edit::Behaviour behvaiour = m_isRMBDown ? Edit::Behaviour::Subtractive : Edit::Behaviour::Additive;
AddEdit(std::make_unique<SphereEdit>(make_float3(uint3(x, y, z)), m_brushRadius, m_brushMaterial, behvaiour));
m_renderer->scene.ApplyEdits(GetEdits());
}
m_renderer->ResetAccumulator(); // reset TAA
}
Light* Player::GetFlashlight()
{
Light* light = m_renderer->scene.GetCurrentLevelRW()->GetLightRWPtr(0);
if (light && m_renderer->scene.GetCurrentLevelR()->m_name == "Planet")
{
return light;
}
return nullptr;
}