1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementações em GameComponent

This commit is contained in:
Danilo 2024-04-27 15:21:47 -03:00
parent 3a66eaf746
commit 2518d7584d
5 changed files with 113 additions and 81 deletions

View File

@ -1,4 +1,53 @@
#include "component.hpp" #include "component.hpp"
namespace xna { namespace xna {
sptr<IGameComponent> GameComponentCollection::operator[](size_t index) const
{
if (index >= components.size())
return nullptr;
return components[index];
}
sptr<IGameComponent> GameComponentCollection::At(size_t index) const
{
if (index >= components.size())
return nullptr;
return components[index];
}
bool GameComponentCollection::UpdateOrderComparer(sptr<IGameComponent> const& x, sptr<IGameComponent> const& y)
{
auto comp1 = std::reinterpret_pointer_cast<GameComponent>(x);
auto comp2 = std::reinterpret_pointer_cast<GameComponent>(y);
if (!comp1 && !comp2)
return false;
if (!comp1)
return true;
if (!comp2)
return false;
return comp1->UpdateOrder() < comp2->UpdateOrder();
}
bool GameComponentCollection::DrawOrderComparer(sptr<IGameComponent> const& x, sptr<IGameComponent> const& y)
{
auto comp1 = std::reinterpret_pointer_cast<DrawableGameComponent>(x);
auto comp2 = std::reinterpret_pointer_cast<DrawableGameComponent>(y);
if (!comp1 && !comp2)
return false;
if (!comp1)
return true;
if (!comp2)
return false;
return comp1->DrawOrder() < comp2->DrawOrder();
}
} }

View File

@ -104,84 +104,60 @@ namespace xna {
class GameComponentCollection { class GameComponentCollection {
public: public:
void InsertItem(size_t index, sptr<IGameComponent> const& item) { constexpr void InsertItem(size_t index, sptr<IGameComponent> const& item) {
const auto it = components.begin(); const auto it = components.begin();
components.insert(it + index, item); components.insert(it + index, item);
std::sort(components.begin(), components.end(), UpdateOrderComparer); if (AutoSort)
std::sort(components.begin(), components.end(), UpdateOrderComparer);
} }
void RemoveItem(size_t index) { constexpr void RemoveItem(size_t index) {
if (index >= components.size()) if (index >= components.size())
return; return;
const auto it = components.begin(); const auto it = components.begin();
components.erase(it + index); components.erase(it + index);
std::sort(components.begin(), components.end(), UpdateOrderComparer); if (AutoSort)
std::sort(components.begin(), components.end(), UpdateOrderComparer);
} }
void ClearItems() { constexpr void ClearItems() {
components.clear(); components.clear();
} }
void Add(sptr<IGameComponent> const& item) { constexpr void Add(sptr<IGameComponent> const& item) {
components.push_back(item); components.push_back(item);
std::sort(components.begin(), components.end(), UpdateOrderComparer); if(AutoSort) std::sort(components.begin(), components.end(), UpdateOrderComparer);
} }
constexpr size_t Count() const { constexpr size_t Count() const {
return components.size(); return components.size();
} }
sptr<IGameComponent> operator[](size_t index) const { sptr<IGameComponent> operator[](size_t index) const;
if (index >= components.size())
return nullptr;
return components[index]; sptr<IGameComponent> At(size_t index) const;
constexpr void Sort() {
std::sort(components.begin(), components.end(), UpdateOrderComparer);
}
static bool UpdateOrderComparer(sptr<IGameComponent> const& x, sptr<IGameComponent> const& y);
static bool DrawOrderComparer(sptr<IGameComponent> const& x, sptr<IGameComponent> const& y);
static constexpr void UpdateSort(std::vector<sptr<IGameComponent>>& components) {
std::sort(components.begin(), components.end(), UpdateOrderComparer);
} }
sptr<IGameComponent> At(size_t index) const { static constexpr void DrawSort(std::vector<sptr<IGameComponent>>& components) {
if (index >= components.size()) std::sort(components.begin(), components.end(), DrawOrderComparer);
return nullptr;
return components[index];
}
static bool UpdateOrderComparer(sptr<IGameComponent> const& x, sptr<IGameComponent> const& y) {
auto comp1 = std::reinterpret_pointer_cast<GameComponent>(x);
auto comp2 = std::reinterpret_pointer_cast<GameComponent>(y);
if (!comp1 && !comp2)
return false;
if (!comp1)
return true;
if (!comp2)
return false;
return comp1->UpdateOrder() < comp2->UpdateOrder();
}
static bool DrawOrderComparer(sptr<IGameComponent> const& x, sptr<IGameComponent> const& y) {
auto comp1 = std::reinterpret_pointer_cast<GameComponent>(x);
auto comp2 = std::reinterpret_pointer_cast<GameComponent>(y);
if (!comp1 && !comp2)
return false;
if (!comp1)
return true;
if (!comp2)
return false;
return comp1->UpdateOrder() < comp2->UpdateOrder();
} }
public: public:
std::vector<sptr<IGameComponent>> components; std::vector<sptr<IGameComponent>> components;
bool AutoSort{ false };
}; };
} }

