응애맘마조

카메라 이동 본문

공부/2D과제

카메라 이동

TH.Wert 2023. 1. 9. 23:58

플레이어가 이동함과 동시에 카메라가 따라오게 하는 것이 과제였습니다.

단, 같이 따라오지 않고 플레이어가 이동했을 때 그 이동에 맞춰서 따라오게 이동 시키는 것이었습니다.

//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(0.5f, 0.0f, 0.0f, 1.0f);
    player.pivot = OFFSET_LB;

    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 * 1.5f);
    firePos.scale = Vector2(80.0f, 80.0f);
    firePos.isAxis = true;

    bg = new ObImage(L"henesis.jpg");
    bg->scale = Vector2(4000.0f, 2800.0f);  

    for (int i = 0; i < 100; i++)
    {
        stars[i] = new ObStar();
        stars[i]->SetWorldPosX(RANDOM->Float(-2000.0f, 2000.0f));
        stars[i]->SetWorldPosY(RANDOM->Float(-1400.0f, 1400.0f));
        float scale = RANDOM->Float(20.0f, 100.0f);
        stars[i]->scale = Vector2(scale, scale);
        stars[i]->color = Color(RANDOM->Float(), RANDOM->Float(), RANDOM->Float(), 1.0f);
    }
}

void Main::Release()
{
    for (int i = 0; i < 100; i++)
    {
        SafeDelete(stars[i]);
    }
}

void Main::Update()
{
    //플레이어 이동
    if (INPUT->KeyPress('W'))
    {
        player.MoveWorldPos(UP * 300.0f * DELTA);
    }
    if (INPUT->KeyPress('S'))
    {
        player.MoveWorldPos(DOWN * 300.0f * DELTA);
    }
    if (INPUT->KeyPress('A'))
    {
        player.MoveWorldPos(LEFT * 300.0f * DELTA);
    }
    if (INPUT->KeyPress('D'))
    {
        player.MoveWorldPos(RIGHT * 300.0f * DELTA);
    }

    //카메라 이동
    if (INPUT->KeyPress(VK_UP))
    {
        CAM->position += UP * 200.0f * DELTA;
    }
    if (INPUT->KeyPress(VK_DOWN))
    {
        CAM->position += DOWN * 200.0f * DELTA;
    }
    if (INPUT->KeyPress(VK_LEFT))
    {
        CAM->position += LEFT * 200.0f * DELTA;
    }
    if (INPUT->KeyPress(VK_RIGHT))
    {
        CAM->position += RIGHT * 200.0f * DELTA;
    }

    Vector2 dir = INPUT->GetWorldMousePos() - player.GetWorldPos();
    player.rotation = Utility::DirToRadian(dir);

    player.rotation = Utility::Saturate(player.rotation, 0.0f, PI);
    ImGui::SliderAngle("Rotation", &player.rotation, 0.0f, 360.0f);

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

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

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

    //발사해라
    if (INPUT->KeyUp(VK_LBUTTON))
    {
        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();
    bg->Update();
    for (int i = 0; i < BMAX; i++)
    {
        bullet[i].Update(player);
    }
    for (int i = 0; i < 100; i++)
    {
        stars[i]->Update();
    }
}

void Main::LateUpdate()
{
    Vector2 velocity = player.GetWorldPos() - CAM->position;
    CAM->position += velocity * DELTA;

    CAM->position.x = Utility::Saturate(CAM->position.x,
        -2000.0f + app.GetHalfWidth(),
        2000.0f - app.GetHalfWidth());
    CAM->position.y = Utility::Saturate(CAM->position.y,
        -1400.0f + app.GetHalfHeight(),
        1400.0f - app.GetHalfHeight());

    player.SetWorldPosX(Utility::Saturate(player.GetWorldPos().x, -2000.0f, 2000.0f));
    player.SetWorldPosY(Utility::Saturate(player.GetWorldPos().y, -1400.0f, 1400.0f));

    //모든 오브젝트가 이동한 뒤
    for (int i = 0; i < BMAX; i++)
    {
        bullet[i].LateUpdate();
    }
}

void Main::Render()
{
    bg->Render();
    player.Render();
    pet.Render();
    gaugeBar.Render();
    //firePos.Render();
    for (int i = 0; i < BMAX; i++)
    {
        bullet[i].Render();
    }
    for (int i = 0; i < 100; i++)
    {
        stars[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.11
카메라 이동 2  (0) 2023.01.11
별 만들기  (0) 2023.01.07
RGB 색상표 만들기  (0) 2023.01.06
중력 반사 미사일  (0) 2023.01.03
Comments