WIN32API FrameWork/원본

220509_WIN32API_6-1_총알 발사 - 총알 클래스 구현

hyrule 2022. 5. 12. 14:08

<구현 순서>

1. GameObject class를 상속받는 총알 클래스를 만들어 준다.

2. 총알을 발사할 객체에서 총알 발사 입력이 들어오면 총알을 생성한다.

3. 총알을 생성하고, 방향과 속도를 지정해 준다.

 

 

 

1. GameObject class를 상속받는 총알 클래스를 만들어 준다.

#pragma once


#include "GameObject.h"

class CBullet :
    public CGameObject
{
    friend class CScene;

protected:
    CBullet();
    CBullet(const CBullet& obj);
    virtual ~CBullet();

    virtual bool Init();
    virtual void Update(float DeltaTime);
    virtual void Render(HDC hDC, float DeltaTime);

private:
    //여기서는 총알의 전진 속도
    float m_Speed;

    //소환한 객체와의 거리
    float m_Dist;

    //총알이 나아갈 방향
    Vector2 m_Dir;

public:
    void SetSpeed(float speed)
    {
        m_Speed = speed;
    }
    float GetSpeed() const
    {
        return m_Speed;
    }

    void SetDir(Vector2 dir)
    {
        m_Dir = dir;
    }
    Vector2 GetDir() const
    {
        return m_Dir;
    }


};
//Class CBullet: public CGameObject
//Bullet.h

#include "Bullet.h"

CBullet::CBullet()
{
}

CBullet::CBullet(const CBullet& obj) :
	CGameObject(obj),
	m_Speed(obj.m_Speed),
	m_Dir(obj.m_Dir)

{

}

CBullet::~CBullet()
{
}

bool CBullet::Init()
{
	SetPivot(0.5f, 0.5f);
	SetSize(10.f, 10.f);
	SetSpeed(2000.f);

	return true;
}

void CBullet::Update(float DeltaTime)
{
	//위치 업데이트
	m_Pos += m_Dir * m_Speed * DeltaTime;


	//화면 밖을 벗어지면 삭제
	if (m_Pos.x <= 0 || m_Pos.x >= 1280 || m_Pos.y <=0 || m_Pos.y >= 720)
	{
		SetActive(false);
	}
	
}

void CBullet::Render(HDC hDC, float DeltaTime)
{
	Vector2 RenderLT = m_Pos - m_Size * m_Pivot;

	Ellipse(hDC, (int)RenderLT.x, (int)RenderLT.y, (int)(RenderLT.x + m_Size.x), (int)(RenderLT.y + m_Size.y));
}