-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneGame.cpp
326 lines (269 loc) · 7.6 KB
/
SceneGame.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "Graphics/Graphics.h"
#include "SceneGame.h"
#include "Camera.h"
#include "EnemyManager.h"
#include "EnemySlime.h"
#include "EffectManager.h"
#include "Audio/Audio.h"
#include "Input/Input.h"
#include "Collision.h"
#include "Stage.h"
#include "StageManager.h"
#include "StageMain.h"
#include "StageMoveFloor.h"
// 初期化
void SceneGame::Initialize()
{
// ゲームBGM
//GameBGM = Audio::Instance().LoadAudioSource("Data/Audio/GameBGM.wav");
// ステージ初期化
StageManager& stageManager = StageManager::Instance();
StageMain* stageMain = new StageMain();
stageManager.Register(stageMain);
StageMoveFloor* stageMoveFloor = new StageMoveFloor();
stageMoveFloor->SetStartPoint(DirectX::XMFLOAT3(0, 1, 3));
stageMoveFloor->SetGoalPoint(DirectX::XMFLOAT3(10, 2, 3));
stageMoveFloor->SetTorque(DirectX::XMFLOAT3(0, 1.0f, 0));
stageManager.Register(stageMoveFloor);
// プレイヤー
player = new Player();
// カメラ初期設定
Graphics& graphics = Graphics::Instance();
Camera& camera = Camera::Instance();
camera.SetLookAt(
DirectX::XMFLOAT3(0, 10, -10),
DirectX::XMFLOAT3(0, 0, 0),
DirectX::XMFLOAT3(0, 1, 0)
);
camera.SetPerspectiveFov(
DirectX::XMConvertToRadians(45),
graphics.GetScreenWidth() / graphics.GetScreenHeight(),
0.1f,
1000.0f
);
// カメラコントローラー初期化
cameraController = new CameraController();
// エネミー初期化
EnemyManager& enemyManager = EnemyManager::Instance();
for (int i = 0; i < 2; ++i)
{
EnemySlime* slime = new EnemySlime();
slime->SetPosition(DirectX::XMFLOAT3(i * 2.0f, 0, 5));
enemyManager.Register(slime);
}
// ゲージスプライト
gauge = new Sprite();
}
// 終了化
void SceneGame::Finalize()
{
// ゲージスプライト終了化
if (gauge != nullptr)
{
delete gauge;
gauge = nullptr;
}
// エネミー終了化
EnemyManager::Instance().Clear();
// カメラコントローラー終了化
if (cameraController != nullptr)
{
delete cameraController;
cameraController = nullptr;
}
// プレイヤー終了化
if (player != nullptr)
{
delete player;
player = nullptr;
}
// ステージ終了化
StageManager::Instance().Clear();
}
// 更新処理
void SceneGame::Update(float elapsedTime)
{
// ゲームBGM
//GameBGM->Play(true);
// カメラコントローラー更新処理
DirectX::XMFLOAT3 target = player->GetPosition();
target.y += 0.5f;
cameraController->SetTarget(target);
cameraController->Update(elapsedTime);
// ステージ更新処理
StageManager::Instance().Update(elapsedTime);
// プレイヤー更新処理
player->Update(elapsedTime);
// エネミー更新処理
EnemyManager::Instance().Update(elapsedTime);
// エフェクト更新処理
EffectManager::Instance().Update(elapsedTime);
}
// 描画処理
void SceneGame::Render()
{
Graphics& graphics = Graphics::Instance();
ID3D11DeviceContext* dc = graphics.GetDeviceContext();
ID3D11RenderTargetView* rtv = graphics.GetRenderTargetView();
ID3D11DepthStencilView* dsv = graphics.GetDepthStencilView();
// 画面クリア&レンダーターゲット設定
FLOAT color[] = { 0.0f, 0.0f, 0.5f, 1.0f }; // RGBA(0.0~1.0)
dc->ClearRenderTargetView(rtv, color);
dc->ClearDepthStencilView(dsv, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
dc->OMSetRenderTargets(1, &rtv, dsv);
// 描画処理
RenderContext rc;
rc.lightDirection = { 0.0f, -1.0f, 0.0f, 0.0f }; // ライト方向(下方向)
// カメラパラメータ設定
Camera& camera = Camera::Instance();
rc.view = camera.GetView();
rc.projection = camera.GetProjection();
rc.viewPosition = camera.GetEye();
// 3Dモデル描画
{
Shader* shader = graphics.GetShader(0);
shader->Begin(dc, rc);
// ステージ描画
StageManager::Instance().Render(dc, shader);
shader->End(dc);
}
{
Shader* shader = graphics.GetShader(1);
shader->Begin(dc, rc);
// プレイヤー描画
player->Render(dc, shader);
// エネミー描画
EnemyManager::Instance().Render(dc, shader);
shader->End(dc);
}
// 3Dエフェクト描画
{
EffectManager::Instance().Render(rc.view, rc.projection);
}
// 3Dデバッグ描画
{
// ラインレンダラ描画実行
graphics.GetLineRenderer()->Render(dc, rc.view, rc.projection);
// デバッグレンダラ描画実行
graphics.GetDebugRenderer()->Render(dc, rc.view, rc.projection);
}
// 2Dスプライト描画
{
RenderEnemyGauge(dc, rc.view, rc.projection);
}
// 2DデバッグGUI描画
{
ImGui::SetNextWindowPos(ImVec2(10, 10), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(300, 300), ImGuiCond_FirstUseEver);
}
}
// エネミーHPゲージ描画
void SceneGame::RenderEnemyGauge(
ID3D11DeviceContext* dc,
const DirectX::XMFLOAT4X4& view,
const DirectX::XMFLOAT4X4& projection)
{
// ビューポート
D3D11_VIEWPORT viewport;
UINT numViewports = 1;
dc->RSGetViewports(&numViewports, &viewport);
// 変換行列
DirectX::XMMATRIX View = DirectX::XMLoadFloat4x4(&view);
DirectX::XMMATRIX Projection = DirectX::XMLoadFloat4x4(&projection);
DirectX::XMMATRIX World = DirectX::XMMatrixIdentity();
// 全ての敵の頭上にHPゲージを表示
EnemyManager& enemyManager = EnemyManager::Instance();
int enemyCount = enemyManager.GetEnemyCount();
DirectX::XMVECTOR ScreenPosition;
DirectX::XMVECTOR WorldPosition;
for (int i = 0; i < enemyCount; ++i)
{
Enemy* enemy = enemyManager.GetEnemy(i);
DirectX::XMFLOAT3 worldPosition = enemy->GetPosition();
worldPosition.y += enemy->GetHeight();
WorldPosition = DirectX::XMLoadFloat3(&worldPosition);
ScreenPosition = DirectX::XMVector3Project(WorldPosition,
viewport.TopLeftX,
viewport.TopLeftY,
viewport.Width,
viewport.Height,
viewport.MinDepth,
viewport.MaxDepth,
Projection,
View,
World);
DirectX::XMFLOAT3 screenPosition;
DirectX::XMStoreFloat3(&screenPosition, ScreenPosition);
// ゲージの幅
const float Gaugew = 50.0f;
const float Gaugeh = 5.0f;
// HPの割合計算
float healthRate = static_cast<float>(enemy->GetHealth()) / static_cast<float>(enemy->GetMaxHealth());
DirectX::XMFLOAT4 healthColor = { 1.0f,0.0f,0.0f,1.0f };
if (healthRate < 0.5)
{
healthColor = DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f);
}
// 描画
gauge->Render(
dc,
screenPosition.x - Gaugew * 0.5f,
screenPosition.y - Gaugeh,
Gaugew * healthRate,
Gaugeh,
0, 0,
static_cast<float>(gauge->GetTextureWidth()),
static_cast<float>(gauge->GetTextureHeight()),
0.0f,
healthColor.x, healthColor.y, healthColor.z, healthColor.w);
}
// エネミー配置処理
Mouse& mouse = Input::Instance().GetMouse();
if (mouse.GetButtonDown() & Mouse::BTN_LEFT)
{
// マウスカーソル座標を取得
DirectX::XMFLOAT3 screenPosition;
screenPosition.x = static_cast<float>(mouse.GetPositionX());
screenPosition.y = static_cast<float>(mouse.GetPositionY());
screenPosition.z = 0.0f;
ScreenPosition = DirectX::XMLoadFloat3(&screenPosition);
WorldPosition = DirectX::XMVector3Unproject(
ScreenPosition,
viewport.TopLeftX,
viewport.TopLeftY,
viewport.Width,
viewport.Height,
viewport.MinDepth,
viewport.MaxDepth,
Projection,
View,
World);
DirectX::XMFLOAT3 RayStart;
DirectX::XMStoreFloat3(&RayStart, WorldPosition);
screenPosition.z = 1.0f;
ScreenPosition = DirectX::XMLoadFloat3(&screenPosition);
WorldPosition = DirectX::XMVector3Unproject(
ScreenPosition,
viewport.TopLeftX,
viewport.TopLeftY,
viewport.Width,
viewport.Height,
viewport.MinDepth,
viewport.MaxDepth,
Projection,
View,
World);
DirectX::XMFLOAT3 RayEnd;
DirectX::XMStoreFloat3(&RayEnd, WorldPosition);
// ここからRayCastを使用してステージに当たったワールド座標を計算し
// そこにスライムを出現させる
HitResult hit;
if (StageManager::Instance().RayCast(RayStart, RayEnd, hit))
{
EnemyManager& enemyManager = EnemyManager::Instance();
EnemySlime* slime = new EnemySlime();
slime->SetPosition(hit.position);
enemyManager.Register(slime);
}
}
}