Snake/src/Core/SnakeProject/iworld.cpp

198 lines
4.3 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-09 16:38:55 +03:00
#include "iground.h"
2021-06-12 18:20:29 +03:00
#include "defaultcontrol.h"
2021-06-06 15:53:49 +03:00
IWorld::IWorld() {
}
IWorld::~IWorld() {
deinit();
}
IControl *IWorld::initUserInterface() const {
2021-06-12 18:20:29 +03:00
return new DefaultControl;
}
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-11 18:09:03 +03:00
bool IWorld::start() {
_player->setposition({0,0,0});
_player->setSpeed(0);
return true;
}
2021-06-09 18:23:07 +03:00
const IWorldItem *IWorld::getItem(int id) const {
return _items.value(id, {}).objectPtr;
}
2021-06-06 15:53:49 +03:00
bool IWorld::init() {
2021-06-13 15:16:46 +03:00
if (isInit())
return true;
2021-06-06 22:10:43 +03:00
_worldRules = initWorldRules();
_hdrMap = initHdrBackGround();
2021-06-07 17:37:03 +03:00
_player = initPlayer();
2021-06-09 16:38:55 +03:00
_player->initOnWorld(this, _player);
_userInterface = initUserInterface();
2021-06-09 16:38:55 +03:00
setCameraReleativePosition(initCameraPosition());
2021-06-06 22:10:43 +03:00
2021-06-13 15:16:46 +03:00
if (!_worldRules->size()) {
deinit();
2021-06-06 22:10:43 +03:00
return false;
2021-06-13 15:16:46 +03:00
}
2021-06-06 22:10:43 +03:00
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-13 15:16:46 +03:00
if (_player) {
delete _player;
_player = nullptr;
}
if (_worldRules) {
delete _worldRules;
_worldRules = nullptr;
}
if (_userInterface) {
delete _userInterface;
_userInterface = nullptr;
}
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
}
2021-06-06 22:10:43 +03:00
2021-06-08 21:56:36 +03:00
void IWorld::generateGround() {
int count = 10;
2021-06-09 16:38:55 +03:00
QVector3D position = {0,0,0};
float increment = cameraReleativePosition().z() * 2;
while (count--) {
auto item = generateGroundTile();
item->initOnWorld(this, _player);
position.setX(position.x() + increment);
item->setposition(position);
addItem("groundTile", item);
}
2021-06-08 21:56:36 +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) {
2021-06-08 21:56:36 +03:00
auto anyObject = _itemsGroup.value(group);
2021-06-06 22:10:43 +03:00
return removeItem(anyObject);
}
IControl *IWorld::userInterface() const {
return _userInterface;
}
2021-06-13 15:16:46 +03:00
bool IWorld::isInit() const {
return _userInterface && _player && _worldRules;
}
2021-06-09 16:38:55 +03:00
void IWorld::setCameraReleativePosition(const QVector3D &newCameraReleativePosition) {
if (_cameraReleativePosition == newCameraReleativePosition)
return;
_cameraReleativePosition = newCameraReleativePosition;
emit cameraReleativePositionChanged();
}
const QVector3D &IWorld::cameraReleativePosition() const {
return _cameraReleativePosition;
}
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) {
2021-06-08 21:56:36 +03:00
int count = it.value() - _itemsGroup.count(it.key());
2021-06-06 22:10:43 +03:00
if (count > 0) {
for ( int i = 0; i < count; ++i ) {
2021-06-07 17:37:03 +03:00
IWorldItem *obj = generate(it.key());
2021-06-09 16:38:55 +03:00
obj->initOnWorld(this, _player);
2021-06-06 22:10:43 +03:00
if (!obj) {
2021-06-09 16:38:55 +03:00
QuasarAppUtils::Params::log("object not created line:" +
QString::fromLatin1(Q_FUNC_INFO),
2021-06-06 22:10:43 +03:00
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;
}
}
}
}
}