View File

@ -18,7 +18,6 @@ namespace xna {
virtual sptr<GameWindow> Window() = 0; virtual sptr<GameWindow> Window() = 0;
virtual sptr<GraphicsDevice> GetGraphicsDevice() = 0; virtual sptr<GraphicsDevice> GetGraphicsDevice() = 0;
virtual sptr<GameComponentCollection> Components() = 0; virtual sptr<GameComponentCollection> Components() = 0;
virtual void DisableGameComponets(bool value) = 0;
protected: protected:
virtual void Draw(GameTime const& gameTime) = 0; virtual void Draw(GameTime const& gameTime) = 0;

View File

@ -36,28 +36,7 @@ namespace xna {
} }
return startLoop(); return startLoop();
} }
void Game::Draw(GameTime const& gameTime) {
if (!_disableGameComponent) {
std::sort(_drawableGameComponents.begin(), _drawableGameComponents.end(), GameComponentCollection::DrawOrderComparer);
for (size_t i = 0; i < _drawableGameComponents.size(); ++i) {
auto& component = _drawableGameComponents[i];
if (!component) continue;
auto drawable = reinterpret_pointer_cast<IDrawable>(component);
if(drawable && drawable->Visible())
drawable->Draw(gameTime);
}
_drawableGameComponents.clear();
}
_graphicsDevice->Present();
}
void Game::Initialize() { void Game::Initialize() {
Keyboard::Initialize(); Keyboard::Initialize();
@ -82,23 +61,51 @@ namespace xna {
LoadContent(); LoadContent();
} }
void Game::Draw(GameTime const& gameTime) {
if (_enabledGameComponents && !_drawableGameComponents.empty()) {
const auto count = _drawableGameComponents.size();
if (count != _drawableGameComponentsCount && _gameComponents->AutoSort) {
GameComponentCollection::DrawSort(_drawableGameComponents);
_drawableGameComponentsCount = count;
}
for (size_t i = 0; i < count; ++i) {
auto& component = _drawableGameComponents[i];
if (!component) continue;
auto drawable = reinterpret_pointer_cast<IDrawable>(component);
if (drawable && drawable->Visible())
drawable->Draw(gameTime);
}
_drawableGameComponents.clear();
}
_graphicsDevice->Present();
}
void Game::Update(GameTime const& gameTime) { void Game::Update(GameTime const& gameTime) {
_audioEngine->Update(); _audioEngine->Update();
if (!_disableGameComponent) { if (_enabledGameComponents && _gameComponents->Count() > 0) {
for (size_t i = 0; i < _gameComponents->Count(); ++i) { const auto count = _gameComponents->Count();
for (size_t i = 0; i < count; ++i) {
auto component = _gameComponents->At(i); auto component = _gameComponents->At(i);
if (!component) continue; if (!component) continue;
if (component->Type() == GameComponentType::Drawable) if (component->Type() == GameComponentType::Drawable) {
_drawableGameComponents.push_back(component); _drawableGameComponents.push_back(component);
}
auto updatable = reinterpret_pointer_cast<IUpdateable>(component); auto updatable = reinterpret_pointer_cast<IUpdateable>(component);
if(updatable && updatable->Enabled()) if(updatable && updatable->Enabled())
updatable->Update(gameTime); updatable->Update(gameTime);
} }
} }
} }

View File

@ -30,8 +30,8 @@ namespace xna {
return _gameComponents; return _gameComponents;
} }
void DisableGameComponets(bool value) override { constexpr void EnableGameComponents(bool value) {
_disableGameComponent = value; _enabledGameComponents = value;
} }
protected: protected:
@ -58,7 +58,8 @@ namespace xna {
void step(); void step();
sptr<GameComponentCollection> _gameComponents = nullptr; sptr<GameComponentCollection> _gameComponents = nullptr;
std::vector<sptr<IGameComponent>> _drawableGameComponents; std::vector<sptr<IGameComponent>> _drawableGameComponents;
bool _disableGameComponent{ false }; Uint _drawableGameComponentsCount{ 0 };
bool _enabledGameComponents{ false };
}; };
} }