Bullet.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
class Bullet
{
public:
    ObLine        arrow;
    ObCircle    arrowPet;
 
    float       scalar;
    float       gravity;
 
    Vector2     fireDir; //추가
   
 
public:
    Bullet();
    void Update(ObRect player);
    void Render();
    bool Shoot(ObRect player, float scalar, Vector2 firePos); //Vector2 firePos 추가
};
 
 
cs

Vector2 fireDir;

bool Shoot(ObRect player, float scalar, Vector2 firePos);

 

 

Bullet.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
#include "stdafx.h"
 
Bullet::Bullet()
{ ... }
 
void Bullet::Update(ObRect player)
{
    if (!arrow.isVisible) return;
    if (!arrowPet.isVisible) return;
 
    gravity += 600.0f * DELTA;  //추가
 
    Vector2 velocity = fireDir * scalar + DOWN * gravity; //추가
 
    arrow.MoveWorldPos(velocity * DELTA);                 //수정
    arrow.rotation = Utility::DirToRadian(velocity);      //추가
 
    arrowPet.rotation2 += 360.0f * ToRadian * DELTA;
 
    arrow.Update();
    arrowPet.Update();
 
    Vector2 Dis = arrow.GetWorldPos() - player.GetWorldPos();
 
    float dis = Dis.Length();
 
    if (dis > 2000.0f)
    {
        arrow.isVisible = false;
        arrowPet.isVisible = false;
    }
}
 
void Bullet::Render()
{
    arrow.Render();
    arrowPet.Render();
}
 
bool Bullet::Shoot(ObRect player, float scalar, Vector2 firePos)
{
    if (!arrow.isVisible)
    {
        arrow.isVisible = true;
        arrowPet.isVisible = true;
        
        arrow.SetWorldPos(firePos);
        arrow.rotation = Utility::DirToRadian(player.GetRight());            
 
        this->scalar = 150.0f + scalar * 2.0f;
 
        fireDir = player.GetRight(); //추가
 
        gravity = 0.0f; //추가
 
        arrowPet.rotation2 = 0.0f;
 
        return true;
    }
    return false;
}
 
cs

gravity += 600.0f * DELTA;  //중력값 설정

Vector2 velocity = fireDir * scalar + DOWN * gravity; //

arrow.MoveWorldPos(velocity * DELTA); 

 

arrow.rotation = Utility::DirToRadian(velocity);  //arrow가 발사된 후 떨어지면서 바닥을 향해 회전함. 

 

 

Main.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
#define MAX 25
 
class Main : public Scene
{
private:
    ObRect        player;
    ObCircle    pet;
 
    ObRect        playerGauge;
    Bullet        bullet[MAX];
    Vector2        lastPos;
 
    GameObject    firePos; // 추가
 
public:
    virtual void Init() override;
    virtual void Release() override; 
    virtual void Update() override;
    virtual void LateUpdate() override;
    virtual void Render() override;
    virtual void ResizeScreen() override;
};
 
cs

GameObject  firePos;

arrow가 발사되는 위치를 바꿔주기 위해 GameObject firePos;를 생성한다. firePos에 arrow 발사 위치를 설정한다.

 

 

Main.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
#include "stdafx.h"
#include "Main.h"
 
void Main::Init()
{
    player.SetWorldPos(Vector2(0.0f, 0.0f));
    player.scale = Vector2(100.0f, 100.0f);
    player.rotation = 0.0f;
    player.isFilled = true;
    player.isAxis = true;
    player.color = Color(0.0f, 0.0f, 1.0f, 1.0f);
    player.pivot = OFFSET_N;
 
    firePos.SetParentRT(player);                //추가
    firePos.SetLocalPos(Vector2(200.0f, 0.0f)); //추가
    firePos.scale = Vector2(100.0f, 100.0f);    //추가
    firePos.isAxis = false;                     //추가, 좌표처럼 보이는 선 안 보이게 꺼줌.  
 
    playerGauge.SetLocalPos(Vector2(-75.0f, 75.0f));
    playerGauge.scale = Vector2(150.0f, 30.0f);
    playerGauge.rotation = 0.0f;
    playerGauge.isFilled = true;
    playerGauge.color = Color(0.5f, 0.5f, 0.0f, 1.0f);
    playerGauge.pivot = OFFSET_L;
 
    playerGauge.SetParentRT(player);
 
    pet.SetLocalPos(Vector2(100.0f, 100.0f));
    pet.scale = Vector2(30.0f, 30.0f);
    pet.rotation = 0.0f;
    pet.isAxis = true;
    pet.isFilled = true;
    pet.SetParentRT(player);
}
 
