Implemented picking up objects and dropping them. Also, made transform part of GameObject instead of a separate component.

This commit is contained in:
2016-12-14 23:32:39 +02:00
parent f8571b36bd
commit ddae4934ef
23 changed files with 237 additions and 180 deletions

View File

@ -6,7 +6,6 @@
*/
#include <components/basic/Grid.h>
#include <components/basic/Transform.h>
#include <utils/Assert.h>
#include <utils/Exceptions.h>
@ -60,16 +59,8 @@ void Grid::onInitialize()
{
GameObject* obj = *it;
// Get transform
Transform* tr = obj->component<Transform>();
if (tr == nullptr)
{
std::cerr << "Grid: ignoring object " << obj->name << ": object has no transform.";
continue;
}
// Compute grid position(s)
Rect<int> bounds(tr->x, tr->y, roundf(tr->w), roundf(tr->h));
Rect<int> bounds(obj->transform.x, obj->transform.y, roundf(obj->transform.w), roundf(obj->transform.h));
if (!bounds.intersects(m_bounds))
{
std::cerr << "Grid: ignoring object " << obj->name << ": object outside allowed bounds.";
@ -139,11 +130,8 @@ void Grid::set(model::GameObject* obj, int x, int y, bool throwOnOverwrite)
}
m_grid[index] = obj;
// Set transform as well
Transform* transf = obj->component<Transform>();
transf->x = x;
transf->y = y;
obj->transform.x = x;
obj->transform.y = y;
}
} /* namespace basic */

View File

@ -142,7 +142,17 @@ void Inventory::setCapacity(size_t capacity)
m_capacity = capacity;
}
size_t Inventory::emptySlots() const
{
size_t count = 0;
for (size_t i = 0; i < m_capacity; i++)
if (m_items[i] == nullptr)
++count;
return count;
}
} /* namespace basic */
} /* namespace components */
} /* namespace farmlands */

View File

@ -65,6 +65,11 @@ namespace basic {
*/
size_t capacity() const { return m_capacity; }
/**
* Gets the number of empty slots.
*/
size_t emptySlots() const;
private:
size_t m_capacity;
model::GameObject** m_items;

View File

@ -6,7 +6,6 @@
*/
#include <GameState.h>
#include <components/basic/Transform.h>
#include <components/basic/Sprite.h>
#include <utils/Assert.h>

View File

@ -1,80 +0,0 @@
/*
* Transform.cpp
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#include <model/GameObject.h>
#include <components/basic/Transform.h>
#include <iostream>
namespace farmlands {
namespace components {
namespace basic {
Transform::Transform()
: x(0), y(0),
w(0), h(0),
m_parent(nullptr)
{
}
Transform::~Transform()
{
}
model::Component* Transform::clone()
{
Transform* clone = new Transform();
clone->x = x;
clone->y = y;
clone->w = w;
clone->h = h;
return clone;
}
void Transform::onCreate()
{
if (gameObject->parent() != nullptr)
m_parent = gameObject->parent()->component<Transform>();
}
float Transform::globalX() const
{
return (m_parent) ? m_parent->globalX() + x : x;
}
float Transform::globalY() const
{
return (m_parent) ? m_parent->globalY() + y : y;
}
void Transform::setGlobalX(float x)
{
this->x = (m_parent) ? x - m_parent->globalX() : x;
}
void Transform::setGlobalY(float y)
{
this->y = (m_parent) ? y - m_parent->globalY() : y;
}
void Transform::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Transform ";
std::cout << "x="<<x<<" ";
std::cout << "y="<<y<<" ";
std::cout << "w="<<w<<" ";
std::cout << "h="<<h<<"\n";
}
}
}
} /* namespace farmlands */

View File

@ -1,48 +0,0 @@
/*
* Transform.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef BASE_TRANSFORM_H_
#define BASE_TRANSFORM_H_
#include <model/Component.h>
namespace farmlands {
namespace components {
namespace basic {
class Transform: public model::Component
{
public:
Transform();
virtual ~Transform();
virtual model::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onCreate();
// Getters
float globalX() const;
float globalY() const;
// Setters
void setGlobalX(float x);
void setGlobalY(float y);
// Local coordinates (relative to parent)
float x, y;
float w, h;
private:
Transform* m_parent;
};
}
}
} /* namespace farmlands */
#endif /* BASE_TRANSFORM_H_ */