Files
farmlands/src/components/items/Hoe.cpp

78 lines
1.4 KiB
C++

/*
* Hoe.cpp
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#include <GameState.h>
#include <assets/Ground.h>
#include <components/items/Hoe.h>
#include <math/GameMath.h>
#include <model/GameObject.h>
#include <model/Scene.h>
#include <iostream>
using namespace farmlands::assets;
namespace farmlands {
namespace components {
namespace items {
Hoe::Hoe()
: m_back(nullptr)
{
}
Hoe::~Hoe()
{
}
model::Component* Hoe::clone()
{
return new Hoe();
}
void Hoe::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Hoe\n";
}
void Hoe::onInitialize()
{
model::GameObject* root = &GameState::current().scene->root;
// Find background object
auto it = root->findByComponent<Background>();
Assert(it != root->childrenEnd(), "Can't find background game object.");
m_back = (*it)->component<Background>();
}
void Hoe::performAction(float x, float y, model::Direction d)
{
Assert(m_back, "No background object!!!");
// Compute watering position
float digX, digY;
translate(x, y, d, 0.5f, &digX, &digY);
size_t col = floorf(digX);
size_t row = floorf(digY);
// See what the cell contains
Cell backCell = m_back->cell(0, row, col);
Cell soilCell = m_back->cell(1, row, col);
if (groundIsDirt(backCell) && soilCell == Ground::None)
m_back->setCell(1, row, col, Ground::SoilDry);
}
} /* namespace items */
} /* namespace components */
} /* namespace farmlands */