Hamutaro
공부 좀 해라★彡/DirectX

크아 모작 9일차

요미 ★ 2024. 4. 19.

이제 물풍선을 만들어 볼것이다.

BubbleManager을 만들어서 Bubble을 Pooling을 할것이다.

왜냐하면 물풍선 놓을때마다 생성하면 효율이 떨어질거 같아서 돌려서 쓰려고 한다.

 

대강 잡은 BubbleManager

#pragma once

class BubbleManager : public Singleton<BubbleManager>, public PoolingManager<Bubble>
{
private:
	friend class Singleton;

	const UINT POOL_SIZE = 50;

	BubbleManager();
	~BubbleManager();

public:
	void Render();
	void Update();

public:
	Bubble* Collision(string key, Collider* collider);

public:
	void Spawn(const Vector2& pos, int power);

	vector<Bubble*> bubbles;
};

//cpp
#include "Framework.h"

BubbleManager::BubbleManager()
{
	totalObject["BasicBubble"].resize(POOL_SIZE);

	for (GameObject*& bubble : totalObject["BasicBubble"]) 
	{
		bubble = new Bubble();
		RenderManager::Get()->Add("GameObject", bubble);
	}
}

BubbleManager::~BubbleManager()
{
}

void BubbleManager::Render()
{
	for (GameObject*& bubble : totalObject["BasicBubble"])
		bubble->Render();

}

void BubbleManager::Update()
{
	for (GameObject*& bubble : totalObject["BasicBubble"])
		bubble->Update();

}

Bubble* BubbleManager::Collision(string key, Collider* collider)
{
	for (GameObject* bullet : totalObject[key])
	{
		if (((Bubble*)bullet)->GetCollider()->IsCollision(collider))
		{
			return (Bubble*)bullet;
		}
	}

	return nullptr;
}

void BubbleManager::Spawn(const Vector2& pos, int power)
{
	Pop("BasicBubble")->Spawn(pos, power);
}

 스폰할때 power을 넘겨주어서 물줄기를 주는 것이다.

 

#pragma once

class Bubble : public GameObject
{
private:
	const string PATH = "ResourcesCA/Textures/Bubble/";
	const float MAX_PLAY_TIME = 2.0f;

	enum State
	{
		STAND, POP
	};

public:
	Bubble();
	~Bubble();

	void Update();
	void Render();

	void CreatActions();

	void Spawn(const Vector2& pos, int power);

	Collider* GetCollider() { return collider; }

	float GetDepth() override;

	void SetAction(int state);

protected:
	State curState = STAND;

	int power;

	Vector2 velocity;

	RectCollider* collider;

	map<State, Action*> actions;

	float playTime = 0.0f;
};

//cpp

#include "Framework.h"

Bubble::Bubble()
{
	SetActive(false);

	CreatActions();

	collider = new RectCollider({ Tile::TILE_SIZE-10.0f, Tile::TILE_SIZE - 10.0f });
	collider->SetParent(this);
}

Bubble::~Bubble()
{
	delete collider;
}

void Bubble::Update()
{
	if (!this->IsActive()) return;

	playTime += DELTA;
	if (playTime > MAX_PLAY_TIME)
	{
		playTime -= MAX_PLAY_TIME;
		SetAction(POP);
	}

	UpdateWorld();
	collider->UpdateWorld();
	actions[curState]->Update();
}

void Bubble::Render()
{
	if (!this->IsActive()) return;

	collider->Render();
	actions[curState]->Render();
	waves->Render();
}

void Bubble::CreatActions()
{
	Action* action = new Action();
	action->LoadClip(ToWString(PATH) + L"Stand.png", 3, 1, true, 1.4f);
	actions[STAND] = action;


	action = new Action();
	action->LoadClip(ToWString(PATH) + L"Pop.png", 6, 1, false, 1.5f);

	action->GetClip(0)->SetEvent([this]() {
		SetActive(false);
		});
	actions[POP] = action;
	
}

void Bubble::Spawn(const Vector2& pos, int speed)
{
	UpdateWorld();
	collider->UpdateWorld();

	Tile* tile = TileManager::Get()->GetNearPosTileState(pos);

	if (!tile) return;

	SetActive(true);
	SetAction(STAND);
	this->power = power;
	this->SetGlobalPosition(tile->GetGlobalPosition());
	tile->SetType(Tile::OBSTACLE);
}

float Bubble::GetDepth()
{
	return collider->Bottom();
}

void Bubble::SetAction(int state)
{
	if (curState == state) return;

	curState = (State)state;
	actions[curState]->Start();
}

여기서 pos를 주면 해당 pos에 맞는 Tile을 반환해주는  GetNearPosTileState()가 핵심이다.

 

Tile이 2차원 배열로 되어있다.

(0,0)의 배열을 찾아서 거리를 비교하면 해당 인덱스를 알 수 있다.

 

대략 이런식임

따라서 해당 인덱스의 Tile을 반환하고 그 Tile의 위치에 맞춰서 Bubble을 스폰해줘야한다 

왜냐하면 타일과 타일 사이에 놓는건 안되기 때문!

Tile* TileManager::GetNearPosTileState(Vector2 pos)
{
    Vector2 firstTilePos = bgTiles[0][0]->GetGlobalPosition();

    Vector2 calPos = { pos.x - firstTilePos.x,firstTilePos.y-pos.y };
     calPos /=  Tile::TILE_SIZE;
     calPos= { round(calPos.x), round(calPos.y) };

    return bgTiles[calPos.x][calPos.y];
}

이렇게 해주면 타일 위치 잘 잡는다!! ㅎㅎ

 

 

'공부 좀 해라★彡 > DirectX' 카테고리의 다른 글

크아 모작 10일차  (0) 2024.04.19
크아 모작 8일차  (1) 2024.04.19
DirectX 충돌처리 AABB , OBB  (1) 2024.04.17
크아 모작 포폴 - 7일차  (0) 2024.04.16
DirectX 2D 타일 맵 깔기  (2) 2024.04.05

댓글