응애맘마조

길 막기 본문

공부/2D과제

길 막기

TH.Wert 2023. 2. 6. 23:30

타일맵에서 움직일 수 없는 타일맵을 구현하는 것이 과제였습니다.

//main.h
#pragma once
class Main : public Scene
{
private:
	ObTileMap* map;
	TitanSoul* player;
	TitanBoss* boss;


	vector<Tile*> PlWay;

	Vector2 plDest;
	Vector2 plSour;

	float   g;

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()
{
	map = new ObTileMap;
	map->file = "map2.txt";
	map->Load();
	map->CreateTileCost();

	player = new TitanSoul;
	boss = new TitanBoss;
}

void Main::Release()
{	
	SafeDelete(player);
	SafeDelete(boss);
	SafeDelete(map);
}

void Main::Update()
{
	if (INPUT->KeyDown(VK_RBUTTON))
	{
		Int2 sour, dest;

		bool isFind;
		isFind = map->WorldPosToTileIdx(player->col->GetWorldPos(), sour);
		isFind &= map->WorldPosToTileIdx(INPUT->GetWorldMousePos(), dest);

		if (isFind)
		{
			if (map->PathFinding(sour, dest, PlWay))
			{
				for (int i = 0; i < PlWay.size(); i++)
				{
					cout << "Way" << i << ":" << PlWay[i]->idx.x <<
						"," << PlWay[i]->idx.y << endl;
				}

				g = 0.0f;
				plSour = player->col->GetWorldPos();
				PlWay.pop_back();
				plDest = PlWay.back()->Pos;
			}
		}
	}
    
	if (!PlWay.empty())
	{
		player->col->SetWorldPos(Utility::Lerp(plSour, plDest, g));
		g += DELTA * 3.0f;

		if (g > 1.0f)
		{
			g = 0.0f;
			plSour = plDest;
			PlWay.pop_back();

			if (!PlWay.empty())
				plDest = PlWay.back()->Pos;
		}
	}

	boss->targetPos = player->col->GetWorldPos();
	CAM->position = player->col->GetWorldPos();

	player->Update();
	boss->Update();
	map->Update();
}

void Main::LateUpdate()
{
	Int2 tileIdx;
	vector<Vector2> footPos = player->SetFoot();
	for (int i = 0; i < 4; i++)
	{
		if (map->WorldPosToTileIdx(footPos[i], tileIdx))
		{
			if (map->GetTileState(tileIdx) == TILE_WALL)
			{
				player->StepBack();
			}
		}
	}
	
}

void Main::Render()
{
	map->Render();
	player->Render();
	boss->Render();
}

void Main::ResizeScreen()
{
}

int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR param, int command)
{
    app.SetAppName(L"Game1");
    app.SetInstance(instance);
	app.InitWidthHeight(1600.0f, 1080.0f);
	WIN->Create();
	Main* main = new Main();
	int wParam = (int)WIN->Run(main);
	SafeDelete(main);
	WIN->Destroy();
	WIN->DeleteSingleton();
	
	return wParam;
}

코드입니다.

 

읽어주셔서 감사합니다.

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

장면전환  (0) 2023.02.14
플레이어와 보스에게 조명  (0) 2023.02.10
맵 만들기  (0) 2023.02.01
8방향 몬스터  (0) 2023.01.30
8방향 캐릭터  (0) 2023.01.27
Comments