응애맘마조

포물선 미사일 본문

공부/2D과제

포물선 미사일

TH.Wert 2022. 12. 30. 01:22

이번엔 중력의 영향을 받아 포물선을 그리는 식으로 미사일 발사를 만들었습니다.

실행 영상입니다.

//bullet.h
#pragma once
class Bullet
{
public:
	ObLine		missile;
	ObCircle	missilePet;

	Vector2		fireDir;

	float		scalar;
	float		gravity;
public:
	Bullet();
	void Update(ObRect player);
	void Render();

	bool Shoot(Vector2 fireDir, float scalar, Vector2 firePos);
};
//bullet.cpp
#include "stdafx.h"

Bullet::Bullet()
{
	missile.SetLocalPos(Vector2(2000.0f, 2000.0f));
	missile.scale.x = 30.0f;
	missile.rotation = 0.0f;
	missile.isVisible = false;

	missilePet.SetParentRT(missile);
	missilePet.SetLocalPos(Vector2(50.0f, 50.0f));
	missilePet.scale = Vector2(10.0f, 10.0f);
	missilePet.rotation = 0.0f;
	missilePet.isAxis = true;
	missilePet.isVisible = false;
}

void Bullet::Update(ObRect player)
{
	if (!missilePet.isVisible) return;
    
	Vector2 dir = fireDir * scalar + DOWN * gravity;

	missile.MoveWorldPos(dir * DELTA);
	missile.rotation = Utility::DirToRadian(dir);

	missilePet.revolution += 360.0f * ToRadian * DELTA;

	gravity += 300.0f * DELTA;

	missile.Update();
	missilePet.Update();

	Vector2 velocity = missile.GetWorldPos() - player.GetWorldPos();
	float dis = velocity.Length();

	if (dis > 2000.0f)
	{
		missile.isVisible = false;
		missilePet.isVisible = false;
	}
}

void Bullet::Render()
{
	if (!missilePet.isVisible) return;
	missile.Render();
	missilePet.Render();
}

bool Bullet::Shoot(Vector2 fireDir, float scalar, Vector2 firePos)
{
	if (!missilePet.isVisible)
	{
		missile.isVisible = true;
		missilePet.isVisible = true;
		missilePet.revolution = 0.0f;
		missile.rotation = Utility::DirToRadian(fireDir);
		missile.SetWorldPos(firePos);
		this->fireDir = fireDir;
		gravity = 0.0f;

		this->scalar = scalar * 6.0f;

		return true;
	}
	return false;
}
//main.h
#pragma once
#define BMAX 70

class Main : public Scene
{
private:
	ObRect		player;
	ObCircle	pet;
	ObRect		gaugeBar;
	Bullet		bullet[BMAX];

	GameObject  firePos;

	Vector2		lastPos;

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;
};
//main.cpp
#include "stdafx.h"
#include "Main.h"

void Main::Init()
{
    player.SetWorldPos(Vector2(0.0f, 0.0f));
    player.scale = Vector2(80.0f, 80.0f);
    player.rotation = 0.0f;
    player.isAxis = true;
    player.isFilled = true;
    player.isVisible = true;
    player.color = Color(4.0f / 255.0f, 176.0f / 255.0f, 153.0f / 255.0f, 1.0f);
    
    pet.SetParentRT(player);
    pet.SetLocalPos(Vector2(100.0f, 100.0f));
    pet.scale = Vector2(30.0f, 30.0f);
    pet.rotation = 0.0f;
    pet.isAxis = true;

    gaugeBar.SetParentT(player);
    gaugeBar.pivot = OFFSET_L;
    gaugeBar.SetLocalPos(Vector2(-70.0f, 80.0f));
    gaugeBar.scale = Vector2(150.0f, 25.0f);
    gaugeBar.color = Color(113.0f / 255.0f, 79.0f / 255.0f, 209.0f / 255.0f, 1.0f);

    firePos.SetParentRT(player);
    firePos.SetLocalPosX(player.scale.x * 2.0f);
    firePos.scale = Vector2(80.0f, 80.0f);
    firePos.isAxis = false;
}

void Main::Release()
{
}

void Main::Update()
{
    if (INPUT->KeyPress(VK_UP))
    {
        player.MoveWorldPos(player.GetRight() * 200.0f * DELTA);
    }
    if (INPUT->KeyPress(VK_DOWN))
    {
        player.MoveWorldPos(-player.GetRight() * 200.0f * DELTA);
    }
    if (INPUT->KeyPress(VK_LEFT))
    {
        player.rotation += 120.0f * ToRadian * DELTA;
    }
    if (INPUT->KeyPress(VK_RIGHT))
    {
        player.rotation -= 120.0f * ToRadian * DELTA;
    }

    if (INPUT->KeyDown(VK_SPACE))
    {
        gaugeBar.scale.x = 0.0f;
    }

    if (INPUT->KeyPress(VK_SPACE))
    {
        gaugeBar.scale.x += 150.0f * DELTA;

        if (gaugeBar.scale.x > 150.0f)
        {
            gaugeBar.scale.x = 150.0f;
        }
    }

    //발사해라
    if (INPUT->KeyUp(VK_SPACE))
    {
        for (int i = 0; i < BMAX; i++)
        {
            if(bullet[i].Shoot(player.GetRight(), gaugeBar.scale.x, firePos.GetWorldPos())) break;
        }       
    }

    pet.revolution += 30.0f * ToRadian * DELTA;

    player.Update();
    pet.Update();
    gaugeBar.Update();
    firePos.Update();
    for (int i = 0; i < BMAX; i++)
    {
        bullet[i].Update(player);
    }
}

void Main::LateUpdate()
{
 /*   lastPos = player.GetWorldPos();
    player.SetWorldPosX(lastPos.x + RANDOM->Float(-10.0f, 10.0f));
    player.SetWorldPosY(lastPos.y + RANDOM->Float(-10.0f, 10.0f));
    player.Update();*/
}

void Main::Render()
{
    player.Render();
    pet.Render();
    gaugeBar.Render();
    firePos.Render();
    for (int i = 0; i < BMAX; 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);
	WIN->Create();
	Main* main = new Main();
	int wParam = (int)WIN->Run(main);
	SafeDelete(main);
	WIN->Destroy();
	WIN->DeleteSingleton();
	
	return wParam;
}

코드입니다.

 

읽어주셔서 감사합니다.

'공부 > 2D과제' 카테고리의 다른 글

반사 미사일  (0) 2023.01.02
마우스 미사일  (0) 2022.12.30
가속도 미사일  (2) 2022.12.28
게이지에 따른 미사일 세기  (0) 2022.12.28
회전하는 미사일 공  (0) 2022.12.26
Comments