Game Development Reference
In-Depth Information
m_spriteBatch->Begin();
{
for (int i = 0; i < Rows(); ++i )
{
for ( int j = 0; j < Columns(); ++j )
{
auto& slot = m_items[i][j];
auto& item = slot.Item();
item.Draw(i, j, slot,
m_spriteBatch, m_font, m_position);
}
}
}
m_spriteBatch->End();
}
The grid_item class wewill create will notonlyneedtodrawitself, butitwill needtohave
some form of unique identifier. The identifier will allow us to test the grid to see if an item
is already in a slot, in which case we increment its count. Normally, game items already
have some unique identifier that we could leverage.
class grid_item
{
public:
grid_item(unsigned int id, std::shared_ptr<render::texture> texture = nullptr)
: m_id(id)
, m_texture(texture)
{}
void Draw(int m, int n, const slot& slot, std::shared_ptr<DirectX::SpriteBatch> spriteBatch, std::shared_ptr<DirectX::SpriteFont> font,
const math::vector2& position)
{
if ( m_texture == nullptr )
return;
auto width = m_texture->Width();
auto height = m_texture->Height();
math::rectangle rc;
rc.Left() = position.x() + (m * width);
rc.Top() = position.y() + (n * height);
rc.Width() = static_cast<float>(width);
rc.Height() = static_cast<float>(height);
spriteBatch->Draw(*m_texture->GetView(), rc);
std::wostringstream ss;
ss << slot.Count();
std::wstring itemCount = ss.str();
font->DrawString(spriteBatch.get(),
itemCount.c_str(),
rc.Position());
}
bool operator == (const grid_game_item& rhs) const
{
Search WWH ::




Custom Search