mirror of
https://github.com/QuasarApp/Snake.git
synced 2025-05-05 22:19:45 +00:00
added support of clastters (not testsed)
This commit is contained in:
parent
748fb4dab6
commit
f4e6c3876c
@ -8,18 +8,27 @@
|
||||
#include "claster.h"
|
||||
#include "singleclasterworlditem.h"
|
||||
|
||||
Claster::Claster() {
|
||||
Claster::Claster(const QString &name,
|
||||
const QString &viewTempalte,
|
||||
QObject *ptr):
|
||||
IWorldItem(name, viewTempalte, ptr) {
|
||||
|
||||
}
|
||||
|
||||
void Claster::add(IWorldItem *object) {
|
||||
Claster::~Claster() {
|
||||
for (auto child : qAsConst(_objects)) {
|
||||
child->removeClaster(this);
|
||||
}
|
||||
}
|
||||
|
||||
void Claster::add(ClasterItem *object) {
|
||||
_objects.insert(object->guiId(), object);
|
||||
if (auto singlClasterObject = dynamic_cast<SingleClasterWorldItem*>(object)) {
|
||||
singlClasterObject->setClaster(this);
|
||||
}
|
||||
}
|
||||
|
||||
void Claster::remove(IWorldItem *object) {
|
||||
void Claster::remove(ClasterItem *object) {
|
||||
_objects.remove(object->guiId());
|
||||
}
|
||||
|
||||
@ -27,6 +36,6 @@ void Claster::remove(int id) {
|
||||
_objects.remove(id);
|
||||
}
|
||||
|
||||
const QHash<int, IWorldItem *> &Claster::objects() const {
|
||||
const QHash<int, ClasterItem *> &Claster::objects() const {
|
||||
return _objects;
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
|
||||
#include "Crawl/iworlditem.h"
|
||||
|
||||
class ClasterItem;
|
||||
|
||||
/**
|
||||
* @brief The Claster class are object with support multiple objects render.
|
||||
@ -19,19 +20,23 @@ class CRAWL_EXPORT Claster: public IWorldItem
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Claster();
|
||||
Claster(const QString& name,
|
||||
const QString& viewTempalte = DEFAULT_VIEW_TEMPLATE,
|
||||
QObject *ptr = nullptr);
|
||||
|
||||
~Claster();
|
||||
|
||||
/**
|
||||
* @brief add This method added new object to claster.
|
||||
* @param object This is model of added object
|
||||
*/
|
||||
void add(IWorldItem* object);
|
||||
void add(ClasterItem* object);
|
||||
|
||||
/**
|
||||
* @brief remove This method remove object from claster
|
||||
* @param object poiter of removed object
|
||||
*/
|
||||
void remove(IWorldItem* object);
|
||||
void remove(ClasterItem* object);
|
||||
|
||||
/**
|
||||
* @brief remove some as a Claster::remove(IWorldItem* object) but by id.
|
||||
@ -43,10 +48,10 @@ public:
|
||||
* @brief objects This method return list of collected objects.
|
||||
* @return return const reference to objects list .
|
||||
*/
|
||||
const QHash<int, IWorldItem*> &objects() const;
|
||||
const QHash<int, ClasterItem*> &objects() const;
|
||||
|
||||
private:
|
||||
QHash<int, IWorldItem*> _objects;
|
||||
QHash<int, ClasterItem*> _objects;
|
||||
};
|
||||
|
||||
#endif // CLASTER_H
|
||||
|
@ -8,7 +8,10 @@
|
||||
#include "claster.h"
|
||||
#include "clasteritem.h"
|
||||
|
||||
ClasterItem::ClasterItem() {
|
||||
ClasterItem::ClasterItem(const QString &name,
|
||||
const QString &viewTempalte,
|
||||
QObject *ptr):
|
||||
IWorldItem(name, viewTempalte, ptr) {
|
||||
|
||||
}
|
||||
|
||||
@ -18,10 +21,18 @@ ClasterItem::~ClasterItem() {
|
||||
}
|
||||
}
|
||||
|
||||
int ClasterItem::parentClastersCount() const {
|
||||
return _parentClasters.size();
|
||||
}
|
||||
|
||||
void ClasterItem::setClaster(Claster *claster) {
|
||||
_parentClasters += claster;
|
||||
}
|
||||
|
||||
void ClasterItem::removeClaster(Claster *claster) {
|
||||
_parentClasters -= claster;
|
||||
}
|
||||
|
||||
const QSet<Claster *> &ClasterItem::parentClasters() const {
|
||||
return _parentClasters;
|
||||
}
|
||||
|
@ -23,9 +23,17 @@ class ClasterItem: public IWorldItem
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ClasterItem();
|
||||
ClasterItem(const QString& name,
|
||||
const QString& viewTempalte = DEFAULT_VIEW_TEMPLATE,
|
||||
QObject *ptr = nullptr);
|
||||
~ClasterItem();
|
||||
|
||||
/**
|
||||
* @brief parentClastersCount This method return count of the parent clasters.
|
||||
* @return parent clasters count
|
||||
*/
|
||||
int parentClastersCount() const;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief setClaster invoked when object added to new claster.
|
||||
|
@ -9,11 +9,11 @@
|
||||
|
||||
#include <QVector3D>
|
||||
|
||||
GuiObject::GuiObject(const QString &viewTempalte, QObject *ptr):
|
||||
GuiObject::GuiObject(const QString &name, const QString &viewTempalte, QObject *ptr):
|
||||
QObject (ptr) {
|
||||
_viewTemplate = viewTempalte;
|
||||
_className = name;
|
||||
generateId();
|
||||
|
||||
}
|
||||
|
||||
QString GuiObject::color() const {
|
||||
@ -33,6 +33,10 @@ void GuiObject::generateId() {
|
||||
_guiId = id++;
|
||||
}
|
||||
|
||||
const QString &GuiObject::className() const {
|
||||
return _className;
|
||||
}
|
||||
|
||||
void GuiObject::reset() {
|
||||
setX(0);
|
||||
setY(0);
|
||||
|
@ -15,6 +15,8 @@
|
||||
#include <QVector3D>
|
||||
#include "Crawl/irender.h"
|
||||
|
||||
#define DEFAULT_VIEW_TEMPLATE "qrc:/CrawlModule/GraphicItem.qml"
|
||||
|
||||
/**
|
||||
* @brief The GuiObject class This base model for gui objects.
|
||||
*/
|
||||
@ -38,7 +40,9 @@ class CRAWL_EXPORT GuiObject: public QObject, public IRender {
|
||||
|
||||
|
||||
public:
|
||||
GuiObject(const QString& viewTempalte = "qrc:/CrawlModule/GraphicItem.qml", QObject *ptr = nullptr);
|
||||
GuiObject(const QString& name,
|
||||
const QString& viewTempalte = DEFAULT_VIEW_TEMPLATE,
|
||||
QObject *ptr = nullptr);
|
||||
|
||||
QString color() const;
|
||||
void setColor(QString color);
|
||||
@ -97,6 +101,14 @@ public:
|
||||
const QString &mash() const;
|
||||
void setMash(const QString &newMash);
|
||||
|
||||
/**
|
||||
* @brief className This method return class name.
|
||||
* The class name using as a group of objects on thw world.
|
||||
* @return class name;
|
||||
* @note the class name should be sets on the consturctor of child classes of this class.
|
||||
*/
|
||||
const QString &className() const;
|
||||
|
||||
signals:
|
||||
void guiIdChanged(int guiId);
|
||||
void colorChanged(QString color);
|
||||
@ -131,6 +143,7 @@ private:
|
||||
QVector3D _size;
|
||||
QQuaternion _ratation;
|
||||
QString _mash;
|
||||
QString _className;
|
||||
};
|
||||
|
||||
#endif // GUIOBJECT_H
|
||||
|
@ -8,7 +8,10 @@
|
||||
#include "iground.h"
|
||||
#include "iworld.h"
|
||||
|
||||
IGround::IGround() {
|
||||
IGround::IGround(const QString &name,
|
||||
const QString &viewTempalte,
|
||||
QObject *ptr):
|
||||
IWorldItem(name, viewTempalte, ptr) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,9 @@
|
||||
*/
|
||||
class CRAWL_EXPORT IGround: public IWorldItem {
|
||||
public:
|
||||
IGround();
|
||||
IGround(const QString& name,
|
||||
const QString& viewTempalte = DEFAULT_VIEW_TEMPLATE,
|
||||
QObject *ptr = nullptr);
|
||||
|
||||
// IRender interface
|
||||
public:
|
||||
|
@ -7,7 +7,10 @@
|
||||
|
||||
#include "iplayer.h"
|
||||
|
||||
IPlayer::IPlayer() {
|
||||
IPlayer::IPlayer(const QString &name,
|
||||
const QString &viewTempalte,
|
||||
QObject *ptr):
|
||||
IWorldItem(name, viewTempalte, ptr) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,9 @@ class IControl;
|
||||
class CRAWL_EXPORT IPlayer: public IWorldItem {
|
||||
Q_OBJECT
|
||||
public:
|
||||
IPlayer();
|
||||
IPlayer(const QString& name,
|
||||
const QString& viewTempalte = DEFAULT_VIEW_TEMPLATE,
|
||||
QObject *ptr = nullptr);
|
||||
|
||||
/**
|
||||
* @brief getCurrentStatus This method return current game state of the player.
|
||||
@ -51,7 +53,7 @@ public:
|
||||
* @param control This is control object.
|
||||
* @note This method can invoked two or more times, for example connect with AI control object and player control object. So your implementation should be contains disconnect methods.
|
||||
*/
|
||||
virtual void setControl(const IControl* control);
|
||||
virtual void setControl(const IControl* control) = 0;
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
#include "defaultcontrol.h"
|
||||
#include "worldstatus.h"
|
||||
#include "iai.h"
|
||||
#include "clasteritem.h"
|
||||
|
||||
IWorld::IWorld() {
|
||||
|
||||
@ -31,11 +32,11 @@ IControl *IWorld::initUserInterface() const {
|
||||
void IWorld::render(unsigned int tbfMsec) {
|
||||
|
||||
for (auto i = _items.begin(); i != _items.end(); ++i) {
|
||||
(*i).objectPtr->render(tbfMsec);
|
||||
(*i)->render(tbfMsec);
|
||||
|
||||
// intersects event.
|
||||
if ((*i).objectPtr->intersects(*_player)) {
|
||||
_player->onIntersects((*i).objectPtr);
|
||||
if ((*i)->intersects(*_player)) {
|
||||
_player->onIntersects((*i));
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,7 +86,7 @@ void IWorld::setPlayer(QObject *newPlayer) {
|
||||
}
|
||||
|
||||
_player = newPlayerObject;
|
||||
addAtomicItem("player", _player);
|
||||
addAtomicItem(_player);
|
||||
|
||||
emit playerChanged();
|
||||
}
|
||||
@ -106,7 +107,7 @@ IAI *IWorld::initBackGroundAI() const {
|
||||
}
|
||||
|
||||
IWorldItem *IWorld::getItem(int id) const {
|
||||
return _items.value(id, {}).objectPtr;
|
||||
return _items.value(id, nullptr);
|
||||
}
|
||||
|
||||
bool IWorld::init() {
|
||||
@ -141,26 +142,57 @@ bool IWorld::init() {
|
||||
|
||||
void IWorld::clearItems() {
|
||||
for (const auto& item : qAsConst(_items)) {
|
||||
delete item.objectPtr;
|
||||
delete item;
|
||||
}
|
||||
|
||||
_items.clear();
|
||||
}
|
||||
|
||||
void IWorld::addItem(IWorldItem *obj) {
|
||||
void IWorld::addItem(IWorldItem *obj, QList<int> *addedObjectsList) {
|
||||
if (!obj)
|
||||
return;
|
||||
|
||||
// Work wih claster
|
||||
if (auto claster = dynamic_cast<Claster*>(obj)) {
|
||||
|
||||
return;
|
||||
for (auto item : claster->objects()) {
|
||||
addAtomicItem(item);
|
||||
if (item && addedObjectsList) {
|
||||
addedObjectsList->push_back(item->guiId());
|
||||
}
|
||||
}
|
||||
}
|
||||
// Work With atomic items
|
||||
|
||||
addAtomicItem(obj);
|
||||
if (addedObjectsList)
|
||||
addedObjectsList->push_back(obj->guiId());
|
||||
|
||||
}
|
||||
|
||||
void IWorld::removeItem(int id) {
|
||||
// Work wih claster
|
||||
void IWorld::removeItem(int id, QList<int> *removedObjectsList) {
|
||||
|
||||
auto obj = getItem(id);
|
||||
|
||||
if (!obj)
|
||||
return;
|
||||
|
||||
// Work wih claster
|
||||
if (auto claster = dynamic_cast<Claster*>(obj)) {
|
||||
for (auto item : claster->objects()) {
|
||||
if (!item || item->parentClastersCount())
|
||||
continue;
|
||||
|
||||
int id = item->guiId();
|
||||
|
||||
removeIAtomictem(item);
|
||||
if (removedObjectsList)
|
||||
removedObjectsList->push_back(id);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
addAtomicItem(obj);
|
||||
removedObjectsList->push_back(obj->guiId());
|
||||
|
||||
// Work With atomic items
|
||||
}
|
||||
|
||||
void IWorld::deinit() {
|
||||
@ -190,34 +222,46 @@ void IWorld::deinit() {
|
||||
}
|
||||
|
||||
|
||||
void IWorld::addAtomicItem(const QString& group, IWorldItem* obj) {
|
||||
_items.insert(obj->guiId(), WorldObjectWraper{obj, group});
|
||||
_itemsGroup.insert(group, obj->guiId());
|
||||
void IWorld::addAtomicItem(IWorldItem* obj) {
|
||||
if (!obj)
|
||||
return;
|
||||
|
||||
_items.insert(obj->guiId(), obj);
|
||||
_itemsGroup.insert(obj->className(), obj->guiId());
|
||||
}
|
||||
|
||||
bool IWorld::removeIAtomictem(int id) {
|
||||
auto obj = _items.value(id);
|
||||
|
||||
if (!obj.objectPtr) {
|
||||
if (!obj) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_itemsGroup.remove(obj.groupName, id);
|
||||
_itemsGroup.remove(obj->className(), id);
|
||||
_items.remove(id);
|
||||
|
||||
delete obj.objectPtr;
|
||||
delete obj;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int IWorld::removeAnyAtomicItemFromGroup(const QString &group) {
|
||||
int anyObjectId = _itemsGroup.value(group);
|
||||
if (!removeIAtomictem(anyObjectId)) {
|
||||
bool IWorld::removeIAtomictem(IWorldItem *obj) {
|
||||
if (!obj) {
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
return anyObjectId;
|
||||
_itemsGroup.remove(obj->className(), obj->guiId());
|
||||
_items.remove(obj->guiId());
|
||||
|
||||
delete obj;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void IWorld::removeAnyItemFromGroup(const QString &group,
|
||||
QList<int> *removedObjectsList) {
|
||||
int anyObjectId = _itemsGroup.value(group);
|
||||
removeItem(anyObjectId, removedObjectsList);
|
||||
}
|
||||
|
||||
const QQuaternion &IWorld::cameraRatation() const {
|
||||
@ -272,31 +316,11 @@ void IWorld::worldChanged(const WorldObjects &objects) {
|
||||
|
||||
if (count > 0) {
|
||||
for ( int i = 0; i < count; ++i ) {
|
||||
IWorldItem *obj = generate(it.key());
|
||||
|
||||
TO-Do Add support of clasters.
|
||||
|
||||
obj->initOnWorld(this, _player);
|
||||
|
||||
if (!obj) {
|
||||
QuasarAppUtils::Params::log("object not created line:" +
|
||||
QString::fromLatin1(Q_FUNC_INFO),
|
||||
QuasarAppUtils::Warning);
|
||||
break;
|
||||
}
|
||||
|
||||
addAtomicItem(it.key(), obj);
|
||||
diff.addedIds.append(obj->guiId());
|
||||
addItem(generate(it.key()), &diff.addedIds);
|
||||
}
|
||||
} else {
|
||||
for (; count < 0; ++count ) {
|
||||
int removedObjectId = removeAnyAtomicItemFromGroup(it.key());
|
||||
if (!removedObjectId) {
|
||||
QuasarAppUtils::Params::log("World::changeCountObjects error delete object!",
|
||||
QuasarAppUtils::Warning);
|
||||
break;
|
||||
}
|
||||
diff.removeIds.append(removedObjectId);
|
||||
removeAnyItemFromGroup(it.key(), &diff.removeIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,14 +36,6 @@ typedef QMap<QString, int> WorldObjects;
|
||||
*/
|
||||
typedef QMap<int, WorldObjects> WorldRule;
|
||||
|
||||
/**
|
||||
* @brief The WorldObjectWraper struct This is simple wraper structure for the internal functionality of the IWorld objects.
|
||||
*/
|
||||
struct WorldObjectWraper {
|
||||
IWorldItem* objectPtr = nullptr;
|
||||
QString groupName = "";
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief The IWorld class use this interface for implementation your own game levels
|
||||
*/
|
||||
@ -315,20 +307,24 @@ private:
|
||||
/**
|
||||
* @brief addItem This method remove object from the scane. If object are calster then this method remove all child objects.
|
||||
* @param obj pointer to any engine object.
|
||||
* @param addedObjectsList This is list of added items into world.
|
||||
*/
|
||||
void addItem(IWorldItem *obj);
|
||||
void addItem(IWorldItem *obj,
|
||||
QList<int>* addedObjectsList = nullptr);
|
||||
|
||||
/**
|
||||
* @brief removeItem This method remove item from the world. If the @a id are id of the claster object then its child object will be removed too.
|
||||
* @param id This is id of removed object.
|
||||
* @param removedObjectsList This is list of removed objects. Leave this argument nullptr for ignore this argument.
|
||||
*/
|
||||
void removeItem(int id);
|
||||
void removeItem(int id,
|
||||
QList<int>* removedObjectsList = nullptr);
|
||||
|
||||
/**
|
||||
* @brief addAtomicItem This method execure atomic operation of add new item. This method support only atomic objects. (not clasters)
|
||||
* @param group This is group of the atomic object.
|
||||
* @param obj This is pointer to the atomic object. If the object are claster then it will be added without childs objects.
|
||||
*/
|
||||
void addAtomicItem(const QString &group, IWorldItem *obj);
|
||||
void addAtomicItem(IWorldItem *obj);
|
||||
|
||||
/**
|
||||
* @brief removeIAtomictem This method remove object with @a id. This method work with atomic objects only. If you rty remove claster objects then it will be ramoved witohout child objects.
|
||||
@ -338,14 +334,22 @@ private:
|
||||
bool removeIAtomictem(int id);
|
||||
|
||||
/**
|
||||
* @brief removeAnyAtomicItemFromGroup This method remove any object from group and return id of removed object.
|
||||
* @param group This is name of the objects group
|
||||
* @return id of removed object.
|
||||
* @note if object not removed return 0
|
||||
* @brief removeIAtomictem This method remove object @a obj. This method work with atomic objects only. If you rty remove claster objects then it will be ramoved witohout child objects.
|
||||
* @param obj This is id of removed objects.
|
||||
* @return return true if object remove successul
|
||||
*/
|
||||
int removeAnyAtomicItemFromGroup(const QString &group);
|
||||
bool removeIAtomictem(IWorldItem *obj);
|
||||
|
||||
QHash<int, WorldObjectWraper> _items;
|
||||
/**
|
||||
* @brief removeAnyItemFromGroup This method remove any object from group and return id of removed object. If The objec are claster then this method remove all child objects.
|
||||
* @param group This is name of the objects group
|
||||
* @param removedObjectsList This is list of removed objcts.
|
||||
* @return id of removed object.
|
||||
*/
|
||||
void removeAnyItemFromGroup(const QString &group,
|
||||
QList<int>* removedObjectsList = nullptr);
|
||||
|
||||
QHash<int, IWorldItem*> _items;
|
||||
QMultiHash<QString, int> _itemsGroup;
|
||||
QVector3D _cameraReleativePosition;
|
||||
QQuaternion _cameraRatation;
|
||||
|
@ -9,7 +9,10 @@
|
||||
#include "iworld.h"
|
||||
#include "quasarapp.h"
|
||||
|
||||
IWorldItem::IWorldItem() {
|
||||
IWorldItem::IWorldItem(const QString& name,
|
||||
const QString& viewTempalte,
|
||||
QObject *ptr):
|
||||
GuiObject(name, viewTempalte, ptr) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -17,8 +17,11 @@ class IWorld;
|
||||
*/
|
||||
class CRAWL_EXPORT IWorldItem: public GuiObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
IWorldItem();
|
||||
IWorldItem(const QString& name,
|
||||
const QString& viewTempalte = DEFAULT_VIEW_TEMPLATE,
|
||||
QObject *ptr = nullptr);
|
||||
|
||||
const IWorld *world() const;
|
||||
|
||||
|
@ -10,7 +10,10 @@
|
||||
#include "singleclasterworlditem.h"
|
||||
#include "quasarapp.h"
|
||||
|
||||
SingleClasterWorldItem::SingleClasterWorldItem() {
|
||||
SingleClasterWorldItem::SingleClasterWorldItem(const QString &name,
|
||||
const QString &viewTempalte,
|
||||
QObject *ptr):
|
||||
ClasterItem(name, viewTempalte, ptr) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,9 @@ class CRAWL_EXPORT SingleClasterWorldItem: public ClasterItem
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SingleClasterWorldItem();
|
||||
SingleClasterWorldItem(const QString& name,
|
||||
const QString& viewTempalte = DEFAULT_VIEW_TEMPLATE,
|
||||
QObject *ptr = nullptr);
|
||||
|
||||
private:
|
||||
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 1b5db3e86f869811ac85b412f6c2392e8524bab9
|
||||
Subproject commit e099a72604c4ed58e752664ac5ed0998429acca4
|
Loading…
x
Reference in New Issue
Block a user