Snake/src/Core/SnakeProject/iworld.cpp

121 lines
2.6 KiB
C++
Raw Normal View History

2021-06-06 15:53:49 +03:00
#include "iworld.h"
2021-06-07 17:37:03 +03:00
#include "iworlditem.h"
2021-06-06 22:10:43 +03:00
#include <quasarapp.h>
2021-06-06 15:53:49 +03:00
IWorld::IWorld() {
}
IWorld::~IWorld() {
deinit();
}
2021-06-06 22:10:43 +03:00
void IWorld::render(unsigned int tbfMsec) {
for (auto i = _items.begin(); i != _items.end(); ++i) {
(*i).objectPtr->render(tbfMsec);
2021-06-07 17:37:03 +03:00
// intersects event.
if ((*i).objectPtr->intersects(*_player)) {
_player->onIntersects((*i).objectPtr);
}
}
if (_player->isDead()) {
emit sigGameFinished(_player->getCurrentStatus());
2021-06-06 22:10:43 +03:00
}
}
2021-06-06 15:53:49 +03:00
bool IWorld::init() {
2021-06-06 22:10:43 +03:00
_worldRules = initWorldRules();
_hdrMap = initHdrBackGround();
2021-06-07 17:37:03 +03:00
_player = initPlayer();
2021-06-06 22:10:43 +03:00
if (!_worldRules->size())
return false;
worldChanged(*_worldRules->begin());
return true;
2021-06-06 15:53:49 +03:00
}
2021-06-06 22:10:43 +03:00
void IWorld::clearItems() {
for (const auto& item : qAsConst(_items)) {
delete item.objectPtr;
2021-06-06 15:53:49 +03:00
}
2021-06-06 22:10:43 +03:00
_items.clear();
}
void IWorld::deinit() {
2021-06-07 17:37:03 +03:00
delete _player;
2021-06-06 15:53:49 +03:00
2021-06-06 22:10:43 +03:00
clearItems();
2021-06-06 15:53:49 +03:00
_hdrMap = "";
2021-06-06 22:10:43 +03:00
2021-06-06 15:53:49 +03:00
delete _worldRules;
}
2021-06-06 22:10:43 +03:00
2021-06-07 17:37:03 +03:00
void IWorld::addItem(const QString& group, IWorldItem* obj) {
2021-06-06 22:10:43 +03:00
_items.insert(obj->guiId(), WorldObjectWraper{obj, group});
_itemsGroup.insert(group, obj->guiId());
}
bool IWorld::removeItem(int id) {
auto obj = _items.value(id);
if (!obj.objectPtr) {
return false;
}
_itemsGroup.remove(obj.groupName, id);
_items.remove(id);
delete obj.objectPtr;
return true;
}
bool IWorld::removeAnyItemFromGroup(const QString &group) {
auto anyObject =_itemsGroup.value(group);
return removeItem(anyObject);
}
2021-06-08 18:00:42 +03:00
const QString &IWorld::hdrMap() const {
return _hdrMap;
}
2021-06-06 22:10:43 +03:00
void IWorld::worldChanged(const WorldObjects &objects) {
for (auto it = objects.begin(); it != objects.end(); ++it) {
int count = it.value() -_itemsGroup.count(it.key());
if (count > 0) {
for ( int i = 0; i < count; ++i ) {
2021-06-07 17:37:03 +03:00
IWorldItem *obj = generate(it.key());
obj->initOnWorld(this);
2021-06-06 22:10:43 +03:00
if (!obj) {
QuasarAppUtils::Params::log("object not created line:" + QString::fromLatin1(Q_FUNC_INFO),
QuasarAppUtils::Warning);
break;
}
addItem(it.key(), obj);
}
} else {
for (; count < 0; ++count ) {
if (!removeAnyItemFromGroup(it.key())) {
QuasarAppUtils::Params::log("World::changeCountObjects error delete object!",
QuasarAppUtils::Warning);
break;
}
}
}
}
}