Repository navigation
Expand file tree
/
Copy pathenemy.cpp
More file actions
805 lines (630 loc) · 17.9 KB
/
Copy pathenemy.cpp
File metadata and controls
805 lines (630 loc) · 17.9 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//=============================================================================
//
//
//
//
//=============================================================================
#pragma once
#include "enemy.h"
#include "player.h"
#include "bullet.h"
#include "debugproc.h"
#include "camera.h"
#include "main.h"
#include "renderer.h"
#include "sprite.h"
#include "input.h"
#include "collision.h"
#include "GameUI.h"
#include "item.h"
#include <cstdlib>
#include <ctime>
//*****************************************************************************
//
//*****************************************************************************
std::vector<BaseEnemy*> g_enemies;
ID3D11Buffer* g_VertexBufferEnemy = nullptr;
#define ENEMY_MAX (1)
static BOOL g_bAlphaTestEnemy;
#define ENEMY_OFFSET_Y (-50.0f)
//PLAYER* player = GetPlayer();
BULLET* bullet = GetBullet();
//*****************************************************************************
//
//*****************************************************************************
BaseEnemy::BaseEnemy() : pos({ 0,0,0 }), scl({ 1,1,1 }), use(false) {
XMStoreFloat4x4(&mtxWorld, XMMatrixIdentity());
}
BaseEnemy::~BaseEnemy() {}
SpiderEnemy::SpiderEnemy() :
texture(nullptr), width(100.0f), height(100.0f)
{
material = new MATERIAL{};
XMStoreFloat4x4(&mtxWorld, XMMatrixIdentity());
}
SpiderEnemy::~SpiderEnemy() {
if (texture) {
texture->Release();
texture = nullptr;
}
delete material;
material = nullptr;
}
void SpiderEnemy::Init() {
D3DX11CreateShaderResourceViewFromFile(
GetDevice(),
"data/2Dpicture/enemy/enemy001.png",
NULL, NULL, &texture, NULL);
*material = {};
material->Diffuse = XMFLOAT4(1, 1, 1, 1);
pos = XMFLOAT3(0.0f, -50.0f, 20.0f);
scl = XMFLOAT3(1.0f, 1.0f, 1.0f);
use = true;
speed = 1.0f;
dropRate = 0.5f;
currentFrame = 0;
frameCounter = 0;
frameInterval = 15;//change speed
maxFrames = 3;
tblNo = 0;
//tblMax = _countof(g_MoveTbl0);
time = 0.0f;
isAttacking = false;
attackFrameTimer = 0.0f;
attackCooldownTimer = 0.0f;
attackCooldown = 1.5f; // 1.5 秒ことに攻撃する
moveDir = XMFLOAT3(0.0f, 0.0f, 1.0f); // 現在の動き方向
moveChangeTimer = 2.0f; // 向き変わるタイマー
speed = 0.5f; //エネミーのスピード
currentFrame = 0;
frameCounter = 0;
frameInterval = 15;//change speed
minDistance = 100.0f;
HP = 1;
EnableGravity(true);
SetMaxFallSpeed(6.0f);
}
void SpiderEnemy::Update() {
if (!use) return;
if (isAttacking) //攻撃のアニメーション処理
{
attackFrameTimer -= 1.0f / 60.0f;
if (attackFrameTimer <= 0.0f) {
isAttacking = false;
frameCounter = 0;
currentFrame = 0;
}
else
{
currentFrame = 2;
}
}
else
{
// 移動のアニメーション処理
frameCounter++;
if (frameCounter >= frameInterval) {
frameCounter = 0;
currentFrame = (currentFrame + 1) % 2;
}
}
// エネミーからプレイヤーまでのベクトル
XMFLOAT3 dir;
dir.x = GetPlayer()->GetPosition().x - pos.x;
dir.y = 0.0f;
dir.z = GetPlayer()->GetPosition().z - pos.z;
//// プレイヤーの座標までの計算
XMFLOAT3 toPlayer = { dir.x, dir.y, dir.z };
float distSq = toPlayer.x * toPlayer.x + toPlayer.y * toPlayer.y + toPlayer.z * toPlayer.z;
float range = 200.0f; // 発射範囲
attackCooldownTimer -= 1.0f / 60.0f;
if (attackCooldownTimer < 0.0f) attackCooldownTimer = 0.0f;
//プレイヤーを追いかける行う範囲
if (distSq < range * range)
{
ChasingPlayer(speed, range);
if (!isAttacking && attackCooldownTimer <= 0.0f)
{
Attack();
}
}
else
{
NormalMovement();
}
//弾と当たり判定?
for (int i = 0; i < MAX_BULLET; i++)
{
if (!bullet[i].use) continue;
XMFLOAT3 enemyHalfSize = { width, height - 20.0f, 50.f }; //エネミーの当たり判定のサイズ
if (CheckSphereAABBCollision(bullet[i].pos, bullet[i].size, pos, enemyHalfSize))
{
bullet[i].use = false;
HP -= 1;
if (HP <= 0)
{
use = false;
DropItems(pos, SPIDER);
}
}
}
#ifdef _DEBUG
float dist = sqrtf(distSq);
PrintDebugProc("Enemy Pos: X:%f Y:%f Z:%f\n", pos.x, pos.y, pos.z);
#endif
}
void SpiderEnemy::Draw() {
if (!use || !texture || !g_VertexBufferEnemy) return;
SetLightEnable(FALSE);
UINT stride = sizeof(VERTEX_3D);
UINT offset = 0;
GetDeviceContext()->IASetVertexBuffers(0, 1, &g_VertexBufferEnemy, &stride, &offset);
GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
CAMERA* cam = GetCamera();
XMMATRIX mtxView = XMLoadFloat4x4(&cam->mtxView);
XMMATRIX mtxWorld = XMMatrixIdentity();
mtxWorld.r[0].m128_f32[0] = mtxView.r[0].m128_f32[0];
mtxWorld.r[0].m128_f32[1] = mtxView.r[1].m128_f32[0];
mtxWorld.r[0].m128_f32[2] = mtxView.r[2].m128_f32[0];
mtxWorld.r[1].m128_f32[0] = mtxView.r[0].m128_f32[1];
mtxWorld.r[1].m128_f32[1] = mtxView.r[1].m128_f32[1];
mtxWorld.r[1].m128_f32[2] = mtxView.r[2].m128_f32[1];
mtxWorld.r[2].m128_f32[0] = mtxView.r[0].m128_f32[2];
mtxWorld.r[2].m128_f32[1] = mtxView.r[1].m128_f32[2];
mtxWorld.r[2].m128_f32[2] = mtxView.r[2].m128_f32[2];
XMMATRIX mtxScl = XMMatrixScaling(scl.x, scl.y, scl.z);
XMMATRIX mtxTranslate = XMMatrixTranslation(pos.x, pos.y, pos.z);
mtxWorld = XMMatrixMultiply(mtxWorld, mtxScl);
mtxWorld = XMMatrixMultiply(mtxWorld, mtxTranslate);
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(g_VertexBufferEnemy, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D* v = (VERTEX_3D*)msr.pData;
float w = width, h = height;
v[0].Position = XMFLOAT3(-w / 2, h, 0);
v[1].Position = XMFLOAT3(w / 2, h, 0);
v[2].Position = XMFLOAT3(-w / 2, 0, 0);
v[3].Position = XMFLOAT3(w / 2, 0, 0);
for (int i = 0; i < 4; ++i) {
v[i].Normal = XMFLOAT3(0, 0, -1);
v[i].Diffuse = XMFLOAT4(1, 1, 1, 1);
}
float tw = 1.0f / maxFrames;
float th = 1.0f;
float tx = currentFrame * tw;
float ty = 0.0f;
v[0].TexCoord = XMFLOAT2(tx, ty);
v[1].TexCoord = XMFLOAT2(tx + tw, ty);
v[2].TexCoord = XMFLOAT2(tx, ty + th);
v[3].TexCoord = XMFLOAT2(tx + tw, ty + th);
GetDeviceContext()->Unmap(g_VertexBufferEnemy, 0);
SetAlphaTestEnable(FALSE);
SetBlendState(BLEND_MODE_ALPHABLEND);
SetWorldMatrix(&mtxWorld);
SetMaterial(*material);
GetDeviceContext()->PSSetShaderResources(0, 1, &texture);
GetDeviceContext()->Draw(4, 0);
}
void SpiderEnemy::NormalMovement()
{
// 動き方向変わりタイマー
moveChangeTimer -= 1.0f / 60.0f; // 60fpsことに動き方向変わり
if (moveChangeTimer <= 0.0f) {
moveChangeTimer = 2.0f; // reset timer
// 動き方向変わることはランダム設定
int dir = rand() % 4;
switch (dir) {
case 0: moveDir = XMFLOAT3(1.0f, 0.0f, 0.0f); break; // 右
case 1: moveDir = XMFLOAT3(-1.0f, 0.0f, 0.0f); break; // 左
case 2: moveDir = XMFLOAT3(0.0f, 0.0f, 1.0f); break; // 前(+z)
case 3: moveDir = XMFLOAT3(0.0f, 0.0f, -1.0f); break; // 後ろ(-z)
}
}
// 新しい位置を計算
XMFLOAT3 newPos = pos;
newPos.x += moveDir.x * speed;
newPos.y += moveDir.y * speed;
newPos.z += moveDir.z * speed;
// 範囲制限(例えば:XとZは -50.0f ? +50.0f)
const float minX = -200.0f;
const float maxX = 200.0f;
const float minZ = -100.0f;
const float maxZ = 100.0f;
// 範囲内なら移動
if (newPos.x >= minX && newPos.x <= maxX &&
newPos.z >= minZ && newPos.z <= maxZ) {
pos = newPos;
}
else {
// 範囲外に出そうなら方向を変える
moveChangeTimer = 0.0f; // すぐ次の方向へ変更
}
}
void SpiderEnemy::Attack()
{
if (attackCooldownTimer <= 0.0f && !isAttacking)
{
isAttacking = true;
GetPlayer()->HP -= 1;
attackFrameTimer = 0.5f; // 攻撃のフレームの描画の時間
currentFrame = 2; // 攻撃のフレームの描画
attackCooldownTimer = attackCooldown; // Reset cooldown
ShowWebEffect(0.5f);
}
}
//*****************************************************************************
//
//*****************************************************************************
void InitEnemy() {
MakeVertexEnemy();
g_enemies.clear();
for (int i = 0; i < ENEMY_MAX; ++i) {
EnemySpawner(XMFLOAT3(-50.0f + i * 30.0f, -50.0f, 20.0f), SPIDER);
EnemySpawner(XMFLOAT3(-50.0f + i * 30.0f, 0.0f, 20.0f), GHOST);
}
}
void UpdateEnemy() {
for (auto enemy : g_enemies) {
if (enemy->IsUsed()) enemy->Update();
}
}
void DrawEnemy() {
for (auto enemy : g_enemies) {
if (enemy->IsUsed()) enemy->Draw();
}
#ifdef _DEBUG
for (auto enemy : g_enemies)
{
if (enemy->IsUsed())
{
XMFLOAT3 pos = enemy->GetPosition();
PrintDebugProc("Enemy Pos: X:%f Y:%f Z:%f\n", pos.x, pos.y, pos.z);
break;
}
}
#endif
}
void UninitEnemy() {
for (auto enemy : g_enemies) {
delete enemy;
}
g_enemies.clear();
if (g_VertexBufferEnemy) {
g_VertexBufferEnemy->Release();
g_VertexBufferEnemy = nullptr;
}
}
std::vector<BaseEnemy*>& GetEnemies() {
return g_enemies;
}
//*****************************************************************************
//
//*****************************************************************************
HRESULT MakeVertexEnemy() {
D3D11_BUFFER_DESC bd = {};
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(VERTEX_3D) * 4;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
HRESULT hr = GetDevice()->CreateBuffer(&bd, nullptr, &g_VertexBufferEnemy);
if (FAILED(hr)) return hr;
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(g_VertexBufferEnemy, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D* v = (VERTEX_3D*)msr.pData;
float w = 60.0f, h = 90.0f;
v[0].Position = XMFLOAT3(-w / 2, h, 0);
v[1].Position = XMFLOAT3(w / 2, h, 0);
v[2].Position = XMFLOAT3(-w / 2, 0, 0);
v[3].Position = XMFLOAT3(w / 2, 0, 0);
for (int i = 0; i < 4; ++i) {
v[i].Normal = XMFLOAT3(0, 0, -1);
v[i].Diffuse = XMFLOAT4(1, 1, 1, 1);
}
v[0].TexCoord = XMFLOAT2(0, 0);
v[1].TexCoord = XMFLOAT2(1, 0);
v[2].TexCoord = XMFLOAT2(0, 1);
v[3].TexCoord = XMFLOAT2(1, 1);
GetDeviceContext()->Unmap(g_VertexBufferEnemy, 0);
return S_OK;
}
void EnemySpawner(XMFLOAT3 position, int type) {
BaseEnemy* newEnemy = nullptr;
switch (type) {
case SPIDER: { // SpiderEnemy
SpiderEnemy* spider = new SpiderEnemy();
spider->Init();
spider->SetUsed(true);
spider->SetPosition(position);
newEnemy = spider;
break;
}
case GHOST: { // GhostEnemy
GhostEnemy* ghost = new GhostEnemy();
ghost->Init();
ghost->SetUsed(true);
ghost->SetPosition(position);
newEnemy = ghost;
break;
}
default:
return;
}
if (newEnemy) {
g_enemies.push_back(newEnemy);
}
}
void DropItems(const XMFLOAT3& pos, ENEMY_TYPE enemyType)
{
int itemCount = 0;
auto getRandomOffsetX = []() -> float {
return ((float)(rand() % 41) - 20.0f);
};
auto dropItemAtOffset = [&](int itemId) {
XMFLOAT3 dropPos = pos;
dropPos.x += getRandomOffsetX();
SetItem(dropPos, itemId);
};
float random = (float)rand() / RAND_MAX;
switch (enemyType)
{
case SPIDER:
dropItemAtOffset(ITEM_APPLE); // Apple 100%
if (random < 0.5f) // San 50%
{
dropItemAtOffset(ITEM_SAN);
}
if (random < 0.2f) // fire 20%
{
dropItemAtOffset(PART_FIRE);
}
if (random < 0.2f) // shutgun 20%
{
dropItemAtOffset(PART_SHUTGUN);
}
if (random < 0.5f) // bullet 50%
{
dropItemAtOffset(ITEM_BULLET);
}
break;
case GHOST:
if (random < 0.5f) // Apple 50%
{
dropItemAtOffset(ITEM_APPLE);
}
if (random < 0.2f) // San 20%
{
dropItemAtOffset(ITEM_SAN);
}
if (random < 0.2f) // fire 20%
{
dropItemAtOffset(PART_FIRE);
}
if (random < 0.2f) // shutgun 20%
{
dropItemAtOffset(PART_SHUTGUN);
}
if (random < 0.5f) // bullet 50%
{
dropItemAtOffset(ITEM_BULLET);
}
break;
default:
break;
}
}
//*****************************************************************************
//
//*****************************************************************************
void BaseEnemy::SetPosition(const XMFLOAT3& p) {
pos = p;
}
XMFLOAT3 BaseEnemy::GetPosition() const {
return pos;
}
void BaseEnemy::SetScale(const XMFLOAT3& s) {
scl = s;
}
XMFLOAT3 BaseEnemy::GetScale() const {
return scl;
}
void BaseEnemy::ChasingPlayer(float speed, float chaseRange)
{
// エネミーからプレイヤーまでのベクトル
XMFLOAT3 dir;
dir.x = GetPlayer()->GetPosition().x - pos.x;
dir.y = 0.0f;
dir.z = GetPlayer()->GetPosition().z - pos.z;
float distSq = dir.x * dir.x + dir.y * dir.y + dir.z * dir.z;
float maxSq = chaseRange * chaseRange;
float minSq = minDistance * minDistance;
if (distSq < maxSq && distSq > minSq) {
// 正規化ベクトル
XMVECTOR vec = XMVector3Normalize(XMLoadFloat3(&dir));
XMStoreFloat3(&dir, vec);
// 位置アップデート
pos.x += dir.x * speed;
pos.y += dir.y * speed;
pos.z += dir.z * speed;
}
}
//*****************************************************************************
//
//*****************************************************************************
GhostEnemy::GhostEnemy() :
texture(nullptr), width(100.0f), height(100.0f)
{
material = new MATERIAL{};
XMStoreFloat4x4(&mtxWorld, XMMatrixIdentity());
}
GhostEnemy::~GhostEnemy() {
if (texture) {
texture->Release();
texture = nullptr;
}
delete material;
material = nullptr;
}
void GhostEnemy::Init()
{
D3DX11CreateShaderResourceViewFromFile(
GetDevice(),
"data/2Dpicture/enemy/ghost.png",
NULL, NULL, &texture, NULL);
*material = {};
material->Diffuse = XMFLOAT4(1, 1, 1, 1);
pos = XMFLOAT3(0.0f, 0.0f, ENEMY_OFFSET_Y);
scl = XMFLOAT3(1.0f, 1.0f, 1.0f);
use = true;
moveDir = XMFLOAT3(0.0f, 0.0f, 1.0f); // 現在の動き方向
moveChangeTimer = 2.0f; // 向き変わるタイマー
speed = 0.5f; //エネミーのスピード
currentFrame = 0;
frameCounter = 0;
frameInterval = 15;//change speed
maxFrames = 2;
HP = 50;
//幽霊は重力いらない
EnableGravity(false);
}
void GhostEnemy::Update()
{
frameCounter++;
if (frameCounter >= frameInterval) {
frameCounter = 0;
currentFrame = (currentFrame + 1) % maxFrames;
}
if (!use) return;
// エネミーからプレイヤーまでのベクトル
XMFLOAT3 dir;
dir.x = GetPlayer()->GetPosition().x - pos.x;
dir.y = GetPlayer()->GetPosition().y - pos.y;
dir.z = GetPlayer()->GetPosition().z - pos.z;
//// プレイヤーの座標までの計算
XMFLOAT3 toPlayer = { dir.x, dir.y, dir.z };
float distSq = toPlayer.x * toPlayer.x + toPlayer.y * toPlayer.y + toPlayer.z * toPlayer.z;
float range = 100.0f; // 発射範囲
//攻撃行う範囲
if (distSq < range * range)
{
ChasingPlayer(speed, range);
}
else
{
NormalMovement();
}
//弾と当たり判定?
for (int i = 0; i < MAX_BULLET; i++)
{
if (!bullet[i].use) continue;
XMFLOAT3 enemyHalfSize = { width / 2, height, 50.f }; //エネミーの当たり判定のサイズ
if (CheckSphereAABBCollision(bullet[i].pos, bullet[i].size, pos, enemyHalfSize))
{
bullet[i].use = false;
HP -= 1;
if (HP <= 0)
{
use = false;
DropItems(pos, GHOST);
}
}
}
#ifdef _DEBUG
float dist = sqrtf(distSq);
PrintDebugProc("Enemy2 HP: %d\n", HP);
#endif
}
void GhostEnemy::Draw()
{
if (!use || !texture || !g_VertexBufferEnemy) return;
SetLightEnable(FALSE);
UINT stride = sizeof(VERTEX_3D);
UINT offset = 0;
GetDeviceContext()->IASetVertexBuffers(0, 1, &g_VertexBufferEnemy, &stride, &offset);
GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
CAMERA* cam = GetCamera();
XMMATRIX mtxView = XMLoadFloat4x4(&cam->mtxView);
XMMATRIX mtxWorld = XMMatrixIdentity();
mtxWorld.r[0].m128_f32[0] = mtxView.r[0].m128_f32[0];
mtxWorld.r[0].m128_f32[1] = mtxView.r[1].m128_f32[0];
mtxWorld.r[0].m128_f32[2] = mtxView.r[2].m128_f32[0];
mtxWorld.r[1].m128_f32[0] = mtxView.r[0].m128_f32[1];
mtxWorld.r[1].m128_f32[1] = mtxView.r[1].m128_f32[1];
mtxWorld.r[1].m128_f32[2] = mtxView.r[2].m128_f32[1];
mtxWorld.r[2].m128_f32[0] = mtxView.r[0].m128_f32[2];
mtxWorld.r[2].m128_f32[1] = mtxView.r[1].m128_f32[2];
mtxWorld.r[2].m128_f32[2] = mtxView.r[2].m128_f32[2];
XMMATRIX mtxScl = XMMatrixScaling(scl.x, scl.y, scl.z);
XMMATRIX mtxTranslate = XMMatrixTranslation(pos.x, pos.y, pos.z);
mtxWorld = XMMatrixMultiply(mtxWorld, mtxScl);
mtxWorld = XMMatrixMultiply(mtxWorld, mtxTranslate);
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(g_VertexBufferEnemy, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D* v = (VERTEX_3D*)msr.pData;
float w = width, h = height;
v[0].Position = XMFLOAT3(-w / 2, h, 0);
v[1].Position = XMFLOAT3(w / 2, h, 0);
v[2].Position = XMFLOAT3(-w / 2, 0, 0);
v[3].Position = XMFLOAT3(w / 2, 0, 0);
for (int i = 0; i < 4; ++i) {
v[i].Normal = XMFLOAT3(0, 0, -1);
v[i].Diffuse = XMFLOAT4(1, 1, 1, 1);
}
float tw = 1.0f / maxFrames;
float th = 1.0f;
float tx = currentFrame * tw;
float ty = 0.0f;
v[0].TexCoord = XMFLOAT2(tx, ty);
v[1].TexCoord = XMFLOAT2(tx + tw, ty);
v[2].TexCoord = XMFLOAT2(tx, ty + th);
v[3].TexCoord = XMFLOAT2(tx + tw, ty + th);
GetDeviceContext()->Unmap(g_VertexBufferEnemy, 0);
SetAlphaTestEnable(FALSE);
SetBlendState(BLEND_MODE_ALPHABLEND);
SetWorldMatrix(&mtxWorld);
SetMaterial(*material);
GetDeviceContext()->PSSetShaderResources(0, 1, &texture);
GetDeviceContext()->Draw(4, 0);
}
void GhostEnemy::NormalMovement()
{
// 動き方向変わりタイマー
moveChangeTimer -= 1.0f / 60.0f; // 60fpsことに動き方向変わり
if (moveChangeTimer <= 0.0f) {
moveChangeTimer = 2.0f; // reset timer
// 動き方向変わることはランダム設定
int dir = rand() % 6;
switch (dir)
{
case 0: moveDir = XMFLOAT3(1.0f, 0.0f, 0.0f); break; // 右
case 1: moveDir = XMFLOAT3(-1.0f, 0.0f, 0.0f); break; // 左
case 2: moveDir = XMFLOAT3(0.0f, 0.0f, 1.0f); break; // 前(+z)
case 3: moveDir = XMFLOAT3(0.0f, 0.0f, -1.0f); break; // 後ろ(-z)
case 4: moveDir = XMFLOAT3(0.0f, 1.0f, 0.0f); break; // 上(+y)
case 5: moveDir = XMFLOAT3(0.0f, -1.0f, 0.0f); break; // 下(-y)
}
}
// 新しい位置を計算
XMFLOAT3 newPos = pos;
newPos.x += moveDir.x * speed;
newPos.y += moveDir.y * speed;
newPos.z += moveDir.z * speed;
// 範囲制限(例えば:XとZは -50.0f ? +50.0f)
const float minX = -200.0f;
const float maxX = 200.0f;
const float minZ = -100.0f;
const float maxZ = 100.0f;
const float minY = -50.0f;
const float maxY = 100.0f;
// 範囲内なら移動
if (newPos.x >= minX && newPos.x <= maxX &&
newPos.y >= minY && newPos.y <= maxY &&
newPos.z >= minZ && newPos.z <= maxZ) {
pos = newPos;
}
else {
// 範囲外に出そうなら方向を変える
moveChangeTimer = 0.0f; // すぐ次の方向へ変更
}
}
void GhostEnemy::Attack()
{
}