#include "td/game/BaseGame.h"

namespace td {
namespace game {

Game::Game(World* world) : m_World(world) {

}

Game::~Game() {

}

void Game::Tick(std::uint64_t delta) {
	if (m_GameState == GameState::Game) {
		m_World->Tick(delta);
	}
}

void Game::Reset() {
	m_World->Reset();

	for (auto team : m_Teams) {
		team.ClearPlayers();
	}

	for (auto& [id, player] : m_Players) {
		player.SetExp(0);
		player.SetGold(0);
		player.SetTeamColor(TeamColor::None);
	}
}

Player* Game::GetPlayerById(PlayerID id) {
	auto it = m_Players.find(id);

	if (it == m_Players.end()) return nullptr;

	return &it->second;
}

const Player* Game::GetPlayerById(PlayerID id) const {
	auto it = m_Players.find(id);

	if (it == m_Players.end()) return nullptr;

	return &it->second;
}

void Game::RemovePlayer(PlayerID pId) {
	Player* player = GetPlayerById(pId);

	if (!player) return;

	TeamColor team = player->GetTeamColor();

	GetTeam(team).RemovePlayer(player);
	auto it = GetPlayers().find(pId);

	if (it == GetPlayers().end()) return;

	GetPlayers().erase(it);
}

} // namespace game 
} // namespace td