void Main::Release()
{
}
 
void Main::Update()
{
    cout << "Mouse : " << INPUT->GetMousePos().x << "," << INPUT->GetMousePos().y << endl;
 
    player.color = Color(RANDOM->Float(), RANDOM->Float(0.0f, 1.0f), RANDOM->Float(0.0f, 1.0f), 1.0f);
 
    player.SetWorldPos(lastPos);
 
    if (INPUT->KeyPress(VK_UP))
    {
        player.MoveWorldPos(player.GetUp() * 200.0f * DELTA);
    }
    else if (INPUT->KeyPress(VK_DOWN))
    {
        player.MoveWorldPos(-player.GetUp() * 200.0f * DELTA);
    }
 
    if (INPUT->KeyPress(VK_LEFT))
    {
        player.rotation += 120.0f * ToRadian * DELTA;
    }
    else if (INPUT->KeyPress(VK_RIGHT))
    {
        player.rotation -= 120.0f * ToRadian * DELTA;
    }
    
    if (INPUT->KeyDown(VK_SPACE))
    {
        playerGauge.scale.x = 0.0f;
    }
 
    if (INPUT->KeyPress(VK_SPACE))
    {
        playerGauge.scale.x += 100.0f * DELTA;
 
        if (playerGauge.scale.x > 150.0f)
        {
            playerGauge.scale.x = 0.0f;
        }
    }
 
    if (INPUT->KeyUp(VK_SPACE))
    {
        for (int i = 0; i < MAX; i++)
        {
            if (bullet[i].Shoot(player, playerGauge.scale.x, firePos.GetWorldPos())) //firePos.GetWorldPos() 
            {
                break;
            }
        }
    }
 
    pet.rotation2 += 60.0f * ToRadian * DELTA;
 
    player.Update();
    pet.Update(); 
 
    for (int i = 0; i < MAX; i++)
    {
        bullet[i].Update(player);
    }
    playerGauge.Update();
 
    firePos.Update();
}
 
void Main::Render()
{
    player.Render();
    pet.Render();
    playerGauge.Render();
    for (int i = 0; i < MAX; i++)
    {
        bullet[i].Render();
    }       
}
 
void Main::ResizeScreen()
{
}
 
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR param, int command)
{
    app.SetAppName(L"Game1");
    app.SetInstance(instance);
    app.InitWidthHeight(1400,800.0f);
    Main* main = new Main();
    int wParam = (int)WIN->Run(main);
    WIN->DeleteSingleton();
    SafeDelete(main);
    return wParam;
}
cs

firePos.SetParentRT(player);                       //추가

firePos.SetLocalPos(Vector2(200.0f, 0.0f)); //추가
firePos.scale = Vector2(100.0f, 100.0f);       //추가
firePos.isAxis = false;                                   //추가, 좌표처럼 보이는 선 안 보이게 꺼줌.

 

 

if (INPUT->KeyUp(VK_SPACE))
    {
        for (int i = 0; i < MAX; i++)
        {
            if (bullet[i].Shoot(player, playerGauge.scale.x, firePos.GetWorldPos())) 
            {  break;  }
        }
    }

 

firePos.GetWorldPos() 추가


Mouse 좌표를 DX좌표로 바꿔준다.

 

 

'⭐ DirectX > DirectX11 2D + WinAPI' 카테고리의 다른 글

[WinAPI] 충돌  (0) 2022.08.10
[WinAPI] 마우스 회전  (0) 2022.08.09
[WinAPI] Bullet 속도를 Gauge Bar와 연동  (0) 2022.07.29
[winAPI] 시계 만들기  (0) 2022.07.22
[WinAPI] 도형 만들기  (0) 2022.07.07