ref #63 snake object are done

This commit is contained in:
Andrei Yankovich 2021-06-30 12:15:57 +03:00
parent 04ceb5fc13
commit 8ccd31cbef
8 changed files with 81 additions and 27 deletions

View File

@ -35,6 +35,11 @@ IControl *IWorld::initUserInterface() const {
void IWorld::render(unsigned int tbfMsec) { void IWorld::render(unsigned int tbfMsec) {
if (!_running) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
return;
}
_ItemsMutex.lock(); _ItemsMutex.lock();
for (auto i = _items.begin(); i != _items.end(); ++i) { for (auto i = _items.begin(); i != _items.end(); ++i) {
@ -77,6 +82,8 @@ bool IWorld::start() {
worldChanged(*_worldRules->begin()); worldChanged(*_worldRules->begin());
setTargetFps(60); setTargetFps(60);
setRunning(true);
return true; return true;
} }
@ -109,15 +116,7 @@ IWorldItem *IWorld::generate(const QString &objectType) const {
} }
bool IWorld::stop() { bool IWorld::stop() {
start(); setRunning(false);
setWorldStatus(WorldStatus::Background);
_player->setControl(dynamic_cast<IControl*>(_backgroundAI));
_backgroundAI->startAI();
setTargetFps(30);
return true; return true;
} }
@ -233,6 +232,7 @@ void IWorld::removeItem(int id, QList<int> *removedObjectsList) {
} }
void IWorld::reset() { void IWorld::reset() {
if (_player) { if (_player) {
delete _player; delete _player;
_player = nullptr; _player = nullptr;
@ -309,6 +309,14 @@ void IWorld::removeAnyItemFromGroup(const QString &group,
removeItem(anyObjectId, removedObjectsList); removeItem(anyObjectId, removedObjectsList);
} }
bool IWorld::running() const {
return _running;
}
void IWorld::setRunning(bool newRunning) {
_running = newRunning;
}
int IWorld::targetFps() const { int IWorld::targetFps() const {
return _targetFps; return _targetFps;
} }
@ -357,7 +365,7 @@ void IWorld::setCameraReleativePosition(const QVector3D &newCameraReleativePosit
} }
void IWorld::handleStop() { void IWorld::handleStop() {
stop(); runAsBackGround();
} }
const QVector3D &IWorld::cameraReleativePosition() const { const QVector3D &IWorld::cameraReleativePosition() const {
@ -400,3 +408,14 @@ void IWorld::setWorldStatus(int newWorldStatus) {
const QString &IWorld::hdr() const { const QString &IWorld::hdr() const {
return _hdrMap; return _hdrMap;
} }
void IWorld::runAsBackGround() {
start();
setWorldStatus(WorldStatus::Background);
_player->setControl(dynamic_cast<IControl*>(_backgroundAI));
_backgroundAI->startAI();
setTargetFps(30);
}

View File

@ -222,6 +222,12 @@ public:
*/ */
const QString &hdr() const; const QString &hdr() const;
/**
* @brief runAsBackGround This method run this world as a backgroud.
* The player start new game and sets new control from the backgroundAI method.
*/
void runAsBackGround();
signals: signals:
/** /**
* @brief sigGameFinished This signal emit when game are finished * @brief sigGameFinished This signal emit when game are finished
@ -375,6 +381,19 @@ private:
bool prepare(); bool prepare();
void reset(); void reset();
/**
* @brief running This varibale check in render function if the running is true then render loop are working correctly
* @return
*/
bool running() const;
/**
* @brief setRunning
* @param newRunning
*/
void setRunning(bool newRunning);
/** /**
* @brief worldChanged This method generate diff for the qml * @brief worldChanged This method generate diff for the qml
* @param objects This is list of object on lvl * @param objects This is list of object on lvl
@ -447,6 +466,7 @@ private:
QHash<QString, std::function<IWorldItem*()>> _registeredTypes; QHash<QString, std::function<IWorldItem*()>> _registeredTypes;
int _targetFps = 60; int _targetFps = 60;
bool _running = false;
// engine // engine
friend class Engine; friend class Engine;

View File

@ -31,12 +31,16 @@ const IWorldItem *IWorldItem::getPlayer() const {
} }
void IWorldItem::render(unsigned int) { void IWorldItem::render(unsigned int) {
if (_playerObject->position().x() - position().x() > if (_playerObject->position().x() - _world->cameraReleativePosition().z() >
_world->cameraReleativePosition().z() * 2) { position().x()) {
setX(_playerObject->position().x() + _world->cameraReleativePosition().z() * 4);
float dY = rand() % static_cast<int>(_world->cameraReleativePosition().z() * 2 float dX = _world->cameraReleativePosition().z() * 2 +
- _world->cameraReleativePosition().z()); (rand() % static_cast<int>(_world->cameraReleativePosition().z()));
setX(_playerObject->position().x() + dX);
float dY = (rand() % static_cast<int>(_world->cameraReleativePosition().z() * 4)
- _world->cameraReleativePosition().z() * 2);
setY(_playerObject->position().y() + dY); setY(_playerObject->position().y() + dY);
} }

View File

@ -26,7 +26,11 @@ void SnakeItem::render(unsigned int tbfMsec) {
auto ratationVector = (_prevObject->position() - position()); auto ratationVector = (_prevObject->position() - position());
if (auto claster = static_cast<Snake*>(parentClaster())) { if (auto claster = static_cast<Snake*>(parentClaster())) {
if (ratationVector.length() > claster->lengthBetwinItems()) { float ratationLength = ratationVector.length();
if (ratationLength > claster->lengthBetwinItems() * 10) {
setposition(_prevObject->position());
} else if (ratationLength > claster->lengthBetwinItems()) {
setMovableVector(ratationVector.normalized() * claster->currentMovableVector().length()); setMovableVector(ratationVector.normalized() * claster->currentMovableVector().length());
} }
} }

View File

@ -11,6 +11,8 @@
DefaultBackgroundAI::DefaultBackgroundAI() { DefaultBackgroundAI::DefaultBackgroundAI() {
_timer = new QTimer(); _timer = new QTimer();
_timer->setInterval(1000); _timer->setInterval(1000);
connect(_timer, &QTimer::timeout, this, &DefaultBackgroundAI::handleTimerTriger);
} }
DefaultBackgroundAI::~DefaultBackgroundAI() { DefaultBackgroundAI::~DefaultBackgroundAI() {
@ -27,7 +29,7 @@ void DefaultBackgroundAI::stopAI() {
} }
void DefaultBackgroundAI::handleTimerTriger() { void DefaultBackgroundAI::handleTimerTriger() {
_timer->setInterval(rand() % 1000 + 10); _timer->setInterval(rand() % 2000 + 200);
emit userTap(); emit userTap();
} }

View File

@ -46,9 +46,16 @@ void Engine::setWorld(IWorld *world) {
_currentWorld = world; _currentWorld = world;
emit worldChanged(); emit worldChanged();
prepareNewWorld(); if (!prepareNewWorld()) {
QuasarAppUtils::Params::log("Failed to init world. World name: " + _currentWorld->name(),
QuasarAppUtils::Error);
_currentWorld = nullptr;
return;
}
startRenderLoop(); startRenderLoop();
_currentWorld->runAsBackGround();
} }
void Engine::setScane(QObject *newScane) { void Engine::setScane(QObject *newScane) {
@ -129,20 +136,18 @@ void Engine::setPrepareLvlProgress(int newPrepareLvlProgress) {
emit prepareLvlProgressChanged(); emit prepareLvlProgressChanged();
} }
void Engine::prepareNewWorld() { bool Engine::prepareNewWorld() {
if (!_currentWorld->prepare()) { if (!_currentWorld->prepare()) {
QuasarAppUtils::Params::log("Failed to init world. World name: " + _currentWorld->name(), return false;
QuasarAppUtils::Error);
_currentWorld = nullptr;
return;
} }
if (!_currentWorld->userInterface()->init()) { if (!_currentWorld->userInterface()->init()) {
return; return false;
} }
setMenu(_currentWorld->userInterface()); setMenu(_currentWorld->userInterface());
return true;
} }
void Engine::renderLoop() { void Engine::renderLoop() {

View File

@ -132,7 +132,7 @@ signals:
private: private:
void setPrepareLvlProgress(int newPrepareLvlProgress); void setPrepareLvlProgress(int newPrepareLvlProgress);
void prepareNewWorld(); bool prepareNewWorld();
void renderLoop(); void renderLoop();

@ -1 +1 @@
Subproject commit 0f022f42093a1e5311cbcd13c9ed40b54163c251 Subproject commit 9f06054ae836016881dc132d336c5da5bb3dca6c