WIN32API FrameWork/한단계씩 직접 구현
21. 총알 발사하기 / 목표물까지의 각도 구하기
hyrule
2022. 5. 20. 13:25
https://hyrule.tistory.com/111
*** 공부 방법 ***
1. 코딩을 해야 하는 부분은 첫 부분에 변수나 함수, 메소드에 대한 선언이 코드블럭으로 표시되어 있다. //ex) MakeFunction(); 2. 코드블럭 하단에는 해당 선언에 대한 구현 로직이 작성되어 있다. 처
hyrule.tistory.com
class CBullet
< 만들어보기 >
- 총알 클래스를 생성한다.
-- 구현이 매우 간단하다. 생성된 위치에서 지정된 방향으로 지정된 속도만큼 이동해주면 끝이기 때문이다.
-- 충돌 처리는 나중에 아예 충돌만 처리하는 클래스를 만들어 처리할 것이다. 일단 충돌이 없더라도 맛만 보자.
--- 화면을 벗어나면 객체를 삭제하는 기능
--- 총알을 생성했는데, 방향과 속도를 지정해주지 않으면 에러 메시지를 발생시키거나 게임을 종료하는 기능
더보기
//Bullet.h
#pragma once
#include "../GameInfo.h"
#include "GameObject.h"
class CBullet :
public CGameObject
{
friend class CScene;
private:
float m_Speed;
Vector2 m_Dir;
float m_Distance;
bool m_isSet;
protected:
CBullet();
CBullet(const CBullet& Obj);
virtual ~CBullet();
public:
bool Init();
void Update(float DeltaTime);
void Render(HDC hDC, float DeltaTime);
//창 밖을 넘어가면 파괴(임시)
bool DestroyCheck();
void SetSpeedDir(float _x, Vector2 Dir)
{
m_isSet = true;
m_Speed = _x;
m_Dir = Dir;
}
};
//Bullet.cpp
#include "Bullet.h"
CBullet::CBullet()
{
}
CBullet::CBullet(const CBullet& Obj) :
CGameObject(Obj),
m_Dir(Obj.m_Dir),
m_Speed(Obj.m_Speed),
m_Distance(Obj.m_Distance)
{
}
CBullet::~CBullet()
{
}
bool CBullet::Init()
{
m_Speed = 100.f;
m_Distance = 0.f;
SetSize(50.f, 50.f);
SetPivot(0.5f, 0.5f);
return true;
}
void CBullet::Update(float DeltaTime)
{
//파괴 조건이 만족되면 총알 제거
//거리를 벗어났거나, 총알 기본세팅을 하지 않았다면 그냥 제거
if (DestroyCheck() || !m_isSet)
{
SetActive(false);
}
m_Distance += m_Speed * DeltaTime;
m_Pos += m_Dir * m_Speed * DeltaTime;
}
void CBullet::Render(HDC hDC, float DeltaTime)
{
Vector2 RenderLeftTop = m_Pos - (m_Size * m_Pivot);
Ellipse(hDC, (int)RenderLeftTop.x,
(int)RenderLeftTop.y,
(int)(RenderLeftTop.x + m_Size.x),
(int)(RenderLeftTop.y + m_Size.y));
}
bool CBullet::DestroyCheck()
{
if (m_Distance >= 1000.f)
return true;
return false;
}
//클래스 수정, 변경
class CMonster
- 몬스터가 왼쪽으로 쭉 나아가는 총알을 발사한다
- 마찬가지로, 발사방법도 어렵지 않다.
-- 일정 시간마다 총알을 생성하고,
-- 총알의 생성 위치와 방향을 지정해주면 되기 때문이다.
더보기
//Monster.h 수정/변경사항
private:
//위는 총알 쿨타임 지정, 아래는 총알 쿨타임 체크용 변수
float m_BulletDuration;
float m_BulletDurationTimer;
//Monster.cpp 수정/변경사항
//추가 포함
#include "Bullet.h"
#include "../Scene/SceneManager.h"
//복사 생성자도 추가
CMonster::CMonster(const CMonster& Obj):
CCharacter(Obj),
m_BulletDuration(Obj.m_BulletDuration),
m_BulletDurationTimer(Obj.m_BulletDurationTimer)
{
}
//초기화 메소드 추가사항
bool CMonster::Init()
{
m_Dir = Vector2(0.f, 1.f);
m_Speed = 400.f;
m_BulletDuration = 1.f;
m_BulletDurationTimer = 0.f;
//...이외의 코드들...
return true;
}
//업데이트 메소드 추가사항
void CMonster::Update(float DeltaTime)
{
//
//...이외의 코드들...//
//
m_BulletDurationTimer += DeltaTime;
if (m_BulletDurationTimer > m_BulletDuration)
{
CBullet* Bullet = CSceneManager::GetInst()->GetScene()->CreateObject<CBullet>("Bullet");
Bullet->SetSpeedDir(1000.f, Vector2(-1.f, 0.f));
Bullet->SetPos(m_Pos.x, m_Pos.y);
m_BulletDurationTimer = 0.f;
}
}

GameFrameworkStepbyStep_21_Bullet, Monster.zip
4.09MB