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

@@ -17,6 +17,7 @@ namespace model {
GameObject::GameObject()
: name("unnamed"),
visible(true),
transform(this),
m_components(),
m_children(),
m_parent(nullptr),
@@ -115,6 +116,7 @@ GameObject* GameObject::removeChild(GameObject* obj)
if (*it == obj)
{
m_children.erase(it);
obj->m_parent = nullptr;
return *it;
}
}

View File

@@ -8,6 +8,7 @@
#ifndef GAMEOBJECT_H_
#define GAMEOBJECT_H_
#include <model/Transform.h>
#include <utils/Assert.h>
#include <utils/ICloneable.h>
#include <utils/INonAssignable.h>
@@ -97,6 +98,7 @@ namespace model {
// Other properties
std::string name;
bool visible;
Transform transform;
private:
// Components
@@ -169,7 +171,16 @@ namespace model {
void GameObject::destroyComponent()
{
std::type_index typeIndex(typeid(T));
m_components.erase(typeIndex);
T* comp = nullptr;
auto it = m_components.find(typeIndex);
if (it != m_components.end())
{
T* comp = dynamic_cast<T*>(it->second);
m_components.erase(it);
delete comp;
}
}
template <typename T>

58
src/model/Transform.cpp Normal file
View File

@@ -0,0 +1,58 @@
/*
* Transform.cpp
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#include <model/GameObject.h>
#include <model/Transform.h>
#include <iostream>
namespace farmlands {
namespace model {
Transform::Transform(GameObject* obj)
: x(0), y(0),
w(0), h(0),
gameObject(obj)
{
}
float Transform::globalX() const
{
if (gameObject->parent())
return gameObject->parent()->transform.globalX() + x;
return x;
}
float Transform::globalY() const
{
if (gameObject->parent())
return gameObject->parent()->transform.globalY() + y;
return y;
}
void Transform::setGlobalX(float x)
{
if (gameObject->parent())
this->x = x - gameObject->parent()->transform.globalX();
this->x = x;
}
void Transform::setGlobalY(float y)
{
if (gameObject->parent())
this->y = y - gameObject->parent()->transform.globalY();
this->y = y;
}
}
} /* namespace farmlands */

39
src/model/Transform.h Normal file
View File

@@ -0,0 +1,39 @@
/*
* Transform.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef BASE_TRANSFORM_H_
#define BASE_TRANSFORM_H_
namespace farmlands {
namespace model {
class GameObject;
class Transform
{
public:
Transform(GameObject* obj);
// 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;
GameObject* gameObject;
};
}
} /* namespace farmlands */
#endif /* BASE_TRANSFORM_H_ */